x86/fpu: Move i387.c and xsave.c to arch/x86/kernel/fpu/
[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 unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
143 unsigned int xstate_size;
144 EXPORT_SYMBOL_GPL(xstate_size);
145 static struct i387_fxsave_struct fx_scratch;
146
147 static void mxcsr_feature_mask_init(void)
148 {
149         unsigned long mask = 0;
150
151         if (cpu_has_fxsr) {
152                 memset(&fx_scratch, 0, sizeof(struct i387_fxsave_struct));
153                 asm volatile("fxsave %0" : "+m" (fx_scratch));
154                 mask = fx_scratch.mxcsr_mask;
155                 if (mask == 0)
156                         mask = 0x0000ffbf;
157         }
158         mxcsr_feature_mask &= mask;
159 }
160
161 static void fpstate_xstate_init_size(void)
162 {
163         /*
164          * Note that xstate_size might be overwriten later during
165          * xsave_init().
166          */
167
168         if (!cpu_has_fpu) {
169                 /*
170                  * Disable xsave as we do not support it if i387
171                  * emulation is enabled.
172                  */
173                 setup_clear_cpu_cap(X86_FEATURE_XSAVE);
174                 setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
175                 xstate_size = sizeof(struct i387_soft_struct);
176                 return;
177         }
178
179         if (cpu_has_fxsr)
180                 xstate_size = sizeof(struct i387_fxsave_struct);
181         else
182                 xstate_size = sizeof(struct i387_fsave_struct);
183 }
184
185 /*
186  * Called on the boot CPU at bootup to set up the initial FPU state that
187  * is later cloned into all processes.
188  *
189  * Also called on secondary CPUs to set up the FPU state of their
190  * idle threads.
191  */
192 void fpu__cpu_init(void)
193 {
194         unsigned long cr0;
195         unsigned long cr4_mask = 0;
196
197 #ifndef CONFIG_MATH_EMULATION
198         if (!cpu_has_fpu) {
199                 pr_emerg("No FPU found and no math emulation present\n");
200                 pr_emerg("Giving up\n");
201                 for (;;)
202                         asm volatile("hlt");
203         }
204 #endif
205         if (cpu_has_fxsr)
206                 cr4_mask |= X86_CR4_OSFXSR;
207         if (cpu_has_xmm)
208                 cr4_mask |= X86_CR4_OSXMMEXCPT;
209         if (cr4_mask)
210                 cr4_set_bits(cr4_mask);
211
212         cr0 = read_cr0();
213         cr0 &= ~(X86_CR0_TS|X86_CR0_EM); /* clear TS and EM */
214         if (!cpu_has_fpu)
215                 cr0 |= X86_CR0_EM;
216         write_cr0(cr0);
217
218         /*
219          * fpstate_xstate_init_size() is only called once, to avoid overriding
220          * 'xstate_size' during (secondary CPU) bootup or during CPU hotplug.
221          */
222         if (xstate_size == 0)
223                 fpstate_xstate_init_size();
224
225         mxcsr_feature_mask_init();
226         xsave_init();
227         eager_fpu_init();
228 }
229
230 void fpstate_init(struct fpu *fpu)
231 {
232         if (!cpu_has_fpu) {
233                 finit_soft_fpu(&fpu->state->soft);
234                 return;
235         }
236
237         memset(fpu->state, 0, xstate_size);
238
239         if (cpu_has_fxsr) {
240                 fx_finit(&fpu->state->fxsave);
241         } else {
242                 struct i387_fsave_struct *fp = &fpu->state->fsave;
243                 fp->cwd = 0xffff037fu;
244                 fp->swd = 0xffff0000u;
245                 fp->twd = 0xffffffffu;
246                 fp->fos = 0xffff0000u;
247         }
248 }
249 EXPORT_SYMBOL_GPL(fpstate_init);
250
251 int fpstate_alloc(struct fpu *fpu)
252 {
253         if (fpu->state)
254                 return 0;
255
256         fpu->state = kmem_cache_alloc(task_xstate_cachep, GFP_KERNEL);
257         if (!fpu->state)
258                 return -ENOMEM;
259
260         /* The CPU requires the FPU state to be aligned to 16 byte boundaries: */
261         WARN_ON((unsigned long)fpu->state & 15);
262
263         return 0;
264 }
265 EXPORT_SYMBOL_GPL(fpstate_alloc);
266
267 /*
268  * Allocate the backing store for the current task's FPU registers
269  * and initialize the registers themselves as well.
270  *
271  * Can fail.
272  */
273 int fpstate_alloc_init(struct task_struct *curr)
274 {
275         int ret;
276
277         if (WARN_ON_ONCE(curr != current))
278                 return -EINVAL;
279         if (WARN_ON_ONCE(curr->flags & PF_USED_MATH))
280                 return -EINVAL;
281
282         /*
283          * Memory allocation at the first usage of the FPU and other state.
284          */
285         ret = fpstate_alloc(&curr->thread.fpu);
286         if (ret)
287                 return ret;
288
289         fpstate_init(&curr->thread.fpu);
290
291         /* Safe to do for the current task: */
292         curr->flags |= PF_USED_MATH;
293
294         return 0;
295 }
296 EXPORT_SYMBOL_GPL(fpstate_alloc_init);
297
298 /*
299  * The _current_ task is using the FPU for the first time
300  * so initialize it and set the mxcsr to its default
301  * value at reset if we support XMM instructions and then
302  * remember the current task has used the FPU.
303  */
304 static int fpu__unlazy_stopped(struct task_struct *child)
305 {
306         int ret;
307
308         if (WARN_ON_ONCE(child == current))
309                 return -EINVAL;
310
311         if (child->flags & PF_USED_MATH) {
312                 task_disable_lazy_fpu_restore(child);
313                 return 0;
314         }
315
316         /*
317          * Memory allocation at the first usage of the FPU and other state.
318          */
319         ret = fpstate_alloc(&child->thread.fpu);
320         if (ret)
321                 return ret;
322
323         fpstate_init(&child->thread.fpu);
324
325         /* Safe to do for stopped child tasks: */
326         child->flags |= PF_USED_MATH;
327
328         return 0;
329 }
330
331 /*
332  * The xstateregs_active() routine is the same as the fpregs_active() routine,
333  * as the "regset->n" for the xstate regset will be updated based on the feature
334  * capabilites supported by the xsave.
335  */
336 int fpregs_active(struct task_struct *target, const struct user_regset *regset)
337 {
338         return tsk_used_math(target) ? regset->n : 0;
339 }
340
341 int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
342 {
343         return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
344 }
345
346 int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
347                 unsigned int pos, unsigned int count,
348                 void *kbuf, void __user *ubuf)
349 {
350         int ret;
351
352         if (!cpu_has_fxsr)
353                 return -ENODEV;
354
355         ret = fpu__unlazy_stopped(target);
356         if (ret)
357                 return ret;
358
359         sanitize_i387_state(target);
360
361         return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
362                                    &target->thread.fpu.state->fxsave, 0, -1);
363 }
364
365 int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
366                 unsigned int pos, unsigned int count,
367                 const void *kbuf, const void __user *ubuf)
368 {
369         int ret;
370
371         if (!cpu_has_fxsr)
372                 return -ENODEV;
373
374         ret = fpu__unlazy_stopped(target);
375         if (ret)
376                 return ret;
377
378         sanitize_i387_state(target);
379
380         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
381                                  &target->thread.fpu.state->fxsave, 0, -1);
382
383         /*
384          * mxcsr reserved bits must be masked to zero for security reasons.
385          */
386         target->thread.fpu.state->fxsave.mxcsr &= mxcsr_feature_mask;
387
388         /*
389          * update the header bits in the xsave header, indicating the
390          * presence of FP and SSE state.
391          */
392         if (cpu_has_xsave)
393                 target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FPSSE;
394
395         return ret;
396 }
397
398 int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
399                 unsigned int pos, unsigned int count,
400                 void *kbuf, void __user *ubuf)
401 {
402         struct xsave_struct *xsave;
403         int ret;
404
405         if (!cpu_has_xsave)
406                 return -ENODEV;
407
408         ret = fpu__unlazy_stopped(target);
409         if (ret)
410                 return ret;
411
412         xsave = &target->thread.fpu.state->xsave;
413
414         /*
415          * Copy the 48bytes defined by the software first into the xstate
416          * memory layout in the thread struct, so that we can copy the entire
417          * xstateregs to the user using one user_regset_copyout().
418          */
419         memcpy(&xsave->i387.sw_reserved,
420                 xstate_fx_sw_bytes, sizeof(xstate_fx_sw_bytes));
421         /*
422          * Copy the xstate memory layout.
423          */
424         ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
425         return ret;
426 }
427
428 int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
429                   unsigned int pos, unsigned int count,
430                   const void *kbuf, const void __user *ubuf)
431 {
432         struct xsave_struct *xsave;
433         int ret;
434
435         if (!cpu_has_xsave)
436                 return -ENODEV;
437
438         ret = fpu__unlazy_stopped(target);
439         if (ret)
440                 return ret;
441
442         xsave = &target->thread.fpu.state->xsave;
443
444         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
445         /*
446          * mxcsr reserved bits must be masked to zero for security reasons.
447          */
448         xsave->i387.mxcsr &= mxcsr_feature_mask;
449         xsave->xsave_hdr.xstate_bv &= pcntxt_mask;
450         /*
451          * These bits must be zero.
452          */
453         memset(&xsave->xsave_hdr.reserved, 0, 48);
454         return ret;
455 }
456
457 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
458
459 /*
460  * FPU tag word conversions.
461  */
462
463 static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
464 {
465         unsigned int tmp; /* to avoid 16 bit prefixes in the code */
466
467         /* Transform each pair of bits into 01 (valid) or 00 (empty) */
468         tmp = ~twd;
469         tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
470         /* and move the valid bits to the lower byte. */
471         tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
472         tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
473         tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
474
475         return tmp;
476 }
477
478 #define FPREG_ADDR(f, n)        ((void *)&(f)->st_space + (n) * 16)
479 #define FP_EXP_TAG_VALID        0
480 #define FP_EXP_TAG_ZERO         1
481 #define FP_EXP_TAG_SPECIAL      2
482 #define FP_EXP_TAG_EMPTY        3
483
484 static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
485 {
486         struct _fpxreg *st;
487         u32 tos = (fxsave->swd >> 11) & 7;
488         u32 twd = (unsigned long) fxsave->twd;
489         u32 tag;
490         u32 ret = 0xffff0000u;
491         int i;
492
493         for (i = 0; i < 8; i++, twd >>= 1) {
494                 if (twd & 0x1) {
495                         st = FPREG_ADDR(fxsave, (i - tos) & 7);
496
497                         switch (st->exponent & 0x7fff) {
498                         case 0x7fff:
499                                 tag = FP_EXP_TAG_SPECIAL;
500                                 break;
501                         case 0x0000:
502                                 if (!st->significand[0] &&
503                                     !st->significand[1] &&
504                                     !st->significand[2] &&
505                                     !st->significand[3])
506                                         tag = FP_EXP_TAG_ZERO;
507                                 else
508                                         tag = FP_EXP_TAG_SPECIAL;
509                                 break;
510                         default:
511                                 if (st->significand[3] & 0x8000)
512                                         tag = FP_EXP_TAG_VALID;
513                                 else
514                                         tag = FP_EXP_TAG_SPECIAL;
515                                 break;
516                         }
517                 } else {
518                         tag = FP_EXP_TAG_EMPTY;
519                 }
520                 ret |= tag << (2 * i);
521         }
522         return ret;
523 }
524
525 /*
526  * FXSR floating point environment conversions.
527  */
528
529 void
530 convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
531 {
532         struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
533         struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
534         struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
535         int i;
536
537         env->cwd = fxsave->cwd | 0xffff0000u;
538         env->swd = fxsave->swd | 0xffff0000u;
539         env->twd = twd_fxsr_to_i387(fxsave);
540
541 #ifdef CONFIG_X86_64
542         env->fip = fxsave->rip;
543         env->foo = fxsave->rdp;
544         /*
545          * should be actually ds/cs at fpu exception time, but
546          * that information is not available in 64bit mode.
547          */
548         env->fcs = task_pt_regs(tsk)->cs;
549         if (tsk == current) {
550                 savesegment(ds, env->fos);
551         } else {
552                 env->fos = tsk->thread.ds;
553         }
554         env->fos |= 0xffff0000;
555 #else
556         env->fip = fxsave->fip;
557         env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
558         env->foo = fxsave->foo;
559         env->fos = fxsave->fos;
560 #endif
561
562         for (i = 0; i < 8; ++i)
563                 memcpy(&to[i], &from[i], sizeof(to[0]));
564 }
565
566 void convert_to_fxsr(struct task_struct *tsk,
567                      const struct user_i387_ia32_struct *env)
568
569 {
570         struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
571         struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
572         struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
573         int i;
574
575         fxsave->cwd = env->cwd;
576         fxsave->swd = env->swd;
577         fxsave->twd = twd_i387_to_fxsr(env->twd);
578         fxsave->fop = (u16) ((u32) env->fcs >> 16);
579 #ifdef CONFIG_X86_64
580         fxsave->rip = env->fip;
581         fxsave->rdp = env->foo;
582         /* cs and ds ignored */
583 #else
584         fxsave->fip = env->fip;
585         fxsave->fcs = (env->fcs & 0xffff);
586         fxsave->foo = env->foo;
587         fxsave->fos = env->fos;
588 #endif
589
590         for (i = 0; i < 8; ++i)
591                 memcpy(&to[i], &from[i], sizeof(from[0]));
592 }
593
594 int fpregs_get(struct task_struct *target, const struct user_regset *regset,
595                unsigned int pos, unsigned int count,
596                void *kbuf, void __user *ubuf)
597 {
598         struct user_i387_ia32_struct env;
599         int ret;
600
601         ret = fpu__unlazy_stopped(target);
602         if (ret)
603                 return ret;
604
605         if (!static_cpu_has(X86_FEATURE_FPU))
606                 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
607
608         if (!cpu_has_fxsr)
609                 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
610                                            &target->thread.fpu.state->fsave, 0,
611                                            -1);
612
613         sanitize_i387_state(target);
614
615         if (kbuf && pos == 0 && count == sizeof(env)) {
616                 convert_from_fxsr(kbuf, target);
617                 return 0;
618         }
619
620         convert_from_fxsr(&env, target);
621
622         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
623 }
624
625 int fpregs_set(struct task_struct *target, const struct user_regset *regset,
626                unsigned int pos, unsigned int count,
627                const void *kbuf, const void __user *ubuf)
628 {
629         struct user_i387_ia32_struct env;
630         int ret;
631
632         ret = fpu__unlazy_stopped(target);
633         if (ret)
634                 return ret;
635
636         sanitize_i387_state(target);
637
638         if (!static_cpu_has(X86_FEATURE_FPU))
639                 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
640
641         if (!cpu_has_fxsr)
642                 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
643                                           &target->thread.fpu.state->fsave, 0,
644                                           -1);
645
646         if (pos > 0 || count < sizeof(env))
647                 convert_from_fxsr(&env, target);
648
649         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
650         if (!ret)
651                 convert_to_fxsr(target, &env);
652
653         /*
654          * update the header bit in the xsave header, indicating the
655          * presence of FP.
656          */
657         if (cpu_has_xsave)
658                 target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FP;
659         return ret;
660 }
661
662 /*
663  * FPU state for core dumps.
664  * This is only used for a.out dumps now.
665  * It is declared generically using elf_fpregset_t (which is
666  * struct user_i387_struct) but is in fact only used for 32-bit
667  * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
668  */
669 int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
670 {
671         struct task_struct *tsk = current;
672         int fpvalid;
673
674         fpvalid = !!used_math();
675         if (fpvalid)
676                 fpvalid = !fpregs_get(tsk, NULL,
677                                       0, sizeof(struct user_i387_ia32_struct),
678                                       fpu, NULL);
679
680         return fpvalid;
681 }
682 EXPORT_SYMBOL(dump_fpu);
683
684 #endif  /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */
685
686 static int __init no_387(char *s)
687 {
688         setup_clear_cpu_cap(X86_FEATURE_FPU);
689         return 1;
690 }
691
692 __setup("no387", no_387);
693
694 /*
695  * Set the X86_FEATURE_FPU CPU-capability bit based on
696  * trying to execute an actual sequence of FPU instructions:
697  */
698 void fpu__detect(struct cpuinfo_x86 *c)
699 {
700         unsigned long cr0;
701         u16 fsw, fcw;
702
703         fsw = fcw = 0xffff;
704
705         cr0 = read_cr0();
706         cr0 &= ~(X86_CR0_TS | X86_CR0_EM);
707         write_cr0(cr0);
708
709         asm volatile("fninit ; fnstsw %0 ; fnstcw %1"
710                      : "+m" (fsw), "+m" (fcw));
711
712         if (fsw == 0 && (fcw & 0x103f) == 0x003f)
713                 set_cpu_cap(c, X86_FEATURE_FPU);
714         else
715                 clear_cpu_cap(c, X86_FEATURE_FPU);
716
717         /* The final cr0 value is set in fpu_init() */
718 }