blob: 6552a16e0e38aa1bfcab1b84f0b0a7929464422c [file] [log] [blame]
Roland McGrath1eeaed72008-01-30 13:31:51 +01001/*
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
H. Peter Anvin1965aae2008-10-22 22:26:29 -070010#ifndef _ASM_X86_I387_H
11#define _ASM_X86_I387_H
Roland McGrath1eeaed72008-01-30 13:31:51 +010012
Herbert Xu3b0d6592009-11-03 09:11:15 -050013#ifndef __ASSEMBLY__
14
Roland McGrath1eeaed72008-01-30 13:31:51 +010015#include <linux/sched.h>
Suresh Siddhae4914012008-08-13 22:02:26 +100016#include <linux/hardirq.h>
Roland McGrath1eeaed72008-01-30 13:31:51 +010017
Linus Torvalds1361b832012-02-21 13:19:22 -080018struct pt_regs;
19struct user_i387_struct;
20
Ingo Molnar97185c92015-04-03 12:02:02 +020021extern int fpstate_alloc_init(struct task_struct *curr);
Ingo Molnarc0ee2cf2015-04-03 13:01:52 +020022extern void fpstate_init(struct fpu *fpu);
Ingo Molnar97185c92015-04-03 12:02:02 +020023
Jaswinder Singh36454932008-07-21 22:31:57 +053024extern int dump_fpu(struct pt_regs *, struct user_i387_struct *);
Linus Torvalds1361b832012-02-21 13:19:22 -080025extern void math_state_restore(void);
Roland McGrath1eeaed72008-01-30 13:31:51 +010026
Linus Torvalds8546c002012-02-21 10:25:45 -080027extern bool irq_fpu_usable(void);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -070028
29/*
30 * Careful: __kernel_fpu_begin/end() must be called with preempt disabled
31 * and they don't touch the preempt state on their own.
32 * If you enable preemption after __kernel_fpu_begin(), preempt notifier
33 * should call the __kernel_fpu_end() to prevent the kernel/user FPU
34 * state from getting corrupted. KVM for example uses this model.
35 *
36 * All other cases use kernel_fpu_begin/end() which disable preemption
37 * during kernel FPU usage.
38 */
39extern void __kernel_fpu_begin(void);
40extern void __kernel_fpu_end(void);
41
42static inline void kernel_fpu_begin(void)
43{
Suresh Siddhab1a74bf2012-09-20 11:01:49 -070044 preempt_disable();
Oleg Nesterov14e153e2015-01-15 20:19:43 +010045 WARN_ON_ONCE(!irq_fpu_usable());
Suresh Siddhab1a74bf2012-09-20 11:01:49 -070046 __kernel_fpu_begin();
47}
48
49static inline void kernel_fpu_end(void)
50{
51 __kernel_fpu_end();
52 preempt_enable();
53}
Roland McGrath1eeaed72008-01-30 13:31:51 +010054
Oleg Nesterov75756372015-01-15 20:20:28 +010055/* Must be called with preempt disabled */
56extern void kernel_fpu_disable(void);
57extern void kernel_fpu_enable(void);
58
Suresh Siddhae4914012008-08-13 22:02:26 +100059/*
60 * Some instructions like VIA's padlock instructions generate a spurious
61 * DNA fault but don't modify SSE registers. And these instructions
Chuck Ebbert0b8c3d52009-06-09 10:40:50 -040062 * get used from interrupt context as well. To prevent these kernel instructions
63 * in interrupt context interacting wrongly with other user/kernel fpu usage, we
Suresh Siddhae4914012008-08-13 22:02:26 +100064 * should use them only in the context of irq_ts_save/restore()
65 */
66static inline int irq_ts_save(void)
67{
68 /*
Chuck Ebbert0b8c3d52009-06-09 10:40:50 -040069 * If in process context and not atomic, we can take a spurious DNA fault.
70 * Otherwise, doing clts() in process context requires disabling preemption
71 * or some heavy lifting like kernel_fpu_begin()
Suresh Siddhae4914012008-08-13 22:02:26 +100072 */
Chuck Ebbert0b8c3d52009-06-09 10:40:50 -040073 if (!in_atomic())
Suresh Siddhae4914012008-08-13 22:02:26 +100074 return 0;
75
76 if (read_cr0() & X86_CR0_TS) {
77 clts();
78 return 1;
79 }
80
81 return 0;
82}
83
84static inline void irq_ts_restore(int TS_state)
85{
86 if (TS_state)
87 stts();
88}
89
Roland McGrath1eeaed72008-01-30 13:31:51 +010090/*
Linus Torvalds15d87912012-02-16 09:15:04 -080091 * The question "does this thread have fpu access?"
92 * is slightly racy, since preemption could come in
93 * and revoke it immediately after the test.
94 *
95 * However, even in that very unlikely scenario,
96 * we can just assume we have FPU access - typically
97 * to save the FP state - we'll just take a #NM
98 * fault and get the FPU access back.
Linus Torvalds15d87912012-02-16 09:15:04 -080099 */
100static inline int user_has_fpu(void)
101{
Linus Torvalds1361b832012-02-21 13:19:22 -0800102 return current->thread.fpu.has_fpu;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100103}
104
Ingo Molnar0a781552015-04-03 10:58:52 +0200105extern void fpu__save(struct task_struct *tsk);
Roland McGrath1eeaed72008-01-30 13:31:51 +0100106
Herbert Xu3b0d6592009-11-03 09:11:15 -0500107#endif /* __ASSEMBLY__ */
108
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700109#endif /* _ASM_X86_I387_H */