x86/fpu: Uninline the irq_ts_save()/restore() functions
[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
78f7f1e5
IM
10#ifndef _ASM_X86_FPU_INTERNAL_H
11#define _ASM_X86_FPU_INTERNAL_H
1361b83a 12
1361b83a 13#include <linux/regset.h>
050902c0 14#include <linux/compat.h>
952f07ec 15#include <linux/sched.h>
1361b83a 16#include <linux/slab.h>
f89e32e0 17
1361b83a 18#include <asm/user.h>
df6b35f4 19#include <asm/fpu/api.h>
a137fb6b 20#include <asm/fpu/xsave.h>
1361b83a 21
72a671ce
SS
22#ifdef CONFIG_X86_64
23# include <asm/sigcontext32.h>
24# include <asm/user32.h>
235b8022
AV
25struct ksignal;
26int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
72a671ce 27 compat_sigset_t *set, struct pt_regs *regs);
235b8022 28int ia32_setup_frame(int sig, struct ksignal *ksig,
72a671ce
SS
29 compat_sigset_t *set, struct pt_regs *regs);
30#else
31# define user_i387_ia32_struct user_i387_struct
32# define user32_fxsr_struct user_fxsr_struct
33# define ia32_setup_frame __setup_frame
34# define ia32_setup_rt_frame __setup_rt_frame
35#endif
36
df639752
IM
37#define MXCSR_DEFAULT 0x1f80
38
72a671ce 39extern unsigned int mxcsr_feature_mask;
21c4cd10 40extern void fpu__init_cpu(void);
5d2bd700 41extern void eager_fpu_init(void);
1361b83a 42
55cc4678
IM
43extern void fpu__init_system_xstate(void);
44extern void fpu__init_cpu_xstate(void);
dd863880 45extern void fpu__init_system(struct cpuinfo_x86 *c);
55cc4678 46
952f07ec
IM
47extern int fpstate_alloc_init(struct fpu *fpu);
48extern void fpstate_init(struct fpu *fpu);
49extern void fpu__clear(struct task_struct *tsk);
50
51extern int dump_fpu(struct pt_regs *, struct user_i387_struct *);
52extern void fpu__restore(void);
53extern void fpu__init_check_bugs(void);
54extern void fpu__resume_cpu(void);
55
36b544dc 56DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
1361b83a 57
72a671ce
SS
58extern void convert_from_fxsr(struct user_i387_ia32_struct *env,
59 struct task_struct *tsk);
60extern void convert_to_fxsr(struct task_struct *tsk,
61 const struct user_i387_ia32_struct *env);
62
678eaf60 63extern user_regset_active_fn regset_fpregs_active, regset_xregset_fpregs_active;
1361b83a
LT
64extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get,
65 xstateregs_get;
66extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set,
67 xstateregs_set;
68
1361b83a 69/*
678eaf60
IM
70 * xstateregs_active == regset_fpregs_active. Please refer to the comment
71 * at the definition of regset_fpregs_active.
1361b83a 72 */
678eaf60 73#define xstateregs_active regset_fpregs_active
1361b83a 74
1361b83a
LT
75#ifdef CONFIG_MATH_EMULATION
76extern void finit_soft_fpu(struct i387_soft_struct *soft);
77#else
78static inline void finit_soft_fpu(struct i387_soft_struct *soft) {}
79#endif
80
1c927eea 81/*
36b544dc 82 * Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx,
1c927eea
RR
83 * on this CPU.
84 *
85 * This will disable any lazy FPU state restore of the current FPU state,
86 * but if the current thread owns the FPU, it will still be saved by.
87 */
88static inline void __cpu_disable_lazy_restore(unsigned int cpu)
89{
36b544dc 90 per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
1c927eea
RR
91}
92
66ddc2cb 93static inline int fpu_want_lazy_restore(struct fpu *fpu, unsigned int cpu)
1c927eea 94{
66ddc2cb 95 return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
1c927eea
RR
96}
97
050902c0
SS
98static 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
1361b83a
LT
114#define X87_FSW_ES (1 << 7) /* Exception Summary */
115
5d2bd700
SS
116static __always_inline __pure bool use_eager_fpu(void)
117{
c6b40691 118 return static_cpu_has_safe(X86_FEATURE_EAGER_FPU);
5d2bd700
SS
119}
120
1361b83a
LT
121static __always_inline __pure bool use_xsaveopt(void)
122{
c6b40691 123 return static_cpu_has_safe(X86_FEATURE_XSAVEOPT);
1361b83a
LT
124}
125
126static __always_inline __pure bool use_xsave(void)
127{
c6b40691 128 return static_cpu_has_safe(X86_FEATURE_XSAVE);
1361b83a
LT
129}
130
131static __always_inline __pure bool use_fxsr(void)
132{
c6b40691 133 return static_cpu_has_safe(X86_FEATURE_FXSR);
1361b83a
LT
134}
135
5d2bd700
SS
136static inline void fx_finit(struct i387_fxsave_struct *fx)
137{
5d2bd700 138 fx->cwd = 0x37f;
a8615af4 139 fx->mxcsr = MXCSR_DEFAULT;
5d2bd700
SS
140}
141
1361b83a
LT
142extern 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
49b8c695
PA
151#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})
166
0ca5bd0d
SS
167#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})
181
182static inline int fsave_user(struct i387_fsave_struct __user *fx)
1361b83a 183{
49b8c695 184 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
1361b83a
LT
185}
186
187static inline int fxsave_user(struct i387_fxsave_struct __user *fx)
188{
0ca5bd0d 189 if (config_enabled(CONFIG_X86_32))
49b8c695 190 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
0ca5bd0d 191 else if (config_enabled(CONFIG_AS_FXSAVEQ))
49b8c695 192 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
1361b83a 193
0ca5bd0d 194 /* See comment in fpu_fxsave() below. */
49b8c695 195 return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
1361b83a
LT
196}
197
0ca5bd0d 198static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
1361b83a 199{
0ca5bd0d
SS
200 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));
1361b83a 204
0ca5bd0d
SS
205 /* See comment in fpu_fxsave() below. */
206 return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
207 "m" (*fx));
1361b83a
LT
208}
209
e139e955
PA
210static 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
0ca5bd0d 222static inline int frstor_checking(struct i387_fsave_struct *fx)
1361b83a 223{
0ca5bd0d 224 return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
e139e955
PA
225}
226
227static inline int frstor_user(struct i387_fsave_struct __user *fx)
228{
229 return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
1361b83a
LT
230}
231
232static inline void fpu_fxsave(struct fpu *fpu)
233{
0ca5bd0d
SS
234 if (config_enabled(CONFIG_X86_32))
235 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state->fxsave));
236 else if (config_enabled(CONFIG_AS_FXSAVEQ))
6ca7a8a1 237 asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state->fxsave));
0ca5bd0d
SS
238 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 }
1361b83a
LT
264}
265
1361b83a
LT
266/*
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()) {
0afc4a94 273 xsave_state(&fpu->state->xsave);
1361b83a
LT
274
275 /*
276 * xsave header may indicate the init state of the FP.
277 */
400e4b20 278 if (!(fpu->state->xsave.header.xfeatures & XSTATE_FP))
1361b83a
LT
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
e2295375
IM
303extern void fpu__save(struct fpu *fpu);
304
1361b83a
LT
305static inline int fpu_restore_checking(struct fpu *fpu)
306{
307 if (use_xsave())
0ca5bd0d
SS
308 return fpu_xrstor_checking(&fpu->state->xsave);
309 else if (use_fxsr())
310 return fxrstor_checking(&fpu->state->fxsave);
1361b83a 311 else
0ca5bd0d 312 return frstor_checking(&fpu->state->fsave);
1361b83a
LT
313}
314
11f2d50b 315static inline int restore_fpu_checking(struct fpu *fpu)
1361b83a 316{
6ca7a8a1
BP
317 /*
318 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
319 * pending. Clear the x87 state here by setting it to fixed values.
320 * "m" is a random variable that should be in L1.
321 */
9b13a93d 322 if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) {
26bef131
LT
323 asm volatile(
324 "fnclex\n\t"
325 "emms\n\t"
326 "fildl %P[addr]" /* set F?P to defined value */
d5cea9b0 327 : : [addr] "m" (fpu->fpregs_active));
26bef131 328 }
1361b83a 329
11f2d50b 330 return fpu_restore_checking(fpu);
1361b83a
LT
331}
332
1361b83a 333/* Must be paired with an 'stts' after! */
723c58e4 334static inline void __fpregs_deactivate(struct fpu *fpu)
1361b83a 335{
d5cea9b0 336 fpu->fpregs_active = 0;
36b544dc 337 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
1361b83a
LT
338}
339
340/* Must be paired with a 'clts' before! */
dfaea4e6 341static inline void __fpregs_activate(struct fpu *fpu)
1361b83a 342{
d5cea9b0 343 fpu->fpregs_active = 1;
c0311f63 344 this_cpu_write(fpu_fpregs_owner_ctx, fpu);
1361b83a
LT
345}
346
952f07ec
IM
347/*
348 * The question "does this thread have fpu access?"
349 * is slightly racy, since preemption could come in
350 * and revoke it immediately after the test.
351 *
352 * However, even in that very unlikely scenario,
353 * we can just assume we have FPU access - typically
354 * to save the FP state - we'll just take a #NM
355 * fault and get the FPU access back.
356 */
357static inline int user_has_fpu(void)
358{
359 return current->thread.fpu.fpregs_active;
360}
361
1361b83a
LT
362/*
363 * Encapsulate the CR0.TS handling together with the
364 * software flag.
365 *
366 * These generally need preemption protection to work,
367 * do try to avoid using these on their own.
368 */
66af8e27 369static inline void fpregs_activate(struct fpu *fpu)
1361b83a 370{
5d2bd700 371 if (!use_eager_fpu())
66af8e27
IM
372 clts();
373 __fpregs_activate(fpu);
1361b83a
LT
374}
375
66af8e27 376static inline void fpregs_deactivate(struct fpu *fpu)
1361b83a 377{
66af8e27 378 __fpregs_deactivate(fpu);
31d96338 379 if (!use_eager_fpu())
66af8e27 380 stts();
1361b83a
LT
381}
382
ca6787ba 383static inline void drop_fpu(struct fpu *fpu)
304bceda 384{
d2d0ac9a
BP
385 /*
386 * Forget coprocessor state..
387 */
388 preempt_disable();
ca6787ba 389 fpu->counter = 0;
d2d0ac9a 390
d5cea9b0 391 if (fpu->fpregs_active) {
304bceda
SS
392 /* Ignore delayed exceptions from user space */
393 asm volatile("1: fwait\n"
394 "2:\n"
395 _ASM_EXTABLE(1b, 2b));
66af8e27 396 fpregs_deactivate(fpu);
304bceda 397 }
304bceda 398
c5bedc68 399 fpu->fpstate_active = 0;
4c138410 400
304bceda
SS
401 preempt_enable();
402}
403
8f4d8186
ON
404static inline void restore_init_xstate(void)
405{
406 if (use_xsave())
3e5e1267 407 xrstor_state(&init_xstate_ctx, -1);
8f4d8186 408 else
3e5e1267 409 fxrstor_checking(&init_xstate_ctx.i387);
8f4d8186
ON
410}
411
b85e67d1
BP
412/*
413 * Reset the FPU state in the eager case and drop it in the lazy case (later use
414 * will reinit it).
415 */
af2d94fd 416static inline void fpu_reset_state(struct fpu *fpu)
304bceda 417{
5d2bd700 418 if (!use_eager_fpu())
ca6787ba 419 drop_fpu(fpu);
8f4d8186
ON
420 else
421 restore_init_xstate();
304bceda
SS
422}
423
1361b83a
LT
424/*
425 * FPU state switching for scheduling.
426 *
427 * This is a two-stage process:
428 *
429 * - switch_fpu_prepare() saves the old state and
430 * sets the new state of the CR0.TS bit. This is
431 * done within the context of the old process.
432 *
433 * - switch_fpu_finish() restores the new state as
434 * necessary.
435 */
436typedef struct { int preload; } fpu_switch_t;
437
cb8818b6
IM
438static inline fpu_switch_t
439switch_fpu_prepare(struct fpu *old_fpu, struct fpu *new_fpu, int cpu)
1361b83a
LT
440{
441 fpu_switch_t fpu;
442
304bceda
SS
443 /*
444 * If the task has used the math, pre-load the FPU on xsave processors
445 * or if the past 5 consecutive context-switches used math.
446 */
c5bedc68 447 fpu.preload = new_fpu->fpstate_active &&
cb8818b6 448 (use_eager_fpu() || new_fpu->counter > 5);
1361ef29 449
d5cea9b0 450 if (old_fpu->fpregs_active) {
cb8818b6
IM
451 if (!fpu_save_init(old_fpu))
452 old_fpu->last_cpu = -1;
1361ef29 453 else
cb8818b6 454 old_fpu->last_cpu = cpu;
1361ef29 455
36b544dc 456 /* But leave fpu_fpregs_owner_ctx! */
d5cea9b0 457 old_fpu->fpregs_active = 0;
1361b83a
LT
458
459 /* Don't change CR0.TS if we just switch! */
460 if (fpu.preload) {
cb8818b6 461 new_fpu->counter++;
dfaea4e6 462 __fpregs_activate(new_fpu);
cb8818b6 463 prefetch(new_fpu->state);
5d2bd700 464 } else if (!use_eager_fpu())
1361b83a
LT
465 stts();
466 } else {
cb8818b6
IM
467 old_fpu->counter = 0;
468 old_fpu->last_cpu = -1;
1361b83a 469 if (fpu.preload) {
cb8818b6 470 new_fpu->counter++;
66ddc2cb 471 if (fpu_want_lazy_restore(new_fpu, cpu))
1361b83a
LT
472 fpu.preload = 0;
473 else
cb8818b6 474 prefetch(new_fpu->state);
232f62cd 475 fpregs_activate(new_fpu);
1361b83a
LT
476 }
477 }
478 return fpu;
479}
480
481/*
482 * By the time this gets called, we've already cleared CR0.TS and
483 * given the process the FPU if we are going to preload the FPU
484 * state - all we need to do is to conditionally restore the register
485 * state itself.
486 */
384a23f9 487static inline void switch_fpu_finish(struct fpu *new_fpu, fpu_switch_t fpu_switch)
1361b83a 488{
384a23f9 489 if (fpu_switch.preload) {
11f2d50b 490 if (unlikely(restore_fpu_checking(new_fpu)))
af2d94fd 491 fpu_reset_state(new_fpu);
1361b83a
LT
492 }
493}
494
495/*
496 * Signal frame handlers...
497 */
72a671ce
SS
498extern int save_xstate_sig(void __user *buf, void __user *fx, int size);
499extern int __restore_xstate_sig(void __user *buf, void __user *fx, int size);
1361b83a 500
72a671ce 501static inline int xstate_sigframe_size(void)
1361b83a 502{
72a671ce
SS
503 return use_xsave() ? xstate_size + FP_XSTATE_MAGIC2_SIZE : xstate_size;
504}
505
506static inline int restore_xstate_sig(void __user *buf, int ia32_frame)
507{
508 void __user *buf_fx = buf;
509 int size = xstate_sigframe_size();
510
511 if (ia32_frame && use_fxsr()) {
512 buf_fx = buf + sizeof(struct i387_fsave_struct);
513 size += sizeof(struct i387_fsave_struct);
1361b83a 514 }
72a671ce
SS
515
516 return __restore_xstate_sig(buf, buf_fx, size);
1361b83a
LT
517}
518
519/*
fb14b4ea 520 * Needs to be preemption-safe.
1361b83a 521 *
377ffbcc 522 * NOTE! user_fpu_begin() must be used only immediately before restoring
fb14b4ea
ON
523 * the save state. It does not do any saving/restoring on its own. In
524 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
525 * the task can lose the FPU right after preempt_enable().
1361b83a 526 */
1361b83a
LT
527static inline void user_fpu_begin(void)
528{
4540d3fa
IM
529 struct fpu *fpu = &current->thread.fpu;
530
1361b83a
LT
531 preempt_disable();
532 if (!user_has_fpu())
232f62cd 533 fpregs_activate(fpu);
1361b83a
LT
534 preempt_enable();
535}
536
1361b83a
LT
537/*
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
8ffb53ab
IM
567extern void fpstate_cache_init(void);
568
ed97b085 569extern int fpstate_alloc(struct fpu *fpu);
5a12bf63 570extern void fpstate_free(struct fpu *fpu);
c69e098b 571extern int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu);
1361b83a 572
72a671ce
SS
573static inline unsigned long
574alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx,
575 unsigned long *size)
576{
577 unsigned long frame_size = xstate_sigframe_size();
578
579 *buf_fx = sp = round_down(sp - frame_size, 64);
580 if (ia32_frame && use_fxsr()) {
581 frame_size += sizeof(struct i387_fsave_struct);
582 sp -= sizeof(struct i387_fsave_struct);
583 }
584
585 *size = frame_size;
586 return sp;
587}
1361b83a 588
78f7f1e5 589#endif /* _ASM_X86_FPU_INTERNAL_H */
This page took 0.181099 seconds and 5 git commands to generate.