blob: ab9ad48b65301557bae2ec40e27226dc94a2a6e4 [file] [log] [blame]
Suresh Siddhadc1e35c2008-07-29 10:29:19 -07001/*
2 * xsave/xrstor support.
3 *
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>
Suresh Siddhac37b5ef2008-07-29 10:29:25 -07009#ifdef CONFIG_IA32_EMULATION
10#include <asm/sigcontext32.h>
11#endif
H. Peter Anvin6152e4b2008-07-29 17:23:16 -070012#include <asm/xcr.h>
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070013
14/*
15 * Supported feature mask by the CPU and the kernel.
16 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -070017u64 pcntxt_mask;
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070018
Suresh Siddhac37b5ef2008-07-29 10:29:25 -070019struct _fpx_sw_bytes fx_sw_reserved;
20#ifdef CONFIG_IA32_EMULATION
21struct _fpx_sw_bytes fx_sw_reserved_ia32;
22#endif
23
Suresh Siddhaa1488f82010-07-19 16:05:48 -070024static unsigned int *xstate_offsets, *xstate_sizes, xstate_features;
25
Suresh Siddhac37b5ef2008-07-29 10:29:25 -070026/*
Suresh Siddha29104e12010-07-19 16:05:49 -070027 * 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.
35 */
36void __sanitize_i387_state(struct task_struct *tsk)
37{
38 u64 xstate_bv;
39 int feature_bit = 0x2;
40 struct i387_fxsave_struct *fx = &tsk->thread.fpu.state->fxsave;
41
42 if (!fx)
43 return;
44
45 BUG_ON(task_thread_info(tsk)->status & TS_USEDFPU);
46
47 xstate_bv = tsk->thread.fpu.state->xsave.xsave_hdr.xstate_bv;
48
49 /*
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.
52 */
53 if ((xstate_bv & pcntxt_mask) == pcntxt_mask)
54 return;
55
56 /*
57 * FP is in init state
58 */
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);
67 }
68
69 /*
70 * SSE is in init state
71 */
72 if (!(xstate_bv & XSTATE_SSE))
73 memset(&fx->xmm_space[0], 0, 256);
74
75 xstate_bv = (pcntxt_mask & ~xstate_bv) >> 2;
76
77 /*
78 * Update all the other memory layouts for which the corresponding
79 * header bit is in the init state.
80 */
81 while (xstate_bv) {
82 if (xstate_bv & 0x1) {
83 int offset = xstate_offsets[feature_bit];
84 int size = xstate_sizes[feature_bit];
85
86 memcpy(((void *) fx) + offset,
87 ((void *) init_xstate_buf) + offset,
88 size);
89 }
90
91 xstate_bv >>= 1;
92 feature_bit++;
93 }
94}
95
96/*
Suresh Siddhac37b5ef2008-07-29 10:29:25 -070097 * Check for the presence of extended state information in the
98 * user fpstate pointer in the sigcontext.
99 */
100int check_for_xstate(struct i387_fxsave_struct __user *buf,
101 void __user *fpstate,
102 struct _fpx_sw_bytes *fx_sw_user)
103{
104 int min_xstate_size = sizeof(struct i387_fxsave_struct) +
105 sizeof(struct xsave_hdr_struct);
106 unsigned int magic2;
107 int err;
108
109 err = __copy_from_user(fx_sw_user, &buf->sw_reserved[0],
110 sizeof(struct _fpx_sw_bytes));
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700111 if (err)
Dan Carpenterd6d4d422010-06-03 12:07:46 +0200112 return -EFAULT;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700113
114 /*
115 * First Magic check failed.
116 */
117 if (fx_sw_user->magic1 != FP_XSTATE_MAGIC1)
Dan Carpenterd6d4d422010-06-03 12:07:46 +0200118 return -EINVAL;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700119
120 /*
121 * Check for error scenarios.
122 */
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)
Dan Carpenterd6d4d422010-06-03 12:07:46 +0200126 return -EINVAL;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700127
128 err = __get_user(magic2, (__u32 *) (((void *)fpstate) +
129 fx_sw_user->extended_size -
130 FP_XSTATE_MAGIC2_SIZE));
Dan Carpenterd6d4d422010-06-03 12:07:46 +0200131 if (err)
132 return err;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700133 /*
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.
138 */
Dan Carpenterd6d4d422010-06-03 12:07:46 +0200139 if (magic2 != FP_XSTATE_MAGIC2)
140 return -EFAULT;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700141
142 return 0;
143}
144
Suresh Siddhaab513702008-07-29 10:29:22 -0700145#ifdef CONFIG_X86_64
146/*
147 * Signal frame handlers.
148 */
149
150int save_i387_xstate(void __user *buf)
151{
152 struct task_struct *tsk = current;
153 int err = 0;
154
155 if (!access_ok(VERIFY_WRITE, buf, sig_xstate_size))
156 return -EACCES;
157
Suresh Siddhaf65bc212008-08-13 11:38:15 -0700158 BUG_ON(sig_xstate_size < xstate_size);
Suresh Siddhaab513702008-07-29 10:29:22 -0700159
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700160 if ((unsigned long)buf % 64)
Suresh Siddhaab513702008-07-29 10:29:22 -0700161 printk("save_i387_xstate: bad fpstate %p\n", buf);
162
163 if (!used_math())
164 return 0;
Suresh Siddha06c38d52009-04-09 15:24:34 -0700165
Suresh Siddhaab513702008-07-29 10:29:22 -0700166 if (task_thread_info(tsk)->status & TS_USEDFPU) {
Suresh Siddhaed405952008-08-13 11:38:14 -0700167 /*
168 * Start with clearing the user buffer. This will present a
169 * clean context for the bytes not touched by the fxsave/xsave.
170 */
Ingo Molnar9f482802008-08-18 12:59:32 +0200171 err = __clear_user(buf, sig_xstate_size);
172 if (err)
173 return err;
Suresh Siddhaed405952008-08-13 11:38:14 -0700174
Avi Kivityc9ad4882010-05-06 11:45:45 +0300175 if (use_xsave())
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700176 err = xsave_user(buf);
177 else
178 err = fxsave_user(buf);
179
Suresh Siddhaab513702008-07-29 10:29:22 -0700180 if (err)
181 return err;
182 task_thread_info(tsk)->status &= ~TS_USEDFPU;
183 stts();
184 } else {
Suresh Siddha29104e12010-07-19 16:05:49 -0700185 sanitize_i387_state(tsk);
Avi Kivity86603282010-05-06 11:45:46 +0300186 if (__copy_to_user(buf, &tsk->thread.fpu.state->fxsave,
Suresh Siddhaab513702008-07-29 10:29:22 -0700187 xstate_size))
188 return -1;
189 }
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700190
Suresh Siddha06c38d52009-04-09 15:24:34 -0700191 clear_used_math(); /* trigger finit */
192
Avi Kivityc9ad4882010-05-06 11:45:45 +0300193 if (use_xsave()) {
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700194 struct _fpstate __user *fx = buf;
Suresh Siddha04944b72008-10-07 14:04:28 -0700195 struct _xstate __user *x = buf;
196 u64 xstate_bv;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700197
198 err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved,
199 sizeof(struct _fpx_sw_bytes));
200
201 err |= __put_user(FP_XSTATE_MAGIC2,
202 (__u32 __user *) (buf + sig_xstate_size
203 - FP_XSTATE_MAGIC2_SIZE));
Suresh Siddha04944b72008-10-07 14:04:28 -0700204
205 /*
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.
209 */
210 err |= __get_user(xstate_bv, &x->xstate_hdr.xstate_bv);
211
212 /*
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.
218 *
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.
222 */
223 xstate_bv |= XSTATE_FPSSE;
224
225 err |= __put_user(xstate_bv, &x->xstate_hdr.xstate_bv);
226
Suresh Siddhaf364ead2008-10-07 14:04:27 -0700227 if (err)
228 return err;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700229 }
230
Suresh Siddhaab513702008-07-29 10:29:22 -0700231 return 1;
232}
233
234/*
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700235 * Restore the extended state if present. Otherwise, restore the FP/SSE
236 * state.
237 */
Jaswinder Singh Rajput7820b752008-12-30 22:05:55 +0530238static int restore_user_xstate(void __user *buf)
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700239{
240 struct _fpx_sw_bytes fx_sw_user;
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700241 u64 mask;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700242 int err;
243
244 if (((unsigned long)buf % 64) ||
245 check_for_xstate(buf, buf, &fx_sw_user))
246 goto fx_only;
247
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700248 mask = fx_sw_user.xstate_bv;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700249
250 /*
251 * restore the state passed by the user.
252 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700253 err = xrestore_user(buf, mask);
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700254 if (err)
255 return err;
256
257 /*
258 * init the state skipped by the user.
259 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700260 mask = pcntxt_mask & ~mask;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700261
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700262 xrstor_state(init_xstate_buf, mask);
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700263
264 return 0;
265
266fx_only:
267 /*
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.
271 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700272 xrstor_state(init_xstate_buf, pcntxt_mask & ~XSTATE_FPSSE);
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700273 return fxrstor_checking((__force struct i387_fxsave_struct *)buf);
274}
275
276/*
Suresh Siddhaab513702008-07-29 10:29:22 -0700277 * This restores directly out of user space. Exceptions are handled.
278 */
279int restore_i387_xstate(void __user *buf)
280{
281 struct task_struct *tsk = current;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700282 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700283
284 if (!buf) {
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700285 if (used_math())
286 goto clear;
Suresh Siddhaab513702008-07-29 10:29:22 -0700287 return 0;
288 } else
289 if (!access_ok(VERIFY_READ, buf, sig_xstate_size))
290 return -EACCES;
291
292 if (!used_math()) {
293 err = init_fpu(tsk);
294 if (err)
295 return err;
296 }
297
298 if (!(task_thread_info(current)->status & TS_USEDFPU)) {
299 clts();
300 task_thread_info(current)->status |= TS_USEDFPU;
301 }
Avi Kivityc9ad4882010-05-06 11:45:45 +0300302 if (use_xsave())
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700303 err = restore_user_xstate(buf);
304 else
305 err = fxrstor_checking((__force struct i387_fxsave_struct *)
306 buf);
Suresh Siddhaab513702008-07-29 10:29:22 -0700307 if (unlikely(err)) {
308 /*
309 * Encountered an error while doing the restore from the
310 * user buffer, clear the fpu state.
311 */
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700312clear:
Suresh Siddhaab513702008-07-29 10:29:22 -0700313 clear_fpu(tsk);
314 clear_used_math();
315 }
316 return err;
317}
318#endif
319
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700320/*
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700321 * 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.
326 */
roel kluin8bcad302008-10-21 19:49:09 -0400327static void prepare_fx_sw_frame(void)
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700328{
329 int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) +
330 FP_XSTATE_MAGIC2_SIZE;
331
332 sig_xstate_size = sizeof(struct _fpstate) + size_extended;
333
334#ifdef CONFIG_IA32_EMULATION
335 sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended;
336#endif
337
338 memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved));
339
340 fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
341 fx_sw_reserved.extended_size = sig_xstate_size;
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700342 fx_sw_reserved.xstate_bv = pcntxt_mask;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700343 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
349}
350
351/*
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700352 * Represents init state for the supported extended state.
353 */
354struct xsave_struct *init_xstate_buf;
355
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700356#ifdef CONFIG_X86_64
357unsigned int sig_xstate_size = sizeof(struct _fpstate);
358#endif
359
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700360/*
361 * Enable the extended processor state save/restore feature
362 */
Robert Richter82d41502010-07-20 20:50:51 +0200363static void __cpuinit __xsave_init(void)
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700364{
365 if (!cpu_has_xsave)
366 return;
367
368 set_in_cr4(X86_CR4_OSXSAVE);
369
370 /*
371 * Enable all the features that the HW is capable of
372 * and the Linux kernel is aware of.
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700373 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700374 xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700375}
376
377/*
Suresh Siddhaa1488f82010-07-19 16:05:48 -0700378 * Record the offsets and sizes of different state managed by the xsave
379 * memory layout.
380 */
381static void setup_xstate_features(void)
382{
383 int eax, ebx, ecx, edx, leaf = 0x2;
384
385 xstate_features = fls64(pcntxt_mask);
386 xstate_offsets = alloc_bootmem(xstate_features * sizeof(int));
387 xstate_sizes = alloc_bootmem(xstate_features * sizeof(int));
388
389 do {
390 cpuid_count(0xd, leaf, &eax, &ebx, &ecx, &edx);
391
392 if (eax == 0)
393 break;
394
395 xstate_offsets[leaf] = ebx;
396 xstate_sizes[leaf] = eax;
397
398 leaf++;
399 } while (1);
400}
401
402/*
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700403 * setup the xstate image representing the init state
404 */
Alexey Dobriyana19aac82008-08-30 06:03:34 +0400405static void __init setup_xstate_init(void)
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700406{
Suresh Siddha29104e12010-07-19 16:05:49 -0700407 setup_xstate_features();
408
409 /*
410 * Setup init_xstate_buf to represent the init state of
411 * all the features managed by the xsave
412 */
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700413 init_xstate_buf = alloc_bootmem(xstate_size);
414 init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
Suresh Siddhaa1488f82010-07-19 16:05:48 -0700415
Suresh Siddha29104e12010-07-19 16:05:49 -0700416 clts();
417 /*
418 * Init all the features state with header_bv being 0x0
419 */
420 xrstor_state(init_xstate_buf, -1);
421 /*
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.
424 */
425 xsave_state(init_xstate_buf, -1);
426 stts();
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700427}
428
429/*
430 * Enable and initialize the xsave feature.
431 */
Rakib Mullickbfe085f2008-11-20 19:12:50 +0600432void __ref xsave_cntxt_init(void)
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700433{
434 unsigned int eax, ebx, ecx, edx;
435
436 cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700437 pcntxt_mask = eax + ((u64)edx << 32);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700438
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700439 if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
440 printk(KERN_ERR "FP/SSE not shown under xsave features 0x%llx\n",
441 pcntxt_mask);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700442 BUG();
443 }
444
445 /*
Suresh Siddhaa30469e2009-04-10 15:21:24 -0700446 * Support only the state known to OS.
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700447 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700448 pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
Robert Richter82d41502010-07-20 20:50:51 +0200449 __xsave_init();
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700450
451 /*
452 * Recompute the context size for enabled features
453 */
454 cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700455 xstate_size = ebx;
456
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800457 update_regset_xstate_info(xstate_size, pcntxt_mask);
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700458 prepare_fx_sw_frame();
459
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700460 setup_xstate_init();
461
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700462 printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%llx, "
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700463 "cntxt size 0x%x\n",
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700464 pcntxt_mask, xstate_size);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700465}
Robert Richter82d41502010-07-20 20:50:51 +0200466
467void __cpuinit xsave_init(void)
468{
469 /*
470 * Boot processor to setup the FP and extended state context info.
471 */
472 if (!smp_processor_id())
473 init_thread_xstate();
474 __xsave_init();
475}