Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 1 | #ifndef __ASM_X86_XSAVE_H |
| 2 | #define __ASM_X86_XSAVE_H |
| 3 | |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 4 | #include <linux/types.h> |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 5 | #include <asm/processor.h> |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 6 | |
Robert Richter | ee813d5 | 2010-07-21 19:03:54 +0200 | [diff] [blame] | 7 | #define XSTATE_CPUID 0x0000000d |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 8 | |
| 9 | #define XSTATE_FP 0x1 |
| 10 | #define XSTATE_SSE 0x2 |
Suresh Siddha | a30469e | 2009-04-10 15:21:24 -0700 | [diff] [blame] | 11 | #define XSTATE_YMM 0x4 |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 12 | |
| 13 | #define XSTATE_FPSSE (XSTATE_FP | XSTATE_SSE) |
| 14 | |
| 15 | #define FXSAVE_SIZE 512 |
| 16 | |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 17 | #define XSAVE_HDR_SIZE 64 |
| 18 | #define XSAVE_HDR_OFFSET FXSAVE_SIZE |
| 19 | |
| 20 | #define XSAVE_YMM_SIZE 256 |
| 21 | #define XSAVE_YMM_OFFSET (XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET) |
Sheng Yang | 5ee481d | 2010-05-17 17:22:23 +0800 | [diff] [blame] | 22 | |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 23 | /* |
| 24 | * These are the features that the OS can handle currently. |
| 25 | */ |
Suresh Siddha | a30469e | 2009-04-10 15:21:24 -0700 | [diff] [blame] | 26 | #define XCNTXT_MASK (XSTATE_FP | XSTATE_SSE | XSTATE_YMM) |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 27 | |
Suresh Siddha | b359e8a | 2008-07-29 10:29:20 -0700 | [diff] [blame] | 28 | #ifdef CONFIG_X86_64 |
| 29 | #define REX_PREFIX "0x48, " |
| 30 | #else |
| 31 | #define REX_PREFIX |
| 32 | #endif |
| 33 | |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 34 | extern unsigned int xstate_size; |
| 35 | extern u64 pcntxt_mask; |
Suresh Siddha | 5b3efd5 | 2010-02-11 11:50:59 -0800 | [diff] [blame] | 36 | extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS]; |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame] | 37 | extern struct xsave_struct *init_xstate_buf; |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 38 | |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 39 | extern void xsave_init(void); |
Suresh Siddha | 5b3efd5 | 2010-02-11 11:50:59 -0800 | [diff] [blame] | 40 | extern void update_regset_xstate_info(unsigned int size, u64 xstate_mask); |
Suresh Siddha | b359e8a | 2008-07-29 10:29:20 -0700 | [diff] [blame] | 41 | extern int init_fpu(struct task_struct *child); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 42 | |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 43 | static inline int fpu_xrstor_checking(struct xsave_struct *fx) |
Suresh Siddha | b359e8a | 2008-07-29 10:29:20 -0700 | [diff] [blame] | 44 | { |
| 45 | int err; |
| 46 | |
| 47 | asm volatile("1: .byte " REX_PREFIX "0x0f,0xae,0x2f\n\t" |
| 48 | "2:\n" |
| 49 | ".section .fixup,\"ax\"\n" |
| 50 | "3: movl $-1,%[err]\n" |
| 51 | " jmp 2b\n" |
| 52 | ".previous\n" |
| 53 | _ASM_EXTABLE(1b, 3b) |
| 54 | : [err] "=r" (err) |
| 55 | : "D" (fx), "m" (*fx), "a" (-1), "d" (-1), "0" (0) |
| 56 | : "memory"); |
| 57 | |
| 58 | return err; |
| 59 | } |
| 60 | |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 61 | static inline int xsave_user(struct xsave_struct __user *buf) |
Suresh Siddha | 9dc89c0 | 2008-07-29 10:29:23 -0700 | [diff] [blame] | 62 | { |
| 63 | int err; |
Suresh Siddha | 8e221b6 | 2010-06-22 16:23:37 -0700 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * Clear the xsave header first, so that reserved fields are |
| 67 | * initialized to zero. |
| 68 | */ |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 69 | err = __clear_user(&buf->xsave_hdr, sizeof(buf->xsave_hdr)); |
Suresh Siddha | 8e221b6 | 2010-06-22 16:23:37 -0700 | [diff] [blame] | 70 | if (unlikely(err)) |
| 71 | return -EFAULT; |
| 72 | |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 73 | __asm__ __volatile__(ASM_STAC "\n" |
| 74 | "1: .byte " REX_PREFIX "0x0f,0xae,0x27\n" |
| 75 | "2: " ASM_CLAC "\n" |
Suresh Siddha | 9dc89c0 | 2008-07-29 10:29:23 -0700 | [diff] [blame] | 76 | ".section .fixup,\"ax\"\n" |
| 77 | "3: movl $-1,%[err]\n" |
| 78 | " jmp 2b\n" |
| 79 | ".previous\n" |
H. Peter Anvin | 7a040a4 | 2012-04-20 13:42:25 -0700 | [diff] [blame] | 80 | _ASM_EXTABLE(1b,3b) |
Suresh Siddha | 9dc89c0 | 2008-07-29 10:29:23 -0700 | [diff] [blame] | 81 | : [err] "=r" (err) |
| 82 | : "D" (buf), "a" (-1), "d" (-1), "0" (0) |
| 83 | : "memory"); |
Suresh Siddha | 9dc89c0 | 2008-07-29 10:29:23 -0700 | [diff] [blame] | 84 | return err; |
| 85 | } |
| 86 | |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 87 | static inline int xrestore_user(struct xsave_struct __user *buf, u64 mask) |
Suresh Siddha | 9dc89c0 | 2008-07-29 10:29:23 -0700 | [diff] [blame] | 88 | { |
| 89 | int err; |
| 90 | struct xsave_struct *xstate = ((__force struct xsave_struct *)buf); |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 91 | u32 lmask = mask; |
| 92 | u32 hmask = mask >> 32; |
Suresh Siddha | 9dc89c0 | 2008-07-29 10:29:23 -0700 | [diff] [blame] | 93 | |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 94 | __asm__ __volatile__(ASM_STAC "\n" |
| 95 | "1: .byte " REX_PREFIX "0x0f,0xae,0x2f\n" |
| 96 | "2: " ASM_CLAC "\n" |
Suresh Siddha | 9dc89c0 | 2008-07-29 10:29:23 -0700 | [diff] [blame] | 97 | ".section .fixup,\"ax\"\n" |
| 98 | "3: movl $-1,%[err]\n" |
| 99 | " jmp 2b\n" |
| 100 | ".previous\n" |
H. Peter Anvin | 7a040a4 | 2012-04-20 13:42:25 -0700 | [diff] [blame] | 101 | _ASM_EXTABLE(1b,3b) |
Suresh Siddha | 9dc89c0 | 2008-07-29 10:29:23 -0700 | [diff] [blame] | 102 | : [err] "=r" (err) |
| 103 | : "D" (xstate), "a" (lmask), "d" (hmask), "0" (0) |
| 104 | : "memory"); /* memory required? */ |
| 105 | return err; |
| 106 | } |
| 107 | |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 108 | static inline void xrstor_state(struct xsave_struct *fx, u64 mask) |
Suresh Siddha | 9dc89c0 | 2008-07-29 10:29:23 -0700 | [diff] [blame] | 109 | { |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 110 | u32 lmask = mask; |
| 111 | u32 hmask = mask >> 32; |
| 112 | |
Suresh Siddha | 9dc89c0 | 2008-07-29 10:29:23 -0700 | [diff] [blame] | 113 | asm volatile(".byte " REX_PREFIX "0x0f,0xae,0x2f\n\t" |
| 114 | : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask) |
| 115 | : "memory"); |
| 116 | } |
| 117 | |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 118 | static inline void xsave_state(struct xsave_struct *fx, u64 mask) |
| 119 | { |
| 120 | u32 lmask = mask; |
| 121 | u32 hmask = mask >> 32; |
| 122 | |
| 123 | asm volatile(".byte " REX_PREFIX "0x0f,0xae,0x27\n\t" |
| 124 | : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask) |
| 125 | : "memory"); |
| 126 | } |
| 127 | |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 128 | static inline void fpu_xsave(struct fpu *fpu) |
Suresh Siddha | b359e8a | 2008-07-29 10:29:20 -0700 | [diff] [blame] | 129 | { |
| 130 | /* This, however, we can work around by forcing the compiler to select |
| 131 | an addressing mode that doesn't require extended registers. */ |
Suresh Siddha | 6bad06b | 2010-07-19 16:05:52 -0700 | [diff] [blame] | 132 | alternative_input( |
| 133 | ".byte " REX_PREFIX "0x0f,0xae,0x27", |
| 134 | ".byte " REX_PREFIX "0x0f,0xae,0x37", |
| 135 | X86_FEATURE_XSAVEOPT, |
| 136 | [fx] "D" (&fpu->state->xsave), "a" (-1), "d" (-1) : |
| 137 | "memory"); |
Suresh Siddha | b359e8a | 2008-07-29 10:29:20 -0700 | [diff] [blame] | 138 | } |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 139 | #endif |