550bf45236f4bdd9f913da94714e352716d732cf
[firefly-linux-kernel-4.4.55.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 static unsigned int *xstate_offsets, *xstate_sizes, xstate_features;
25
26 /*
27  * If a processor implementation discern that a processor state component is
28  * in its initialized state it may modify the corresponding bit in the
29  * xsave_hdr.xstate_bv as '0', with out modifying the corresponding memory
30  * layout in the case of xsaveopt. While presenting the xstate information to
31  * the user, we always ensure that the memory layout of a feature will be in
32  * the init state if the corresponding header bit is zero. This is to ensure
33  * that the user doesn't see some stale state in the memory layout during
34  * signal handling, debugging etc.
35  */
36 void __sanitize_i387_state(struct task_struct *tsk)
37 {
38         u64 xstate_bv;
39         int feature_bit = 0x2;
40         struct i387_fxsave_struct *fx = &tsk->thread.fpu.state->fxsave;
41
42         if (!fx)
43                 return;
44
45         BUG_ON(task_thread_info(tsk)->status & TS_USEDFPU);
46
47         xstate_bv = tsk->thread.fpu.state->xsave.xsave_hdr.xstate_bv;
48
49         /*
50          * None of the feature bits are in init state. So nothing else
51          * to do for us, as the memory layout is upto date.
52          */
53         if ((xstate_bv & pcntxt_mask) == pcntxt_mask)
54                 return;
55
56         /*
57          * FP is in init state
58          */
59         if (!(xstate_bv & XSTATE_FP)) {
60                 fx->cwd = 0x37f;
61                 fx->swd = 0;
62                 fx->twd = 0;
63                 fx->fop = 0;
64                 fx->rip = 0;
65                 fx->rdp = 0;
66                 memset(&fx->st_space[0], 0, 128);
67         }
68
69         /*
70          * SSE is in init state
71          */
72         if (!(xstate_bv & XSTATE_SSE))
73                 memset(&fx->xmm_space[0], 0, 256);
74
75         xstate_bv = (pcntxt_mask & ~xstate_bv) >> 2;
76
77         /*
78          * Update all the other memory layouts for which the corresponding
79          * header bit is in the init state.
80          */
81         while (xstate_bv) {
82                 if (xstate_bv & 0x1) {
83                         int offset = xstate_offsets[feature_bit];
84                         int size = xstate_sizes[feature_bit];
85
86                         memcpy(((void *) fx) + offset,
87                                ((void *) init_xstate_buf) + offset,
88                                size);
89                 }
90
91                 xstate_bv >>= 1;
92                 feature_bit++;
93         }
94 }
95
96 /*
97  * Check for the presence of extended state information in the
98  * user fpstate pointer in the sigcontext.
99  */
100 int check_for_xstate(struct i387_fxsave_struct __user *buf,
101                      void __user *fpstate,
102                      struct _fpx_sw_bytes *fx_sw_user)
103 {
104         int min_xstate_size = sizeof(struct i387_fxsave_struct) +
105                               sizeof(struct xsave_hdr_struct);
106         unsigned int magic2;
107         int err;
108
109         err = __copy_from_user(fx_sw_user, &buf->sw_reserved[0],
110                                sizeof(struct _fpx_sw_bytes));
111         if (err)
112                 return -EFAULT;
113
114         /*
115          * First Magic check failed.
116          */
117         if (fx_sw_user->magic1 != FP_XSTATE_MAGIC1)
118                 return -EINVAL;
119
120         /*
121          * Check for error scenarios.
122          */
123         if (fx_sw_user->xstate_size < min_xstate_size ||
124             fx_sw_user->xstate_size > xstate_size ||
125             fx_sw_user->xstate_size > fx_sw_user->extended_size)
126                 return -EINVAL;
127
128         err = __get_user(magic2, (__u32 *) (((void *)fpstate) +
129                                             fx_sw_user->extended_size -
130                                             FP_XSTATE_MAGIC2_SIZE));
131         if (err)
132                 return err;
133         /*
134          * Check for the presence of second magic word at the end of memory
135          * layout. This detects the case where the user just copied the legacy
136          * fpstate layout with out copying the extended state information
137          * in the memory layout.
138          */
139         if (magic2 != FP_XSTATE_MAGIC2)
140                 return -EFAULT;
141
142         return 0;
143 }
144
145 #ifdef CONFIG_X86_64
146 /*
147  * Signal frame handlers.
148  */
149
150 int save_i387_xstate(void __user *buf)
151 {
152         struct task_struct *tsk = current;
153         int err = 0;
154
155         if (!access_ok(VERIFY_WRITE, buf, sig_xstate_size))
156                 return -EACCES;
157
158         BUG_ON(sig_xstate_size < xstate_size);
159
160         if ((unsigned long)buf % 64)
161                 printk("save_i387_xstate: bad fpstate %p\n", buf);
162
163         if (!used_math())
164                 return 0;
165
166         if (task_thread_info(tsk)->status & TS_USEDFPU) {
167                 /*
168                  * Start with clearing the user buffer. This will present a
169                  * clean context for the bytes not touched by the fxsave/xsave.
170                  */
171                 err = __clear_user(buf, sig_xstate_size);
172                 if (err)
173                         return err;
174
175                 if (use_xsave())
176                         err = xsave_user(buf);
177                 else
178                         err = fxsave_user(buf);
179
180                 if (err)
181                         return err;
182                 task_thread_info(tsk)->status &= ~TS_USEDFPU;
183                 stts();
184         } else {
185                 sanitize_i387_state(tsk);
186                 if (__copy_to_user(buf, &tsk->thread.fpu.state->fxsave,
187                                    xstate_size))
188                         return -1;
189         }
190
191         clear_used_math(); /* trigger finit */
192
193         if (use_xsave()) {
194                 struct _fpstate __user *fx = buf;
195                 struct _xstate __user *x = buf;
196                 u64 xstate_bv;
197
198                 err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved,
199                                      sizeof(struct _fpx_sw_bytes));
200
201                 err |= __put_user(FP_XSTATE_MAGIC2,
202                                   (__u32 __user *) (buf + sig_xstate_size
203                                                     - FP_XSTATE_MAGIC2_SIZE));
204
205                 /*
206                  * Read the xstate_bv which we copied (directly from the cpu or
207                  * from the state in task struct) to the user buffers and
208                  * set the FP/SSE bits.
209                  */
210                 err |= __get_user(xstate_bv, &x->xstate_hdr.xstate_bv);
211
212                 /*
213                  * For legacy compatible, we always set FP/SSE bits in the bit
214                  * vector while saving the state to the user context. This will
215                  * enable us capturing any changes(during sigreturn) to
216                  * the FP/SSE bits by the legacy applications which don't touch
217                  * xstate_bv in the xsave header.
218                  *
219                  * xsave aware apps can change the xstate_bv in the xsave
220                  * header as well as change any contents in the memory layout.
221                  * xrestore as part of sigreturn will capture all the changes.
222                  */
223                 xstate_bv |= XSTATE_FPSSE;
224
225                 err |= __put_user(xstate_bv, &x->xstate_hdr.xstate_bv);
226
227                 if (err)
228                         return err;
229         }
230
231         return 1;
232 }
233
234 /*
235  * Restore the extended state if present. Otherwise, restore the FP/SSE
236  * state.
237  */
238 static int restore_user_xstate(void __user *buf)
239 {
240         struct _fpx_sw_bytes fx_sw_user;
241         u64 mask;
242         int err;
243
244         if (((unsigned long)buf % 64) ||
245              check_for_xstate(buf, buf, &fx_sw_user))
246                 goto fx_only;
247
248         mask = fx_sw_user.xstate_bv;
249
250         /*
251          * restore the state passed by the user.
252          */
253         err = xrestore_user(buf, mask);
254         if (err)
255                 return err;
256
257         /*
258          * init the state skipped by the user.
259          */
260         mask = pcntxt_mask & ~mask;
261
262         xrstor_state(init_xstate_buf, mask);
263
264         return 0;
265
266 fx_only:
267         /*
268          * couldn't find the extended state information in the
269          * memory layout. Restore just the FP/SSE and init all
270          * the other extended state.
271          */
272         xrstor_state(init_xstate_buf, pcntxt_mask & ~XSTATE_FPSSE);
273         return fxrstor_checking((__force struct i387_fxsave_struct *)buf);
274 }
275
276 /*
277  * This restores directly out of user space. Exceptions are handled.
278  */
279 int restore_i387_xstate(void __user *buf)
280 {
281         struct task_struct *tsk = current;
282         int err = 0;
283
284         if (!buf) {
285                 if (used_math())
286                         goto clear;
287                 return 0;
288         } else
289                 if (!access_ok(VERIFY_READ, buf, sig_xstate_size))
290                         return -EACCES;
291
292         if (!used_math()) {
293                 err = init_fpu(tsk);
294                 if (err)
295                         return err;
296         }
297
298         if (!(task_thread_info(current)->status & TS_USEDFPU)) {
299                 clts();
300                 task_thread_info(current)->status |= TS_USEDFPU;
301         }
302         if (use_xsave())
303                 err = restore_user_xstate(buf);
304         else
305                 err = fxrstor_checking((__force struct i387_fxsave_struct *)
306                                        buf);
307         if (unlikely(err)) {
308                 /*
309                  * Encountered an error while doing the restore from the
310                  * user buffer, clear the fpu state.
311                  */
312 clear:
313                 clear_fpu(tsk);
314                 clear_used_math();
315         }
316         return err;
317 }
318 #endif
319
320 /*
321  * Prepare the SW reserved portion of the fxsave memory layout, indicating
322  * the presence of the extended state information in the memory layout
323  * pointed by the fpstate pointer in the sigcontext.
324  * This will be saved when ever the FP and extended state context is
325  * saved on the user stack during the signal handler delivery to the user.
326  */
327 static void prepare_fx_sw_frame(void)
328 {
329         int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) +
330                              FP_XSTATE_MAGIC2_SIZE;
331
332         sig_xstate_size = sizeof(struct _fpstate) + size_extended;
333
334 #ifdef CONFIG_IA32_EMULATION
335         sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended;
336 #endif
337
338         memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved));
339
340         fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
341         fx_sw_reserved.extended_size = sig_xstate_size;
342         fx_sw_reserved.xstate_bv = pcntxt_mask;
343         fx_sw_reserved.xstate_size = xstate_size;
344 #ifdef CONFIG_IA32_EMULATION
345         memcpy(&fx_sw_reserved_ia32, &fx_sw_reserved,
346                sizeof(struct _fpx_sw_bytes));
347         fx_sw_reserved_ia32.extended_size = sig_xstate_ia32_size;
348 #endif
349 }
350
351 /*
352  * Represents init state for the supported extended state.
353  */
354 struct xsave_struct *init_xstate_buf;
355
356 #ifdef CONFIG_X86_64
357 unsigned int sig_xstate_size = sizeof(struct _fpstate);
358 #endif
359
360 /*
361  * Enable the extended processor state save/restore feature
362  */
363 static void __cpuinit __xsave_init(void)
364 {
365         set_in_cr4(X86_CR4_OSXSAVE);
366
367         /*
368          * Enable all the features that the HW is capable of
369          * and the Linux kernel is aware of.
370          */
371         xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
372 }
373
374 /*
375  * Record the offsets and sizes of different state managed by the xsave
376  * memory layout.
377  */
378 static void setup_xstate_features(void)
379 {
380         int eax, ebx, ecx, edx, leaf = 0x2;
381
382         xstate_features = fls64(pcntxt_mask);
383         xstate_offsets = alloc_bootmem(xstate_features * sizeof(int));
384         xstate_sizes = alloc_bootmem(xstate_features * sizeof(int));
385
386         do {
387                 cpuid_count(0xd, leaf, &eax, &ebx, &ecx, &edx);
388
389                 if (eax == 0)
390                         break;
391
392                 xstate_offsets[leaf] = ebx;
393                 xstate_sizes[leaf] = eax;
394
395                 leaf++;
396         } while (1);
397 }
398
399 /*
400  * setup the xstate image representing the init state
401  */
402 static void __init setup_xstate_init(void)
403 {
404         setup_xstate_features();
405
406         /*
407          * Setup init_xstate_buf to represent the init state of
408          * all the features managed by the xsave
409          */
410         init_xstate_buf = alloc_bootmem(xstate_size);
411         init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
412
413         clts();
414         /*
415          * Init all the features state with header_bv being 0x0
416          */
417         xrstor_state(init_xstate_buf, -1);
418         /*
419          * Dump the init state again. This is to identify the init state
420          * of any feature which is not represented by all zero's.
421          */
422         xsave_state(init_xstate_buf, -1);
423         stts();
424 }
425
426 /*
427  * Enable and initialize the xsave feature.
428  */
429 static void __cpuinit xsave_cntxt_init(void)
430 {
431         unsigned int eax, ebx, ecx, edx;
432
433         cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
434         pcntxt_mask = eax + ((u64)edx << 32);
435
436         if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
437                 printk(KERN_ERR "FP/SSE not shown under xsave features 0x%llx\n",
438                        pcntxt_mask);
439                 BUG();
440         }
441
442         /*
443          * Support only the state known to OS.
444          */
445         pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
446         __xsave_init();
447
448         /*
449          * Recompute the context size for enabled features
450          */
451         cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
452         xstate_size = ebx;
453
454         update_regset_xstate_info(xstate_size, pcntxt_mask);
455         prepare_fx_sw_frame();
456
457         setup_xstate_init();
458
459         printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%llx, "
460                "cntxt size 0x%x\n",
461                pcntxt_mask, xstate_size);
462 }
463
464 void __cpuinit xsave_init(void)
465 {
466         if (!cpu_has_xsave)
467                 return;
468
469         /*
470          * Boot processor to setup the FP and extended state context info.
471          */
472         if (!smp_processor_id())
473                 xsave_cntxt_init();
474         __xsave_init();
475 }