blob: 711091114119532a1c9d2dff819da9c41e8a39b9 [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
Robert Richter45c2d7f2010-07-21 19:03:55 +020019/*
20 * Represents init state for the supported extended state.
21 */
22static struct xsave_struct *init_xstate_buf;
23
Suresh Siddhac37b5ef2008-07-29 10:29:25 -070024struct _fpx_sw_bytes fx_sw_reserved;
25#ifdef CONFIG_IA32_EMULATION
26struct _fpx_sw_bytes fx_sw_reserved_ia32;
27#endif
28
Suresh Siddhaa1488f82010-07-19 16:05:48 -070029static unsigned int *xstate_offsets, *xstate_sizes, xstate_features;
30
Suresh Siddhac37b5ef2008-07-29 10:29:25 -070031/*
Suresh Siddha29104e12010-07-19 16:05:49 -070032 * If a processor implementation discern that a processor state component is
33 * in its initialized state it may modify the corresponding bit in the
34 * xsave_hdr.xstate_bv as '0', with out modifying the corresponding memory
35 * layout in the case of xsaveopt. While presenting the xstate information to
36 * the user, we always ensure that the memory layout of a feature will be in
37 * the init state if the corresponding header bit is zero. This is to ensure
38 * that the user doesn't see some stale state in the memory layout during
39 * signal handling, debugging etc.
40 */
41void __sanitize_i387_state(struct task_struct *tsk)
42{
43 u64 xstate_bv;
44 int feature_bit = 0x2;
45 struct i387_fxsave_struct *fx = &tsk->thread.fpu.state->fxsave;
46
47 if (!fx)
48 return;
49
Linus Torvaldsf94edac2012-02-17 21:48:54 -080050 BUG_ON(__thread_has_fpu(tsk));
Suresh Siddha29104e12010-07-19 16:05:49 -070051
52 xstate_bv = tsk->thread.fpu.state->xsave.xsave_hdr.xstate_bv;
53
54 /*
55 * None of the feature bits are in init state. So nothing else
Lucas De Marchi0d2eb442011-03-17 16:24:16 -030056 * to do for us, as the memory layout is up to date.
Suresh Siddha29104e12010-07-19 16:05:49 -070057 */
58 if ((xstate_bv & pcntxt_mask) == pcntxt_mask)
59 return;
60
61 /*
62 * FP is in init state
63 */
64 if (!(xstate_bv & XSTATE_FP)) {
65 fx->cwd = 0x37f;
66 fx->swd = 0;
67 fx->twd = 0;
68 fx->fop = 0;
69 fx->rip = 0;
70 fx->rdp = 0;
71 memset(&fx->st_space[0], 0, 128);
72 }
73
74 /*
75 * SSE is in init state
76 */
77 if (!(xstate_bv & XSTATE_SSE))
78 memset(&fx->xmm_space[0], 0, 256);
79
80 xstate_bv = (pcntxt_mask & ~xstate_bv) >> 2;
81
82 /*
83 * Update all the other memory layouts for which the corresponding
84 * header bit is in the init state.
85 */
86 while (xstate_bv) {
87 if (xstate_bv & 0x1) {
88 int offset = xstate_offsets[feature_bit];
89 int size = xstate_sizes[feature_bit];
90
91 memcpy(((void *) fx) + offset,
92 ((void *) init_xstate_buf) + offset,
93 size);
94 }
95
96 xstate_bv >>= 1;
97 feature_bit++;
98 }
99}
100
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700101/*
102 * Check for the presence of extended state information in the
103 * user fpstate pointer in the sigcontext.
104 */
105int check_for_xstate(struct i387_fxsave_struct __user *buf,
106 void __user *fpstate,
107 struct _fpx_sw_bytes *fx_sw_user)
108{
109 int min_xstate_size = sizeof(struct i387_fxsave_struct) +
110 sizeof(struct xsave_hdr_struct);
111 unsigned int magic2;
112 int err;
113
114 err = __copy_from_user(fx_sw_user, &buf->sw_reserved[0],
115 sizeof(struct _fpx_sw_bytes));
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700116 if (err)
Dan Carpenterd6d4d422010-06-03 12:07:46 +0200117 return -EFAULT;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700118
119 /*
120 * First Magic check failed.
121 */
122 if (fx_sw_user->magic1 != FP_XSTATE_MAGIC1)
Dan Carpenterd6d4d422010-06-03 12:07:46 +0200123 return -EINVAL;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700124
125 /*
126 * Check for error scenarios.
127 */
128 if (fx_sw_user->xstate_size < min_xstate_size ||
129 fx_sw_user->xstate_size > xstate_size ||
130 fx_sw_user->xstate_size > fx_sw_user->extended_size)
Dan Carpenterd6d4d422010-06-03 12:07:46 +0200131 return -EINVAL;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700132
133 err = __get_user(magic2, (__u32 *) (((void *)fpstate) +
134 fx_sw_user->extended_size -
135 FP_XSTATE_MAGIC2_SIZE));
Dan Carpenterd6d4d422010-06-03 12:07:46 +0200136 if (err)
137 return err;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700138 /*
139 * Check for the presence of second magic word at the end of memory
140 * layout. This detects the case where the user just copied the legacy
141 * fpstate layout with out copying the extended state information
142 * in the memory layout.
143 */
Dan Carpenterd6d4d422010-06-03 12:07:46 +0200144 if (magic2 != FP_XSTATE_MAGIC2)
145 return -EFAULT;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700146
147 return 0;
148}
149
Suresh Siddhaab513702008-07-29 10:29:22 -0700150#ifdef CONFIG_X86_64
151/*
152 * Signal frame handlers.
153 */
154
155int save_i387_xstate(void __user *buf)
156{
157 struct task_struct *tsk = current;
158 int err = 0;
159
160 if (!access_ok(VERIFY_WRITE, buf, sig_xstate_size))
161 return -EACCES;
162
Suresh Siddhaf65bc212008-08-13 11:38:15 -0700163 BUG_ON(sig_xstate_size < xstate_size);
Suresh Siddhaab513702008-07-29 10:29:22 -0700164
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700165 if ((unsigned long)buf % 64)
Suresh Siddhaab513702008-07-29 10:29:22 -0700166 printk("save_i387_xstate: bad fpstate %p\n", buf);
167
168 if (!used_math())
169 return 0;
Suresh Siddha06c38d52009-04-09 15:24:34 -0700170
Linus Torvalds15d87912012-02-16 09:15:04 -0800171 if (user_has_fpu()) {
Avi Kivityc9ad4882010-05-06 11:45:45 +0300172 if (use_xsave())
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700173 err = xsave_user(buf);
174 else
175 err = fxsave_user(buf);
176
Suresh Siddhaab513702008-07-29 10:29:22 -0700177 if (err)
178 return err;
Linus Torvalds15d87912012-02-16 09:15:04 -0800179 user_fpu_end();
Suresh Siddhaab513702008-07-29 10:29:22 -0700180 } else {
Suresh Siddha29104e12010-07-19 16:05:49 -0700181 sanitize_i387_state(tsk);
Avi Kivity86603282010-05-06 11:45:46 +0300182 if (__copy_to_user(buf, &tsk->thread.fpu.state->fxsave,
Suresh Siddhaab513702008-07-29 10:29:22 -0700183 xstate_size))
184 return -1;
185 }
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700186
Suresh Siddha06c38d52009-04-09 15:24:34 -0700187 clear_used_math(); /* trigger finit */
188
Avi Kivityc9ad4882010-05-06 11:45:45 +0300189 if (use_xsave()) {
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700190 struct _fpstate __user *fx = buf;
Suresh Siddha04944b72008-10-07 14:04:28 -0700191 struct _xstate __user *x = buf;
192 u64 xstate_bv;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700193
194 err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved,
195 sizeof(struct _fpx_sw_bytes));
196
197 err |= __put_user(FP_XSTATE_MAGIC2,
198 (__u32 __user *) (buf + sig_xstate_size
199 - FP_XSTATE_MAGIC2_SIZE));
Suresh Siddha04944b72008-10-07 14:04:28 -0700200
201 /*
202 * Read the xstate_bv which we copied (directly from the cpu or
203 * from the state in task struct) to the user buffers and
204 * set the FP/SSE bits.
205 */
206 err |= __get_user(xstate_bv, &x->xstate_hdr.xstate_bv);
207
208 /*
209 * For legacy compatible, we always set FP/SSE bits in the bit
210 * vector while saving the state to the user context. This will
211 * enable us capturing any changes(during sigreturn) to
212 * the FP/SSE bits by the legacy applications which don't touch
213 * xstate_bv in the xsave header.
214 *
215 * xsave aware apps can change the xstate_bv in the xsave
216 * header as well as change any contents in the memory layout.
217 * xrestore as part of sigreturn will capture all the changes.
218 */
219 xstate_bv |= XSTATE_FPSSE;
220
221 err |= __put_user(xstate_bv, &x->xstate_hdr.xstate_bv);
222
Suresh Siddhaf364ead2008-10-07 14:04:27 -0700223 if (err)
224 return err;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700225 }
226
Suresh Siddhaab513702008-07-29 10:29:22 -0700227 return 1;
228}
229
230/*
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700231 * Restore the extended state if present. Otherwise, restore the FP/SSE
232 * state.
233 */
Jaswinder Singh Rajput7820b752008-12-30 22:05:55 +0530234static int restore_user_xstate(void __user *buf)
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700235{
236 struct _fpx_sw_bytes fx_sw_user;
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700237 u64 mask;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700238 int err;
239
240 if (((unsigned long)buf % 64) ||
241 check_for_xstate(buf, buf, &fx_sw_user))
242 goto fx_only;
243
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700244 mask = fx_sw_user.xstate_bv;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700245
246 /*
247 * restore the state passed by the user.
248 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700249 err = xrestore_user(buf, mask);
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700250 if (err)
251 return err;
252
253 /*
254 * init the state skipped by the user.
255 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700256 mask = pcntxt_mask & ~mask;
Suresh Siddha8e221b62010-06-22 16:23:37 -0700257 if (unlikely(mask))
258 xrstor_state(init_xstate_buf, mask);
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700259
260 return 0;
261
262fx_only:
263 /*
264 * couldn't find the extended state information in the
265 * memory layout. Restore just the FP/SSE and init all
266 * the other extended state.
267 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700268 xrstor_state(init_xstate_buf, pcntxt_mask & ~XSTATE_FPSSE);
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700269 return fxrstor_checking((__force struct i387_fxsave_struct *)buf);
270}
271
272/*
Suresh Siddhaab513702008-07-29 10:29:22 -0700273 * This restores directly out of user space. Exceptions are handled.
274 */
275int restore_i387_xstate(void __user *buf)
276{
277 struct task_struct *tsk = current;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700278 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700279
280 if (!buf) {
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700281 if (used_math())
282 goto clear;
Suresh Siddhaab513702008-07-29 10:29:22 -0700283 return 0;
284 } else
285 if (!access_ok(VERIFY_READ, buf, sig_xstate_size))
286 return -EACCES;
287
288 if (!used_math()) {
289 err = init_fpu(tsk);
290 if (err)
291 return err;
292 }
293
Linus Torvalds15d87912012-02-16 09:15:04 -0800294 user_fpu_begin();
Avi Kivityc9ad4882010-05-06 11:45:45 +0300295 if (use_xsave())
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700296 err = restore_user_xstate(buf);
297 else
298 err = fxrstor_checking((__force struct i387_fxsave_struct *)
299 buf);
Suresh Siddhaab513702008-07-29 10:29:22 -0700300 if (unlikely(err)) {
301 /*
302 * Encountered an error while doing the restore from the
303 * user buffer, clear the fpu state.
304 */
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700305clear:
Suresh Siddhaab513702008-07-29 10:29:22 -0700306 clear_fpu(tsk);
307 clear_used_math();
308 }
309 return err;
310}
311#endif
312
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700313/*
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700314 * Prepare the SW reserved portion of the fxsave memory layout, indicating
315 * the presence of the extended state information in the memory layout
316 * pointed by the fpstate pointer in the sigcontext.
317 * This will be saved when ever the FP and extended state context is
318 * saved on the user stack during the signal handler delivery to the user.
319 */
roel kluin8bcad302008-10-21 19:49:09 -0400320static void prepare_fx_sw_frame(void)
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700321{
322 int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) +
323 FP_XSTATE_MAGIC2_SIZE;
324
325 sig_xstate_size = sizeof(struct _fpstate) + size_extended;
326
327#ifdef CONFIG_IA32_EMULATION
328 sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended;
329#endif
330
331 memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved));
332
333 fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
334 fx_sw_reserved.extended_size = sig_xstate_size;
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700335 fx_sw_reserved.xstate_bv = pcntxt_mask;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700336 fx_sw_reserved.xstate_size = xstate_size;
337#ifdef CONFIG_IA32_EMULATION
338 memcpy(&fx_sw_reserved_ia32, &fx_sw_reserved,
339 sizeof(struct _fpx_sw_bytes));
340 fx_sw_reserved_ia32.extended_size = sig_xstate_ia32_size;
341#endif
342}
343
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700344#ifdef CONFIG_X86_64
345unsigned int sig_xstate_size = sizeof(struct _fpstate);
346#endif
347
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700348/*
349 * Enable the extended processor state save/restore feature
350 */
H. Peter Anvin1cff92d2010-07-21 14:23:10 -0700351static inline void xstate_enable(void)
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700352{
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700353 set_in_cr4(X86_CR4_OSXSAVE);
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700354 xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700355}
356
357/*
Suresh Siddhaa1488f82010-07-19 16:05:48 -0700358 * Record the offsets and sizes of different state managed by the xsave
359 * memory layout.
360 */
Robert Richter4995b9d2010-07-21 19:03:56 +0200361static void __init setup_xstate_features(void)
Suresh Siddhaa1488f82010-07-19 16:05:48 -0700362{
363 int eax, ebx, ecx, edx, leaf = 0x2;
364
365 xstate_features = fls64(pcntxt_mask);
366 xstate_offsets = alloc_bootmem(xstate_features * sizeof(int));
367 xstate_sizes = alloc_bootmem(xstate_features * sizeof(int));
368
369 do {
Robert Richteree813d52010-07-21 19:03:54 +0200370 cpuid_count(XSTATE_CPUID, leaf, &eax, &ebx, &ecx, &edx);
Suresh Siddhaa1488f82010-07-19 16:05:48 -0700371
372 if (eax == 0)
373 break;
374
375 xstate_offsets[leaf] = ebx;
376 xstate_sizes[leaf] = eax;
377
378 leaf++;
379 } while (1);
380}
381
382/*
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700383 * setup the xstate image representing the init state
384 */
Alexey Dobriyana19aac82008-08-30 06:03:34 +0400385static void __init setup_xstate_init(void)
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700386{
Suresh Siddha29104e12010-07-19 16:05:49 -0700387 setup_xstate_features();
388
389 /*
390 * Setup init_xstate_buf to represent the init state of
391 * all the features managed by the xsave
392 */
Suresh Siddha10340ae2010-11-16 13:23:51 -0800393 init_xstate_buf = alloc_bootmem_align(xstate_size,
394 __alignof__(struct xsave_struct));
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700395 init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
Suresh Siddhaa1488f82010-07-19 16:05:48 -0700396
Suresh Siddha29104e12010-07-19 16:05:49 -0700397 clts();
398 /*
399 * Init all the features state with header_bv being 0x0
400 */
401 xrstor_state(init_xstate_buf, -1);
402 /*
403 * Dump the init state again. This is to identify the init state
404 * of any feature which is not represented by all zero's.
405 */
406 xsave_state(init_xstate_buf, -1);
407 stts();
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700408}
409
410/*
411 * Enable and initialize the xsave feature.
412 */
H. Peter Anvin1cff92d2010-07-21 14:23:10 -0700413static void __init xstate_enable_boot_cpu(void)
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700414{
415 unsigned int eax, ebx, ecx, edx;
416
Robert Richteree813d52010-07-21 19:03:54 +0200417 if (boot_cpu_data.cpuid_level < XSTATE_CPUID) {
418 WARN(1, KERN_ERR "XSTATE_CPUID missing\n");
419 return;
420 }
421
422 cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700423 pcntxt_mask = eax + ((u64)edx << 32);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700424
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700425 if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
426 printk(KERN_ERR "FP/SSE not shown under xsave features 0x%llx\n",
427 pcntxt_mask);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700428 BUG();
429 }
430
431 /*
Suresh Siddhaa30469e2009-04-10 15:21:24 -0700432 * Support only the state known to OS.
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700433 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700434 pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
Robert Richter97e80a72010-07-21 19:03:53 +0200435
H. Peter Anvin1cff92d2010-07-21 14:23:10 -0700436 xstate_enable();
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700437
438 /*
439 * Recompute the context size for enabled features
440 */
Robert Richteree813d52010-07-21 19:03:54 +0200441 cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700442 xstate_size = ebx;
443
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800444 update_regset_xstate_info(xstate_size, pcntxt_mask);
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700445 prepare_fx_sw_frame();
446
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700447 setup_xstate_init();
448
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700449 printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%llx, "
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700450 "cntxt size 0x%x\n",
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700451 pcntxt_mask, xstate_size);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700452}
Robert Richter82d41502010-07-20 20:50:51 +0200453
H. Peter Anvin1cff92d2010-07-21 14:23:10 -0700454/*
455 * For the very first instance, this calls xstate_enable_boot_cpu();
456 * for all subsequent instances, this calls xstate_enable().
457 *
458 * This is somewhat obfuscated due to the lack of powerful enough
459 * overrides for the section checks.
460 */
Robert Richter82d41502010-07-20 20:50:51 +0200461void __cpuinit xsave_init(void)
462{
H. Peter Anvin1cff92d2010-07-21 14:23:10 -0700463 static __refdata void (*next_func)(void) = xstate_enable_boot_cpu;
464 void (*this_func)(void);
465
Robert Richter0e49bf62010-07-21 19:03:52 +0200466 if (!cpu_has_xsave)
467 return;
468
H. Peter Anvin1cff92d2010-07-21 14:23:10 -0700469 this_func = next_func;
470 next_func = xstate_enable;
471 this_func();
Robert Richter82d41502010-07-20 20:50:51 +0200472}