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