blob: d4ab9e3af234df20cf6de54f192fba4d0b3914f5 [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
Ingo Molnardf6b35f2015-04-24 02:46:00 +020010#ifndef _ASM_X86_FPU_API_H
11#define _ASM_X86_FPU_API_H
Roland McGrath1eeaed72008-01-30 13:31:51 +010012
13#include <linux/sched.h>
Suresh Siddhae4914012008-08-13 22:02:26 +100014#include <linux/hardirq.h>
Roland McGrath1eeaed72008-01-30 13:31:51 +010015
Linus Torvalds1361b832012-02-21 13:19:22 -080016struct pt_regs;
17struct user_i387_struct;
18
Ingo Molnardb2b1d32015-04-24 02:13:09 +020019extern int fpstate_alloc_init(struct fpu *fpu);
Ingo Molnarc0ee2cf2015-04-03 13:01:52 +020020extern void fpstate_init(struct fpu *fpu);
Ingo Molnar2e8a3102015-04-24 02:28:23 +020021extern void fpu__clear(struct task_struct *tsk);
Ingo Molnar97185c92015-04-03 12:02:02 +020022
Jaswinder Singh36454932008-07-21 22:31:57 +053023extern int dump_fpu(struct pt_regs *, struct user_i387_struct *);
Ingo Molnar3a0aee42015-04-22 13:16:47 +020024extern void fpu__restore(void);
Ingo Molnar4d164092015-04-22 13:44:25 +020025extern void fpu__init_check_bugs(void);
Ingo Molnar9254aaa2015-04-24 10:02:32 +020026extern void fpu__resume_cpu(void);
Roland McGrath1eeaed72008-01-30 13:31:51 +010027
Linus Torvalds8546c002012-02-21 10:25:45 -080028extern bool irq_fpu_usable(void);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -070029
30/*
31 * Careful: __kernel_fpu_begin/end() must be called with preempt disabled
32 * and they don't touch the preempt state on their own.
33 * If you enable preemption after __kernel_fpu_begin(), preempt notifier
34 * should call the __kernel_fpu_end() to prevent the kernel/user FPU
35 * state from getting corrupted. KVM for example uses this model.
36 *
37 * All other cases use kernel_fpu_begin/end() which disable preemption
38 * during kernel FPU usage.
39 */
40extern void __kernel_fpu_begin(void);
41extern void __kernel_fpu_end(void);
Ingo Molnard63e79b2015-04-26 12:07:18 +020042extern void kernel_fpu_begin(void);
43extern void kernel_fpu_end(void);
Roland McGrath1eeaed72008-01-30 13:31:51 +010044
Suresh Siddhae4914012008-08-13 22:02:26 +100045/*
46 * Some instructions like VIA's padlock instructions generate a spurious
47 * DNA fault but don't modify SSE registers. And these instructions
Chuck Ebbert0b8c3d52009-06-09 10:40:50 -040048 * get used from interrupt context as well. To prevent these kernel instructions
49 * in interrupt context interacting wrongly with other user/kernel fpu usage, we
Suresh Siddhae4914012008-08-13 22:02:26 +100050 * should use them only in the context of irq_ts_save/restore()
51 */
52static inline int irq_ts_save(void)
53{
54 /*
Chuck Ebbert0b8c3d52009-06-09 10:40:50 -040055 * If in process context and not atomic, we can take a spurious DNA fault.
56 * Otherwise, doing clts() in process context requires disabling preemption
57 * or some heavy lifting like kernel_fpu_begin()
Suresh Siddhae4914012008-08-13 22:02:26 +100058 */
Chuck Ebbert0b8c3d52009-06-09 10:40:50 -040059 if (!in_atomic())
Suresh Siddhae4914012008-08-13 22:02:26 +100060 return 0;
61
62 if (read_cr0() & X86_CR0_TS) {
63 clts();
64 return 1;
65 }
66
67 return 0;
68}
69
70static inline void irq_ts_restore(int TS_state)
71{
72 if (TS_state)
73 stts();
74}
75
Roland McGrath1eeaed72008-01-30 13:31:51 +010076/*
Linus Torvalds15d87912012-02-16 09:15:04 -080077 * The question "does this thread have fpu access?"
78 * is slightly racy, since preemption could come in
79 * and revoke it immediately after the test.
80 *
81 * However, even in that very unlikely scenario,
82 * we can just assume we have FPU access - typically
83 * to save the FP state - we'll just take a #NM
84 * fault and get the FPU access back.
Linus Torvalds15d87912012-02-16 09:15:04 -080085 */
86static inline int user_has_fpu(void)
87{
Ingo Molnard5cea9b2015-04-24 14:19:26 +020088 return current->thread.fpu.fpregs_active;
Roland McGrath1eeaed72008-01-30 13:31:51 +010089}
90
Ingo Molnardf6b35f2015-04-24 02:46:00 +020091#endif /* _ASM_X86_FPU_API_H */