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