| Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 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 | |
| Ingo Molnar | df6b35f | 2015-04-24 02:46:00 +0200 | [diff] [blame] | 10 | #ifndef _ASM_X86_FPU_API_H |
| 11 | #define _ASM_X86_FPU_API_H |
| Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 12 | |
| 13 | #include <linux/sched.h> |
| Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 14 | #include <linux/hardirq.h> |
| Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 15 | |
| Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 16 | struct pt_regs; |
| 17 | struct user_i387_struct; |
| 18 | |
| Ingo Molnar | db2b1d3 | 2015-04-24 02:13:09 +0200 | [diff] [blame] | 19 | extern int fpstate_alloc_init(struct fpu *fpu); |
| Ingo Molnar | c0ee2cf | 2015-04-03 13:01:52 +0200 | [diff] [blame] | 20 | extern void fpstate_init(struct fpu *fpu); |
| Ingo Molnar | 2e8a310 | 2015-04-24 02:28:23 +0200 | [diff] [blame] | 21 | extern void fpu__clear(struct task_struct *tsk); |
| Ingo Molnar | 97185c9 | 2015-04-03 12:02:02 +0200 | [diff] [blame] | 22 | |
| Jaswinder Singh | 3645493 | 2008-07-21 22:31:57 +0530 | [diff] [blame] | 23 | extern int dump_fpu(struct pt_regs *, struct user_i387_struct *); |
| Ingo Molnar | 3a0aee4 | 2015-04-22 13:16:47 +0200 | [diff] [blame] | 24 | extern void fpu__restore(void); |
| Ingo Molnar | 4d16409 | 2015-04-22 13:44:25 +0200 | [diff] [blame] | 25 | extern void fpu__init_check_bugs(void); |
| Ingo Molnar | 9254aaa | 2015-04-24 10:02:32 +0200 | [diff] [blame] | 26 | extern void fpu__resume_cpu(void); |
| Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 27 | |
| Linus Torvalds | 8546c00 | 2012-02-21 10:25:45 -0800 | [diff] [blame] | 28 | extern bool irq_fpu_usable(void); |
| Suresh Siddha | b1a74bf | 2012-09-20 11:01:49 -0700 | [diff] [blame] | 29 | |
| 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 | */ |
| 40 | extern void __kernel_fpu_begin(void); |
| 41 | extern void __kernel_fpu_end(void); |
| 42 | |
| 43 | static inline void kernel_fpu_begin(void) |
| 44 | { |
| Suresh Siddha | b1a74bf | 2012-09-20 11:01:49 -0700 | [diff] [blame] | 45 | preempt_disable(); |
| Oleg Nesterov | 14e153e | 2015-01-15 20:19:43 +0100 | [diff] [blame] | 46 | WARN_ON_ONCE(!irq_fpu_usable()); |
| Suresh Siddha | b1a74bf | 2012-09-20 11:01:49 -0700 | [diff] [blame] | 47 | __kernel_fpu_begin(); |
| 48 | } |
| 49 | |
| 50 | static inline void kernel_fpu_end(void) |
| 51 | { |
| 52 | __kernel_fpu_end(); |
| 53 | preempt_enable(); |
| 54 | } |
| Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 55 | |
| Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 56 | /* |
| 57 | * Some instructions like VIA's padlock instructions generate a spurious |
| 58 | * DNA fault but don't modify SSE registers. And these instructions |
| Chuck Ebbert | 0b8c3d5 | 2009-06-09 10:40:50 -0400 | [diff] [blame] | 59 | * get used from interrupt context as well. To prevent these kernel instructions |
| 60 | * in interrupt context interacting wrongly with other user/kernel fpu usage, we |
| Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 61 | * should use them only in the context of irq_ts_save/restore() |
| 62 | */ |
| 63 | static inline int irq_ts_save(void) |
| 64 | { |
| 65 | /* |
| Chuck Ebbert | 0b8c3d5 | 2009-06-09 10:40:50 -0400 | [diff] [blame] | 66 | * If in process context and not atomic, we can take a spurious DNA fault. |
| 67 | * Otherwise, doing clts() in process context requires disabling preemption |
| 68 | * or some heavy lifting like kernel_fpu_begin() |
| Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 69 | */ |
| Chuck Ebbert | 0b8c3d5 | 2009-06-09 10:40:50 -0400 | [diff] [blame] | 70 | if (!in_atomic()) |
| Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 71 | return 0; |
| 72 | |
| 73 | if (read_cr0() & X86_CR0_TS) { |
| 74 | clts(); |
| 75 | return 1; |
| 76 | } |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static inline void irq_ts_restore(int TS_state) |
| 82 | { |
| 83 | if (TS_state) |
| 84 | stts(); |
| 85 | } |
| 86 | |
| Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 87 | /* |
| Linus Torvalds | 15d8791 | 2012-02-16 09:15:04 -0800 | [diff] [blame] | 88 | * The question "does this thread have fpu access?" |
| 89 | * is slightly racy, since preemption could come in |
| 90 | * and revoke it immediately after the test. |
| 91 | * |
| 92 | * However, even in that very unlikely scenario, |
| 93 | * we can just assume we have FPU access - typically |
| 94 | * to save the FP state - we'll just take a #NM |
| 95 | * fault and get the FPU access back. |
| Linus Torvalds | 15d8791 | 2012-02-16 09:15:04 -0800 | [diff] [blame] | 96 | */ |
| 97 | static inline int user_has_fpu(void) |
| 98 | { |
| Ingo Molnar | d5cea9b | 2015-04-24 14:19:26 +0200 | [diff] [blame] | 99 | return current->thread.fpu.fpregs_active; |
| Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| Ingo Molnar | df6b35f | 2015-04-24 02:46:00 +0200 | [diff] [blame] | 102 | #endif /* _ASM_X86_FPU_API_H */ |