x86/fpu: Split out the boot time FPU init code into fpu/init.c
[firefly-linux-kernel-4.4.55.git] / arch / x86 / kernel / fpu / core.c
1 /*
2  *  Copyright (C) 1994 Linus Torvalds
3  *
4  *  Pentium III FXSR, SSE support
5  *  General FPU state handling cleanups
6  *      Gareth Hughes <gareth@valinux.com>, May 2000
7  */
8 #include <linux/module.h>
9 #include <linux/regset.h>
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12
13 #include <asm/sigcontext.h>
14 #include <asm/processor.h>
15 #include <asm/math_emu.h>
16 #include <asm/tlbflush.h>
17 #include <asm/uaccess.h>
18 #include <asm/ptrace.h>
19 #include <asm/i387.h>
20 #include <asm/fpu-internal.h>
21 #include <asm/user.h>
22
23 static DEFINE_PER_CPU(bool, in_kernel_fpu);
24
25 void kernel_fpu_disable(void)
26 {
27         WARN_ON(this_cpu_read(in_kernel_fpu));
28         this_cpu_write(in_kernel_fpu, true);
29 }
30
31 void kernel_fpu_enable(void)
32 {
33         this_cpu_write(in_kernel_fpu, false);
34 }
35
36 /*
37  * Were we in an interrupt that interrupted kernel mode?
38  *
39  * On others, we can do a kernel_fpu_begin/end() pair *ONLY* if that
40  * pair does nothing at all: the thread must not have fpu (so
41  * that we don't try to save the FPU state), and TS must
42  * be set (so that the clts/stts pair does nothing that is
43  * visible in the interrupted kernel thread).
44  *
45  * Except for the eagerfpu case when we return true; in the likely case
46  * the thread has FPU but we are not going to set/clear TS.
47  */
48 static inline bool interrupted_kernel_fpu_idle(void)
49 {
50         if (this_cpu_read(in_kernel_fpu))
51                 return false;
52
53         if (use_eager_fpu())
54                 return true;
55
56         return !__thread_has_fpu(current) &&
57                 (read_cr0() & X86_CR0_TS);
58 }
59
60 /*
61  * Were we in user mode (or vm86 mode) when we were
62  * interrupted?
63  *
64  * Doing kernel_fpu_begin/end() is ok if we are running
65  * in an interrupt context from user mode - we'll just
66  * save the FPU state as required.
67  */
68 static inline bool interrupted_user_mode(void)
69 {
70         struct pt_regs *regs = get_irq_regs();
71         return regs && user_mode(regs);
72 }
73
74 /*
75  * Can we use the FPU in kernel mode with the
76  * whole "kernel_fpu_begin/end()" sequence?
77  *
78  * It's always ok in process context (ie "not interrupt")
79  * but it is sometimes ok even from an irq.
80  */
81 bool irq_fpu_usable(void)
82 {
83         return !in_interrupt() ||
84                 interrupted_user_mode() ||
85                 interrupted_kernel_fpu_idle();
86 }
87 EXPORT_SYMBOL(irq_fpu_usable);
88
89 void __kernel_fpu_begin(void)
90 {
91         struct task_struct *me = current;
92
93         this_cpu_write(in_kernel_fpu, true);
94
95         if (__thread_has_fpu(me)) {
96                 __save_init_fpu(me);
97         } else {
98                 this_cpu_write(fpu_owner_task, NULL);
99                 if (!use_eager_fpu())
100                         clts();
101         }
102 }
103 EXPORT_SYMBOL(__kernel_fpu_begin);
104
105 void __kernel_fpu_end(void)
106 {
107         struct task_struct *me = current;
108
109         if (__thread_has_fpu(me)) {
110                 if (WARN_ON(restore_fpu_checking(me)))
111                         fpu_reset_state(me);
112         } else if (!use_eager_fpu()) {
113                 stts();
114         }
115
116         this_cpu_write(in_kernel_fpu, false);
117 }
118 EXPORT_SYMBOL(__kernel_fpu_end);
119
120 /*
121  * Save the FPU state (initialize it if necessary):
122  *
123  * This only ever gets called for the current task.
124  */
125 void fpu__save(struct task_struct *tsk)
126 {
127         WARN_ON(tsk != current);
128
129         preempt_disable();
130         if (__thread_has_fpu(tsk)) {
131                 if (use_eager_fpu()) {
132                         __save_fpu(tsk);
133                 } else {
134                         __save_init_fpu(tsk);
135                         __thread_fpu_end(tsk);
136                 }
137         }
138         preempt_enable();
139 }
140 EXPORT_SYMBOL_GPL(fpu__save);
141
142 void fpstate_init(struct fpu *fpu)
143 {
144         if (!cpu_has_fpu) {
145                 finit_soft_fpu(&fpu->state->soft);
146                 return;
147         }
148
149         memset(fpu->state, 0, xstate_size);
150
151         if (cpu_has_fxsr) {
152                 fx_finit(&fpu->state->fxsave);
153         } else {
154                 struct i387_fsave_struct *fp = &fpu->state->fsave;
155                 fp->cwd = 0xffff037fu;
156                 fp->swd = 0xffff0000u;
157                 fp->twd = 0xffffffffu;
158                 fp->fos = 0xffff0000u;
159         }
160 }
161 EXPORT_SYMBOL_GPL(fpstate_init);
162
163 int fpstate_alloc(struct fpu *fpu)
164 {
165         if (fpu->state)
166                 return 0;
167
168         fpu->state = kmem_cache_alloc(task_xstate_cachep, GFP_KERNEL);
169         if (!fpu->state)
170                 return -ENOMEM;
171
172         /* The CPU requires the FPU state to be aligned to 16 byte boundaries: */
173         WARN_ON((unsigned long)fpu->state & 15);
174
175         return 0;
176 }
177 EXPORT_SYMBOL_GPL(fpstate_alloc);
178
179 /*
180  * Allocate the backing store for the current task's FPU registers
181  * and initialize the registers themselves as well.
182  *
183  * Can fail.
184  */
185 int fpstate_alloc_init(struct task_struct *curr)
186 {
187         int ret;
188
189         if (WARN_ON_ONCE(curr != current))
190                 return -EINVAL;
191         if (WARN_ON_ONCE(curr->flags & PF_USED_MATH))
192                 return -EINVAL;
193
194         /*
195          * Memory allocation at the first usage of the FPU and other state.
196          */
197         ret = fpstate_alloc(&curr->thread.fpu);
198         if (ret)
199                 return ret;
200
201         fpstate_init(&curr->thread.fpu);
202
203         /* Safe to do for the current task: */
204         curr->flags |= PF_USED_MATH;
205
206         return 0;
207 }
208 EXPORT_SYMBOL_GPL(fpstate_alloc_init);
209
210 /*
211  * The _current_ task is using the FPU for the first time
212  * so initialize it and set the mxcsr to its default
213  * value at reset if we support XMM instructions and then
214  * remember the current task has used the FPU.
215  */
216 static int fpu__unlazy_stopped(struct task_struct *child)
217 {
218         int ret;
219
220         if (WARN_ON_ONCE(child == current))
221                 return -EINVAL;
222
223         if (child->flags & PF_USED_MATH) {
224                 task_disable_lazy_fpu_restore(child);
225                 return 0;
226         }
227
228         /*
229          * Memory allocation at the first usage of the FPU and other state.
230          */
231         ret = fpstate_alloc(&child->thread.fpu);
232         if (ret)
233                 return ret;
234
235         fpstate_init(&child->thread.fpu);
236
237         /* Safe to do for stopped child tasks: */
238         child->flags |= PF_USED_MATH;
239
240         return 0;
241 }
242
243 /*
244  * The xstateregs_active() routine is the same as the fpregs_active() routine,
245  * as the "regset->n" for the xstate regset will be updated based on the feature
246  * capabilites supported by the xsave.
247  */
248 int fpregs_active(struct task_struct *target, const struct user_regset *regset)
249 {
250         return tsk_used_math(target) ? regset->n : 0;
251 }
252
253 int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
254 {
255         return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
256 }
257
258 int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
259                 unsigned int pos, unsigned int count,
260                 void *kbuf, void __user *ubuf)
261 {
262         int ret;
263
264         if (!cpu_has_fxsr)
265                 return -ENODEV;
266
267         ret = fpu__unlazy_stopped(target);
268         if (ret)
269                 return ret;
270
271         sanitize_i387_state(target);
272
273         return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
274                                    &target->thread.fpu.state->fxsave, 0, -1);
275 }
276
277 int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
278                 unsigned int pos, unsigned int count,
279                 const void *kbuf, const void __user *ubuf)
280 {
281         int ret;
282
283         if (!cpu_has_fxsr)
284                 return -ENODEV;
285
286         ret = fpu__unlazy_stopped(target);
287         if (ret)
288                 return ret;
289
290         sanitize_i387_state(target);
291
292         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
293                                  &target->thread.fpu.state->fxsave, 0, -1);
294
295         /*
296          * mxcsr reserved bits must be masked to zero for security reasons.
297          */
298         target->thread.fpu.state->fxsave.mxcsr &= mxcsr_feature_mask;
299
300         /*
301          * update the header bits in the xsave header, indicating the
302          * presence of FP and SSE state.
303          */
304         if (cpu_has_xsave)
305                 target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FPSSE;
306
307         return ret;
308 }
309
310 int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
311                 unsigned int pos, unsigned int count,
312                 void *kbuf, void __user *ubuf)
313 {
314         struct xsave_struct *xsave;
315         int ret;
316
317         if (!cpu_has_xsave)
318                 return -ENODEV;
319
320         ret = fpu__unlazy_stopped(target);
321         if (ret)
322                 return ret;
323
324         xsave = &target->thread.fpu.state->xsave;
325
326         /*
327          * Copy the 48bytes defined by the software first into the xstate
328          * memory layout in the thread struct, so that we can copy the entire
329          * xstateregs to the user using one user_regset_copyout().
330          */
331         memcpy(&xsave->i387.sw_reserved,
332                 xstate_fx_sw_bytes, sizeof(xstate_fx_sw_bytes));
333         /*
334          * Copy the xstate memory layout.
335          */
336         ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
337         return ret;
338 }
339
340 int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
341                   unsigned int pos, unsigned int count,
342                   const void *kbuf, const void __user *ubuf)
343 {
344         struct xsave_struct *xsave;
345         int ret;
346
347         if (!cpu_has_xsave)
348                 return -ENODEV;
349
350         ret = fpu__unlazy_stopped(target);
351         if (ret)
352                 return ret;
353
354         xsave = &target->thread.fpu.state->xsave;
355
356         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
357         /*
358          * mxcsr reserved bits must be masked to zero for security reasons.
359          */
360         xsave->i387.mxcsr &= mxcsr_feature_mask;
361         xsave->xsave_hdr.xstate_bv &= pcntxt_mask;
362         /*
363          * These bits must be zero.
364          */
365         memset(&xsave->xsave_hdr.reserved, 0, 48);
366         return ret;
367 }
368
369 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
370
371 /*
372  * FPU tag word conversions.
373  */
374
375 static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
376 {
377         unsigned int tmp; /* to avoid 16 bit prefixes in the code */
378
379         /* Transform each pair of bits into 01 (valid) or 00 (empty) */
380         tmp = ~twd;
381         tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
382         /* and move the valid bits to the lower byte. */
383         tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
384         tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
385         tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
386
387         return tmp;
388 }
389
390 #define FPREG_ADDR(f, n)        ((void *)&(f)->st_space + (n) * 16)
391 #define FP_EXP_TAG_VALID        0
392 #define FP_EXP_TAG_ZERO         1
393 #define FP_EXP_TAG_SPECIAL      2
394 #define FP_EXP_TAG_EMPTY        3
395
396 static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
397 {
398         struct _fpxreg *st;
399         u32 tos = (fxsave->swd >> 11) & 7;
400         u32 twd = (unsigned long) fxsave->twd;
401         u32 tag;
402         u32 ret = 0xffff0000u;
403         int i;
404
405         for (i = 0; i < 8; i++, twd >>= 1) {
406                 if (twd & 0x1) {
407                         st = FPREG_ADDR(fxsave, (i - tos) & 7);
408
409                         switch (st->exponent & 0x7fff) {
410                         case 0x7fff:
411                                 tag = FP_EXP_TAG_SPECIAL;
412                                 break;
413                         case 0x0000:
414                                 if (!st->significand[0] &&
415                                     !st->significand[1] &&
416                                     !st->significand[2] &&
417                                     !st->significand[3])
418                                         tag = FP_EXP_TAG_ZERO;
419                                 else
420                                         tag = FP_EXP_TAG_SPECIAL;
421                                 break;
422                         default:
423                                 if (st->significand[3] & 0x8000)
424                                         tag = FP_EXP_TAG_VALID;
425                                 else
426                                         tag = FP_EXP_TAG_SPECIAL;
427                                 break;
428                         }
429                 } else {
430                         tag = FP_EXP_TAG_EMPTY;
431                 }
432                 ret |= tag << (2 * i);
433         }
434         return ret;
435 }
436
437 /*
438  * FXSR floating point environment conversions.
439  */
440
441 void
442 convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
443 {
444         struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
445         struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
446         struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
447         int i;
448
449         env->cwd = fxsave->cwd | 0xffff0000u;
450         env->swd = fxsave->swd | 0xffff0000u;
451         env->twd = twd_fxsr_to_i387(fxsave);
452
453 #ifdef CONFIG_X86_64
454         env->fip = fxsave->rip;
455         env->foo = fxsave->rdp;
456         /*
457          * should be actually ds/cs at fpu exception time, but
458          * that information is not available in 64bit mode.
459          */
460         env->fcs = task_pt_regs(tsk)->cs;
461         if (tsk == current) {
462                 savesegment(ds, env->fos);
463         } else {
464                 env->fos = tsk->thread.ds;
465         }
466         env->fos |= 0xffff0000;
467 #else
468         env->fip = fxsave->fip;
469         env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
470         env->foo = fxsave->foo;
471         env->fos = fxsave->fos;
472 #endif
473
474         for (i = 0; i < 8; ++i)
475                 memcpy(&to[i], &from[i], sizeof(to[0]));
476 }
477
478 void convert_to_fxsr(struct task_struct *tsk,
479                      const struct user_i387_ia32_struct *env)
480
481 {
482         struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
483         struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
484         struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
485         int i;
486
487         fxsave->cwd = env->cwd;
488         fxsave->swd = env->swd;
489         fxsave->twd = twd_i387_to_fxsr(env->twd);
490         fxsave->fop = (u16) ((u32) env->fcs >> 16);
491 #ifdef CONFIG_X86_64
492         fxsave->rip = env->fip;
493         fxsave->rdp = env->foo;
494         /* cs and ds ignored */
495 #else
496         fxsave->fip = env->fip;
497         fxsave->fcs = (env->fcs & 0xffff);
498         fxsave->foo = env->foo;
499         fxsave->fos = env->fos;
500 #endif
501
502         for (i = 0; i < 8; ++i)
503                 memcpy(&to[i], &from[i], sizeof(from[0]));
504 }
505
506 int fpregs_get(struct task_struct *target, const struct user_regset *regset,
507                unsigned int pos, unsigned int count,
508                void *kbuf, void __user *ubuf)
509 {
510         struct user_i387_ia32_struct env;
511         int ret;
512
513         ret = fpu__unlazy_stopped(target);
514         if (ret)
515                 return ret;
516
517         if (!static_cpu_has(X86_FEATURE_FPU))
518                 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
519
520         if (!cpu_has_fxsr)
521                 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
522                                            &target->thread.fpu.state->fsave, 0,
523                                            -1);
524
525         sanitize_i387_state(target);
526
527         if (kbuf && pos == 0 && count == sizeof(env)) {
528                 convert_from_fxsr(kbuf, target);
529                 return 0;
530         }
531
532         convert_from_fxsr(&env, target);
533
534         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
535 }
536
537 int fpregs_set(struct task_struct *target, const struct user_regset *regset,
538                unsigned int pos, unsigned int count,
539                const void *kbuf, const void __user *ubuf)
540 {
541         struct user_i387_ia32_struct env;
542         int ret;
543
544         ret = fpu__unlazy_stopped(target);
545         if (ret)
546                 return ret;
547
548         sanitize_i387_state(target);
549
550         if (!static_cpu_has(X86_FEATURE_FPU))
551                 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
552
553         if (!cpu_has_fxsr)
554                 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
555                                           &target->thread.fpu.state->fsave, 0,
556                                           -1);
557
558         if (pos > 0 || count < sizeof(env))
559                 convert_from_fxsr(&env, target);
560
561         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
562         if (!ret)
563                 convert_to_fxsr(target, &env);
564
565         /*
566          * update the header bit in the xsave header, indicating the
567          * presence of FP.
568          */
569         if (cpu_has_xsave)
570                 target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FP;
571         return ret;
572 }
573
574 /*
575  * FPU state for core dumps.
576  * This is only used for a.out dumps now.
577  * It is declared generically using elf_fpregset_t (which is
578  * struct user_i387_struct) but is in fact only used for 32-bit
579  * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
580  */
581 int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
582 {
583         struct task_struct *tsk = current;
584         int fpvalid;
585
586         fpvalid = !!used_math();
587         if (fpvalid)
588                 fpvalid = !fpregs_get(tsk, NULL,
589                                       0, sizeof(struct user_i387_ia32_struct),
590                                       fpu, NULL);
591
592         return fpvalid;
593 }
594 EXPORT_SYMBOL(dump_fpu);
595
596 #endif  /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */
597
598 static int __init no_387(char *s)
599 {
600         setup_clear_cpu_cap(X86_FEATURE_FPU);
601         return 1;
602 }
603
604 __setup("no387", no_387);
605
606 /*
607  * Set the X86_FEATURE_FPU CPU-capability bit based on
608  * trying to execute an actual sequence of FPU instructions:
609  */
610 void fpu__detect(struct cpuinfo_x86 *c)
611 {
612         unsigned long cr0;
613         u16 fsw, fcw;
614
615         fsw = fcw = 0xffff;
616
617         cr0 = read_cr0();
618         cr0 &= ~(X86_CR0_TS | X86_CR0_EM);
619         write_cr0(cr0);
620
621         asm volatile("fninit ; fnstsw %0 ; fnstcw %1"
622                      : "+m" (fsw), "+m" (fcw));
623
624         if (fsw == 0 && (fcw & 0x103f) == 0x003f)
625                 set_cpu_cap(c, X86_FEATURE_FPU);
626         else
627                 clear_cpu_cap(c, X86_FEATURE_FPU);
628
629         /* The final cr0 value is set in fpu_init() */
630 }