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.
19 struct _fpx_sw_bytes fx_sw_reserved;
20 #ifdef CONFIG_IA32_EMULATION
21 struct _fpx_sw_bytes fx_sw_reserved_ia32;
24 static unsigned int *xstate_offsets, *xstate_sizes, xstate_features;
27 * Check for the presence of extended state information in the
28 * user fpstate pointer in the sigcontext.
30 int check_for_xstate(struct i387_fxsave_struct __user *buf,
32 struct _fpx_sw_bytes *fx_sw_user)
34 int min_xstate_size = sizeof(struct i387_fxsave_struct) +
35 sizeof(struct xsave_hdr_struct);
39 err = __copy_from_user(fx_sw_user, &buf->sw_reserved[0],
40 sizeof(struct _fpx_sw_bytes));
45 * First Magic check failed.
47 if (fx_sw_user->magic1 != FP_XSTATE_MAGIC1)
51 * Check for error scenarios.
53 if (fx_sw_user->xstate_size < min_xstate_size ||
54 fx_sw_user->xstate_size > xstate_size ||
55 fx_sw_user->xstate_size > fx_sw_user->extended_size)
58 err = __get_user(magic2, (__u32 *) (((void *)fpstate) +
59 fx_sw_user->extended_size -
60 FP_XSTATE_MAGIC2_SIZE));
64 * Check for the presence of second magic word at the end of memory
65 * layout. This detects the case where the user just copied the legacy
66 * fpstate layout with out copying the extended state information
67 * in the memory layout.
69 if (magic2 != FP_XSTATE_MAGIC2)
77 * Signal frame handlers.
80 int save_i387_xstate(void __user *buf)
82 struct task_struct *tsk = current;
85 if (!access_ok(VERIFY_WRITE, buf, sig_xstate_size))
88 BUG_ON(sig_xstate_size < xstate_size);
90 if ((unsigned long)buf % 64)
91 printk("save_i387_xstate: bad fpstate %p\n", buf);
96 if (task_thread_info(tsk)->status & TS_USEDFPU) {
98 * Start with clearing the user buffer. This will present a
99 * clean context for the bytes not touched by the fxsave/xsave.
101 err = __clear_user(buf, sig_xstate_size);
106 err = xsave_user(buf);
108 err = fxsave_user(buf);
112 task_thread_info(tsk)->status &= ~TS_USEDFPU;
115 if (__copy_to_user(buf, &tsk->thread.fpu.state->fxsave,
120 clear_used_math(); /* trigger finit */
123 struct _fpstate __user *fx = buf;
124 struct _xstate __user *x = buf;
127 err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved,
128 sizeof(struct _fpx_sw_bytes));
130 err |= __put_user(FP_XSTATE_MAGIC2,
131 (__u32 __user *) (buf + sig_xstate_size
132 - FP_XSTATE_MAGIC2_SIZE));
135 * Read the xstate_bv which we copied (directly from the cpu or
136 * from the state in task struct) to the user buffers and
137 * set the FP/SSE bits.
139 err |= __get_user(xstate_bv, &x->xstate_hdr.xstate_bv);
142 * For legacy compatible, we always set FP/SSE bits in the bit
143 * vector while saving the state to the user context. This will
144 * enable us capturing any changes(during sigreturn) to
145 * the FP/SSE bits by the legacy applications which don't touch
146 * xstate_bv in the xsave header.
148 * xsave aware apps can change the xstate_bv in the xsave
149 * header as well as change any contents in the memory layout.
150 * xrestore as part of sigreturn will capture all the changes.
152 xstate_bv |= XSTATE_FPSSE;
154 err |= __put_user(xstate_bv, &x->xstate_hdr.xstate_bv);
164 * Restore the extended state if present. Otherwise, restore the FP/SSE
167 static int restore_user_xstate(void __user *buf)
169 struct _fpx_sw_bytes fx_sw_user;
173 if (((unsigned long)buf % 64) ||
174 check_for_xstate(buf, buf, &fx_sw_user))
177 mask = fx_sw_user.xstate_bv;
180 * restore the state passed by the user.
182 err = xrestore_user(buf, mask);
187 * init the state skipped by the user.
189 mask = pcntxt_mask & ~mask;
191 xrstor_state(init_xstate_buf, mask);
197 * couldn't find the extended state information in the
198 * memory layout. Restore just the FP/SSE and init all
199 * the other extended state.
201 xrstor_state(init_xstate_buf, pcntxt_mask & ~XSTATE_FPSSE);
202 return fxrstor_checking((__force struct i387_fxsave_struct *)buf);
206 * This restores directly out of user space. Exceptions are handled.
208 int restore_i387_xstate(void __user *buf)
210 struct task_struct *tsk = current;
218 if (!access_ok(VERIFY_READ, buf, sig_xstate_size))
227 if (!(task_thread_info(current)->status & TS_USEDFPU)) {
229 task_thread_info(current)->status |= TS_USEDFPU;
232 err = restore_user_xstate(buf);
234 err = fxrstor_checking((__force struct i387_fxsave_struct *)
238 * Encountered an error while doing the restore from the
239 * user buffer, clear the fpu state.
250 * Prepare the SW reserved portion of the fxsave memory layout, indicating
251 * the presence of the extended state information in the memory layout
252 * pointed by the fpstate pointer in the sigcontext.
253 * This will be saved when ever the FP and extended state context is
254 * saved on the user stack during the signal handler delivery to the user.
256 static void prepare_fx_sw_frame(void)
258 int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) +
259 FP_XSTATE_MAGIC2_SIZE;
261 sig_xstate_size = sizeof(struct _fpstate) + size_extended;
263 #ifdef CONFIG_IA32_EMULATION
264 sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended;
267 memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved));
269 fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
270 fx_sw_reserved.extended_size = sig_xstate_size;
271 fx_sw_reserved.xstate_bv = pcntxt_mask;
272 fx_sw_reserved.xstate_size = xstate_size;
273 #ifdef CONFIG_IA32_EMULATION
274 memcpy(&fx_sw_reserved_ia32, &fx_sw_reserved,
275 sizeof(struct _fpx_sw_bytes));
276 fx_sw_reserved_ia32.extended_size = sig_xstate_ia32_size;
281 * Represents init state for the supported extended state.
283 struct xsave_struct *init_xstate_buf;
286 unsigned int sig_xstate_size = sizeof(struct _fpstate);
290 * Enable the extended processor state save/restore feature
292 void __cpuinit xsave_init(void)
297 set_in_cr4(X86_CR4_OSXSAVE);
300 * Enable all the features that the HW is capable of
301 * and the Linux kernel is aware of.
303 xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
307 * Record the offsets and sizes of different state managed by the xsave
310 static void setup_xstate_features(void)
312 int eax, ebx, ecx, edx, leaf = 0x2;
314 xstate_features = fls64(pcntxt_mask);
315 xstate_offsets = alloc_bootmem(xstate_features * sizeof(int));
316 xstate_sizes = alloc_bootmem(xstate_features * sizeof(int));
319 cpuid_count(0xd, leaf, &eax, &ebx, &ecx, &edx);
324 xstate_offsets[leaf] = ebx;
325 xstate_sizes[leaf] = eax;
332 * setup the xstate image representing the init state
334 static void __init setup_xstate_init(void)
336 init_xstate_buf = alloc_bootmem(xstate_size);
337 init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
339 setup_xstate_features();
343 * Enable and initialize the xsave feature.
345 void __ref xsave_cntxt_init(void)
347 unsigned int eax, ebx, ecx, edx;
349 cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
350 pcntxt_mask = eax + ((u64)edx << 32);
352 if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
353 printk(KERN_ERR "FP/SSE not shown under xsave features 0x%llx\n",
359 * Support only the state known to OS.
361 pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
365 * Recompute the context size for enabled features
367 cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
370 update_regset_xstate_info(xstate_size, pcntxt_mask);
371 prepare_fx_sw_frame();
375 printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%llx, "
377 pcntxt_mask, xstate_size);