blob: ed8089d69094066985794cf4db39c04ecd545379 [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
Suresh Siddhaaa283f42008-03-10 15:28:05 -070021extern int init_fpu(struct task_struct *child);
Suresh Siddha304bced2012-08-24 14:13:02 -070022extern void fpu_finit(struct fpu *fpu);
Jaswinder Singh36454932008-07-21 22:31:57 +053023extern int dump_fpu(struct pt_regs *, struct user_i387_struct *);
Linus Torvalds1361b832012-02-21 13:19:22 -080024extern void math_state_restore(void);
Roland McGrath1eeaed72008-01-30 13:31:51 +010025
Linus Torvalds8546c002012-02-21 10:25:45 -080026extern bool irq_fpu_usable(void);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -070027
28/*
29 * Careful: __kernel_fpu_begin/end() must be called with preempt disabled
30 * and they don't touch the preempt state on their own.
31 * If you enable preemption after __kernel_fpu_begin(), preempt notifier
32 * should call the __kernel_fpu_end() to prevent the kernel/user FPU
33 * state from getting corrupted. KVM for example uses this model.
34 *
35 * All other cases use kernel_fpu_begin/end() which disable preemption
36 * during kernel FPU usage.
37 */
38extern void __kernel_fpu_begin(void);
39extern void __kernel_fpu_end(void);
40
41static inline void kernel_fpu_begin(void)
42{
43 WARN_ON_ONCE(!irq_fpu_usable());
44 preempt_disable();
45 __kernel_fpu_begin();
46}
47
48static inline void kernel_fpu_end(void)
49{
50 __kernel_fpu_end();
51 preempt_enable();
52}
Roland McGrath1eeaed72008-01-30 13:31:51 +010053
Suresh Siddhae4914012008-08-13 22:02:26 +100054/*
55 * Some instructions like VIA's padlock instructions generate a spurious
56 * DNA fault but don't modify SSE registers. And these instructions
Chuck Ebbert0b8c3d52009-06-09 10:40:50 -040057 * get used from interrupt context as well. To prevent these kernel instructions
58 * in interrupt context interacting wrongly with other user/kernel fpu usage, we
Suresh Siddhae4914012008-08-13 22:02:26 +100059 * should use them only in the context of irq_ts_save/restore()
60 */
61static inline int irq_ts_save(void)
62{
63 /*
Chuck Ebbert0b8c3d52009-06-09 10:40:50 -040064 * If in process context and not atomic, we can take a spurious DNA fault.
65 * Otherwise, doing clts() in process context requires disabling preemption
66 * or some heavy lifting like kernel_fpu_begin()
Suresh Siddhae4914012008-08-13 22:02:26 +100067 */
Chuck Ebbert0b8c3d52009-06-09 10:40:50 -040068 if (!in_atomic())
Suresh Siddhae4914012008-08-13 22:02:26 +100069 return 0;
70
71 if (read_cr0() & X86_CR0_TS) {
72 clts();
73 return 1;
74 }
75
76 return 0;
77}
78
79static inline void irq_ts_restore(int TS_state)
80{
81 if (TS_state)
82 stts();
83}
84
Roland McGrath1eeaed72008-01-30 13:31:51 +010085/*
Linus Torvalds15d87912012-02-16 09:15:04 -080086 * The question "does this thread have fpu access?"
87 * is slightly racy, since preemption could come in
88 * and revoke it immediately after the test.
89 *
90 * However, even in that very unlikely scenario,
91 * we can just assume we have FPU access - typically
92 * to save the FP state - we'll just take a #NM
93 * fault and get the FPU access back.
Linus Torvalds15d87912012-02-16 09:15:04 -080094 */
95static inline int user_has_fpu(void)
96{
Linus Torvalds1361b832012-02-21 13:19:22 -080097 return current->thread.fpu.has_fpu;
Roland McGrath1eeaed72008-01-30 13:31:51 +010098}
99
Linus Torvalds8546c002012-02-21 10:25:45 -0800100extern void unlazy_fpu(struct task_struct *tsk);
Roland McGrath1eeaed72008-01-30 13:31:51 +0100101
Herbert Xu3b0d6592009-11-03 09:11:15 -0500102#endif /* __ASSEMBLY__ */
103
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700104#endif /* _ASM_X86_I387_H */