blob: 6e73db1b7b4ee67fe2a8a8d5b99d0e8b7c84a669 [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
24/*
25 * Check for the presence of extended state information in the
26 * user fpstate pointer in the sigcontext.
27 */
28int check_for_xstate(struct i387_fxsave_struct __user *buf,
29 void __user *fpstate,
30 struct _fpx_sw_bytes *fx_sw_user)
31{
32 int min_xstate_size = sizeof(struct i387_fxsave_struct) +
33 sizeof(struct xsave_hdr_struct);
34 unsigned int magic2;
35 int err;
36
37 err = __copy_from_user(fx_sw_user, &buf->sw_reserved[0],
38 sizeof(struct _fpx_sw_bytes));
39
40 if (err)
41 return err;
42
43 /*
44 * First Magic check failed.
45 */
46 if (fx_sw_user->magic1 != FP_XSTATE_MAGIC1)
47 return -1;
48
49 /*
50 * Check for error scenarios.
51 */
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)
55 return -1;
56
57 err = __get_user(magic2, (__u32 *) (((void *)fpstate) +
58 fx_sw_user->extended_size -
59 FP_XSTATE_MAGIC2_SIZE));
60 /*
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.
65 */
66 if (err || magic2 != FP_XSTATE_MAGIC2)
67 return -1;
68
69 return 0;
70}
71
Suresh Siddhaab513702008-07-29 10:29:22 -070072#ifdef CONFIG_X86_64
73/*
74 * Signal frame handlers.
75 */
76
77int save_i387_xstate(void __user *buf)
78{
79 struct task_struct *tsk = current;
80 int err = 0;
81
82 if (!access_ok(VERIFY_WRITE, buf, sig_xstate_size))
83 return -EACCES;
84
Suresh Siddhaf65bc212008-08-13 11:38:15 -070085 BUG_ON(sig_xstate_size < xstate_size);
Suresh Siddhaab513702008-07-29 10:29:22 -070086
Suresh Siddhac37b5ef2008-07-29 10:29:25 -070087 if ((unsigned long)buf % 64)
Suresh Siddhaab513702008-07-29 10:29:22 -070088 printk("save_i387_xstate: bad fpstate %p\n", buf);
89
90 if (!used_math())
91 return 0;
Suresh Siddha06c38d52009-04-09 15:24:34 -070092
Suresh Siddhaab513702008-07-29 10:29:22 -070093 if (task_thread_info(tsk)->status & TS_USEDFPU) {
Avi Kivityc9ad4882010-05-06 11:45:45 +030094 if (use_xsave())
Suresh Siddhac37b5ef2008-07-29 10:29:25 -070095 err = xsave_user(buf);
96 else
97 err = fxsave_user(buf);
98
Suresh Siddhaab513702008-07-29 10:29:22 -070099 if (err)
100 return err;
101 task_thread_info(tsk)->status &= ~TS_USEDFPU;
102 stts();
103 } else {
Avi Kivity86603282010-05-06 11:45:46 +0300104 if (__copy_to_user(buf, &tsk->thread.fpu.state->fxsave,
Suresh Siddhaab513702008-07-29 10:29:22 -0700105 xstate_size))
106 return -1;
107 }
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700108
Suresh Siddha06c38d52009-04-09 15:24:34 -0700109 clear_used_math(); /* trigger finit */
110
Avi Kivityc9ad4882010-05-06 11:45:45 +0300111 if (use_xsave()) {
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700112 struct _fpstate __user *fx = buf;
Suresh Siddha04944b72008-10-07 14:04:28 -0700113 struct _xstate __user *x = buf;
114 u64 xstate_bv;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700115
116 err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved,
117 sizeof(struct _fpx_sw_bytes));
118
119 err |= __put_user(FP_XSTATE_MAGIC2,
120 (__u32 __user *) (buf + sig_xstate_size
121 - FP_XSTATE_MAGIC2_SIZE));
Suresh Siddha04944b72008-10-07 14:04:28 -0700122
123 /*
124 * Read the xstate_bv which we copied (directly from the cpu or
125 * from the state in task struct) to the user buffers and
126 * set the FP/SSE bits.
127 */
128 err |= __get_user(xstate_bv, &x->xstate_hdr.xstate_bv);
129
130 /*
131 * For legacy compatible, we always set FP/SSE bits in the bit
132 * vector while saving the state to the user context. This will
133 * enable us capturing any changes(during sigreturn) to
134 * the FP/SSE bits by the legacy applications which don't touch
135 * xstate_bv in the xsave header.
136 *
137 * xsave aware apps can change the xstate_bv in the xsave
138 * header as well as change any contents in the memory layout.
139 * xrestore as part of sigreturn will capture all the changes.
140 */
141 xstate_bv |= XSTATE_FPSSE;
142
143 err |= __put_user(xstate_bv, &x->xstate_hdr.xstate_bv);
144
Suresh Siddhaf364ead2008-10-07 14:04:27 -0700145 if (err)
146 return err;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700147 }
148
Suresh Siddhaab513702008-07-29 10:29:22 -0700149 return 1;
150}
151
152/*
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700153 * Restore the extended state if present. Otherwise, restore the FP/SSE
154 * state.
155 */
Jaswinder Singh Rajput7820b752008-12-30 22:05:55 +0530156static int restore_user_xstate(void __user *buf)
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700157{
158 struct _fpx_sw_bytes fx_sw_user;
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700159 u64 mask;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700160 int err;
161
162 if (((unsigned long)buf % 64) ||
163 check_for_xstate(buf, buf, &fx_sw_user))
164 goto fx_only;
165
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700166 mask = fx_sw_user.xstate_bv;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700167
168 /*
169 * restore the state passed by the user.
170 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700171 err = xrestore_user(buf, mask);
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700172 if (err)
173 return err;
174
175 /*
176 * init the state skipped by the user.
177 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700178 mask = pcntxt_mask & ~mask;
Suresh Siddha8e221b62010-06-22 16:23:37 -0700179 if (unlikely(mask))
180 xrstor_state(init_xstate_buf, mask);
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700181
182 return 0;
183
184fx_only:
185 /*
186 * couldn't find the extended state information in the
187 * memory layout. Restore just the FP/SSE and init all
188 * the other extended state.
189 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700190 xrstor_state(init_xstate_buf, pcntxt_mask & ~XSTATE_FPSSE);
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700191 return fxrstor_checking((__force struct i387_fxsave_struct *)buf);
192}
193
194/*
Suresh Siddhaab513702008-07-29 10:29:22 -0700195 * This restores directly out of user space. Exceptions are handled.
196 */
197int restore_i387_xstate(void __user *buf)
198{
199 struct task_struct *tsk = current;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700200 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700201
202 if (!buf) {
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700203 if (used_math())
204 goto clear;
Suresh Siddhaab513702008-07-29 10:29:22 -0700205 return 0;
206 } else
207 if (!access_ok(VERIFY_READ, buf, sig_xstate_size))
208 return -EACCES;
209
210 if (!used_math()) {
211 err = init_fpu(tsk);
212 if (err)
213 return err;
214 }
215
216 if (!(task_thread_info(current)->status & TS_USEDFPU)) {
217 clts();
218 task_thread_info(current)->status |= TS_USEDFPU;
219 }
Avi Kivityc9ad4882010-05-06 11:45:45 +0300220 if (use_xsave())
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700221 err = restore_user_xstate(buf);
222 else
223 err = fxrstor_checking((__force struct i387_fxsave_struct *)
224 buf);
Suresh Siddhaab513702008-07-29 10:29:22 -0700225 if (unlikely(err)) {
226 /*
227 * Encountered an error while doing the restore from the
228 * user buffer, clear the fpu state.
229 */
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700230clear:
Suresh Siddhaab513702008-07-29 10:29:22 -0700231 clear_fpu(tsk);
232 clear_used_math();
233 }
234 return err;
235}
236#endif
237
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700238/*
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700239 * Prepare the SW reserved portion of the fxsave memory layout, indicating
240 * the presence of the extended state information in the memory layout
241 * pointed by the fpstate pointer in the sigcontext.
242 * This will be saved when ever the FP and extended state context is
243 * saved on the user stack during the signal handler delivery to the user.
244 */
roel kluin8bcad302008-10-21 19:49:09 -0400245static void prepare_fx_sw_frame(void)
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700246{
247 int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) +
248 FP_XSTATE_MAGIC2_SIZE;
249
250 sig_xstate_size = sizeof(struct _fpstate) + size_extended;
251
252#ifdef CONFIG_IA32_EMULATION
253 sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended;
254#endif
255
256 memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved));
257
258 fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
259 fx_sw_reserved.extended_size = sig_xstate_size;
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700260 fx_sw_reserved.xstate_bv = pcntxt_mask;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700261 fx_sw_reserved.xstate_size = xstate_size;
262#ifdef CONFIG_IA32_EMULATION
263 memcpy(&fx_sw_reserved_ia32, &fx_sw_reserved,
264 sizeof(struct _fpx_sw_bytes));
265 fx_sw_reserved_ia32.extended_size = sig_xstate_ia32_size;
266#endif
267}
268
269/*
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700270 * Represents init state for the supported extended state.
271 */
272struct xsave_struct *init_xstate_buf;
273
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700274#ifdef CONFIG_X86_64
275unsigned int sig_xstate_size = sizeof(struct _fpstate);
276#endif
277
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700278/*
279 * Enable the extended processor state save/restore feature
280 */
281void __cpuinit xsave_init(void)
282{
283 if (!cpu_has_xsave)
284 return;
285
286 set_in_cr4(X86_CR4_OSXSAVE);
287
288 /*
289 * Enable all the features that the HW is capable of
290 * and the Linux kernel is aware of.
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700291 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700292 xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700293}
294
295/*
296 * setup the xstate image representing the init state
297 */
Alexey Dobriyana19aac82008-08-30 06:03:34 +0400298static void __init setup_xstate_init(void)
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700299{
300 init_xstate_buf = alloc_bootmem(xstate_size);
301 init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
302}
303
304/*
305 * Enable and initialize the xsave feature.
306 */
Rakib Mullickbfe085f2008-11-20 19:12:50 +0600307void __ref xsave_cntxt_init(void)
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700308{
309 unsigned int eax, ebx, ecx, edx;
310
311 cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700312 pcntxt_mask = eax + ((u64)edx << 32);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700313
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700314 if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
315 printk(KERN_ERR "FP/SSE not shown under xsave features 0x%llx\n",
316 pcntxt_mask);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700317 BUG();
318 }
319
320 /*
Suresh Siddhaa30469e2009-04-10 15:21:24 -0700321 * Support only the state known to OS.
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700322 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700323 pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700324 xsave_init();
325
326 /*
327 * Recompute the context size for enabled features
328 */
329 cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700330 xstate_size = ebx;
331
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800332 update_regset_xstate_info(xstate_size, pcntxt_mask);
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700333 prepare_fx_sw_frame();
334
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700335 setup_xstate_init();
336
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700337 printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%llx, "
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700338 "cntxt size 0x%x\n",
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700339 pcntxt_mask, xstate_size);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700340}