x86, xsave: Sync xsave memory layout with its header for user handling
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / kernel / xsave.c
blob368047c8d5074e594551a2bce5ef10e2a38b763e
1 /*
2 * xsave/xrstor support.
4 * Author: Suresh Siddha <suresh.b.siddha@intel.com>
5 */
6 #include <linux/bootmem.h>
7 #include <linux/compat.h>
8 #include <asm/i387.h>
9 #ifdef CONFIG_IA32_EMULATION
10 #include <asm/sigcontext32.h>
11 #endif
12 #include <asm/xcr.h>
15 * Supported feature mask by the CPU and the kernel.
17 u64 pcntxt_mask;
19 struct _fpx_sw_bytes fx_sw_reserved;
20 #ifdef CONFIG_IA32_EMULATION
21 struct _fpx_sw_bytes fx_sw_reserved_ia32;
22 #endif
24 static unsigned int *xstate_offsets, *xstate_sizes, xstate_features;
27 * If a processor implementation discern that a processor state component is
28 * in its initialized state it may modify the corresponding bit in the
29 * xsave_hdr.xstate_bv as '0', with out modifying the corresponding memory
30 * layout in the case of xsaveopt. While presenting the xstate information to
31 * the user, we always ensure that the memory layout of a feature will be in
32 * the init state if the corresponding header bit is zero. This is to ensure
33 * that the user doesn't see some stale state in the memory layout during
34 * signal handling, debugging etc.
36 void __sanitize_i387_state(struct task_struct *tsk)
38 u64 xstate_bv;
39 int feature_bit = 0x2;
40 struct i387_fxsave_struct *fx = &tsk->thread.fpu.state->fxsave;
42 if (!fx)
43 return;
45 BUG_ON(task_thread_info(tsk)->status & TS_USEDFPU);
47 xstate_bv = tsk->thread.fpu.state->xsave.xsave_hdr.xstate_bv;
50 * None of the feature bits are in init state. So nothing else
51 * to do for us, as the memory layout is upto date.
53 if ((xstate_bv & pcntxt_mask) == pcntxt_mask)
54 return;
57 * FP is in init state
59 if (!(xstate_bv & XSTATE_FP)) {
60 fx->cwd = 0x37f;
61 fx->swd = 0;
62 fx->twd = 0;
63 fx->fop = 0;
64 fx->rip = 0;
65 fx->rdp = 0;
66 memset(&fx->st_space[0], 0, 128);
70 * SSE is in init state
72 if (!(xstate_bv & XSTATE_SSE))
73 memset(&fx->xmm_space[0], 0, 256);
75 xstate_bv = (pcntxt_mask & ~xstate_bv) >> 2;
78 * Update all the other memory layouts for which the corresponding
79 * header bit is in the init state.
81 while (xstate_bv) {
82 if (xstate_bv & 0x1) {
83 int offset = xstate_offsets[feature_bit];
84 int size = xstate_sizes[feature_bit];
86 memcpy(((void *) fx) + offset,
87 ((void *) init_xstate_buf) + offset,
88 size);
91 xstate_bv >>= 1;
92 feature_bit++;
97 * Check for the presence of extended state information in the
98 * user fpstate pointer in the sigcontext.
100 int check_for_xstate(struct i387_fxsave_struct __user *buf,
101 void __user *fpstate,
102 struct _fpx_sw_bytes *fx_sw_user)
104 int min_xstate_size = sizeof(struct i387_fxsave_struct) +
105 sizeof(struct xsave_hdr_struct);
106 unsigned int magic2;
107 int err;
109 err = __copy_from_user(fx_sw_user, &buf->sw_reserved[0],
110 sizeof(struct _fpx_sw_bytes));
111 if (err)
112 return -EFAULT;
115 * First Magic check failed.
117 if (fx_sw_user->magic1 != FP_XSTATE_MAGIC1)
118 return -EINVAL;
121 * Check for error scenarios.
123 if (fx_sw_user->xstate_size < min_xstate_size ||
124 fx_sw_user->xstate_size > xstate_size ||
125 fx_sw_user->xstate_size > fx_sw_user->extended_size)
126 return -EINVAL;
128 err = __get_user(magic2, (__u32 *) (((void *)fpstate) +
129 fx_sw_user->extended_size -
130 FP_XSTATE_MAGIC2_SIZE));
131 if (err)
132 return err;
134 * Check for the presence of second magic word at the end of memory
135 * layout. This detects the case where the user just copied the legacy
136 * fpstate layout with out copying the extended state information
137 * in the memory layout.
139 if (magic2 != FP_XSTATE_MAGIC2)
140 return -EFAULT;
142 return 0;
145 #ifdef CONFIG_X86_64
147 * Signal frame handlers.
150 int save_i387_xstate(void __user *buf)
152 struct task_struct *tsk = current;
153 int err = 0;
155 if (!access_ok(VERIFY_WRITE, buf, sig_xstate_size))
156 return -EACCES;
158 BUG_ON(sig_xstate_size < xstate_size);
160 if ((unsigned long)buf % 64)
161 printk("save_i387_xstate: bad fpstate %p\n", buf);
163 if (!used_math())
164 return 0;
166 if (task_thread_info(tsk)->status & TS_USEDFPU) {
168 * Start with clearing the user buffer. This will present a
169 * clean context for the bytes not touched by the fxsave/xsave.
171 err = __clear_user(buf, sig_xstate_size);
172 if (err)
173 return err;
175 if (use_xsave())
176 err = xsave_user(buf);
177 else
178 err = fxsave_user(buf);
180 if (err)
181 return err;
182 task_thread_info(tsk)->status &= ~TS_USEDFPU;
183 stts();
184 } else {
185 sanitize_i387_state(tsk);
186 if (__copy_to_user(buf, &tsk->thread.fpu.state->fxsave,
187 xstate_size))
188 return -1;
191 clear_used_math(); /* trigger finit */
193 if (use_xsave()) {
194 struct _fpstate __user *fx = buf;
195 struct _xstate __user *x = buf;
196 u64 xstate_bv;
198 err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved,
199 sizeof(struct _fpx_sw_bytes));
201 err |= __put_user(FP_XSTATE_MAGIC2,
202 (__u32 __user *) (buf + sig_xstate_size
203 - FP_XSTATE_MAGIC2_SIZE));
206 * Read the xstate_bv which we copied (directly from the cpu or
207 * from the state in task struct) to the user buffers and
208 * set the FP/SSE bits.
210 err |= __get_user(xstate_bv, &x->xstate_hdr.xstate_bv);
213 * For legacy compatible, we always set FP/SSE bits in the bit
214 * vector while saving the state to the user context. This will
215 * enable us capturing any changes(during sigreturn) to
216 * the FP/SSE bits by the legacy applications which don't touch
217 * xstate_bv in the xsave header.
219 * xsave aware apps can change the xstate_bv in the xsave
220 * header as well as change any contents in the memory layout.
221 * xrestore as part of sigreturn will capture all the changes.
223 xstate_bv |= XSTATE_FPSSE;
225 err |= __put_user(xstate_bv, &x->xstate_hdr.xstate_bv);
227 if (err)
228 return err;
231 return 1;
235 * Restore the extended state if present. Otherwise, restore the FP/SSE
236 * state.
238 static int restore_user_xstate(void __user *buf)
240 struct _fpx_sw_bytes fx_sw_user;
241 u64 mask;
242 int err;
244 if (((unsigned long)buf % 64) ||
245 check_for_xstate(buf, buf, &fx_sw_user))
246 goto fx_only;
248 mask = fx_sw_user.xstate_bv;
251 * restore the state passed by the user.
253 err = xrestore_user(buf, mask);
254 if (err)
255 return err;
258 * init the state skipped by the user.
260 mask = pcntxt_mask & ~mask;
262 xrstor_state(init_xstate_buf, mask);
264 return 0;
266 fx_only:
268 * couldn't find the extended state information in the
269 * memory layout. Restore just the FP/SSE and init all
270 * the other extended state.
272 xrstor_state(init_xstate_buf, pcntxt_mask & ~XSTATE_FPSSE);
273 return fxrstor_checking((__force struct i387_fxsave_struct *)buf);
277 * This restores directly out of user space. Exceptions are handled.
279 int restore_i387_xstate(void __user *buf)
281 struct task_struct *tsk = current;
282 int err = 0;
284 if (!buf) {
285 if (used_math())
286 goto clear;
287 return 0;
288 } else
289 if (!access_ok(VERIFY_READ, buf, sig_xstate_size))
290 return -EACCES;
292 if (!used_math()) {
293 err = init_fpu(tsk);
294 if (err)
295 return err;
298 if (!(task_thread_info(current)->status & TS_USEDFPU)) {
299 clts();
300 task_thread_info(current)->status |= TS_USEDFPU;
302 if (use_xsave())
303 err = restore_user_xstate(buf);
304 else
305 err = fxrstor_checking((__force struct i387_fxsave_struct *)
306 buf);
307 if (unlikely(err)) {
309 * Encountered an error while doing the restore from the
310 * user buffer, clear the fpu state.
312 clear:
313 clear_fpu(tsk);
314 clear_used_math();
316 return err;
318 #endif
321 * Prepare the SW reserved portion of the fxsave memory layout, indicating
322 * the presence of the extended state information in the memory layout
323 * pointed by the fpstate pointer in the sigcontext.
324 * This will be saved when ever the FP and extended state context is
325 * saved on the user stack during the signal handler delivery to the user.
327 static void prepare_fx_sw_frame(void)
329 int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) +
330 FP_XSTATE_MAGIC2_SIZE;
332 sig_xstate_size = sizeof(struct _fpstate) + size_extended;
334 #ifdef CONFIG_IA32_EMULATION
335 sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended;
336 #endif
338 memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved));
340 fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
341 fx_sw_reserved.extended_size = sig_xstate_size;
342 fx_sw_reserved.xstate_bv = pcntxt_mask;
343 fx_sw_reserved.xstate_size = xstate_size;
344 #ifdef CONFIG_IA32_EMULATION
345 memcpy(&fx_sw_reserved_ia32, &fx_sw_reserved,
346 sizeof(struct _fpx_sw_bytes));
347 fx_sw_reserved_ia32.extended_size = sig_xstate_ia32_size;
348 #endif
352 * Represents init state for the supported extended state.
354 struct xsave_struct *init_xstate_buf;
356 #ifdef CONFIG_X86_64
357 unsigned int sig_xstate_size = sizeof(struct _fpstate);
358 #endif
361 * Enable the extended processor state save/restore feature
363 void __cpuinit xsave_init(void)
365 if (!cpu_has_xsave)
366 return;
368 set_in_cr4(X86_CR4_OSXSAVE);
371 * Enable all the features that the HW is capable of
372 * and the Linux kernel is aware of.
374 xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
378 * Record the offsets and sizes of different state managed by the xsave
379 * memory layout.
381 static void setup_xstate_features(void)
383 int eax, ebx, ecx, edx, leaf = 0x2;
385 xstate_features = fls64(pcntxt_mask);
386 xstate_offsets = alloc_bootmem(xstate_features * sizeof(int));
387 xstate_sizes = alloc_bootmem(xstate_features * sizeof(int));
389 do {
390 cpuid_count(0xd, leaf, &eax, &ebx, &ecx, &edx);
392 if (eax == 0)
393 break;
395 xstate_offsets[leaf] = ebx;
396 xstate_sizes[leaf] = eax;
398 leaf++;
399 } while (1);
403 * setup the xstate image representing the init state
405 static void __init setup_xstate_init(void)
407 setup_xstate_features();
410 * Setup init_xstate_buf to represent the init state of
411 * all the features managed by the xsave
413 init_xstate_buf = alloc_bootmem(xstate_size);
414 init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
416 clts();
418 * Init all the features state with header_bv being 0x0
420 xrstor_state(init_xstate_buf, -1);
422 * Dump the init state again. This is to identify the init state
423 * of any feature which is not represented by all zero's.
425 xsave_state(init_xstate_buf, -1);
426 stts();
430 * Enable and initialize the xsave feature.
432 void __ref xsave_cntxt_init(void)
434 unsigned int eax, ebx, ecx, edx;
436 cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
437 pcntxt_mask = eax + ((u64)edx << 32);
439 if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
440 printk(KERN_ERR "FP/SSE not shown under xsave features 0x%llx\n",
441 pcntxt_mask);
442 BUG();
446 * Support only the state known to OS.
448 pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
449 xsave_init();
452 * Recompute the context size for enabled features
454 cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
455 xstate_size = ebx;
457 update_regset_xstate_info(xstate_size, pcntxt_mask);
458 prepare_fx_sw_frame();
460 setup_xstate_init();
462 printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%llx, "
463 "cntxt size 0x%x\n",
464 pcntxt_mask, xstate_size);