2 * xsave/xrstor support.
4 * Author: Suresh Siddha <suresh.b.siddha@intel.com>
6 #include <linux/bootmem.h>
7 #include <linux/compat.h>
9 #include <asm/fpu-internal.h>
10 #ifdef CONFIG_IA32_EMULATION
11 #include <asm/sigcontext32.h>
16 * Supported feature mask by the CPU and the kernel.
21 * Represents init state for the supported extended state.
23 static struct xsave_struct *init_xstate_buf;
25 struct _fpx_sw_bytes fx_sw_reserved;
26 #ifdef CONFIG_IA32_EMULATION
27 struct _fpx_sw_bytes fx_sw_reserved_ia32;
30 static unsigned int *xstate_offsets, *xstate_sizes, xstate_features;
33 * If a processor implementation discern that a processor state component is
34 * in its initialized state it may modify the corresponding bit in the
35 * xsave_hdr.xstate_bv as '0', with out modifying the corresponding memory
36 * layout in the case of xsaveopt. While presenting the xstate information to
37 * the user, we always ensure that the memory layout of a feature will be in
38 * the init state if the corresponding header bit is zero. This is to ensure
39 * that the user doesn't see some stale state in the memory layout during
40 * signal handling, debugging etc.
42 void __sanitize_i387_state(struct task_struct *tsk)
45 int feature_bit = 0x2;
46 struct i387_fxsave_struct *fx = &tsk->thread.fpu.state->fxsave;
51 xstate_bv = tsk->thread.fpu.state->xsave.xsave_hdr.xstate_bv;
54 * None of the feature bits are in init state. So nothing else
55 * to do for us, as the memory layout is up to date.
57 if ((xstate_bv & pcntxt_mask) == pcntxt_mask)
63 if (!(xstate_bv & XSTATE_FP)) {
70 memset(&fx->st_space[0], 0, 128);
74 * SSE is in init state
76 if (!(xstate_bv & XSTATE_SSE))
77 memset(&fx->xmm_space[0], 0, 256);
79 xstate_bv = (pcntxt_mask & ~xstate_bv) >> 2;
82 * Update all the other memory layouts for which the corresponding
83 * header bit is in the init state.
86 if (xstate_bv & 0x1) {
87 int offset = xstate_offsets[feature_bit];
88 int size = xstate_sizes[feature_bit];
90 memcpy(((void *) fx) + offset,
91 ((void *) init_xstate_buf) + offset,
101 * Check for the presence of extended state information in the
102 * user fpstate pointer in the sigcontext.
104 int check_for_xstate(struct i387_fxsave_struct __user *buf,
105 void __user *fpstate,
106 struct _fpx_sw_bytes *fx_sw_user)
108 int min_xstate_size = sizeof(struct i387_fxsave_struct) +
109 sizeof(struct xsave_hdr_struct);
113 err = __copy_from_user(fx_sw_user, &buf->sw_reserved[0],
114 sizeof(struct _fpx_sw_bytes));
119 * First Magic check failed.
121 if (fx_sw_user->magic1 != FP_XSTATE_MAGIC1)
125 * Check for error scenarios.
127 if (fx_sw_user->xstate_size < min_xstate_size ||
128 fx_sw_user->xstate_size > xstate_size ||
129 fx_sw_user->xstate_size > fx_sw_user->extended_size)
132 err = __get_user(magic2, (__u32 *) (((void *)fpstate) +
133 fx_sw_user->extended_size -
134 FP_XSTATE_MAGIC2_SIZE));
138 * Check for the presence of second magic word at the end of memory
139 * layout. This detects the case where the user just copied the legacy
140 * fpstate layout with out copying the extended state information
141 * in the memory layout.
143 if (magic2 != FP_XSTATE_MAGIC2)
151 * Signal frame handlers.
154 int save_i387_xstate(void __user *buf)
156 struct task_struct *tsk = current;
159 if (!access_ok(VERIFY_WRITE, buf, sig_xstate_size))
162 BUG_ON(sig_xstate_size < xstate_size);
164 if ((unsigned long)buf % 64)
165 printk("save_i387_xstate: bad fpstate %p\n", buf);
170 if (user_has_fpu()) {
172 err = xsave_user(buf);
174 err = fxsave_user(buf);
180 sanitize_i387_state(tsk);
181 if (__copy_to_user(buf, &tsk->thread.fpu.state->fxsave,
186 clear_used_math(); /* trigger finit */
189 struct _fpstate __user *fx = buf;
190 struct _xstate __user *x = buf;
193 err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved,
194 sizeof(struct _fpx_sw_bytes));
196 err |= __put_user(FP_XSTATE_MAGIC2,
197 (__u32 __user *) (buf + sig_xstate_size
198 - FP_XSTATE_MAGIC2_SIZE));
201 * Read the xstate_bv which we copied (directly from the cpu or
202 * from the state in task struct) to the user buffers and
203 * set the FP/SSE bits.
205 err |= __get_user(xstate_bv, &x->xstate_hdr.xstate_bv);
208 * For legacy compatible, we always set FP/SSE bits in the bit
209 * vector while saving the state to the user context. This will
210 * enable us capturing any changes(during sigreturn) to
211 * the FP/SSE bits by the legacy applications which don't touch
212 * xstate_bv in the xsave header.
214 * xsave aware apps can change the xstate_bv in the xsave
215 * header as well as change any contents in the memory layout.
216 * xrestore as part of sigreturn will capture all the changes.
218 xstate_bv |= XSTATE_FPSSE;
220 err |= __put_user(xstate_bv, &x->xstate_hdr.xstate_bv);
230 * Restore the extended state if present. Otherwise, restore the FP/SSE
233 static int restore_user_xstate(void __user *buf)
235 struct _fpx_sw_bytes fx_sw_user;
239 if (((unsigned long)buf % 64) ||
240 check_for_xstate(buf, buf, &fx_sw_user))
243 mask = fx_sw_user.xstate_bv;
246 * restore the state passed by the user.
248 err = xrestore_user(buf, mask);
253 * init the state skipped by the user.
255 mask = pcntxt_mask & ~mask;
257 xrstor_state(init_xstate_buf, mask);
263 * couldn't find the extended state information in the
264 * memory layout. Restore just the FP/SSE and init all
265 * the other extended state.
267 xrstor_state(init_xstate_buf, pcntxt_mask & ~XSTATE_FPSSE);
268 return fxrstor_checking((__force struct i387_fxsave_struct *)buf);
272 * This restores directly out of user space. Exceptions are handled.
274 int restore_i387_xstate(void __user *buf)
276 struct task_struct *tsk = current;
284 if (!access_ok(VERIFY_READ, buf, sig_xstate_size))
295 err = restore_user_xstate(buf);
297 err = fxrstor_checking((__force struct i387_fxsave_struct *)
301 * Encountered an error while doing the restore from the
302 * user buffer, clear the fpu state.
313 * Prepare the SW reserved portion of the fxsave memory layout, indicating
314 * the presence of the extended state information in the memory layout
315 * pointed by the fpstate pointer in the sigcontext.
316 * This will be saved when ever the FP and extended state context is
317 * saved on the user stack during the signal handler delivery to the user.
319 static void prepare_fx_sw_frame(void)
321 int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) +
322 FP_XSTATE_MAGIC2_SIZE;
324 sig_xstate_size = sizeof(struct _fpstate) + size_extended;
326 #ifdef CONFIG_IA32_EMULATION
327 sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended;
330 memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved));
332 fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
333 fx_sw_reserved.extended_size = sig_xstate_size;
334 fx_sw_reserved.xstate_bv = pcntxt_mask;
335 fx_sw_reserved.xstate_size = xstate_size;
336 #ifdef CONFIG_IA32_EMULATION
337 memcpy(&fx_sw_reserved_ia32, &fx_sw_reserved,
338 sizeof(struct _fpx_sw_bytes));
339 fx_sw_reserved_ia32.extended_size = sig_xstate_ia32_size;
344 unsigned int sig_xstate_size = sizeof(struct _fpstate);
348 * Enable the extended processor state save/restore feature
350 static inline void xstate_enable(void)
352 set_in_cr4(X86_CR4_OSXSAVE);
353 xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
357 * Record the offsets and sizes of different state managed by the xsave
360 static void __init setup_xstate_features(void)
362 int eax, ebx, ecx, edx, leaf = 0x2;
364 xstate_features = fls64(pcntxt_mask);
365 xstate_offsets = alloc_bootmem(xstate_features * sizeof(int));
366 xstate_sizes = alloc_bootmem(xstate_features * sizeof(int));
369 cpuid_count(XSTATE_CPUID, leaf, &eax, &ebx, &ecx, &edx);
374 xstate_offsets[leaf] = ebx;
375 xstate_sizes[leaf] = eax;
382 * setup the xstate image representing the init state
384 static void __init setup_xstate_init(void)
386 setup_xstate_features();
389 * Setup init_xstate_buf to represent the init state of
390 * all the features managed by the xsave
392 init_xstate_buf = alloc_bootmem_align(xstate_size,
393 __alignof__(struct xsave_struct));
394 init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
398 * Init all the features state with header_bv being 0x0
400 xrstor_state(init_xstate_buf, -1);
402 * Dump the init state again. This is to identify the init state
403 * of any feature which is not represented by all zero's.
405 xsave_state(init_xstate_buf, -1);
410 * Enable and initialize the xsave feature.
412 static void __init xstate_enable_boot_cpu(void)
414 unsigned int eax, ebx, ecx, edx;
416 if (boot_cpu_data.cpuid_level < XSTATE_CPUID) {
417 WARN(1, KERN_ERR "XSTATE_CPUID missing\n");
421 cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
422 pcntxt_mask = eax + ((u64)edx << 32);
424 if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
425 printk(KERN_ERR "FP/SSE not shown under xsave features 0x%llx\n",
431 * Support only the state known to OS.
433 pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
438 * Recompute the context size for enabled features
440 cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
443 update_regset_xstate_info(xstate_size, pcntxt_mask);
444 prepare_fx_sw_frame();
448 printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%llx, "
450 pcntxt_mask, xstate_size);
454 * For the very first instance, this calls xstate_enable_boot_cpu();
455 * for all subsequent instances, this calls xstate_enable().
457 * This is somewhat obfuscated due to the lack of powerful enough
458 * overrides for the section checks.
460 void __cpuinit xsave_init(void)
462 static __refdata void (*next_func)(void) = xstate_enable_boot_cpu;
463 void (*this_func)(void);
468 this_func = next_func;
469 next_func = xstate_enable;