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;
25 * Check for the presence of extended state information in the
26 * user fpstate pointer in the sigcontext.
28 int check_for_xstate(struct i387_fxsave_struct __user *buf,
30 struct _fpx_sw_bytes *fx_sw_user)
32 int min_xstate_size = sizeof(struct i387_fxsave_struct) +
33 sizeof(struct xsave_hdr_struct);
37 err = __copy_from_user(fx_sw_user, &buf->sw_reserved[0],
38 sizeof(struct _fpx_sw_bytes));
44 * First Magic check failed.
46 if (fx_sw_user->magic1 != FP_XSTATE_MAGIC1)
50 * Check for error scenarios.
52 if (fx_sw_user->xstate_size < min_xstate_size ||
53 fx_sw_user->xstate_size > xstate_size ||
54 fx_sw_user->xstate_size > fx_sw_user->extended_size)
57 err = __get_user(magic2, (__u32 *) (((void *)fpstate) +
58 fx_sw_user->extended_size -
59 FP_XSTATE_MAGIC2_SIZE));
61 * Check for the presence of second magic word at the end of memory
62 * layout. This detects the case where the user just copied the legacy
63 * fpstate layout with out copying the extended state information
64 * in the memory layout.
66 if (err || magic2 != FP_XSTATE_MAGIC2)
74 * Signal frame handlers.
77 int save_i387_xstate(void __user *buf)
79 struct task_struct *tsk = current;
82 if (!access_ok(VERIFY_WRITE, buf, sig_xstate_size))
85 BUG_ON(sig_xstate_size < xstate_size);
87 if ((unsigned long)buf % 64)
88 printk("save_i387_xstate: bad fpstate %p\n", buf);
92 clear_used_math(); /* trigger finit */
93 if (task_thread_info(tsk)->status & TS_USEDFPU) {
95 * Start with clearing the user buffer. This will present a
96 * clean context for the bytes not touched by the fxsave/xsave.
98 __clear_user(buf, sig_xstate_size);
100 if (task_thread_info(tsk)->status & TS_XSAVE)
101 err = xsave_user(buf);
103 err = fxsave_user(buf);
107 task_thread_info(tsk)->status &= ~TS_USEDFPU;
110 if (__copy_to_user(buf, &tsk->thread.xstate->fxsave,
115 if (task_thread_info(tsk)->status & TS_XSAVE) {
116 struct _fpstate __user *fx = buf;
118 err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved,
119 sizeof(struct _fpx_sw_bytes));
121 err |= __put_user(FP_XSTATE_MAGIC2,
122 (__u32 __user *) (buf + sig_xstate_size
123 - FP_XSTATE_MAGIC2_SIZE));
132 * Restore the extended state if present. Otherwise, restore the FP/SSE
135 int restore_user_xstate(void __user *buf)
137 struct _fpx_sw_bytes fx_sw_user;
141 if (((unsigned long)buf % 64) ||
142 check_for_xstate(buf, buf, &fx_sw_user))
145 mask = fx_sw_user.xstate_bv;
148 * restore the state passed by the user.
150 err = xrestore_user(buf, mask);
155 * init the state skipped by the user.
157 mask = pcntxt_mask & ~mask;
159 xrstor_state(init_xstate_buf, mask);
165 * couldn't find the extended state information in the
166 * memory layout. Restore just the FP/SSE and init all
167 * the other extended state.
169 xrstor_state(init_xstate_buf, pcntxt_mask & ~XSTATE_FPSSE);
170 return fxrstor_checking((__force struct i387_fxsave_struct *)buf);
174 * This restores directly out of user space. Exceptions are handled.
176 int restore_i387_xstate(void __user *buf)
178 struct task_struct *tsk = current;
186 if (!access_ok(VERIFY_READ, buf, sig_xstate_size))
195 if (!(task_thread_info(current)->status & TS_USEDFPU)) {
197 task_thread_info(current)->status |= TS_USEDFPU;
199 if (task_thread_info(tsk)->status & TS_XSAVE)
200 err = restore_user_xstate(buf);
202 err = fxrstor_checking((__force struct i387_fxsave_struct *)
206 * Encountered an error while doing the restore from the
207 * user buffer, clear the fpu state.
218 * Prepare the SW reserved portion of the fxsave memory layout, indicating
219 * the presence of the extended state information in the memory layout
220 * pointed by the fpstate pointer in the sigcontext.
221 * This will be saved when ever the FP and extended state context is
222 * saved on the user stack during the signal handler delivery to the user.
224 void prepare_fx_sw_frame(void)
226 int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) +
227 FP_XSTATE_MAGIC2_SIZE;
229 sig_xstate_size = sizeof(struct _fpstate) + size_extended;
231 #ifdef CONFIG_IA32_EMULATION
232 sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended;
235 memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved));
237 fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
238 fx_sw_reserved.extended_size = sig_xstate_size;
239 fx_sw_reserved.xstate_bv = pcntxt_mask;
240 fx_sw_reserved.xstate_size = xstate_size;
241 #ifdef CONFIG_IA32_EMULATION
242 memcpy(&fx_sw_reserved_ia32, &fx_sw_reserved,
243 sizeof(struct _fpx_sw_bytes));
244 fx_sw_reserved_ia32.extended_size = sig_xstate_ia32_size;
249 * Represents init state for the supported extended state.
251 struct xsave_struct *init_xstate_buf;
254 unsigned int sig_xstate_size = sizeof(struct _fpstate);
258 * Enable the extended processor state save/restore feature
260 void __cpuinit xsave_init(void)
265 set_in_cr4(X86_CR4_OSXSAVE);
268 * Enable all the features that the HW is capable of
269 * and the Linux kernel is aware of.
271 xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
275 * setup the xstate image representing the init state
277 static void __init setup_xstate_init(void)
279 init_xstate_buf = alloc_bootmem(xstate_size);
280 init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
284 * Enable and initialize the xsave feature.
286 void __init xsave_cntxt_init(void)
288 unsigned int eax, ebx, ecx, edx;
290 cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
291 pcntxt_mask = eax + ((u64)edx << 32);
293 if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
294 printk(KERN_ERR "FP/SSE not shown under xsave features 0x%llx\n",
300 * for now OS knows only about FP/SSE
302 pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
306 * Recompute the context size for enabled features
308 cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
311 prepare_fx_sw_frame();
315 printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%llx, "
317 pcntxt_mask, xstate_size);