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