Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * xsave/xrstor support. |
| 3 | * |
| 4 | * Author: Suresh Siddha <suresh.b.siddha@intel.com> |
| 5 | */ |
| 6 | #include <linux/bootmem.h> |
| 7 | #include <linux/compat.h> |
| 8 | #include <asm/i387.h> |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 9 | #ifdef CONFIG_IA32_EMULATION |
| 10 | #include <asm/sigcontext32.h> |
| 11 | #endif |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 12 | #include <asm/xcr.h> |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 13 | |
| 14 | /* |
| 15 | * Supported feature mask by the CPU and the kernel. |
| 16 | */ |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 17 | u64 pcntxt_mask; |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 18 | |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 19 | struct _fpx_sw_bytes fx_sw_reserved; |
| 20 | #ifdef CONFIG_IA32_EMULATION |
| 21 | struct _fpx_sw_bytes fx_sw_reserved_ia32; |
| 22 | #endif |
| 23 | |
| 24 | /* |
| 25 | * Check for the presence of extended state information in the |
| 26 | * user fpstate pointer in the sigcontext. |
| 27 | */ |
| 28 | int check_for_xstate(struct i387_fxsave_struct __user *buf, |
| 29 | void __user *fpstate, |
| 30 | struct _fpx_sw_bytes *fx_sw_user) |
| 31 | { |
| 32 | int min_xstate_size = sizeof(struct i387_fxsave_struct) + |
| 33 | sizeof(struct xsave_hdr_struct); |
| 34 | unsigned int magic2; |
| 35 | int err; |
| 36 | |
| 37 | err = __copy_from_user(fx_sw_user, &buf->sw_reserved[0], |
| 38 | sizeof(struct _fpx_sw_bytes)); |
| 39 | |
| 40 | if (err) |
| 41 | return err; |
| 42 | |
| 43 | /* |
| 44 | * First Magic check failed. |
| 45 | */ |
| 46 | if (fx_sw_user->magic1 != FP_XSTATE_MAGIC1) |
| 47 | return -1; |
| 48 | |
| 49 | /* |
| 50 | * Check for error scenarios. |
| 51 | */ |
| 52 | if (fx_sw_user->xstate_size < min_xstate_size || |
| 53 | fx_sw_user->xstate_size > xstate_size || |
| 54 | fx_sw_user->xstate_size > fx_sw_user->extended_size) |
| 55 | return -1; |
| 56 | |
| 57 | err = __get_user(magic2, (__u32 *) (((void *)fpstate) + |
| 58 | fx_sw_user->extended_size - |
| 59 | FP_XSTATE_MAGIC2_SIZE)); |
| 60 | /* |
| 61 | * Check for the presence of second magic word at the end of memory |
| 62 | * layout. This detects the case where the user just copied the legacy |
| 63 | * fpstate layout with out copying the extended state information |
| 64 | * in the memory layout. |
| 65 | */ |
| 66 | if (err || magic2 != FP_XSTATE_MAGIC2) |
| 67 | return -1; |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 72 | #ifdef CONFIG_X86_64 |
| 73 | /* |
| 74 | * Signal frame handlers. |
| 75 | */ |
| 76 | |
| 77 | int save_i387_xstate(void __user *buf) |
| 78 | { |
| 79 | struct task_struct *tsk = current; |
| 80 | int err = 0; |
| 81 | |
| 82 | if (!access_ok(VERIFY_WRITE, buf, sig_xstate_size)) |
| 83 | return -EACCES; |
| 84 | |
Suresh Siddha | f65bc21 | 2008-08-13 11:38:15 -0700 | [diff] [blame] | 85 | BUG_ON(sig_xstate_size < xstate_size); |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 86 | |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 87 | if ((unsigned long)buf % 64) |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 88 | printk("save_i387_xstate: bad fpstate %p\n", buf); |
| 89 | |
| 90 | if (!used_math()) |
| 91 | return 0; |
| 92 | clear_used_math(); /* trigger finit */ |
| 93 | if (task_thread_info(tsk)->status & TS_USEDFPU) { |
Suresh Siddha | ed40595 | 2008-08-13 11:38:14 -0700 | [diff] [blame] | 94 | /* |
| 95 | * Start with clearing the user buffer. This will present a |
| 96 | * clean context for the bytes not touched by the fxsave/xsave. |
| 97 | */ |
| 98 | __clear_user(buf, sig_xstate_size); |
| 99 | |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 100 | if (task_thread_info(tsk)->status & TS_XSAVE) |
| 101 | err = xsave_user(buf); |
| 102 | else |
| 103 | err = fxsave_user(buf); |
| 104 | |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 105 | if (err) |
| 106 | return err; |
| 107 | task_thread_info(tsk)->status &= ~TS_USEDFPU; |
| 108 | stts(); |
| 109 | } else { |
| 110 | if (__copy_to_user(buf, &tsk->thread.xstate->fxsave, |
| 111 | xstate_size)) |
| 112 | return -1; |
| 113 | } |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 114 | |
| 115 | if (task_thread_info(tsk)->status & TS_XSAVE) { |
| 116 | struct _fpstate __user *fx = buf; |
| 117 | |
| 118 | err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved, |
| 119 | sizeof(struct _fpx_sw_bytes)); |
| 120 | |
| 121 | err |= __put_user(FP_XSTATE_MAGIC2, |
| 122 | (__u32 __user *) (buf + sig_xstate_size |
| 123 | - FP_XSTATE_MAGIC2_SIZE)); |
| 124 | } |
| 125 | |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 126 | return 1; |
| 127 | } |
| 128 | |
| 129 | /* |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 130 | * Restore the extended state if present. Otherwise, restore the FP/SSE |
| 131 | * state. |
| 132 | */ |
| 133 | int restore_user_xstate(void __user *buf) |
| 134 | { |
| 135 | struct _fpx_sw_bytes fx_sw_user; |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 136 | u64 mask; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 137 | int err; |
| 138 | |
| 139 | if (((unsigned long)buf % 64) || |
| 140 | check_for_xstate(buf, buf, &fx_sw_user)) |
| 141 | goto fx_only; |
| 142 | |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 143 | mask = fx_sw_user.xstate_bv; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 144 | |
| 145 | /* |
| 146 | * restore the state passed by the user. |
| 147 | */ |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 148 | err = xrestore_user(buf, mask); |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 149 | if (err) |
| 150 | return err; |
| 151 | |
| 152 | /* |
| 153 | * init the state skipped by the user. |
| 154 | */ |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 155 | mask = pcntxt_mask & ~mask; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 156 | |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 157 | xrstor_state(init_xstate_buf, mask); |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 158 | |
| 159 | return 0; |
| 160 | |
| 161 | fx_only: |
| 162 | /* |
| 163 | * couldn't find the extended state information in the |
| 164 | * memory layout. Restore just the FP/SSE and init all |
| 165 | * the other extended state. |
| 166 | */ |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 167 | xrstor_state(init_xstate_buf, pcntxt_mask & ~XSTATE_FPSSE); |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 168 | return fxrstor_checking((__force struct i387_fxsave_struct *)buf); |
| 169 | } |
| 170 | |
| 171 | /* |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 172 | * This restores directly out of user space. Exceptions are handled. |
| 173 | */ |
| 174 | int restore_i387_xstate(void __user *buf) |
| 175 | { |
| 176 | struct task_struct *tsk = current; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 177 | int err = 0; |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 178 | |
| 179 | if (!buf) { |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 180 | if (used_math()) |
| 181 | goto clear; |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 182 | return 0; |
| 183 | } else |
| 184 | if (!access_ok(VERIFY_READ, buf, sig_xstate_size)) |
| 185 | return -EACCES; |
| 186 | |
| 187 | if (!used_math()) { |
| 188 | err = init_fpu(tsk); |
| 189 | if (err) |
| 190 | return err; |
| 191 | } |
| 192 | |
| 193 | if (!(task_thread_info(current)->status & TS_USEDFPU)) { |
| 194 | clts(); |
| 195 | task_thread_info(current)->status |= TS_USEDFPU; |
| 196 | } |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 197 | if (task_thread_info(tsk)->status & TS_XSAVE) |
| 198 | err = restore_user_xstate(buf); |
| 199 | else |
| 200 | err = fxrstor_checking((__force struct i387_fxsave_struct *) |
| 201 | buf); |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 202 | if (unlikely(err)) { |
| 203 | /* |
| 204 | * Encountered an error while doing the restore from the |
| 205 | * user buffer, clear the fpu state. |
| 206 | */ |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 207 | clear: |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 208 | clear_fpu(tsk); |
| 209 | clear_used_math(); |
| 210 | } |
| 211 | return err; |
| 212 | } |
| 213 | #endif |
| 214 | |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 215 | /* |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 216 | * Prepare the SW reserved portion of the fxsave memory layout, indicating |
| 217 | * the presence of the extended state information in the memory layout |
| 218 | * pointed by the fpstate pointer in the sigcontext. |
| 219 | * This will be saved when ever the FP and extended state context is |
| 220 | * saved on the user stack during the signal handler delivery to the user. |
| 221 | */ |
| 222 | void prepare_fx_sw_frame(void) |
| 223 | { |
| 224 | int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) + |
| 225 | FP_XSTATE_MAGIC2_SIZE; |
| 226 | |
| 227 | sig_xstate_size = sizeof(struct _fpstate) + size_extended; |
| 228 | |
| 229 | #ifdef CONFIG_IA32_EMULATION |
| 230 | sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended; |
| 231 | #endif |
| 232 | |
| 233 | memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved)); |
| 234 | |
| 235 | fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1; |
| 236 | fx_sw_reserved.extended_size = sig_xstate_size; |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 237 | fx_sw_reserved.xstate_bv = pcntxt_mask; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 238 | fx_sw_reserved.xstate_size = xstate_size; |
| 239 | #ifdef CONFIG_IA32_EMULATION |
| 240 | memcpy(&fx_sw_reserved_ia32, &fx_sw_reserved, |
| 241 | sizeof(struct _fpx_sw_bytes)); |
| 242 | fx_sw_reserved_ia32.extended_size = sig_xstate_ia32_size; |
| 243 | #endif |
| 244 | } |
| 245 | |
| 246 | /* |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 247 | * Represents init state for the supported extended state. |
| 248 | */ |
| 249 | struct xsave_struct *init_xstate_buf; |
| 250 | |
Suresh Siddha | 3c1c7f1 | 2008-07-29 10:29:21 -0700 | [diff] [blame] | 251 | #ifdef CONFIG_X86_64 |
| 252 | unsigned int sig_xstate_size = sizeof(struct _fpstate); |
| 253 | #endif |
| 254 | |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 255 | /* |
| 256 | * Enable the extended processor state save/restore feature |
| 257 | */ |
| 258 | void __cpuinit xsave_init(void) |
| 259 | { |
| 260 | if (!cpu_has_xsave) |
| 261 | return; |
| 262 | |
| 263 | set_in_cr4(X86_CR4_OSXSAVE); |
| 264 | |
| 265 | /* |
| 266 | * Enable all the features that the HW is capable of |
| 267 | * and the Linux kernel is aware of. |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 268 | */ |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 269 | xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /* |
| 273 | * setup the xstate image representing the init state |
| 274 | */ |
Alexey Dobriyan | a19aac8 | 2008-08-30 06:03:34 +0400 | [diff] [blame^] | 275 | static void __init setup_xstate_init(void) |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 276 | { |
| 277 | init_xstate_buf = alloc_bootmem(xstate_size); |
| 278 | init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT; |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * Enable and initialize the xsave feature. |
| 283 | */ |
| 284 | void __init xsave_cntxt_init(void) |
| 285 | { |
| 286 | unsigned int eax, ebx, ecx, edx; |
| 287 | |
| 288 | cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx); |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 289 | pcntxt_mask = eax + ((u64)edx << 32); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 290 | |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 291 | if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) { |
| 292 | printk(KERN_ERR "FP/SSE not shown under xsave features 0x%llx\n", |
| 293 | pcntxt_mask); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 294 | BUG(); |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * for now OS knows only about FP/SSE |
| 299 | */ |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 300 | pcntxt_mask = pcntxt_mask & XCNTXT_MASK; |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 301 | xsave_init(); |
| 302 | |
| 303 | /* |
| 304 | * Recompute the context size for enabled features |
| 305 | */ |
| 306 | cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 307 | xstate_size = ebx; |
| 308 | |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 309 | prepare_fx_sw_frame(); |
| 310 | |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 311 | setup_xstate_init(); |
| 312 | |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 313 | printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%llx, " |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 314 | "cntxt size 0x%x\n", |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 315 | pcntxt_mask, xstate_size); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 316 | } |