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 | |
Robert Richter | 45c2d7f | 2010-07-21 19:03:55 +0200 | [diff] [blame] | 19 | /* |
| 20 | * Represents init state for the supported extended state. |
| 21 | */ |
| 22 | static struct xsave_struct *init_xstate_buf; |
| 23 | |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 24 | struct _fpx_sw_bytes fx_sw_reserved; |
| 25 | #ifdef CONFIG_IA32_EMULATION |
| 26 | struct _fpx_sw_bytes fx_sw_reserved_ia32; |
| 27 | #endif |
| 28 | |
Suresh Siddha | a1488f8 | 2010-07-19 16:05:48 -0700 | [diff] [blame] | 29 | static unsigned int *xstate_offsets, *xstate_sizes, xstate_features; |
| 30 | |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 31 | /* |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 32 | * If a processor implementation discern that a processor state component is |
| 33 | * in its initialized state it may modify the corresponding bit in the |
| 34 | * xsave_hdr.xstate_bv as '0', with out modifying the corresponding memory |
| 35 | * layout in the case of xsaveopt. While presenting the xstate information to |
| 36 | * the user, we always ensure that the memory layout of a feature will be in |
| 37 | * the init state if the corresponding header bit is zero. This is to ensure |
| 38 | * that the user doesn't see some stale state in the memory layout during |
| 39 | * signal handling, debugging etc. |
| 40 | */ |
| 41 | void __sanitize_i387_state(struct task_struct *tsk) |
| 42 | { |
| 43 | u64 xstate_bv; |
| 44 | int feature_bit = 0x2; |
| 45 | struct i387_fxsave_struct *fx = &tsk->thread.fpu.state->fxsave; |
| 46 | |
| 47 | if (!fx) |
| 48 | return; |
| 49 | |
| 50 | BUG_ON(task_thread_info(tsk)->status & TS_USEDFPU); |
| 51 | |
| 52 | xstate_bv = tsk->thread.fpu.state->xsave.xsave_hdr.xstate_bv; |
| 53 | |
| 54 | /* |
| 55 | * None of the feature bits are in init state. So nothing else |
| 56 | * to do for us, as the memory layout is upto date. |
| 57 | */ |
| 58 | if ((xstate_bv & pcntxt_mask) == pcntxt_mask) |
| 59 | return; |
| 60 | |
| 61 | /* |
| 62 | * FP is in init state |
| 63 | */ |
| 64 | if (!(xstate_bv & XSTATE_FP)) { |
| 65 | fx->cwd = 0x37f; |
| 66 | fx->swd = 0; |
| 67 | fx->twd = 0; |
| 68 | fx->fop = 0; |
| 69 | fx->rip = 0; |
| 70 | fx->rdp = 0; |
| 71 | memset(&fx->st_space[0], 0, 128); |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * SSE is in init state |
| 76 | */ |
| 77 | if (!(xstate_bv & XSTATE_SSE)) |
| 78 | memset(&fx->xmm_space[0], 0, 256); |
| 79 | |
| 80 | xstate_bv = (pcntxt_mask & ~xstate_bv) >> 2; |
| 81 | |
| 82 | /* |
| 83 | * Update all the other memory layouts for which the corresponding |
| 84 | * header bit is in the init state. |
| 85 | */ |
| 86 | while (xstate_bv) { |
| 87 | if (xstate_bv & 0x1) { |
| 88 | int offset = xstate_offsets[feature_bit]; |
| 89 | int size = xstate_sizes[feature_bit]; |
| 90 | |
| 91 | memcpy(((void *) fx) + offset, |
| 92 | ((void *) init_xstate_buf) + offset, |
| 93 | size); |
| 94 | } |
| 95 | |
| 96 | xstate_bv >>= 1; |
| 97 | feature_bit++; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /* |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 102 | * Check for the presence of extended state information in the |
| 103 | * user fpstate pointer in the sigcontext. |
| 104 | */ |
| 105 | int check_for_xstate(struct i387_fxsave_struct __user *buf, |
| 106 | void __user *fpstate, |
| 107 | struct _fpx_sw_bytes *fx_sw_user) |
| 108 | { |
| 109 | int min_xstate_size = sizeof(struct i387_fxsave_struct) + |
| 110 | sizeof(struct xsave_hdr_struct); |
| 111 | unsigned int magic2; |
| 112 | int err; |
| 113 | |
| 114 | err = __copy_from_user(fx_sw_user, &buf->sw_reserved[0], |
| 115 | sizeof(struct _fpx_sw_bytes)); |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 116 | if (err) |
Dan Carpenter | d6d4d42 | 2010-06-03 12:07:46 +0200 | [diff] [blame] | 117 | return -EFAULT; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * First Magic check failed. |
| 121 | */ |
| 122 | if (fx_sw_user->magic1 != FP_XSTATE_MAGIC1) |
Dan Carpenter | d6d4d42 | 2010-06-03 12:07:46 +0200 | [diff] [blame] | 123 | return -EINVAL; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 124 | |
| 125 | /* |
| 126 | * Check for error scenarios. |
| 127 | */ |
| 128 | if (fx_sw_user->xstate_size < min_xstate_size || |
| 129 | fx_sw_user->xstate_size > xstate_size || |
| 130 | fx_sw_user->xstate_size > fx_sw_user->extended_size) |
Dan Carpenter | d6d4d42 | 2010-06-03 12:07:46 +0200 | [diff] [blame] | 131 | return -EINVAL; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 132 | |
| 133 | err = __get_user(magic2, (__u32 *) (((void *)fpstate) + |
| 134 | fx_sw_user->extended_size - |
| 135 | FP_XSTATE_MAGIC2_SIZE)); |
Dan Carpenter | d6d4d42 | 2010-06-03 12:07:46 +0200 | [diff] [blame] | 136 | if (err) |
| 137 | return err; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 138 | /* |
| 139 | * Check for the presence of second magic word at the end of memory |
| 140 | * layout. This detects the case where the user just copied the legacy |
| 141 | * fpstate layout with out copying the extended state information |
| 142 | * in the memory layout. |
| 143 | */ |
Dan Carpenter | d6d4d42 | 2010-06-03 12:07:46 +0200 | [diff] [blame] | 144 | if (magic2 != FP_XSTATE_MAGIC2) |
| 145 | return -EFAULT; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 150 | #ifdef CONFIG_X86_64 |
| 151 | /* |
| 152 | * Signal frame handlers. |
| 153 | */ |
| 154 | |
| 155 | int save_i387_xstate(void __user *buf) |
| 156 | { |
| 157 | struct task_struct *tsk = current; |
| 158 | int err = 0; |
| 159 | |
| 160 | if (!access_ok(VERIFY_WRITE, buf, sig_xstate_size)) |
| 161 | return -EACCES; |
| 162 | |
Suresh Siddha | f65bc21 | 2008-08-13 11:38:15 -0700 | [diff] [blame] | 163 | BUG_ON(sig_xstate_size < xstate_size); |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 164 | |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 165 | if ((unsigned long)buf % 64) |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 166 | printk("save_i387_xstate: bad fpstate %p\n", buf); |
| 167 | |
| 168 | if (!used_math()) |
| 169 | return 0; |
Suresh Siddha | 06c38d5 | 2009-04-09 15:24:34 -0700 | [diff] [blame] | 170 | |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 171 | if (task_thread_info(tsk)->status & TS_USEDFPU) { |
Suresh Siddha | ed40595 | 2008-08-13 11:38:14 -0700 | [diff] [blame] | 172 | /* |
| 173 | * Start with clearing the user buffer. This will present a |
| 174 | * clean context for the bytes not touched by the fxsave/xsave. |
| 175 | */ |
Ingo Molnar | 9f48280 | 2008-08-18 12:59:32 +0200 | [diff] [blame] | 176 | err = __clear_user(buf, sig_xstate_size); |
| 177 | if (err) |
| 178 | return err; |
Suresh Siddha | ed40595 | 2008-08-13 11:38:14 -0700 | [diff] [blame] | 179 | |
Avi Kivity | c9ad488 | 2010-05-06 11:45:45 +0300 | [diff] [blame] | 180 | if (use_xsave()) |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 181 | err = xsave_user(buf); |
| 182 | else |
| 183 | err = fxsave_user(buf); |
| 184 | |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 185 | if (err) |
| 186 | return err; |
| 187 | task_thread_info(tsk)->status &= ~TS_USEDFPU; |
| 188 | stts(); |
| 189 | } else { |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 190 | sanitize_i387_state(tsk); |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 191 | if (__copy_to_user(buf, &tsk->thread.fpu.state->fxsave, |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 192 | xstate_size)) |
| 193 | return -1; |
| 194 | } |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 195 | |
Suresh Siddha | 06c38d5 | 2009-04-09 15:24:34 -0700 | [diff] [blame] | 196 | clear_used_math(); /* trigger finit */ |
| 197 | |
Avi Kivity | c9ad488 | 2010-05-06 11:45:45 +0300 | [diff] [blame] | 198 | if (use_xsave()) { |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 199 | struct _fpstate __user *fx = buf; |
Suresh Siddha | 04944b7 | 2008-10-07 14:04:28 -0700 | [diff] [blame] | 200 | struct _xstate __user *x = buf; |
| 201 | u64 xstate_bv; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 202 | |
| 203 | err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved, |
| 204 | sizeof(struct _fpx_sw_bytes)); |
| 205 | |
| 206 | err |= __put_user(FP_XSTATE_MAGIC2, |
| 207 | (__u32 __user *) (buf + sig_xstate_size |
| 208 | - FP_XSTATE_MAGIC2_SIZE)); |
Suresh Siddha | 04944b7 | 2008-10-07 14:04:28 -0700 | [diff] [blame] | 209 | |
| 210 | /* |
| 211 | * Read the xstate_bv which we copied (directly from the cpu or |
| 212 | * from the state in task struct) to the user buffers and |
| 213 | * set the FP/SSE bits. |
| 214 | */ |
| 215 | err |= __get_user(xstate_bv, &x->xstate_hdr.xstate_bv); |
| 216 | |
| 217 | /* |
| 218 | * For legacy compatible, we always set FP/SSE bits in the bit |
| 219 | * vector while saving the state to the user context. This will |
| 220 | * enable us capturing any changes(during sigreturn) to |
| 221 | * the FP/SSE bits by the legacy applications which don't touch |
| 222 | * xstate_bv in the xsave header. |
| 223 | * |
| 224 | * xsave aware apps can change the xstate_bv in the xsave |
| 225 | * header as well as change any contents in the memory layout. |
| 226 | * xrestore as part of sigreturn will capture all the changes. |
| 227 | */ |
| 228 | xstate_bv |= XSTATE_FPSSE; |
| 229 | |
| 230 | err |= __put_user(xstate_bv, &x->xstate_hdr.xstate_bv); |
| 231 | |
Suresh Siddha | f364ead | 2008-10-07 14:04:27 -0700 | [diff] [blame] | 232 | if (err) |
| 233 | return err; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 236 | return 1; |
| 237 | } |
| 238 | |
| 239 | /* |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 240 | * Restore the extended state if present. Otherwise, restore the FP/SSE |
| 241 | * state. |
| 242 | */ |
Jaswinder Singh Rajput | 7820b75 | 2008-12-30 22:05:55 +0530 | [diff] [blame] | 243 | static int restore_user_xstate(void __user *buf) |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 244 | { |
| 245 | struct _fpx_sw_bytes fx_sw_user; |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 246 | u64 mask; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 247 | int err; |
| 248 | |
| 249 | if (((unsigned long)buf % 64) || |
| 250 | check_for_xstate(buf, buf, &fx_sw_user)) |
| 251 | goto fx_only; |
| 252 | |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 253 | mask = fx_sw_user.xstate_bv; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 254 | |
| 255 | /* |
| 256 | * restore the state passed by the user. |
| 257 | */ |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 258 | err = xrestore_user(buf, mask); |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 259 | if (err) |
| 260 | return err; |
| 261 | |
| 262 | /* |
| 263 | * init the state skipped by the user. |
| 264 | */ |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 265 | mask = pcntxt_mask & ~mask; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 266 | |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 267 | xrstor_state(init_xstate_buf, mask); |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 268 | |
| 269 | return 0; |
| 270 | |
| 271 | fx_only: |
| 272 | /* |
| 273 | * couldn't find the extended state information in the |
| 274 | * memory layout. Restore just the FP/SSE and init all |
| 275 | * the other extended state. |
| 276 | */ |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 277 | xrstor_state(init_xstate_buf, pcntxt_mask & ~XSTATE_FPSSE); |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 278 | return fxrstor_checking((__force struct i387_fxsave_struct *)buf); |
| 279 | } |
| 280 | |
| 281 | /* |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 282 | * This restores directly out of user space. Exceptions are handled. |
| 283 | */ |
| 284 | int restore_i387_xstate(void __user *buf) |
| 285 | { |
| 286 | struct task_struct *tsk = current; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 287 | int err = 0; |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 288 | |
| 289 | if (!buf) { |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 290 | if (used_math()) |
| 291 | goto clear; |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 292 | return 0; |
| 293 | } else |
| 294 | if (!access_ok(VERIFY_READ, buf, sig_xstate_size)) |
| 295 | return -EACCES; |
| 296 | |
| 297 | if (!used_math()) { |
| 298 | err = init_fpu(tsk); |
| 299 | if (err) |
| 300 | return err; |
| 301 | } |
| 302 | |
| 303 | if (!(task_thread_info(current)->status & TS_USEDFPU)) { |
| 304 | clts(); |
| 305 | task_thread_info(current)->status |= TS_USEDFPU; |
| 306 | } |
Avi Kivity | c9ad488 | 2010-05-06 11:45:45 +0300 | [diff] [blame] | 307 | if (use_xsave()) |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 308 | err = restore_user_xstate(buf); |
| 309 | else |
| 310 | err = fxrstor_checking((__force struct i387_fxsave_struct *) |
| 311 | buf); |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 312 | if (unlikely(err)) { |
| 313 | /* |
| 314 | * Encountered an error while doing the restore from the |
| 315 | * user buffer, clear the fpu state. |
| 316 | */ |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 317 | clear: |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 318 | clear_fpu(tsk); |
| 319 | clear_used_math(); |
| 320 | } |
| 321 | return err; |
| 322 | } |
| 323 | #endif |
| 324 | |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 325 | /* |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 326 | * Prepare the SW reserved portion of the fxsave memory layout, indicating |
| 327 | * the presence of the extended state information in the memory layout |
| 328 | * pointed by the fpstate pointer in the sigcontext. |
| 329 | * This will be saved when ever the FP and extended state context is |
| 330 | * saved on the user stack during the signal handler delivery to the user. |
| 331 | */ |
roel kluin | 8bcad30 | 2008-10-21 19:49:09 -0400 | [diff] [blame] | 332 | static void prepare_fx_sw_frame(void) |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 333 | { |
| 334 | int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) + |
| 335 | FP_XSTATE_MAGIC2_SIZE; |
| 336 | |
| 337 | sig_xstate_size = sizeof(struct _fpstate) + size_extended; |
| 338 | |
| 339 | #ifdef CONFIG_IA32_EMULATION |
| 340 | sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended; |
| 341 | #endif |
| 342 | |
| 343 | memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved)); |
| 344 | |
| 345 | fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1; |
| 346 | fx_sw_reserved.extended_size = sig_xstate_size; |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 347 | fx_sw_reserved.xstate_bv = pcntxt_mask; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 348 | fx_sw_reserved.xstate_size = xstate_size; |
| 349 | #ifdef CONFIG_IA32_EMULATION |
| 350 | memcpy(&fx_sw_reserved_ia32, &fx_sw_reserved, |
| 351 | sizeof(struct _fpx_sw_bytes)); |
| 352 | fx_sw_reserved_ia32.extended_size = sig_xstate_ia32_size; |
| 353 | #endif |
| 354 | } |
| 355 | |
Suresh Siddha | 3c1c7f1 | 2008-07-29 10:29:21 -0700 | [diff] [blame] | 356 | #ifdef CONFIG_X86_64 |
| 357 | unsigned int sig_xstate_size = sizeof(struct _fpstate); |
| 358 | #endif |
| 359 | |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 360 | /* |
| 361 | * Enable the extended processor state save/restore feature |
| 362 | */ |
H. Peter Anvin | 1cff92d | 2010-07-21 14:23:10 -0700 | [diff] [blame^] | 363 | static inline void xstate_enable(void) |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 364 | { |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 365 | set_in_cr4(X86_CR4_OSXSAVE); |
H. Peter Anvin | 1cff92d | 2010-07-21 14:23:10 -0700 | [diff] [blame^] | 366 | xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /* |
Suresh Siddha | a1488f8 | 2010-07-19 16:05:48 -0700 | [diff] [blame] | 370 | * Record the offsets and sizes of different state managed by the xsave |
| 371 | * memory layout. |
| 372 | */ |
Robert Richter | 4995b9d | 2010-07-21 19:03:56 +0200 | [diff] [blame] | 373 | static void __init setup_xstate_features(void) |
Suresh Siddha | a1488f8 | 2010-07-19 16:05:48 -0700 | [diff] [blame] | 374 | { |
| 375 | int eax, ebx, ecx, edx, leaf = 0x2; |
| 376 | |
| 377 | xstate_features = fls64(pcntxt_mask); |
| 378 | xstate_offsets = alloc_bootmem(xstate_features * sizeof(int)); |
| 379 | xstate_sizes = alloc_bootmem(xstate_features * sizeof(int)); |
| 380 | |
| 381 | do { |
Robert Richter | ee813d5 | 2010-07-21 19:03:54 +0200 | [diff] [blame] | 382 | cpuid_count(XSTATE_CPUID, leaf, &eax, &ebx, &ecx, &edx); |
Suresh Siddha | a1488f8 | 2010-07-19 16:05:48 -0700 | [diff] [blame] | 383 | |
| 384 | if (eax == 0) |
| 385 | break; |
| 386 | |
| 387 | xstate_offsets[leaf] = ebx; |
| 388 | xstate_sizes[leaf] = eax; |
| 389 | |
| 390 | leaf++; |
| 391 | } while (1); |
| 392 | } |
| 393 | |
| 394 | /* |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 395 | * setup the xstate image representing the init state |
| 396 | */ |
Alexey Dobriyan | a19aac8 | 2008-08-30 06:03:34 +0400 | [diff] [blame] | 397 | static void __init setup_xstate_init(void) |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 398 | { |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 399 | setup_xstate_features(); |
| 400 | |
| 401 | /* |
| 402 | * Setup init_xstate_buf to represent the init state of |
| 403 | * all the features managed by the xsave |
| 404 | */ |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 405 | init_xstate_buf = alloc_bootmem(xstate_size); |
| 406 | init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT; |
Suresh Siddha | a1488f8 | 2010-07-19 16:05:48 -0700 | [diff] [blame] | 407 | |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 408 | clts(); |
| 409 | /* |
| 410 | * Init all the features state with header_bv being 0x0 |
| 411 | */ |
| 412 | xrstor_state(init_xstate_buf, -1); |
| 413 | /* |
| 414 | * Dump the init state again. This is to identify the init state |
| 415 | * of any feature which is not represented by all zero's. |
| 416 | */ |
| 417 | xsave_state(init_xstate_buf, -1); |
| 418 | stts(); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /* |
| 422 | * Enable and initialize the xsave feature. |
| 423 | */ |
H. Peter Anvin | 1cff92d | 2010-07-21 14:23:10 -0700 | [diff] [blame^] | 424 | static void __init xstate_enable_boot_cpu(void) |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 425 | { |
| 426 | unsigned int eax, ebx, ecx, edx; |
| 427 | |
Robert Richter | ee813d5 | 2010-07-21 19:03:54 +0200 | [diff] [blame] | 428 | if (boot_cpu_data.cpuid_level < XSTATE_CPUID) { |
| 429 | WARN(1, KERN_ERR "XSTATE_CPUID missing\n"); |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx); |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 434 | pcntxt_mask = eax + ((u64)edx << 32); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 435 | |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 436 | if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) { |
| 437 | printk(KERN_ERR "FP/SSE not shown under xsave features 0x%llx\n", |
| 438 | pcntxt_mask); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 439 | BUG(); |
| 440 | } |
| 441 | |
| 442 | /* |
Suresh Siddha | a30469e | 2009-04-10 15:21:24 -0700 | [diff] [blame] | 443 | * Support only the state known to OS. |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 444 | */ |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 445 | pcntxt_mask = pcntxt_mask & XCNTXT_MASK; |
Robert Richter | 97e80a7 | 2010-07-21 19:03:53 +0200 | [diff] [blame] | 446 | |
H. Peter Anvin | 1cff92d | 2010-07-21 14:23:10 -0700 | [diff] [blame^] | 447 | xstate_enable(); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 448 | |
| 449 | /* |
| 450 | * Recompute the context size for enabled features |
| 451 | */ |
Robert Richter | ee813d5 | 2010-07-21 19:03:54 +0200 | [diff] [blame] | 452 | cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 453 | xstate_size = ebx; |
| 454 | |
Suresh Siddha | 5b3efd5 | 2010-02-11 11:50:59 -0800 | [diff] [blame] | 455 | update_regset_xstate_info(xstate_size, pcntxt_mask); |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 456 | prepare_fx_sw_frame(); |
| 457 | |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 458 | setup_xstate_init(); |
| 459 | |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 460 | printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%llx, " |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 461 | "cntxt size 0x%x\n", |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 462 | pcntxt_mask, xstate_size); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 463 | } |
Robert Richter | 82d4150 | 2010-07-20 20:50:51 +0200 | [diff] [blame] | 464 | |
H. Peter Anvin | 1cff92d | 2010-07-21 14:23:10 -0700 | [diff] [blame^] | 465 | /* |
| 466 | * For the very first instance, this calls xstate_enable_boot_cpu(); |
| 467 | * for all subsequent instances, this calls xstate_enable(). |
| 468 | * |
| 469 | * This is somewhat obfuscated due to the lack of powerful enough |
| 470 | * overrides for the section checks. |
| 471 | */ |
Robert Richter | 82d4150 | 2010-07-20 20:50:51 +0200 | [diff] [blame] | 472 | void __cpuinit xsave_init(void) |
| 473 | { |
H. Peter Anvin | 1cff92d | 2010-07-21 14:23:10 -0700 | [diff] [blame^] | 474 | static __refdata void (*next_func)(void) = xstate_enable_boot_cpu; |
| 475 | void (*this_func)(void); |
| 476 | |
Robert Richter | 0e49bf6 | 2010-07-21 19:03:52 +0200 | [diff] [blame] | 477 | if (!cpu_has_xsave) |
| 478 | return; |
| 479 | |
H. Peter Anvin | 1cff92d | 2010-07-21 14:23:10 -0700 | [diff] [blame^] | 480 | this_func = next_func; |
| 481 | next_func = xstate_enable; |
| 482 | this_func(); |
Robert Richter | 82d4150 | 2010-07-20 20:50:51 +0200 | [diff] [blame] | 483 | } |