2 * xsave/xrstor support.
4 * Author: Suresh Siddha <suresh.b.siddha@intel.com>
6 #include <linux/bootmem.h>
7 #include <linux/compat.h>
9 #ifdef CONFIG_IA32_EMULATION
10 #include <asm/sigcontext32.h>
15 * Supported feature mask by the CPU and the kernel.
20 * Represents init state for the supported extended state.
22 static struct xsave_struct *init_xstate_buf;
24 struct _fpx_sw_bytes fx_sw_reserved;
25 #ifdef CONFIG_IA32_EMULATION
26 struct _fpx_sw_bytes fx_sw_reserved_ia32;
29 static unsigned int *xstate_offsets, *xstate_sizes, xstate_features;
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.
41 void __sanitize_i387_state(struct task_struct *tsk)
44 int feature_bit = 0x2;
45 struct i387_fxsave_struct *fx = &tsk->thread.fpu.state->fxsave;
50 BUG_ON(task_thread_info(tsk)->status & TS_USEDFPU);
52 xstate_bv = tsk->thread.fpu.state->xsave.xsave_hdr.xstate_bv;
55 * None of the feature bits are in init state. So nothing else
56 * to do for us, as the memory layout is up to date.
58 if ((xstate_bv & pcntxt_mask) == pcntxt_mask)
64 if (!(xstate_bv & XSTATE_FP)) {
71 memset(&fx->st_space[0], 0, 128);
75 * SSE is in init state
77 if (!(xstate_bv & XSTATE_SSE))
78 memset(&fx->xmm_space[0], 0, 256);
80 xstate_bv = (pcntxt_mask & ~xstate_bv) >> 2;
83 * Update all the other memory layouts for which the corresponding
84 * header bit is in the init state.
87 if (xstate_bv & 0x1) {
88 int offset = xstate_offsets[feature_bit];
89 int size = xstate_sizes[feature_bit];
91 memcpy(((void *) fx) + offset,
92 ((void *) init_xstate_buf) + offset,
102 * Check for the presence of extended state information in the
103 * user fpstate pointer in the sigcontext.
105 int check_for_xstate(struct i387_fxsave_struct __user *buf,
106 void __user *fpstate,
107 struct _fpx_sw_bytes *fx_sw_user)
109 int min_xstate_size = sizeof(struct i387_fxsave_struct) +
110 sizeof(struct xsave_hdr_struct);
114 err = __copy_from_user(fx_sw_user, &buf->sw_reserved[0],
115 sizeof(struct _fpx_sw_bytes));
120 * First Magic check failed.
122 if (fx_sw_user->magic1 != FP_XSTATE_MAGIC1)
126 * Check for error scenarios.
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)
133 err = __get_user(magic2, (__u32 *) (((void *)fpstate) +
134 fx_sw_user->extended_size -
135 FP_XSTATE_MAGIC2_SIZE));
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.
144 if (magic2 != FP_XSTATE_MAGIC2)
152 * Signal frame handlers.
155 int save_i387_xstate(void __user *buf)
157 struct task_struct *tsk = current;
160 if (!access_ok(VERIFY_WRITE, buf, sig_xstate_size))
163 BUG_ON(sig_xstate_size < xstate_size);
165 if ((unsigned long)buf % 64)
166 printk("save_i387_xstate: bad fpstate %p\n", buf);
171 if (user_has_fpu()) {
173 err = xsave_user(buf);
175 err = fxsave_user(buf);
181 sanitize_i387_state(tsk);
182 if (__copy_to_user(buf, &tsk->thread.fpu.state->fxsave,
187 clear_used_math(); /* trigger finit */
190 struct _fpstate __user *fx = buf;
191 struct _xstate __user *x = buf;
194 err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved,
195 sizeof(struct _fpx_sw_bytes));
197 err |= __put_user(FP_XSTATE_MAGIC2,
198 (__u32 __user *) (buf + sig_xstate_size
199 - FP_XSTATE_MAGIC2_SIZE));
202 * Read the xstate_bv which we copied (directly from the cpu or
203 * from the state in task struct) to the user buffers and
204 * set the FP/SSE bits.
206 err |= __get_user(xstate_bv, &x->xstate_hdr.xstate_bv);
209 * For legacy compatible, we always set FP/SSE bits in the bit
210 * vector while saving the state to the user context. This will
211 * enable us capturing any changes(during sigreturn) to
212 * the FP/SSE bits by the legacy applications which don't touch
213 * xstate_bv in the xsave header.
215 * xsave aware apps can change the xstate_bv in the xsave
216 * header as well as change any contents in the memory layout.
217 * xrestore as part of sigreturn will capture all the changes.
219 xstate_bv |= XSTATE_FPSSE;
221 err |= __put_user(xstate_bv, &x->xstate_hdr.xstate_bv);
231 * Restore the extended state if present. Otherwise, restore the FP/SSE
234 static int restore_user_xstate(void __user *buf)
236 struct _fpx_sw_bytes fx_sw_user;
240 if (((unsigned long)buf % 64) ||
241 check_for_xstate(buf, buf, &fx_sw_user))
244 mask = fx_sw_user.xstate_bv;
247 * restore the state passed by the user.
249 err = xrestore_user(buf, mask);
254 * init the state skipped by the user.
256 mask = pcntxt_mask & ~mask;
258 xrstor_state(init_xstate_buf, mask);
264 * couldn't find the extended state information in the
265 * memory layout. Restore just the FP/SSE and init all
266 * the other extended state.
268 xrstor_state(init_xstate_buf, pcntxt_mask & ~XSTATE_FPSSE);
269 return fxrstor_checking((__force struct i387_fxsave_struct *)buf);
273 * This restores directly out of user space. Exceptions are handled.
275 int restore_i387_xstate(void __user *buf)
277 struct task_struct *tsk = current;
285 if (!access_ok(VERIFY_READ, buf, sig_xstate_size))
296 err = restore_user_xstate(buf);
298 err = fxrstor_checking((__force struct i387_fxsave_struct *)
302 * Encountered an error while doing the restore from the
303 * user buffer, clear the fpu state.
314 * Prepare the SW reserved portion of the fxsave memory layout, indicating
315 * the presence of the extended state information in the memory layout
316 * pointed by the fpstate pointer in the sigcontext.
317 * This will be saved when ever the FP and extended state context is
318 * saved on the user stack during the signal handler delivery to the user.
320 static void prepare_fx_sw_frame(void)
322 int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) +
323 FP_XSTATE_MAGIC2_SIZE;
325 sig_xstate_size = sizeof(struct _fpstate) + size_extended;
327 #ifdef CONFIG_IA32_EMULATION
328 sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended;
331 memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved));
333 fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
334 fx_sw_reserved.extended_size = sig_xstate_size;
335 fx_sw_reserved.xstate_bv = pcntxt_mask;
336 fx_sw_reserved.xstate_size = xstate_size;
337 #ifdef CONFIG_IA32_EMULATION
338 memcpy(&fx_sw_reserved_ia32, &fx_sw_reserved,
339 sizeof(struct _fpx_sw_bytes));
340 fx_sw_reserved_ia32.extended_size = sig_xstate_ia32_size;
345 unsigned int sig_xstate_size = sizeof(struct _fpstate);
349 * Enable the extended processor state save/restore feature
351 static inline void xstate_enable(void)
353 set_in_cr4(X86_CR4_OSXSAVE);
354 xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
358 * Record the offsets and sizes of different state managed by the xsave
361 static void __init setup_xstate_features(void)
363 int eax, ebx, ecx, edx, leaf = 0x2;
365 xstate_features = fls64(pcntxt_mask);
366 xstate_offsets = alloc_bootmem(xstate_features * sizeof(int));
367 xstate_sizes = alloc_bootmem(xstate_features * sizeof(int));
370 cpuid_count(XSTATE_CPUID, leaf, &eax, &ebx, &ecx, &edx);
375 xstate_offsets[leaf] = ebx;
376 xstate_sizes[leaf] = eax;
383 * setup the xstate image representing the init state
385 static void __init setup_xstate_init(void)
387 setup_xstate_features();
390 * Setup init_xstate_buf to represent the init state of
391 * all the features managed by the xsave
393 init_xstate_buf = alloc_bootmem_align(xstate_size,
394 __alignof__(struct xsave_struct));
395 init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
399 * Init all the features state with header_bv being 0x0
401 xrstor_state(init_xstate_buf, -1);
403 * Dump the init state again. This is to identify the init state
404 * of any feature which is not represented by all zero's.
406 xsave_state(init_xstate_buf, -1);
411 * Enable and initialize the xsave feature.
413 static void __init xstate_enable_boot_cpu(void)
415 unsigned int eax, ebx, ecx, edx;
417 if (boot_cpu_data.cpuid_level < XSTATE_CPUID) {
418 WARN(1, KERN_ERR "XSTATE_CPUID missing\n");
422 cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
423 pcntxt_mask = eax + ((u64)edx << 32);
425 if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
426 printk(KERN_ERR "FP/SSE not shown under xsave features 0x%llx\n",
432 * Support only the state known to OS.
434 pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
439 * Recompute the context size for enabled features
441 cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
444 update_regset_xstate_info(xstate_size, pcntxt_mask);
445 prepare_fx_sw_frame();
449 printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%llx, "
451 pcntxt_mask, xstate_size);
455 * For the very first instance, this calls xstate_enable_boot_cpu();
456 * for all subsequent instances, this calls xstate_enable().
458 * This is somewhat obfuscated due to the lack of powerful enough
459 * overrides for the section checks.
461 void __cpuinit xsave_init(void)
463 static __refdata void (*next_func)(void) = xstate_enable_boot_cpu;
464 void (*this_func)(void);
469 this_func = next_func;
470 next_func = xstate_enable;