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) |
Liu, Jinsong | 56c103e | 2014-02-21 17:39:02 +0000 | [diff] [blame] | 19 | /* Bit 63 of XCR0 is reserved for future expansion */ |
| 20 | #define XSTATE_EXTEND_MASK (~(XSTATE_FPSSE | (1ULL << 63))) |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 21 | |
| 22 | #define FXSAVE_SIZE 512 |
| 23 | |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 24 | #define XSAVE_HDR_SIZE 64 |
| 25 | #define XSAVE_HDR_OFFSET FXSAVE_SIZE |
| 26 | |
| 27 | #define XSAVE_YMM_SIZE 256 |
| 28 | #define XSAVE_YMM_OFFSET (XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET) |
Sheng Yang | 5ee481d | 2010-05-17 17:22:23 +0800 | [diff] [blame] | 29 | |
Qiaowei Ren | e7d820a | 2013-12-05 17:15:34 +0800 | [diff] [blame] | 30 | /* Supported features which support lazy state saving */ |
Fenghua Yu | c2bc11f | 2014-02-20 13:24:51 -0800 | [diff] [blame] | 31 | #define XSTATE_LAZY (XSTATE_FP | XSTATE_SSE | XSTATE_YMM \ |
| 32 | | XSTATE_OPMASK | XSTATE_ZMM_Hi256 | XSTATE_Hi16_ZMM) |
Qiaowei Ren | e7d820a | 2013-12-05 17:15:34 +0800 | [diff] [blame] | 33 | |
| 34 | /* Supported features which require eager state saving */ |
| 35 | #define XSTATE_EAGER (XSTATE_BNDREGS | XSTATE_BNDCSR) |
| 36 | |
| 37 | /* All currently supported features */ |
| 38 | #define XCNTXT_MASK (XSTATE_LAZY | XSTATE_EAGER) |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 39 | |
Suresh Siddha | b359e8a | 2008-07-29 10:29:20 -0700 | [diff] [blame] | 40 | #ifdef CONFIG_X86_64 |
| 41 | #define REX_PREFIX "0x48, " |
| 42 | #else |
| 43 | #define REX_PREFIX |
| 44 | #endif |
| 45 | |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 46 | extern unsigned int xstate_size; |
| 47 | extern u64 pcntxt_mask; |
Suresh Siddha | 5b3efd5 | 2010-02-11 11:50:59 -0800 | [diff] [blame] | 48 | extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS]; |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame] | 49 | extern struct xsave_struct *init_xstate_buf; |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 50 | |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 51 | extern void xsave_init(void); |
Suresh Siddha | 5b3efd5 | 2010-02-11 11:50:59 -0800 | [diff] [blame] | 52 | extern void update_regset_xstate_info(unsigned int size, u64 xstate_mask); |
Suresh Siddha | b359e8a | 2008-07-29 10:29:20 -0700 | [diff] [blame] | 53 | extern int init_fpu(struct task_struct *child); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 54 | |
H. Peter Anvin | c9e5a5a | 2014-05-30 08:19:21 -0700 | [diff] [blame] | 55 | /* These macros all use (%edi)/(%rdi) as the single memory argument. */ |
Fenghua Yu | 200b08a | 2014-05-29 11:12:34 -0700 | [diff] [blame] | 56 | #define XSAVE ".byte " REX_PREFIX "0x0f,0xae,0x27" |
| 57 | #define XSAVEOPT ".byte " REX_PREFIX "0x0f,0xae,0x37" |
| 58 | #define XSAVES ".byte " REX_PREFIX "0x0f,0xc7,0x2f" |
| 59 | #define XRSTOR ".byte " REX_PREFIX "0x0f,0xae,0x2f" |
| 60 | #define XRSTORS ".byte " REX_PREFIX "0x0f,0xc7,0x1f" |
| 61 | |
Fenghua Yu | b84e705 | 2014-05-29 11:12:35 -0700 | [diff] [blame] | 62 | #define xstate_fault ".section .fixup,\"ax\"\n" \ |
| 63 | "3: movl $-1,%[err]\n" \ |
| 64 | " jmp 2b\n" \ |
| 65 | ".previous\n" \ |
| 66 | _ASM_EXTABLE(1b, 3b) \ |
| 67 | : [err] "=r" (err) |
| 68 | |
Fenghua Yu | f31a9f7 | 2014-05-29 11:12:36 -0700 | [diff] [blame] | 69 | /* |
Fenghua Yu | adb9d52 | 2014-05-29 11:12:40 -0700 | [diff] [blame] | 70 | * This function is called only during boot time when x86 caps are not set |
| 71 | * up and alternative can not be used yet. |
| 72 | */ |
Borislav Petkov | d0f2dd1 | 2014-06-17 12:21:08 +0200 | [diff] [blame] | 73 | static inline int xsave_state_booting(struct xsave_struct *fx, u64 mask) |
Fenghua Yu | adb9d52 | 2014-05-29 11:12:40 -0700 | [diff] [blame] | 74 | { |
| 75 | u32 lmask = mask; |
| 76 | u32 hmask = mask >> 32; |
| 77 | int err = 0; |
| 78 | |
| 79 | WARN_ON(system_state != SYSTEM_BOOTING); |
| 80 | |
| 81 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
| 82 | asm volatile("1:"XSAVES"\n\t" |
| 83 | "2:\n\t" |
| 84 | : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask) |
| 85 | : "memory"); |
| 86 | else |
| 87 | asm volatile("1:"XSAVE"\n\t" |
| 88 | "2:\n\t" |
| 89 | : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask) |
| 90 | : "memory"); |
| 91 | |
| 92 | asm volatile(xstate_fault |
| 93 | : "0" (0) |
| 94 | : "memory"); |
| 95 | |
| 96 | return err; |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * This function is called only during boot time when x86 caps are not set |
| 101 | * up and alternative can not be used yet. |
| 102 | */ |
| 103 | static inline int xrstor_state_booting(struct xsave_struct *fx, u64 mask) |
| 104 | { |
| 105 | u32 lmask = mask; |
| 106 | u32 hmask = mask >> 32; |
| 107 | int err = 0; |
| 108 | |
| 109 | WARN_ON(system_state != SYSTEM_BOOTING); |
| 110 | |
| 111 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
| 112 | asm volatile("1:"XRSTORS"\n\t" |
| 113 | "2:\n\t" |
| 114 | : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask) |
| 115 | : "memory"); |
| 116 | else |
| 117 | asm volatile("1:"XRSTOR"\n\t" |
| 118 | "2:\n\t" |
| 119 | : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask) |
| 120 | : "memory"); |
| 121 | |
| 122 | asm volatile(xstate_fault |
| 123 | : "0" (0) |
| 124 | : "memory"); |
| 125 | |
| 126 | return err; |
| 127 | } |
| 128 | |
| 129 | /* |
Fenghua Yu | f31a9f7 | 2014-05-29 11:12:36 -0700 | [diff] [blame] | 130 | * Save processor xstate to xsave area. |
| 131 | */ |
| 132 | static inline int xsave_state(struct xsave_struct *fx, u64 mask) |
| 133 | { |
| 134 | u32 lmask = mask; |
| 135 | u32 hmask = mask >> 32; |
| 136 | int err = 0; |
| 137 | |
| 138 | /* |
| 139 | * If xsaves is enabled, xsaves replaces xsaveopt because |
| 140 | * it supports compact format and supervisor states in addition to |
| 141 | * modified optimization in xsaveopt. |
| 142 | * |
| 143 | * Otherwise, if xsaveopt is enabled, xsaveopt replaces xsave |
| 144 | * because xsaveopt supports modified optimization which is not |
| 145 | * supported by xsave. |
| 146 | * |
| 147 | * If none of xsaves and xsaveopt is enabled, use xsave. |
| 148 | */ |
| 149 | alternative_input_2( |
| 150 | "1:"XSAVE, |
| 151 | "1:"XSAVEOPT, |
| 152 | X86_FEATURE_XSAVEOPT, |
| 153 | "1:"XSAVES, |
| 154 | X86_FEATURE_XSAVES, |
| 155 | [fx] "D" (fx), "a" (lmask), "d" (hmask) : |
| 156 | "memory"); |
| 157 | asm volatile("2:\n\t" |
| 158 | xstate_fault |
| 159 | : "0" (0) |
| 160 | : "memory"); |
| 161 | |
| 162 | return err; |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Restore processor xstate from xsave area. |
| 167 | */ |
| 168 | static inline int xrstor_state(struct xsave_struct *fx, u64 mask) |
| 169 | { |
| 170 | int err = 0; |
| 171 | u32 lmask = mask; |
| 172 | u32 hmask = mask >> 32; |
| 173 | |
| 174 | /* |
| 175 | * Use xrstors to restore context if it is enabled. xrstors supports |
| 176 | * compacted format of xsave area which is not supported by xrstor. |
| 177 | */ |
| 178 | alternative_input( |
| 179 | "1: " XRSTOR, |
| 180 | "1: " XRSTORS, |
| 181 | X86_FEATURE_XSAVES, |
| 182 | "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask) |
| 183 | : "memory"); |
| 184 | |
| 185 | asm volatile("2:\n" |
| 186 | xstate_fault |
| 187 | : "0" (0) |
| 188 | : "memory"); |
| 189 | |
| 190 | return err; |
| 191 | } |
| 192 | |
Fenghua Yu | f9de314 | 2014-05-29 11:12:37 -0700 | [diff] [blame] | 193 | /* |
| 194 | * Save xstate context for old process during context switch. |
| 195 | */ |
| 196 | static inline void fpu_xsave(struct fpu *fpu) |
| 197 | { |
| 198 | xsave_state(&fpu->state->xsave, -1); |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Restore xstate context for new process during context switch. |
| 203 | */ |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 204 | static inline int fpu_xrstor_checking(struct xsave_struct *fx) |
Suresh Siddha | b359e8a | 2008-07-29 10:29:20 -0700 | [diff] [blame] | 205 | { |
Fenghua Yu | f9de314 | 2014-05-29 11:12:37 -0700 | [diff] [blame] | 206 | return xrstor_state(fx, -1); |
Suresh Siddha | b359e8a | 2008-07-29 10:29:20 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Fenghua Yu | facbf4d | 2014-05-29 11:12:38 -0700 | [diff] [blame] | 209 | /* |
| 210 | * Save xstate to user space xsave area. |
| 211 | * |
| 212 | * We don't use modified optimization because xrstor/xrstors might track |
| 213 | * a different application. |
| 214 | * |
| 215 | * We don't use compacted format xsave area for |
| 216 | * backward compatibility for old applications which don't understand |
| 217 | * compacted format of xsave area. |
| 218 | */ |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 219 | static inline int xsave_user(struct xsave_struct __user *buf) |
Suresh Siddha | 9dc89c0 | 2008-07-29 10:29:23 -0700 | [diff] [blame] | 220 | { |
| 221 | int err; |
Suresh Siddha | 8e221b6 | 2010-06-22 16:23:37 -0700 | [diff] [blame] | 222 | |
| 223 | /* |
| 224 | * Clear the xsave header first, so that reserved fields are |
| 225 | * initialized to zero. |
| 226 | */ |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 227 | err = __clear_user(&buf->xsave_hdr, sizeof(buf->xsave_hdr)); |
Suresh Siddha | 8e221b6 | 2010-06-22 16:23:37 -0700 | [diff] [blame] | 228 | if (unlikely(err)) |
| 229 | return -EFAULT; |
| 230 | |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 231 | __asm__ __volatile__(ASM_STAC "\n" |
Fenghua Yu | facbf4d | 2014-05-29 11:12:38 -0700 | [diff] [blame] | 232 | "1:"XSAVE"\n" |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 233 | "2: " ASM_CLAC "\n" |
Fenghua Yu | facbf4d | 2014-05-29 11:12:38 -0700 | [diff] [blame] | 234 | xstate_fault |
Suresh Siddha | 9dc89c0 | 2008-07-29 10:29:23 -0700 | [diff] [blame] | 235 | : "D" (buf), "a" (-1), "d" (-1), "0" (0) |
| 236 | : "memory"); |
Suresh Siddha | 9dc89c0 | 2008-07-29 10:29:23 -0700 | [diff] [blame] | 237 | return err; |
| 238 | } |
| 239 | |
Fenghua Yu | facbf4d | 2014-05-29 11:12:38 -0700 | [diff] [blame] | 240 | /* |
| 241 | * Restore xstate from user space xsave area. |
| 242 | */ |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 243 | static inline int xrestore_user(struct xsave_struct __user *buf, u64 mask) |
Suresh Siddha | 9dc89c0 | 2008-07-29 10:29:23 -0700 | [diff] [blame] | 244 | { |
Fenghua Yu | facbf4d | 2014-05-29 11:12:38 -0700 | [diff] [blame] | 245 | int err = 0; |
Suresh Siddha | 9dc89c0 | 2008-07-29 10:29:23 -0700 | [diff] [blame] | 246 | struct xsave_struct *xstate = ((__force struct xsave_struct *)buf); |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 247 | u32 lmask = mask; |
| 248 | u32 hmask = mask >> 32; |
Suresh Siddha | 9dc89c0 | 2008-07-29 10:29:23 -0700 | [diff] [blame] | 249 | |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 250 | __asm__ __volatile__(ASM_STAC "\n" |
Fenghua Yu | facbf4d | 2014-05-29 11:12:38 -0700 | [diff] [blame] | 251 | "1:"XRSTOR"\n" |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 252 | "2: " ASM_CLAC "\n" |
Fenghua Yu | facbf4d | 2014-05-29 11:12:38 -0700 | [diff] [blame] | 253 | xstate_fault |
Suresh Siddha | 9dc89c0 | 2008-07-29 10:29:23 -0700 | [diff] [blame] | 254 | : "D" (xstate), "a" (lmask), "d" (hmask), "0" (0) |
| 255 | : "memory"); /* memory required? */ |
| 256 | return err; |
| 257 | } |
| 258 | |
Fenghua Yu | 7496d64 | 2014-05-29 11:12:44 -0700 | [diff] [blame] | 259 | void *get_xsave_addr(struct xsave_struct *xsave, int xstate); |
| 260 | void setup_xstate_comp(void); |
| 261 | |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 262 | #endif |