blob: 7415f3e38a5160a9fc9914345f1c1efcc8be0606 [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
85 BUILD_BUG_ON(sizeof(struct user_i387_struct) !=
86 sizeof(tsk->thread.xstate->fxsave));
87
Suresh Siddhac37b5ef2008-07-29 10:29:25 -070088 if ((unsigned long)buf % 64)
Suresh Siddhaab513702008-07-29 10:29:22 -070089 printk("save_i387_xstate: bad fpstate %p\n", buf);
90
91 if (!used_math())
92 return 0;
93 clear_used_math(); /* trigger finit */
94 if (task_thread_info(tsk)->status & TS_USEDFPU) {
Suresh Siddhac37b5ef2008-07-29 10:29:25 -070095 if (task_thread_info(tsk)->status & TS_XSAVE)
96 err = xsave_user(buf);
97 else
98 err = fxsave_user(buf);
99
Suresh Siddhaab513702008-07-29 10:29:22 -0700100 if (err)
101 return err;
102 task_thread_info(tsk)->status &= ~TS_USEDFPU;
103 stts();
104 } else {
105 if (__copy_to_user(buf, &tsk->thread.xstate->fxsave,
106 xstate_size))
107 return -1;
108 }
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700109
110 if (task_thread_info(tsk)->status & TS_XSAVE) {
111 struct _fpstate __user *fx = buf;
112
113 err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved,
114 sizeof(struct _fpx_sw_bytes));
115
116 err |= __put_user(FP_XSTATE_MAGIC2,
117 (__u32 __user *) (buf + sig_xstate_size
118 - FP_XSTATE_MAGIC2_SIZE));
119 }
120
Suresh Siddhaab513702008-07-29 10:29:22 -0700121 return 1;
122}
123
124/*
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700125 * Restore the extended state if present. Otherwise, restore the FP/SSE
126 * state.
127 */
128int restore_user_xstate(void __user *buf)
129{
130 struct _fpx_sw_bytes fx_sw_user;
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700131 u64 mask;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700132 int err;
133
134 if (((unsigned long)buf % 64) ||
135 check_for_xstate(buf, buf, &fx_sw_user))
136 goto fx_only;
137
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700138 mask = fx_sw_user.xstate_bv;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700139
140 /*
141 * restore the state passed by the user.
142 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700143 err = xrestore_user(buf, mask);
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700144 if (err)
145 return err;
146
147 /*
148 * init the state skipped by the user.
149 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700150 mask = pcntxt_mask & ~mask;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700151
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700152 xrstor_state(init_xstate_buf, mask);
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700153
154 return 0;
155
156fx_only:
157 /*
158 * couldn't find the extended state information in the
159 * memory layout. Restore just the FP/SSE and init all
160 * the other extended state.
161 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700162 xrstor_state(init_xstate_buf, pcntxt_mask & ~XSTATE_FPSSE);
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700163 return fxrstor_checking((__force struct i387_fxsave_struct *)buf);
164}
165
166/*
Suresh Siddhaab513702008-07-29 10:29:22 -0700167 * This restores directly out of user space. Exceptions are handled.
168 */
169int restore_i387_xstate(void __user *buf)
170{
171 struct task_struct *tsk = current;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700172 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700173
174 if (!buf) {
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700175 if (used_math())
176 goto clear;
Suresh Siddhaab513702008-07-29 10:29:22 -0700177 return 0;
178 } else
179 if (!access_ok(VERIFY_READ, buf, sig_xstate_size))
180 return -EACCES;
181
182 if (!used_math()) {
183 err = init_fpu(tsk);
184 if (err)
185 return err;
186 }
187
188 if (!(task_thread_info(current)->status & TS_USEDFPU)) {
189 clts();
190 task_thread_info(current)->status |= TS_USEDFPU;
191 }
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700192 if (task_thread_info(tsk)->status & TS_XSAVE)
193 err = restore_user_xstate(buf);
194 else
195 err = fxrstor_checking((__force struct i387_fxsave_struct *)
196 buf);
Suresh Siddhaab513702008-07-29 10:29:22 -0700197 if (unlikely(err)) {
198 /*
199 * Encountered an error while doing the restore from the
200 * user buffer, clear the fpu state.
201 */
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700202clear:
Suresh Siddhaab513702008-07-29 10:29:22 -0700203 clear_fpu(tsk);
204 clear_used_math();
205 }
206 return err;
207}
208#endif
209
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700210/*
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700211 * Prepare the SW reserved portion of the fxsave memory layout, indicating
212 * the presence of the extended state information in the memory layout
213 * pointed by the fpstate pointer in the sigcontext.
214 * This will be saved when ever the FP and extended state context is
215 * saved on the user stack during the signal handler delivery to the user.
216 */
217void prepare_fx_sw_frame(void)
218{
219 int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) +
220 FP_XSTATE_MAGIC2_SIZE;
221
222 sig_xstate_size = sizeof(struct _fpstate) + size_extended;
223
224#ifdef CONFIG_IA32_EMULATION
225 sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended;
226#endif
227
228 memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved));
229
230 fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
231 fx_sw_reserved.extended_size = sig_xstate_size;
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700232 fx_sw_reserved.xstate_bv = pcntxt_mask;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700233 fx_sw_reserved.xstate_size = xstate_size;
234#ifdef CONFIG_IA32_EMULATION
235 memcpy(&fx_sw_reserved_ia32, &fx_sw_reserved,
236 sizeof(struct _fpx_sw_bytes));
237 fx_sw_reserved_ia32.extended_size = sig_xstate_ia32_size;
238#endif
239}
240
241/*
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700242 * Represents init state for the supported extended state.
243 */
244struct xsave_struct *init_xstate_buf;
245
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700246#ifdef CONFIG_X86_64
247unsigned int sig_xstate_size = sizeof(struct _fpstate);
248#endif
249
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700250/*
251 * Enable the extended processor state save/restore feature
252 */
253void __cpuinit xsave_init(void)
254{
255 if (!cpu_has_xsave)
256 return;
257
258 set_in_cr4(X86_CR4_OSXSAVE);
259
260 /*
261 * Enable all the features that the HW is capable of
262 * and the Linux kernel is aware of.
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700263 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700264 xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700265}
266
267/*
268 * setup the xstate image representing the init state
269 */
270void setup_xstate_init(void)
271{
272 init_xstate_buf = alloc_bootmem(xstate_size);
273 init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
274}
275
276/*
277 * Enable and initialize the xsave feature.
278 */
279void __init xsave_cntxt_init(void)
280{
281 unsigned int eax, ebx, ecx, edx;
282
283 cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700284 pcntxt_mask = eax + ((u64)edx << 32);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700285
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700286 if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
287 printk(KERN_ERR "FP/SSE not shown under xsave features 0x%llx\n",
288 pcntxt_mask);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700289 BUG();
290 }
291
292 /*
293 * for now OS knows only about FP/SSE
294 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700295 pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700296 xsave_init();
297
298 /*
299 * Recompute the context size for enabled features
300 */
301 cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700302 xstate_size = ebx;
303
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700304 prepare_fx_sw_frame();
305
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700306 setup_xstate_init();
307
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700308 printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%llx, "
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700309 "cntxt size 0x%x\n",
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700310 pcntxt_mask, xstate_size);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700311}