x86, fpu: Consolidate inline asm routines for saving/restoring fpu state
[profile/ivi/kernel-adaptation-intel-automotive.git] / arch / x86 / kernel / xsave.c
1 /*
2  * xsave/xrstor support.
3  *
4  * Author: Suresh Siddha <suresh.b.siddha@intel.com>
5  */
6
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
9 #include <linux/bootmem.h>
10 #include <linux/compat.h>
11 #include <asm/i387.h>
12 #include <asm/fpu-internal.h>
13 #ifdef CONFIG_IA32_EMULATION
14 #include <asm/sigcontext32.h>
15 #endif
16 #include <asm/xcr.h>
17
18 /*
19  * Supported feature mask by the CPU and the kernel.
20  */
21 u64 pcntxt_mask;
22
23 /*
24  * Represents init state for the supported extended state.
25  */
26 static struct xsave_struct *init_xstate_buf;
27
28 struct _fpx_sw_bytes fx_sw_reserved;
29 #ifdef CONFIG_IA32_EMULATION
30 struct _fpx_sw_bytes fx_sw_reserved_ia32;
31 #endif
32
33 static unsigned int *xstate_offsets, *xstate_sizes, xstate_features;
34
35 /*
36  * If a processor implementation discern that a processor state component is
37  * in its initialized state it may modify the corresponding bit in the
38  * xsave_hdr.xstate_bv as '0', with out modifying the corresponding memory
39  * layout in the case of xsaveopt. While presenting the xstate information to
40  * the user, we always ensure that the memory layout of a feature will be in
41  * the init state if the corresponding header bit is zero. This is to ensure
42  * that the user doesn't see some stale state in the memory layout during
43  * signal handling, debugging etc.
44  */
45 void __sanitize_i387_state(struct task_struct *tsk)
46 {
47         u64 xstate_bv;
48         int feature_bit = 0x2;
49         struct i387_fxsave_struct *fx = &tsk->thread.fpu.state->fxsave;
50
51         if (!fx)
52                 return;
53
54         xstate_bv = tsk->thread.fpu.state->xsave.xsave_hdr.xstate_bv;
55
56         /*
57          * None of the feature bits are in init state. So nothing else
58          * to do for us, as the memory layout is up to date.
59          */
60         if ((xstate_bv & pcntxt_mask) == pcntxt_mask)
61                 return;
62
63         /*
64          * FP is in init state
65          */
66         if (!(xstate_bv & XSTATE_FP)) {
67                 fx->cwd = 0x37f;
68                 fx->swd = 0;
69                 fx->twd = 0;
70                 fx->fop = 0;
71                 fx->rip = 0;
72                 fx->rdp = 0;
73                 memset(&fx->st_space[0], 0, 128);
74         }
75
76         /*
77          * SSE is in init state
78          */
79         if (!(xstate_bv & XSTATE_SSE))
80                 memset(&fx->xmm_space[0], 0, 256);
81
82         xstate_bv = (pcntxt_mask & ~xstate_bv) >> 2;
83
84         /*
85          * Update all the other memory layouts for which the corresponding
86          * header bit is in the init state.
87          */
88         while (xstate_bv) {
89                 if (xstate_bv & 0x1) {
90                         int offset = xstate_offsets[feature_bit];
91                         int size = xstate_sizes[feature_bit];
92
93                         memcpy(((void *) fx) + offset,
94                                ((void *) init_xstate_buf) + offset,
95                                size);
96                 }
97
98                 xstate_bv >>= 1;
99                 feature_bit++;
100         }
101 }
102
103 /*
104  * Check for the presence of extended state information in the
105  * user fpstate pointer in the sigcontext.
106  */
107 int check_for_xstate(struct i387_fxsave_struct __user *buf,
108                      void __user *fpstate,
109                      struct _fpx_sw_bytes *fx_sw_user)
110 {
111         int min_xstate_size = sizeof(struct i387_fxsave_struct) +
112                               sizeof(struct xsave_hdr_struct);
113         unsigned int magic2;
114         int err;
115
116         err = __copy_from_user(fx_sw_user, &buf->sw_reserved[0],
117                                sizeof(struct _fpx_sw_bytes));
118         if (err)
119                 return -EFAULT;
120
121         /*
122          * First Magic check failed.
123          */
124         if (fx_sw_user->magic1 != FP_XSTATE_MAGIC1)
125                 return -EINVAL;
126
127         /*
128          * Check for error scenarios.
129          */
130         if (fx_sw_user->xstate_size < min_xstate_size ||
131             fx_sw_user->xstate_size > xstate_size ||
132             fx_sw_user->xstate_size > fx_sw_user->extended_size)
133                 return -EINVAL;
134
135         err = __get_user(magic2, (__u32 __user *) (fpstate +
136                                                    fx_sw_user->extended_size -
137                                                    FP_XSTATE_MAGIC2_SIZE));
138         if (err)
139                 return err;
140         /*
141          * Check for the presence of second magic word at the end of memory
142          * layout. This detects the case where the user just copied the legacy
143          * fpstate layout with out copying the extended state information
144          * in the memory layout.
145          */
146         if (magic2 != FP_XSTATE_MAGIC2)
147                 return -EFAULT;
148
149         return 0;
150 }
151
152 #ifdef CONFIG_X86_64
153 /*
154  * Signal frame handlers.
155  */
156
157 int save_i387_xstate(void __user *buf)
158 {
159         struct task_struct *tsk = current;
160         int err = 0;
161
162         if (!access_ok(VERIFY_WRITE, buf, sig_xstate_size))
163                 return -EACCES;
164
165         BUG_ON(sig_xstate_size < xstate_size);
166
167         if ((unsigned long)buf % 64)
168                 pr_err("%s: bad fpstate %p\n", __func__, buf);
169
170         if (!used_math())
171                 return 0;
172
173         if (user_has_fpu()) {
174                 if (use_xsave())
175                         err = xsave_user(buf);
176                 else
177                         err = fxsave_user(buf);
178
179                 if (unlikely(err)) {
180                         __clear_user(buf, xstate_size);
181                         return err;
182                 }
183                 user_fpu_end();
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         if (unlikely(mask))
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         user_fpu_begin();
299         if (use_xsave())
300                 err = restore_user_xstate(buf);
301         else
302                 err = fxrstor_checking((__force struct i387_fxsave_struct *)
303                                        buf);
304         if (unlikely(err)) {
305                 /*
306                  * Encountered an error while doing the restore from the
307                  * user buffer, clear the fpu state.
308                  */
309 clear:
310                 clear_fpu(tsk);
311                 clear_used_math();
312         }
313         return err;
314 }
315 #endif
316
317 /*
318  * Prepare the SW reserved portion of the fxsave memory layout, indicating
319  * the presence of the extended state information in the memory layout
320  * pointed by the fpstate pointer in the sigcontext.
321  * This will be saved when ever the FP and extended state context is
322  * saved on the user stack during the signal handler delivery to the user.
323  */
324 static void prepare_fx_sw_frame(void)
325 {
326         int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) +
327                              FP_XSTATE_MAGIC2_SIZE;
328
329         sig_xstate_size = sizeof(struct _fpstate) + size_extended;
330
331 #ifdef CONFIG_IA32_EMULATION
332         sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended;
333 #endif
334
335         memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved));
336
337         fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
338         fx_sw_reserved.extended_size = sig_xstate_size;
339         fx_sw_reserved.xstate_bv = pcntxt_mask;
340         fx_sw_reserved.xstate_size = xstate_size;
341 #ifdef CONFIG_IA32_EMULATION
342         memcpy(&fx_sw_reserved_ia32, &fx_sw_reserved,
343                sizeof(struct _fpx_sw_bytes));
344         fx_sw_reserved_ia32.extended_size = sig_xstate_ia32_size;
345 #endif
346 }
347
348 #ifdef CONFIG_X86_64
349 unsigned int sig_xstate_size = sizeof(struct _fpstate);
350 #endif
351
352 /*
353  * Enable the extended processor state save/restore feature
354  */
355 static inline void xstate_enable(void)
356 {
357         set_in_cr4(X86_CR4_OSXSAVE);
358         xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
359 }
360
361 /*
362  * Record the offsets and sizes of different state managed by the xsave
363  * memory layout.
364  */
365 static void __init setup_xstate_features(void)
366 {
367         int eax, ebx, ecx, edx, leaf = 0x2;
368
369         xstate_features = fls64(pcntxt_mask);
370         xstate_offsets = alloc_bootmem(xstate_features * sizeof(int));
371         xstate_sizes = alloc_bootmem(xstate_features * sizeof(int));
372
373         do {
374                 cpuid_count(XSTATE_CPUID, leaf, &eax, &ebx, &ecx, &edx);
375
376                 if (eax == 0)
377                         break;
378
379                 xstate_offsets[leaf] = ebx;
380                 xstate_sizes[leaf] = eax;
381
382                 leaf++;
383         } while (1);
384 }
385
386 /*
387  * setup the xstate image representing the init state
388  */
389 static void __init setup_xstate_init(void)
390 {
391         setup_xstate_features();
392
393         /*
394          * Setup init_xstate_buf to represent the init state of
395          * all the features managed by the xsave
396          */
397         init_xstate_buf = alloc_bootmem_align(xstate_size,
398                                               __alignof__(struct xsave_struct));
399         init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
400
401         clts();
402         /*
403          * Init all the features state with header_bv being 0x0
404          */
405         xrstor_state(init_xstate_buf, -1);
406         /*
407          * Dump the init state again. This is to identify the init state
408          * of any feature which is not represented by all zero's.
409          */
410         xsave_state(init_xstate_buf, -1);
411         stts();
412 }
413
414 /*
415  * Enable and initialize the xsave feature.
416  */
417 static void __init xstate_enable_boot_cpu(void)
418 {
419         unsigned int eax, ebx, ecx, edx;
420
421         if (boot_cpu_data.cpuid_level < XSTATE_CPUID) {
422                 WARN(1, KERN_ERR "XSTATE_CPUID missing\n");
423                 return;
424         }
425
426         cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
427         pcntxt_mask = eax + ((u64)edx << 32);
428
429         if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
430                 pr_err("FP/SSE not shown under xsave features 0x%llx\n",
431                        pcntxt_mask);
432                 BUG();
433         }
434
435         /*
436          * Support only the state known to OS.
437          */
438         pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
439
440         xstate_enable();
441
442         /*
443          * Recompute the context size for enabled features
444          */
445         cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
446         xstate_size = ebx;
447
448         update_regset_xstate_info(xstate_size, pcntxt_mask);
449         prepare_fx_sw_frame();
450
451         setup_xstate_init();
452
453         pr_info("enabled xstate_bv 0x%llx, cntxt size 0x%x\n",
454                 pcntxt_mask, xstate_size);
455 }
456
457 /*
458  * For the very first instance, this calls xstate_enable_boot_cpu();
459  * for all subsequent instances, this calls xstate_enable().
460  *
461  * This is somewhat obfuscated due to the lack of powerful enough
462  * overrides for the section checks.
463  */
464 void __cpuinit xsave_init(void)
465 {
466         static __refdata void (*next_func)(void) = xstate_enable_boot_cpu;
467         void (*this_func)(void);
468
469         if (!cpu_has_xsave)
470                 return;
471
472         this_func = next_func;
473         next_func = xstate_enable;
474         this_func();
475 }