blob: 448fde96963c77fae62e6d952b0d3b673be0e3c1 [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;
92 clear_used_math(); /* trigger finit */
93 if (task_thread_info(tsk)->status & TS_USEDFPU) {
Suresh Siddhaed405952008-08-13 11:38:14 -070094 /*
95 * Start with clearing the user buffer. This will present a
96 * clean context for the bytes not touched by the fxsave/xsave.
97 */
98 __clear_user(buf, sig_xstate_size);
99
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700100 if (task_thread_info(tsk)->status & TS_XSAVE)
101 err = xsave_user(buf);
102 else
103 err = fxsave_user(buf);
104
Suresh Siddhaab513702008-07-29 10:29:22 -0700105 if (err)
106 return err;
107 task_thread_info(tsk)->status &= ~TS_USEDFPU;
108 stts();
109 } else {
110 if (__copy_to_user(buf, &tsk->thread.xstate->fxsave,
111 xstate_size))
112 return -1;
113 }
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700114
115 if (task_thread_info(tsk)->status & TS_XSAVE) {
116 struct _fpstate __user *fx = buf;
117
118 err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved,
119 sizeof(struct _fpx_sw_bytes));
120
121 err |= __put_user(FP_XSTATE_MAGIC2,
122 (__u32 __user *) (buf + sig_xstate_size
123 - FP_XSTATE_MAGIC2_SIZE));
Suresh Siddhaf364ead2008-10-07 14:04:27 -0700124 if (err)
125 return err;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700126 }
127
Suresh Siddhaab513702008-07-29 10:29:22 -0700128 return 1;
129}
130
131/*
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700132 * Restore the extended state if present. Otherwise, restore the FP/SSE
133 * state.
134 */
135int restore_user_xstate(void __user *buf)
136{
137 struct _fpx_sw_bytes fx_sw_user;
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700138 u64 mask;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700139 int err;
140
141 if (((unsigned long)buf % 64) ||
142 check_for_xstate(buf, buf, &fx_sw_user))
143 goto fx_only;
144
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700145 mask = fx_sw_user.xstate_bv;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700146
147 /*
148 * restore the state passed by the user.
149 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700150 err = xrestore_user(buf, mask);
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700151 if (err)
152 return err;
153
154 /*
155 * init the state skipped by the user.
156 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700157 mask = pcntxt_mask & ~mask;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700158
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700159 xrstor_state(init_xstate_buf, mask);
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700160
161 return 0;
162
163fx_only:
164 /*
165 * couldn't find the extended state information in the
166 * memory layout. Restore just the FP/SSE and init all
167 * the other extended state.
168 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700169 xrstor_state(init_xstate_buf, pcntxt_mask & ~XSTATE_FPSSE);
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700170 return fxrstor_checking((__force struct i387_fxsave_struct *)buf);
171}
172
173/*
Suresh Siddhaab513702008-07-29 10:29:22 -0700174 * This restores directly out of user space. Exceptions are handled.
175 */
176int restore_i387_xstate(void __user *buf)
177{
178 struct task_struct *tsk = current;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700179 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700180
181 if (!buf) {
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700182 if (used_math())
183 goto clear;
Suresh Siddhaab513702008-07-29 10:29:22 -0700184 return 0;
185 } else
186 if (!access_ok(VERIFY_READ, buf, sig_xstate_size))
187 return -EACCES;
188
189 if (!used_math()) {
190 err = init_fpu(tsk);
191 if (err)
192 return err;
193 }
194
195 if (!(task_thread_info(current)->status & TS_USEDFPU)) {
196 clts();
197 task_thread_info(current)->status |= TS_USEDFPU;
198 }
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700199 if (task_thread_info(tsk)->status & TS_XSAVE)
200 err = restore_user_xstate(buf);
201 else
202 err = fxrstor_checking((__force struct i387_fxsave_struct *)
203 buf);
Suresh Siddhaab513702008-07-29 10:29:22 -0700204 if (unlikely(err)) {
205 /*
206 * Encountered an error while doing the restore from the
207 * user buffer, clear the fpu state.
208 */
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700209clear:
Suresh Siddhaab513702008-07-29 10:29:22 -0700210 clear_fpu(tsk);
211 clear_used_math();
212 }
213 return err;
214}
215#endif
216
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700217/*
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700218 * Prepare the SW reserved portion of the fxsave memory layout, indicating
219 * the presence of the extended state information in the memory layout
220 * pointed by the fpstate pointer in the sigcontext.
221 * This will be saved when ever the FP and extended state context is
222 * saved on the user stack during the signal handler delivery to the user.
223 */
224void prepare_fx_sw_frame(void)
225{
226 int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) +
227 FP_XSTATE_MAGIC2_SIZE;
228
229 sig_xstate_size = sizeof(struct _fpstate) + size_extended;
230
231#ifdef CONFIG_IA32_EMULATION
232 sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended;
233#endif
234
235 memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved));
236
237 fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
238 fx_sw_reserved.extended_size = sig_xstate_size;
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700239 fx_sw_reserved.xstate_bv = pcntxt_mask;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700240 fx_sw_reserved.xstate_size = xstate_size;
241#ifdef CONFIG_IA32_EMULATION
242 memcpy(&fx_sw_reserved_ia32, &fx_sw_reserved,
243 sizeof(struct _fpx_sw_bytes));
244 fx_sw_reserved_ia32.extended_size = sig_xstate_ia32_size;
245#endif
246}
247
248/*
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700249 * Represents init state for the supported extended state.
250 */
251struct xsave_struct *init_xstate_buf;
252
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700253#ifdef CONFIG_X86_64
254unsigned int sig_xstate_size = sizeof(struct _fpstate);
255#endif
256
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700257/*
258 * Enable the extended processor state save/restore feature
259 */
260void __cpuinit xsave_init(void)
261{
262 if (!cpu_has_xsave)
263 return;
264
265 set_in_cr4(X86_CR4_OSXSAVE);
266
267 /*
268 * Enable all the features that the HW is capable of
269 * and the Linux kernel is aware of.
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700270 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700271 xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700272}
273
274/*
275 * setup the xstate image representing the init state
276 */
Alexey Dobriyana19aac82008-08-30 06:03:34 +0400277static void __init setup_xstate_init(void)
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700278{
279 init_xstate_buf = alloc_bootmem(xstate_size);
280 init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
281}
282
283/*
284 * Enable and initialize the xsave feature.
285 */
286void __init xsave_cntxt_init(void)
287{
288 unsigned int eax, ebx, ecx, edx;
289
290 cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700291 pcntxt_mask = eax + ((u64)edx << 32);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700292
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700293 if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
294 printk(KERN_ERR "FP/SSE not shown under xsave features 0x%llx\n",
295 pcntxt_mask);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700296 BUG();
297 }
298
299 /*
300 * for now OS knows only about FP/SSE
301 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700302 pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700303 xsave_init();
304
305 /*
306 * Recompute the context size for enabled features
307 */
308 cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700309 xstate_size = ebx;
310
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700311 prepare_fx_sw_frame();
312
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700313 setup_xstate_init();
314
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700315 printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%llx, "
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700316 "cntxt size 0x%x\n",
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700317 pcntxt_mask, xstate_size);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700318}