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 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 10 | #ifndef _ASM_X86_I387_H |
| 11 | #define _ASM_X86_I387_H |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 12 | |
Herbert Xu | 3b0d659 | 2009-11-03 09:11:15 -0500 | [diff] [blame] | 13 | #ifndef __ASSEMBLY__ |
| 14 | |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 15 | #include <linux/sched.h> |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 16 | #include <linux/hardirq.h> |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 17 | |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 18 | struct pt_regs; |
| 19 | struct user_i387_struct; |
| 20 | |
Suresh Siddha | aa283f4 | 2008-03-10 15:28:05 -0700 | [diff] [blame] | 21 | extern int init_fpu(struct task_struct *child); |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame^] | 22 | extern void fpu_finit(struct fpu *fpu); |
Jaswinder Singh | 3645493 | 2008-07-21 22:31:57 +0530 | [diff] [blame] | 23 | extern int dump_fpu(struct pt_regs *, struct user_i387_struct *); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 24 | extern void math_state_restore(void); |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 25 | |
Linus Torvalds | 8546c00 | 2012-02-21 10:25:45 -0800 | [diff] [blame] | 26 | extern bool irq_fpu_usable(void); |
| 27 | extern void kernel_fpu_begin(void); |
| 28 | extern void kernel_fpu_end(void); |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 29 | |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 30 | /* |
| 31 | * Some instructions like VIA's padlock instructions generate a spurious |
| 32 | * DNA fault but don't modify SSE registers. And these instructions |
Chuck Ebbert | 0b8c3d5 | 2009-06-09 10:40:50 -0400 | [diff] [blame] | 33 | * get used from interrupt context as well. To prevent these kernel instructions |
| 34 | * in interrupt context interacting wrongly with other user/kernel fpu usage, we |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 35 | * should use them only in the context of irq_ts_save/restore() |
| 36 | */ |
| 37 | static inline int irq_ts_save(void) |
| 38 | { |
| 39 | /* |
Chuck Ebbert | 0b8c3d5 | 2009-06-09 10:40:50 -0400 | [diff] [blame] | 40 | * If in process context and not atomic, we can take a spurious DNA fault. |
| 41 | * Otherwise, doing clts() in process context requires disabling preemption |
| 42 | * or some heavy lifting like kernel_fpu_begin() |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 43 | */ |
Chuck Ebbert | 0b8c3d5 | 2009-06-09 10:40:50 -0400 | [diff] [blame] | 44 | if (!in_atomic()) |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 45 | return 0; |
| 46 | |
| 47 | if (read_cr0() & X86_CR0_TS) { |
| 48 | clts(); |
| 49 | return 1; |
| 50 | } |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static inline void irq_ts_restore(int TS_state) |
| 56 | { |
| 57 | if (TS_state) |
| 58 | stts(); |
| 59 | } |
| 60 | |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 61 | /* |
Linus Torvalds | 15d8791 | 2012-02-16 09:15:04 -0800 | [diff] [blame] | 62 | * The question "does this thread have fpu access?" |
| 63 | * is slightly racy, since preemption could come in |
| 64 | * and revoke it immediately after the test. |
| 65 | * |
| 66 | * However, even in that very unlikely scenario, |
| 67 | * we can just assume we have FPU access - typically |
| 68 | * to save the FP state - we'll just take a #NM |
| 69 | * fault and get the FPU access back. |
Linus Torvalds | 15d8791 | 2012-02-16 09:15:04 -0800 | [diff] [blame] | 70 | */ |
| 71 | static inline int user_has_fpu(void) |
| 72 | { |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 73 | return current->thread.fpu.has_fpu; |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Linus Torvalds | 8546c00 | 2012-02-21 10:25:45 -0800 | [diff] [blame] | 76 | extern void unlazy_fpu(struct task_struct *tsk); |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 77 | |
Herbert Xu | 3b0d659 | 2009-11-03 09:11:15 -0500 | [diff] [blame] | 78 | #endif /* __ASSEMBLY__ */ |
| 79 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 80 | #endif /* _ASM_X86_I387_H */ |