blob: bd18149b2b0f09532870075a274ff1576b5de1d7 [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>
Linus Torvalds1361b832012-02-21 13:19:22 -08009#include <asm/fpu-internal.h>
Suresh Siddhac37b5ef2008-07-29 10:29:25 -070010#ifdef CONFIG_IA32_EMULATION
11#include <asm/sigcontext32.h>
12#endif
H. Peter Anvin6152e4b2008-07-29 17:23:16 -070013#include <asm/xcr.h>
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070014
15/*
16 * Supported feature mask by the CPU and the kernel.
17 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -070018u64 pcntxt_mask;
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070019
Robert Richter45c2d7f2010-07-21 19:03:55 +020020/*
21 * Represents init state for the supported extended state.
22 */
23static struct xsave_struct *init_xstate_buf;
24
Suresh Siddhac37b5ef2008-07-29 10:29:25 -070025struct _fpx_sw_bytes fx_sw_reserved;
26#ifdef CONFIG_IA32_EMULATION
27struct _fpx_sw_bytes fx_sw_reserved_ia32;
28#endif
29
Suresh Siddhaa1488f82010-07-19 16:05:48 -070030static unsigned int *xstate_offsets, *xstate_sizes, xstate_features;
31
Suresh Siddhac37b5ef2008-07-29 10:29:25 -070032/*
Suresh Siddha29104e12010-07-19 16:05:49 -070033 * If a processor implementation discern that a processor state component is
34 * in its initialized state it may modify the corresponding bit in the
35 * xsave_hdr.xstate_bv as '0', with out modifying the corresponding memory
36 * layout in the case of xsaveopt. While presenting the xstate information to
37 * the user, we always ensure that the memory layout of a feature will be in
38 * the init state if the corresponding header bit is zero. This is to ensure
39 * that the user doesn't see some stale state in the memory layout during
40 * signal handling, debugging etc.
41 */
42void __sanitize_i387_state(struct task_struct *tsk)
43{
44 u64 xstate_bv;
45 int feature_bit = 0x2;
46 struct i387_fxsave_struct *fx = &tsk->thread.fpu.state->fxsave;
47
48 if (!fx)
49 return;
50
Suresh Siddha29104e12010-07-19 16:05:49 -070051 xstate_bv = tsk->thread.fpu.state->xsave.xsave_hdr.xstate_bv;
52
53 /*
54 * None of the feature bits are in init state. So nothing else
Lucas De Marchi0d2eb442011-03-17 16:24:16 -030055 * to do for us, as the memory layout is up to date.
Suresh Siddha29104e12010-07-19 16:05:49 -070056 */
57 if ((xstate_bv & pcntxt_mask) == pcntxt_mask)
58 return;
59
60 /*
61 * FP is in init state
62 */
63 if (!(xstate_bv & XSTATE_FP)) {
64 fx->cwd = 0x37f;
65 fx->swd = 0;
66 fx->twd = 0;
67 fx->fop = 0;
68 fx->rip = 0;
69 fx->rdp = 0;
70 memset(&fx->st_space[0], 0, 128);
71 }
72
73 /*
74 * SSE is in init state
75 */
76 if (!(xstate_bv & XSTATE_SSE))
77 memset(&fx->xmm_space[0], 0, 256);
78
79 xstate_bv = (pcntxt_mask & ~xstate_bv) >> 2;
80
81 /*
82 * Update all the other memory layouts for which the corresponding
83 * header bit is in the init state.
84 */
85 while (xstate_bv) {
86 if (xstate_bv & 0x1) {
87 int offset = xstate_offsets[feature_bit];
88 int size = xstate_sizes[feature_bit];
89
90 memcpy(((void *) fx) + offset,
91 ((void *) init_xstate_buf) + offset,
92 size);
93 }
94
95 xstate_bv >>= 1;
96 feature_bit++;
97 }
98}
99
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700100/*
101 * Check for the presence of extended state information in the
102 * user fpstate pointer in the sigcontext.
103 */
104int check_for_xstate(struct i387_fxsave_struct __user *buf,
105 void __user *fpstate,
106 struct _fpx_sw_bytes *fx_sw_user)
107{
108 int min_xstate_size = sizeof(struct i387_fxsave_struct) +
109 sizeof(struct xsave_hdr_struct);
110 unsigned int magic2;
111 int err;
112
113 err = __copy_from_user(fx_sw_user, &buf->sw_reserved[0],
114 sizeof(struct _fpx_sw_bytes));
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700115 if (err)
Dan Carpenterd6d4d422010-06-03 12:07:46 +0200116 return -EFAULT;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700117
118 /*
119 * First Magic check failed.
120 */
121 if (fx_sw_user->magic1 != FP_XSTATE_MAGIC1)
Dan Carpenterd6d4d422010-06-03 12:07:46 +0200122 return -EINVAL;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700123
124 /*
125 * Check for error scenarios.
126 */
127 if (fx_sw_user->xstate_size < min_xstate_size ||
128 fx_sw_user->xstate_size > xstate_size ||
129 fx_sw_user->xstate_size > fx_sw_user->extended_size)
Dan Carpenterd6d4d422010-06-03 12:07:46 +0200130 return -EINVAL;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700131
132 err = __get_user(magic2, (__u32 *) (((void *)fpstate) +
133 fx_sw_user->extended_size -
134 FP_XSTATE_MAGIC2_SIZE));
Dan Carpenterd6d4d422010-06-03 12:07:46 +0200135 if (err)
136 return err;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700137 /*
138 * Check for the presence of second magic word at the end of memory
139 * layout. This detects the case where the user just copied the legacy
140 * fpstate layout with out copying the extended state information
141 * in the memory layout.
142 */
Dan Carpenterd6d4d422010-06-03 12:07:46 +0200143 if (magic2 != FP_XSTATE_MAGIC2)
144 return -EFAULT;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700145
146 return 0;
147}
148
Suresh Siddhaab513702008-07-29 10:29:22 -0700149#ifdef CONFIG_X86_64
150/*
151 * Signal frame handlers.
152 */
153
154int save_i387_xstate(void __user *buf)
155{
156 struct task_struct *tsk = current;
157 int err = 0;
158
159 if (!access_ok(VERIFY_WRITE, buf, sig_xstate_size))
160 return -EACCES;
161
Suresh Siddhaf65bc212008-08-13 11:38:15 -0700162 BUG_ON(sig_xstate_size < xstate_size);
Suresh Siddhaab513702008-07-29 10:29:22 -0700163
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700164 if ((unsigned long)buf % 64)
Suresh Siddhaab513702008-07-29 10:29:22 -0700165 printk("save_i387_xstate: bad fpstate %p\n", buf);
166
167 if (!used_math())
168 return 0;
Suresh Siddha06c38d52009-04-09 15:24:34 -0700169
Linus Torvalds15d87912012-02-16 09:15:04 -0800170 if (user_has_fpu()) {
Avi Kivityc9ad4882010-05-06 11:45:45 +0300171 if (use_xsave())
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700172 err = xsave_user(buf);
173 else
174 err = fxsave_user(buf);
175
Suresh Siddhaab513702008-07-29 10:29:22 -0700176 if (err)
177 return err;
Linus Torvalds15d87912012-02-16 09:15:04 -0800178 user_fpu_end();
Suresh Siddhaab513702008-07-29 10:29:22 -0700179 } else {
Suresh Siddha29104e12010-07-19 16:05:49 -0700180 sanitize_i387_state(tsk);
Avi Kivity86603282010-05-06 11:45:46 +0300181 if (__copy_to_user(buf, &tsk->thread.fpu.state->fxsave,
Suresh Siddhaab513702008-07-29 10:29:22 -0700182 xstate_size))
183 return -1;
184 }
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700185
Suresh Siddha06c38d52009-04-09 15:24:34 -0700186 clear_used_math(); /* trigger finit */
187
Avi Kivityc9ad4882010-05-06 11:45:45 +0300188 if (use_xsave()) {
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700189 struct _fpstate __user *fx = buf;
Suresh Siddha04944b72008-10-07 14:04:28 -0700190 struct _xstate __user *x = buf;
191 u64 xstate_bv;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700192
193 err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved,
194 sizeof(struct _fpx_sw_bytes));
195
196 err |= __put_user(FP_XSTATE_MAGIC2,
197 (__u32 __user *) (buf + sig_xstate_size
198 - FP_XSTATE_MAGIC2_SIZE));
Suresh Siddha04944b72008-10-07 14:04:28 -0700199
200 /*
201 * Read the xstate_bv which we copied (directly from the cpu or
202 * from the state in task struct) to the user buffers and
203 * set the FP/SSE bits.
204 */
205 err |= __get_user(xstate_bv, &x->xstate_hdr.xstate_bv);
206
207 /*
208 * For legacy compatible, we always set FP/SSE bits in the bit
209 * vector while saving the state to the user context. This will
210 * enable us capturing any changes(during sigreturn) to
211 * the FP/SSE bits by the legacy applications which don't touch
212 * xstate_bv in the xsave header.
213 *
214 * xsave aware apps can change the xstate_bv in the xsave
215 * header as well as change any contents in the memory layout.
216 * xrestore as part of sigreturn will capture all the changes.
217 */
218 xstate_bv |= XSTATE_FPSSE;
219
220 err |= __put_user(xstate_bv, &x->xstate_hdr.xstate_bv);
221
Suresh Siddhaf364ead2008-10-07 14:04:27 -0700222 if (err)
223 return err;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700224 }
225
Suresh Siddhaab513702008-07-29 10:29:22 -0700226 return 1;
227}
228
229/*
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700230 * Restore the extended state if present. Otherwise, restore the FP/SSE
231 * state.
232 */
Jaswinder Singh Rajput7820b752008-12-30 22:05:55 +0530233static int restore_user_xstate(void __user *buf)
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700234{
235 struct _fpx_sw_bytes fx_sw_user;
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700236 u64 mask;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700237 int err;
238
239 if (((unsigned long)buf % 64) ||
240 check_for_xstate(buf, buf, &fx_sw_user))
241 goto fx_only;
242
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700243 mask = fx_sw_user.xstate_bv;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700244
245 /*
246 * restore the state passed by the user.
247 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700248 err = xrestore_user(buf, mask);
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700249 if (err)
250 return err;
251
252 /*
253 * init the state skipped by the user.
254 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700255 mask = pcntxt_mask & ~mask;
Suresh Siddha8e221b62010-06-22 16:23:37 -0700256 if (unlikely(mask))
257 xrstor_state(init_xstate_buf, mask);
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700258
259 return 0;
260
261fx_only:
262 /*
263 * couldn't find the extended state information in the
264 * memory layout. Restore just the FP/SSE and init all
265 * the other extended state.
266 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700267 xrstor_state(init_xstate_buf, pcntxt_mask & ~XSTATE_FPSSE);
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700268 return fxrstor_checking((__force struct i387_fxsave_struct *)buf);
269}
270
271/*
Suresh Siddhaab513702008-07-29 10:29:22 -0700272 * This restores directly out of user space. Exceptions are handled.
273 */
274int restore_i387_xstate(void __user *buf)
275{
276 struct task_struct *tsk = current;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700277 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700278
279 if (!buf) {
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700280 if (used_math())
281 goto clear;
Suresh Siddhaab513702008-07-29 10:29:22 -0700282 return 0;
283 } else
284 if (!access_ok(VERIFY_READ, buf, sig_xstate_size))
285 return -EACCES;
286
287 if (!used_math()) {
288 err = init_fpu(tsk);
289 if (err)
290 return err;
291 }
292
Linus Torvalds15d87912012-02-16 09:15:04 -0800293 user_fpu_begin();
Avi Kivityc9ad4882010-05-06 11:45:45 +0300294 if (use_xsave())
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700295 err = restore_user_xstate(buf);
296 else
297 err = fxrstor_checking((__force struct i387_fxsave_struct *)
298 buf);
Suresh Siddhaab513702008-07-29 10:29:22 -0700299 if (unlikely(err)) {
300 /*
301 * Encountered an error while doing the restore from the
302 * user buffer, clear the fpu state.
303 */
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700304clear:
Suresh Siddhaab513702008-07-29 10:29:22 -0700305 clear_fpu(tsk);
306 clear_used_math();
307 }
308 return err;
309}
310#endif
311
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700312/*
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700313 * Prepare the SW reserved portion of the fxsave memory layout, indicating
314 * the presence of the extended state information in the memory layout
315 * pointed by the fpstate pointer in the sigcontext.
316 * This will be saved when ever the FP and extended state context is
317 * saved on the user stack during the signal handler delivery to the user.
318 */
roel kluin8bcad302008-10-21 19:49:09 -0400319static void prepare_fx_sw_frame(void)
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700320{
321 int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) +
322 FP_XSTATE_MAGIC2_SIZE;
323
324 sig_xstate_size = sizeof(struct _fpstate) + size_extended;
325
326#ifdef CONFIG_IA32_EMULATION
327 sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended;
328#endif
329
330 memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved));
331
332 fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
333 fx_sw_reserved.extended_size = sig_xstate_size;
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700334 fx_sw_reserved.xstate_bv = pcntxt_mask;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700335 fx_sw_reserved.xstate_size = xstate_size;
336#ifdef CONFIG_IA32_EMULATION
337 memcpy(&fx_sw_reserved_ia32, &fx_sw_reserved,
338 sizeof(struct _fpx_sw_bytes));
339 fx_sw_reserved_ia32.extended_size = sig_xstate_ia32_size;
340#endif
341}
342
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700343#ifdef CONFIG_X86_64
344unsigned int sig_xstate_size = sizeof(struct _fpstate);
345#endif
346
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700347/*
348 * Enable the extended processor state save/restore feature
349 */
H. Peter Anvin1cff92d2010-07-21 14:23:10 -0700350static inline void xstate_enable(void)
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700351{
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700352 set_in_cr4(X86_CR4_OSXSAVE);
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700353 xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700354}
355
356/*
Suresh Siddhaa1488f82010-07-19 16:05:48 -0700357 * Record the offsets and sizes of different state managed by the xsave
358 * memory layout.
359 */
Robert Richter4995b9d2010-07-21 19:03:56 +0200360static void __init setup_xstate_features(void)
Suresh Siddhaa1488f82010-07-19 16:05:48 -0700361{
362 int eax, ebx, ecx, edx, leaf = 0x2;
363
364 xstate_features = fls64(pcntxt_mask);
365 xstate_offsets = alloc_bootmem(xstate_features * sizeof(int));
366 xstate_sizes = alloc_bootmem(xstate_features * sizeof(int));
367
368 do {
Robert Richteree813d52010-07-21 19:03:54 +0200369 cpuid_count(XSTATE_CPUID, leaf, &eax, &ebx, &ecx, &edx);
Suresh Siddhaa1488f82010-07-19 16:05:48 -0700370
371 if (eax == 0)
372 break;
373
374 xstate_offsets[leaf] = ebx;
375 xstate_sizes[leaf] = eax;
376
377 leaf++;
378 } while (1);
379}
380
381/*
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700382 * setup the xstate image representing the init state
383 */
Alexey Dobriyana19aac82008-08-30 06:03:34 +0400384static void __init setup_xstate_init(void)
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700385{
Suresh Siddha29104e12010-07-19 16:05:49 -0700386 setup_xstate_features();
387
388 /*
389 * Setup init_xstate_buf to represent the init state of
390 * all the features managed by the xsave
391 */
Suresh Siddha10340ae2010-11-16 13:23:51 -0800392 init_xstate_buf = alloc_bootmem_align(xstate_size,
393 __alignof__(struct xsave_struct));
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700394 init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
Suresh Siddhaa1488f82010-07-19 16:05:48 -0700395
Suresh Siddha29104e12010-07-19 16:05:49 -0700396 clts();
397 /*
398 * Init all the features state with header_bv being 0x0
399 */
400 xrstor_state(init_xstate_buf, -1);
401 /*
402 * Dump the init state again. This is to identify the init state
403 * of any feature which is not represented by all zero's.
404 */
405 xsave_state(init_xstate_buf, -1);
406 stts();
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700407}
408
409/*
410 * Enable and initialize the xsave feature.
411 */
H. Peter Anvin1cff92d2010-07-21 14:23:10 -0700412static void __init xstate_enable_boot_cpu(void)
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700413{
414 unsigned int eax, ebx, ecx, edx;
415
Robert Richteree813d52010-07-21 19:03:54 +0200416 if (boot_cpu_data.cpuid_level < XSTATE_CPUID) {
417 WARN(1, KERN_ERR "XSTATE_CPUID missing\n");
418 return;
419 }
420
421 cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700422 pcntxt_mask = eax + ((u64)edx << 32);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700423
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700424 if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
425 printk(KERN_ERR "FP/SSE not shown under xsave features 0x%llx\n",
426 pcntxt_mask);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700427 BUG();
428 }
429
430 /*
Suresh Siddhaa30469e2009-04-10 15:21:24 -0700431 * Support only the state known to OS.
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700432 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700433 pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
Robert Richter97e80a72010-07-21 19:03:53 +0200434
H. Peter Anvin1cff92d2010-07-21 14:23:10 -0700435 xstate_enable();
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700436
437 /*
438 * Recompute the context size for enabled features
439 */
Robert Richteree813d52010-07-21 19:03:54 +0200440 cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700441 xstate_size = ebx;
442
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800443 update_regset_xstate_info(xstate_size, pcntxt_mask);
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700444 prepare_fx_sw_frame();
445
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700446 setup_xstate_init();
447
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700448 printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%llx, "
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700449 "cntxt size 0x%x\n",
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700450 pcntxt_mask, xstate_size);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700451}
Robert Richter82d41502010-07-20 20:50:51 +0200452
H. Peter Anvin1cff92d2010-07-21 14:23:10 -0700453/*
454 * For the very first instance, this calls xstate_enable_boot_cpu();
455 * for all subsequent instances, this calls xstate_enable().
456 *
457 * This is somewhat obfuscated due to the lack of powerful enough
458 * overrides for the section checks.
459 */
Robert Richter82d41502010-07-20 20:50:51 +0200460void __cpuinit xsave_init(void)
461{
H. Peter Anvin1cff92d2010-07-21 14:23:10 -0700462 static __refdata void (*next_func)(void) = xstate_enable_boot_cpu;
463 void (*this_func)(void);
464
Robert Richter0e49bf62010-07-21 19:03:52 +0200465 if (!cpu_has_xsave)
466 return;
467
H. Peter Anvin1cff92d2010-07-21 14:23:10 -0700468 this_func = next_func;
469 next_func = xstate_enable;
470 this_func();
Robert Richter82d41502010-07-20 20:50:51 +0200471}