]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/x86/kernel/xsave.c
x86, xsave: clear the user buffer before doing fxsave/xsave
[linux-2.6-omap-h63xx.git] / arch / x86 / kernel / xsave.c
1 /*
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>
9 #ifdef CONFIG_IA32_EMULATION
10 #include <asm/sigcontext32.h>
11 #endif
12 #include <asm/xcr.h>
13
14 /*
15  * Supported feature mask by the CPU and the kernel.
16  */
17 u64 pcntxt_mask;
18
19 struct _fpx_sw_bytes fx_sw_reserved;
20 #ifdef CONFIG_IA32_EMULATION
21 struct _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  */
28 int 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
72 #ifdef CONFIG_X86_64
73 /*
74  * Signal frame handlers.
75  */
76
77 int 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
88         if ((unsigned long)buf % 64)
89                 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) {
95                 /*
96                  * Start with clearing the user buffer. This will present a
97                  * clean context for the bytes not touched by the fxsave/xsave.
98                  */
99                 __clear_user(buf, sig_xstate_size);
100
101                 if (task_thread_info(tsk)->status & TS_XSAVE)
102                         err = xsave_user(buf);
103                 else
104                         err = fxsave_user(buf);
105
106                 if (err)
107                         return err;
108                 task_thread_info(tsk)->status &= ~TS_USEDFPU;
109                 stts();
110         } else {
111                 if (__copy_to_user(buf, &tsk->thread.xstate->fxsave,
112                                    xstate_size))
113                         return -1;
114         }
115
116         if (task_thread_info(tsk)->status & TS_XSAVE) {
117                 struct _fpstate __user *fx = buf;
118
119                 err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved,
120                                      sizeof(struct _fpx_sw_bytes));
121
122                 err |= __put_user(FP_XSTATE_MAGIC2,
123                                   (__u32 __user *) (buf + sig_xstate_size
124                                                     - FP_XSTATE_MAGIC2_SIZE));
125         }
126
127         return 1;
128 }
129
130 /*
131  * Restore the extended state if present. Otherwise, restore the FP/SSE
132  * state.
133  */
134 int restore_user_xstate(void __user *buf)
135 {
136         struct _fpx_sw_bytes fx_sw_user;
137         u64 mask;
138         int err;
139
140         if (((unsigned long)buf % 64) ||
141              check_for_xstate(buf, buf, &fx_sw_user))
142                 goto fx_only;
143
144         mask = fx_sw_user.xstate_bv;
145
146         /*
147          * restore the state passed by the user.
148          */
149         err = xrestore_user(buf, mask);
150         if (err)
151                 return err;
152
153         /*
154          * init the state skipped by the user.
155          */
156         mask = pcntxt_mask & ~mask;
157
158         xrstor_state(init_xstate_buf, mask);
159
160         return 0;
161
162 fx_only:
163         /*
164          * couldn't find the extended state information in the
165          * memory layout. Restore just the FP/SSE and init all
166          * the other extended state.
167          */
168         xrstor_state(init_xstate_buf, pcntxt_mask & ~XSTATE_FPSSE);
169         return fxrstor_checking((__force struct i387_fxsave_struct *)buf);
170 }
171
172 /*
173  * This restores directly out of user space. Exceptions are handled.
174  */
175 int restore_i387_xstate(void __user *buf)
176 {
177         struct task_struct *tsk = current;
178         int err = 0;
179
180         if (!buf) {
181                 if (used_math())
182                         goto clear;
183                 return 0;
184         } else
185                 if (!access_ok(VERIFY_READ, buf, sig_xstate_size))
186                         return -EACCES;
187
188         if (!used_math()) {
189                 err = init_fpu(tsk);
190                 if (err)
191                         return err;
192         }
193
194         if (!(task_thread_info(current)->status & TS_USEDFPU)) {
195                 clts();
196                 task_thread_info(current)->status |= TS_USEDFPU;
197         }
198         if (task_thread_info(tsk)->status & TS_XSAVE)
199                 err = restore_user_xstate(buf);
200         else
201                 err = fxrstor_checking((__force struct i387_fxsave_struct *)
202                                        buf);
203         if (unlikely(err)) {
204                 /*
205                  * Encountered an error while doing the restore from the
206                  * user buffer, clear the fpu state.
207                  */
208 clear:
209                 clear_fpu(tsk);
210                 clear_used_math();
211         }
212         return err;
213 }
214 #endif
215
216 /*
217  * Prepare the SW reserved portion of the fxsave memory layout, indicating
218  * the presence of the extended state information in the memory layout
219  * pointed by the fpstate pointer in the sigcontext.
220  * This will be saved when ever the FP and extended state context is
221  * saved on the user stack during the signal handler delivery to the user.
222  */
223 void prepare_fx_sw_frame(void)
224 {
225         int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) +
226                              FP_XSTATE_MAGIC2_SIZE;
227
228         sig_xstate_size = sizeof(struct _fpstate) + size_extended;
229
230 #ifdef CONFIG_IA32_EMULATION
231         sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended;
232 #endif
233
234         memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved));
235
236         fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
237         fx_sw_reserved.extended_size = sig_xstate_size;
238         fx_sw_reserved.xstate_bv = pcntxt_mask;
239         fx_sw_reserved.xstate_size = xstate_size;
240 #ifdef CONFIG_IA32_EMULATION
241         memcpy(&fx_sw_reserved_ia32, &fx_sw_reserved,
242                sizeof(struct _fpx_sw_bytes));
243         fx_sw_reserved_ia32.extended_size = sig_xstate_ia32_size;
244 #endif
245 }
246
247 /*
248  * Represents init state for the supported extended state.
249  */
250 struct xsave_struct *init_xstate_buf;
251
252 #ifdef CONFIG_X86_64
253 unsigned int sig_xstate_size = sizeof(struct _fpstate);
254 #endif
255
256 /*
257  * Enable the extended processor state save/restore feature
258  */
259 void __cpuinit xsave_init(void)
260 {
261         if (!cpu_has_xsave)
262                 return;
263
264         set_in_cr4(X86_CR4_OSXSAVE);
265
266         /*
267          * Enable all the features that the HW is capable of
268          * and the Linux kernel is aware of.
269          */
270         xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
271 }
272
273 /*
274  * setup the xstate image representing the init state
275  */
276 void setup_xstate_init(void)
277 {
278         init_xstate_buf = alloc_bootmem(xstate_size);
279         init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
280 }
281
282 /*
283  * Enable and initialize the xsave feature.
284  */
285 void __init xsave_cntxt_init(void)
286 {
287         unsigned int eax, ebx, ecx, edx;
288
289         cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
290         pcntxt_mask = eax + ((u64)edx << 32);
291
292         if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
293                 printk(KERN_ERR "FP/SSE not shown under xsave features 0x%llx\n",
294                        pcntxt_mask);
295                 BUG();
296         }
297
298         /*
299          * for now OS knows only about FP/SSE
300          */
301         pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
302         xsave_init();
303
304         /*
305          * Recompute the context size for enabled features
306          */
307         cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
308         xstate_size = ebx;
309
310         prepare_fx_sw_frame();
311
312         setup_xstate_init();
313
314         printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%llx, "
315                "cntxt size 0x%x\n",
316                pcntxt_mask, xstate_size);
317 }