x86: xsave: set FP, SSE bits in the xsave header in the user sigcontext
[safe/jmp/linux-2.6] / 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         BUG_ON(sig_xstate_size < xstate_size);
86
87         if ((unsigned long)buf % 64)
88                 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) {
94                 /*
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
100                 if (task_thread_info(tsk)->status & TS_XSAVE)
101                         err = xsave_user(buf);
102                 else
103                         err = fxsave_user(buf);
104
105                 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         }
114
115         if (task_thread_info(tsk)->status & TS_XSAVE) {
116                 struct _fpstate __user *fx = buf;
117                 struct _xstate __user *x = buf;
118                 u64 xstate_bv;
119
120                 err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved,
121                                      sizeof(struct _fpx_sw_bytes));
122
123                 err |= __put_user(FP_XSTATE_MAGIC2,
124                                   (__u32 __user *) (buf + sig_xstate_size
125                                                     - FP_XSTATE_MAGIC2_SIZE));
126
127                 /*
128                  * Read the xstate_bv which we copied (directly from the cpu or
129                  * from the state in task struct) to the user buffers and
130                  * set the FP/SSE bits.
131                  */
132                 err |= __get_user(xstate_bv, &x->xstate_hdr.xstate_bv);
133
134                 /*
135                  * For legacy compatible, we always set FP/SSE bits in the bit
136                  * vector while saving the state to the user context. This will
137                  * enable us capturing any changes(during sigreturn) to
138                  * the FP/SSE bits by the legacy applications which don't touch
139                  * xstate_bv in the xsave header.
140                  *
141                  * xsave aware apps can change the xstate_bv in the xsave
142                  * header as well as change any contents in the memory layout.
143                  * xrestore as part of sigreturn will capture all the changes.
144                  */
145                 xstate_bv |= XSTATE_FPSSE;
146
147                 err |= __put_user(xstate_bv, &x->xstate_hdr.xstate_bv);
148
149                 if (err)
150                         return err;
151         }
152
153         return 1;
154 }
155
156 /*
157  * Restore the extended state if present. Otherwise, restore the FP/SSE
158  * state.
159  */
160 int restore_user_xstate(void __user *buf)
161 {
162         struct _fpx_sw_bytes fx_sw_user;
163         u64 mask;
164         int err;
165
166         if (((unsigned long)buf % 64) ||
167              check_for_xstate(buf, buf, &fx_sw_user))
168                 goto fx_only;
169
170         mask = fx_sw_user.xstate_bv;
171
172         /*
173          * restore the state passed by the user.
174          */
175         err = xrestore_user(buf, mask);
176         if (err)
177                 return err;
178
179         /*
180          * init the state skipped by the user.
181          */
182         mask = pcntxt_mask & ~mask;
183
184         xrstor_state(init_xstate_buf, mask);
185
186         return 0;
187
188 fx_only:
189         /*
190          * couldn't find the extended state information in the
191          * memory layout. Restore just the FP/SSE and init all
192          * the other extended state.
193          */
194         xrstor_state(init_xstate_buf, pcntxt_mask & ~XSTATE_FPSSE);
195         return fxrstor_checking((__force struct i387_fxsave_struct *)buf);
196 }
197
198 /*
199  * This restores directly out of user space. Exceptions are handled.
200  */
201 int restore_i387_xstate(void __user *buf)
202 {
203         struct task_struct *tsk = current;
204         int err = 0;
205
206         if (!buf) {
207                 if (used_math())
208                         goto clear;
209                 return 0;
210         } else
211                 if (!access_ok(VERIFY_READ, buf, sig_xstate_size))
212                         return -EACCES;
213
214         if (!used_math()) {
215                 err = init_fpu(tsk);
216                 if (err)
217                         return err;
218         }
219
220         if (!(task_thread_info(current)->status & TS_USEDFPU)) {
221                 clts();
222                 task_thread_info(current)->status |= TS_USEDFPU;
223         }
224         if (task_thread_info(tsk)->status & TS_XSAVE)
225                 err = restore_user_xstate(buf);
226         else
227                 err = fxrstor_checking((__force struct i387_fxsave_struct *)
228                                        buf);
229         if (unlikely(err)) {
230                 /*
231                  * Encountered an error while doing the restore from the
232                  * user buffer, clear the fpu state.
233                  */
234 clear:
235                 clear_fpu(tsk);
236                 clear_used_math();
237         }
238         return err;
239 }
240 #endif
241
242 /*
243  * Prepare the SW reserved portion of the fxsave memory layout, indicating
244  * the presence of the extended state information in the memory layout
245  * pointed by the fpstate pointer in the sigcontext.
246  * This will be saved when ever the FP and extended state context is
247  * saved on the user stack during the signal handler delivery to the user.
248  */
249 void prepare_fx_sw_frame(void)
250 {
251         int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) +
252                              FP_XSTATE_MAGIC2_SIZE;
253
254         sig_xstate_size = sizeof(struct _fpstate) + size_extended;
255
256 #ifdef CONFIG_IA32_EMULATION
257         sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended;
258 #endif
259
260         memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved));
261
262         fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
263         fx_sw_reserved.extended_size = sig_xstate_size;
264         fx_sw_reserved.xstate_bv = pcntxt_mask;
265         fx_sw_reserved.xstate_size = xstate_size;
266 #ifdef CONFIG_IA32_EMULATION
267         memcpy(&fx_sw_reserved_ia32, &fx_sw_reserved,
268                sizeof(struct _fpx_sw_bytes));
269         fx_sw_reserved_ia32.extended_size = sig_xstate_ia32_size;
270 #endif
271 }
272
273 /*
274  * Represents init state for the supported extended state.
275  */
276 struct xsave_struct *init_xstate_buf;
277
278 #ifdef CONFIG_X86_64
279 unsigned int sig_xstate_size = sizeof(struct _fpstate);
280 #endif
281
282 /*
283  * Enable the extended processor state save/restore feature
284  */
285 void __cpuinit xsave_init(void)
286 {
287         if (!cpu_has_xsave)
288                 return;
289
290         set_in_cr4(X86_CR4_OSXSAVE);
291
292         /*
293          * Enable all the features that the HW is capable of
294          * and the Linux kernel is aware of.
295          */
296         xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
297 }
298
299 /*
300  * setup the xstate image representing the init state
301  */
302 static void __init setup_xstate_init(void)
303 {
304         init_xstate_buf = alloc_bootmem(xstate_size);
305         init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
306 }
307
308 /*
309  * Enable and initialize the xsave feature.
310  */
311 void __init xsave_cntxt_init(void)
312 {
313         unsigned int eax, ebx, ecx, edx;
314
315         cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
316         pcntxt_mask = eax + ((u64)edx << 32);
317
318         if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
319                 printk(KERN_ERR "FP/SSE not shown under xsave features 0x%llx\n",
320                        pcntxt_mask);
321                 BUG();
322         }
323
324         /*
325          * for now OS knows only about FP/SSE
326          */
327         pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
328         xsave_init();
329
330         /*
331          * Recompute the context size for enabled features
332          */
333         cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
334         xstate_size = ebx;
335
336         prepare_fx_sw_frame();
337
338         setup_xstate_init();
339
340         printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%llx, "
341                "cntxt size 0x%x\n",
342                pcntxt_mask, xstate_size);
343 }