x86/fpu: Harmonize the names of the fpstate_init() helper functions
[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 <asm/fpu/internal.h>
9 #include <asm/fpu/regset.h>
10 #include <asm/fpu/signal.h>
11 #include <asm/traps.h>
12
13 #include <linux/hardirq.h>
14
15 /*
16  * Track whether the kernel is using the FPU state
17  * currently.
18  *
19  * This flag is used:
20  *
21  *   - by IRQ context code to potentially use the FPU
22  *     if it's unused.
23  *
24  *   - to debug kernel_fpu_begin()/end() correctness
25  */
26 static DEFINE_PER_CPU(bool, in_kernel_fpu);
27
28 /*
29  * Track which context is using the FPU on the CPU:
30  */
31 DEFINE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
32
33 static void kernel_fpu_disable(void)
34 {
35         WARN_ON(this_cpu_read(in_kernel_fpu));
36         this_cpu_write(in_kernel_fpu, true);
37 }
38
39 static void kernel_fpu_enable(void)
40 {
41         WARN_ON_ONCE(!this_cpu_read(in_kernel_fpu));
42         this_cpu_write(in_kernel_fpu, false);
43 }
44
45 static bool kernel_fpu_disabled(void)
46 {
47         return this_cpu_read(in_kernel_fpu);
48 }
49
50 /*
51  * Were we in an interrupt that interrupted kernel mode?
52  *
53  * On others, we can do a kernel_fpu_begin/end() pair *ONLY* if that
54  * pair does nothing at all: the thread must not have fpu (so
55  * that we don't try to save the FPU state), and TS must
56  * be set (so that the clts/stts pair does nothing that is
57  * visible in the interrupted kernel thread).
58  *
59  * Except for the eagerfpu case when we return true; in the likely case
60  * the thread has FPU but we are not going to set/clear TS.
61  */
62 static bool interrupted_kernel_fpu_idle(void)
63 {
64         if (kernel_fpu_disabled())
65                 return false;
66
67         if (use_eager_fpu())
68                 return true;
69
70         return !current->thread.fpu.fpregs_active && (read_cr0() & X86_CR0_TS);
71 }
72
73 /*
74  * Were we in user mode (or vm86 mode) when we were
75  * interrupted?
76  *
77  * Doing kernel_fpu_begin/end() is ok if we are running
78  * in an interrupt context from user mode - we'll just
79  * save the FPU state as required.
80  */
81 static bool interrupted_user_mode(void)
82 {
83         struct pt_regs *regs = get_irq_regs();
84         return regs && user_mode(regs);
85 }
86
87 /*
88  * Can we use the FPU in kernel mode with the
89  * whole "kernel_fpu_begin/end()" sequence?
90  *
91  * It's always ok in process context (ie "not interrupt")
92  * but it is sometimes ok even from an irq.
93  */
94 bool irq_fpu_usable(void)
95 {
96         return !in_interrupt() ||
97                 interrupted_user_mode() ||
98                 interrupted_kernel_fpu_idle();
99 }
100 EXPORT_SYMBOL(irq_fpu_usable);
101
102 void __kernel_fpu_begin(void)
103 {
104         struct fpu *fpu = &current->thread.fpu;
105
106         kernel_fpu_disable();
107
108         if (fpu->fpregs_active) {
109                 copy_fpregs_to_fpstate(fpu);
110         } else {
111                 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
112                 __fpregs_activate_hw();
113         }
114 }
115 EXPORT_SYMBOL(__kernel_fpu_begin);
116
117 void __kernel_fpu_end(void)
118 {
119         struct fpu *fpu = &current->thread.fpu;
120
121         if (fpu->fpregs_active) {
122                 if (WARN_ON(copy_fpstate_to_fpregs(fpu)))
123                         fpu__clear(fpu);
124         } else {
125                 __fpregs_deactivate_hw();
126         }
127
128         kernel_fpu_enable();
129 }
130 EXPORT_SYMBOL(__kernel_fpu_end);
131
132 void kernel_fpu_begin(void)
133 {
134         preempt_disable();
135         WARN_ON_ONCE(!irq_fpu_usable());
136         __kernel_fpu_begin();
137 }
138 EXPORT_SYMBOL_GPL(kernel_fpu_begin);
139
140 void kernel_fpu_end(void)
141 {
142         __kernel_fpu_end();
143         preempt_enable();
144 }
145 EXPORT_SYMBOL_GPL(kernel_fpu_end);
146
147 /*
148  * CR0::TS save/restore functions:
149  */
150 int irq_ts_save(void)
151 {
152         /*
153          * If in process context and not atomic, we can take a spurious DNA fault.
154          * Otherwise, doing clts() in process context requires disabling preemption
155          * or some heavy lifting like kernel_fpu_begin()
156          */
157         if (!in_atomic())
158                 return 0;
159
160         if (read_cr0() & X86_CR0_TS) {
161                 clts();
162                 return 1;
163         }
164
165         return 0;
166 }
167 EXPORT_SYMBOL_GPL(irq_ts_save);
168
169 void irq_ts_restore(int TS_state)
170 {
171         if (TS_state)
172                 stts();
173 }
174 EXPORT_SYMBOL_GPL(irq_ts_restore);
175
176 /*
177  * Save the FPU state (mark it for reload if necessary):
178  *
179  * This only ever gets called for the current task.
180  */
181 void fpu__save(struct fpu *fpu)
182 {
183         WARN_ON(fpu != &current->thread.fpu);
184
185         preempt_disable();
186         if (fpu->fpregs_active) {
187                 if (!copy_fpregs_to_fpstate(fpu))
188                         fpregs_deactivate(fpu);
189         }
190         preempt_enable();
191 }
192 EXPORT_SYMBOL_GPL(fpu__save);
193
194 /*
195  * Legacy x87 fpstate state init:
196  */
197 static inline void fpstate_init_fstate(struct i387_fsave_struct *fp)
198 {
199         fp->cwd = 0xffff037fu;
200         fp->swd = 0xffff0000u;
201         fp->twd = 0xffffffffu;
202         fp->fos = 0xffff0000u;
203 }
204
205 void fpstate_init(struct fpu *fpu)
206 {
207         if (!cpu_has_fpu) {
208                 fpstate_init_soft(&fpu->state.soft);
209                 return;
210         }
211
212         memset(&fpu->state, 0, xstate_size);
213
214         if (cpu_has_fxsr)
215                 fpstate_init_fxstate(&fpu->state.fxsave);
216         else
217                 fpstate_init_fstate(&fpu->state.fsave);
218 }
219 EXPORT_SYMBOL_GPL(fpstate_init);
220
221 /*
222  * Copy the current task's FPU state to a new task's FPU context.
223  *
224  * In the 'eager' case we just save to the destination context.
225  *
226  * In the 'lazy' case we save to the source context, mark the FPU lazy
227  * via stts() and copy the source context into the destination context.
228  */
229 static void fpu_copy(struct fpu *dst_fpu, struct fpu *src_fpu)
230 {
231         WARN_ON(src_fpu != &current->thread.fpu);
232
233         /*
234          * Don't let 'init optimized' areas of the XSAVE area
235          * leak into the child task:
236          */
237         if (use_eager_fpu())
238                 memset(&dst_fpu->state.xsave, 0, xstate_size);
239
240         /*
241          * Save current FPU registers directly into the child
242          * FPU context, without any memory-to-memory copying.
243          *
244          * If the FPU context got destroyed in the process (FNSAVE
245          * done on old CPUs) then copy it back into the source
246          * context and mark the current task for lazy restore.
247          *
248          * We have to do all this with preemption disabled,
249          * mostly because of the FNSAVE case, because in that
250          * case we must not allow preemption in the window
251          * between the FNSAVE and us marking the context lazy.
252          *
253          * It shouldn't be an issue as even FNSAVE is plenty
254          * fast in terms of critical section length.
255          */
256         preempt_disable();
257         if (!copy_fpregs_to_fpstate(dst_fpu)) {
258                 memcpy(&src_fpu->state, &dst_fpu->state, xstate_size);
259                 fpregs_deactivate(src_fpu);
260         }
261         preempt_enable();
262 }
263
264 int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu)
265 {
266         dst_fpu->counter = 0;
267         dst_fpu->fpregs_active = 0;
268         dst_fpu->last_cpu = -1;
269
270         if (src_fpu->fpstate_active)
271                 fpu_copy(dst_fpu, src_fpu);
272
273         return 0;
274 }
275
276 /*
277  * Activate the current task's in-memory FPU context,
278  * if it has not been used before:
279  */
280 void fpu__activate_curr(struct fpu *fpu)
281 {
282         WARN_ON_ONCE(fpu != &current->thread.fpu);
283
284         if (!fpu->fpstate_active) {
285                 fpstate_init(fpu);
286
287                 /* Safe to do for the current task: */
288                 fpu->fpstate_active = 1;
289         }
290 }
291 EXPORT_SYMBOL_GPL(fpu__activate_curr);
292
293 /*
294  * This function must be called before we modify a stopped child's
295  * fpstate.
296  *
297  * If the child has not used the FPU before then initialize its
298  * fpstate.
299  *
300  * If the child has used the FPU before then unlazy it.
301  *
302  * [ After this function call, after registers in the fpstate are
303  *   modified and the child task has woken up, the child task will
304  *   restore the modified FPU state from the modified context. If we
305  *   didn't clear its lazy status here then the lazy in-registers
306  *   state pending on its former CPU could be restored, corrupting
307  *   the modifications. ]
308  *
309  * This function is also called before we read a stopped child's
310  * FPU state - to make sure it's initialized if the child has
311  * no active FPU state.
312  *
313  * TODO: A future optimization would be to skip the unlazying in
314  *       the read-only case, it's not strictly necessary for
315  *       read-only access to the context.
316  */
317 static void fpu__activate_stopped(struct fpu *child_fpu)
318 {
319         WARN_ON_ONCE(child_fpu == &current->thread.fpu);
320
321         if (child_fpu->fpstate_active) {
322                 child_fpu->last_cpu = -1;
323         } else {
324                 fpstate_init(child_fpu);
325
326                 /* Safe to do for stopped child tasks: */
327                 child_fpu->fpstate_active = 1;
328         }
329 }
330
331 /*
332  * 'fpu__restore()' is called to copy FPU registers from
333  * the FPU fpstate to the live hw registers and to activate
334  * access to the hardware registers, so that FPU instructions
335  * can be used afterwards.
336  *
337  * Must be called with kernel preemption disabled (for example
338  * with local interrupts disabled, as it is in the case of
339  * do_device_not_available()).
340  */
341 void fpu__restore(void)
342 {
343         struct task_struct *tsk = current;
344         struct fpu *fpu = &tsk->thread.fpu;
345
346         fpu__activate_curr(fpu);
347
348         /* Avoid __kernel_fpu_begin() right after fpregs_activate() */
349         kernel_fpu_disable();
350         fpregs_activate(fpu);
351         if (unlikely(copy_fpstate_to_fpregs(fpu))) {
352                 fpu__clear(fpu);
353                 force_sig_info(SIGSEGV, SEND_SIG_PRIV, tsk);
354         } else {
355                 tsk->thread.fpu.counter++;
356         }
357         kernel_fpu_enable();
358 }
359 EXPORT_SYMBOL_GPL(fpu__restore);
360
361 /*
362  * Drops current FPU state: deactivates the fpregs and
363  * the fpstate. NOTE: it still leaves previous contents
364  * in the fpregs in the eager-FPU case.
365  *
366  * This function can be used in cases where we know that
367  * a state-restore is coming: either an explicit one,
368  * or a reschedule.
369  */
370 void fpu__drop(struct fpu *fpu)
371 {
372         preempt_disable();
373         fpu->counter = 0;
374
375         if (fpu->fpregs_active) {
376                 /* Ignore delayed exceptions from user space */
377                 asm volatile("1: fwait\n"
378                              "2:\n"
379                              _ASM_EXTABLE(1b, 2b));
380                 fpregs_deactivate(fpu);
381         }
382
383         fpu->fpstate_active = 0;
384
385         preempt_enable();
386 }
387
388 /*
389  * Clear the FPU state back to init state.
390  *
391  * Called by sys_execve(), by the signal handler code and by various
392  * error paths.
393  */
394 void fpu__clear(struct fpu *fpu)
395 {
396         WARN_ON_ONCE(fpu != &current->thread.fpu); /* Almost certainly an anomaly */
397
398         if (!use_eager_fpu()) {
399                 /* FPU state will be reallocated lazily at the first use. */
400                 fpu__drop(fpu);
401         } else {
402                 if (!fpu->fpstate_active) {
403                         fpu__activate_curr(fpu);
404                         user_fpu_begin();
405                 }
406                 restore_init_xstate();
407         }
408 }
409
410 /*
411  * The xstateregs_active() routine is the same as the regset_fpregs_active() routine,
412  * as the "regset->n" for the xstate regset will be updated based on the feature
413  * capabilites supported by the xsave.
414  */
415 int regset_fpregs_active(struct task_struct *target, const struct user_regset *regset)
416 {
417         struct fpu *target_fpu = &target->thread.fpu;
418
419         return target_fpu->fpstate_active ? regset->n : 0;
420 }
421
422 int regset_xregset_fpregs_active(struct task_struct *target, const struct user_regset *regset)
423 {
424         struct fpu *target_fpu = &target->thread.fpu;
425
426         return (cpu_has_fxsr && target_fpu->fpstate_active) ? regset->n : 0;
427 }
428
429 int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
430                 unsigned int pos, unsigned int count,
431                 void *kbuf, void __user *ubuf)
432 {
433         struct fpu *fpu = &target->thread.fpu;
434
435         if (!cpu_has_fxsr)
436                 return -ENODEV;
437
438         fpu__activate_stopped(fpu);
439         fpstate_sanitize_xstate(fpu);
440
441         return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
442                                    &fpu->state.fxsave, 0, -1);
443 }
444
445 int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
446                 unsigned int pos, unsigned int count,
447                 const void *kbuf, const void __user *ubuf)
448 {
449         struct fpu *fpu = &target->thread.fpu;
450         int ret;
451
452         if (!cpu_has_fxsr)
453                 return -ENODEV;
454
455         fpu__activate_stopped(fpu);
456         fpstate_sanitize_xstate(fpu);
457
458         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
459                                  &fpu->state.fxsave, 0, -1);
460
461         /*
462          * mxcsr reserved bits must be masked to zero for security reasons.
463          */
464         fpu->state.fxsave.mxcsr &= mxcsr_feature_mask;
465
466         /*
467          * update the header bits in the xsave header, indicating the
468          * presence of FP and SSE state.
469          */
470         if (cpu_has_xsave)
471                 fpu->state.xsave.header.xfeatures |= XSTATE_FPSSE;
472
473         return ret;
474 }
475
476 int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
477                 unsigned int pos, unsigned int count,
478                 void *kbuf, void __user *ubuf)
479 {
480         struct fpu *fpu = &target->thread.fpu;
481         struct xsave_struct *xsave;
482         int ret;
483
484         if (!cpu_has_xsave)
485                 return -ENODEV;
486
487         fpu__activate_stopped(fpu);
488
489         xsave = &fpu->state.xsave;
490
491         /*
492          * Copy the 48bytes defined by the software first into the xstate
493          * memory layout in the thread struct, so that we can copy the entire
494          * xstateregs to the user using one user_regset_copyout().
495          */
496         memcpy(&xsave->i387.sw_reserved,
497                 xstate_fx_sw_bytes, sizeof(xstate_fx_sw_bytes));
498         /*
499          * Copy the xstate memory layout.
500          */
501         ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
502         return ret;
503 }
504
505 int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
506                   unsigned int pos, unsigned int count,
507                   const void *kbuf, const void __user *ubuf)
508 {
509         struct fpu *fpu = &target->thread.fpu;
510         struct xsave_struct *xsave;
511         int ret;
512
513         if (!cpu_has_xsave)
514                 return -ENODEV;
515
516         fpu__activate_stopped(fpu);
517
518         xsave = &fpu->state.xsave;
519
520         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
521         /*
522          * mxcsr reserved bits must be masked to zero for security reasons.
523          */
524         xsave->i387.mxcsr &= mxcsr_feature_mask;
525         xsave->header.xfeatures &= xfeatures_mask;
526         /*
527          * These bits must be zero.
528          */
529         memset(&xsave->header.reserved, 0, 48);
530
531         return ret;
532 }
533
534 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
535
536 /*
537  * FPU tag word conversions.
538  */
539
540 static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
541 {
542         unsigned int tmp; /* to avoid 16 bit prefixes in the code */
543
544         /* Transform each pair of bits into 01 (valid) or 00 (empty) */
545         tmp = ~twd;
546         tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
547         /* and move the valid bits to the lower byte. */
548         tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
549         tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
550         tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
551
552         return tmp;
553 }
554
555 #define FPREG_ADDR(f, n)        ((void *)&(f)->st_space + (n) * 16)
556 #define FP_EXP_TAG_VALID        0
557 #define FP_EXP_TAG_ZERO         1
558 #define FP_EXP_TAG_SPECIAL      2
559 #define FP_EXP_TAG_EMPTY        3
560
561 static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
562 {
563         struct _fpxreg *st;
564         u32 tos = (fxsave->swd >> 11) & 7;
565         u32 twd = (unsigned long) fxsave->twd;
566         u32 tag;
567         u32 ret = 0xffff0000u;
568         int i;
569
570         for (i = 0; i < 8; i++, twd >>= 1) {
571                 if (twd & 0x1) {
572                         st = FPREG_ADDR(fxsave, (i - tos) & 7);
573
574                         switch (st->exponent & 0x7fff) {
575                         case 0x7fff:
576                                 tag = FP_EXP_TAG_SPECIAL;
577                                 break;
578                         case 0x0000:
579                                 if (!st->significand[0] &&
580                                     !st->significand[1] &&
581                                     !st->significand[2] &&
582                                     !st->significand[3])
583                                         tag = FP_EXP_TAG_ZERO;
584                                 else
585                                         tag = FP_EXP_TAG_SPECIAL;
586                                 break;
587                         default:
588                                 if (st->significand[3] & 0x8000)
589                                         tag = FP_EXP_TAG_VALID;
590                                 else
591                                         tag = FP_EXP_TAG_SPECIAL;
592                                 break;
593                         }
594                 } else {
595                         tag = FP_EXP_TAG_EMPTY;
596                 }
597                 ret |= tag << (2 * i);
598         }
599         return ret;
600 }
601
602 /*
603  * FXSR floating point environment conversions.
604  */
605
606 void
607 convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
608 {
609         struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state.fxsave;
610         struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
611         struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
612         int i;
613
614         env->cwd = fxsave->cwd | 0xffff0000u;
615         env->swd = fxsave->swd | 0xffff0000u;
616         env->twd = twd_fxsr_to_i387(fxsave);
617
618 #ifdef CONFIG_X86_64
619         env->fip = fxsave->rip;
620         env->foo = fxsave->rdp;
621         /*
622          * should be actually ds/cs at fpu exception time, but
623          * that information is not available in 64bit mode.
624          */
625         env->fcs = task_pt_regs(tsk)->cs;
626         if (tsk == current) {
627                 savesegment(ds, env->fos);
628         } else {
629                 env->fos = tsk->thread.ds;
630         }
631         env->fos |= 0xffff0000;
632 #else
633         env->fip = fxsave->fip;
634         env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
635         env->foo = fxsave->foo;
636         env->fos = fxsave->fos;
637 #endif
638
639         for (i = 0; i < 8; ++i)
640                 memcpy(&to[i], &from[i], sizeof(to[0]));
641 }
642
643 void convert_to_fxsr(struct task_struct *tsk,
644                      const struct user_i387_ia32_struct *env)
645
646 {
647         struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state.fxsave;
648         struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
649         struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
650         int i;
651
652         fxsave->cwd = env->cwd;
653         fxsave->swd = env->swd;
654         fxsave->twd = twd_i387_to_fxsr(env->twd);
655         fxsave->fop = (u16) ((u32) env->fcs >> 16);
656 #ifdef CONFIG_X86_64
657         fxsave->rip = env->fip;
658         fxsave->rdp = env->foo;
659         /* cs and ds ignored */
660 #else
661         fxsave->fip = env->fip;
662         fxsave->fcs = (env->fcs & 0xffff);
663         fxsave->foo = env->foo;
664         fxsave->fos = env->fos;
665 #endif
666
667         for (i = 0; i < 8; ++i)
668                 memcpy(&to[i], &from[i], sizeof(from[0]));
669 }
670
671 int fpregs_get(struct task_struct *target, const struct user_regset *regset,
672                unsigned int pos, unsigned int count,
673                void *kbuf, void __user *ubuf)
674 {
675         struct fpu *fpu = &target->thread.fpu;
676         struct user_i387_ia32_struct env;
677
678         fpu__activate_stopped(fpu);
679
680         if (!static_cpu_has(X86_FEATURE_FPU))
681                 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
682
683         if (!cpu_has_fxsr)
684                 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
685                                            &fpu->state.fsave, 0,
686                                            -1);
687
688         fpstate_sanitize_xstate(fpu);
689
690         if (kbuf && pos == 0 && count == sizeof(env)) {
691                 convert_from_fxsr(kbuf, target);
692                 return 0;
693         }
694
695         convert_from_fxsr(&env, target);
696
697         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
698 }
699
700 int fpregs_set(struct task_struct *target, const struct user_regset *regset,
701                unsigned int pos, unsigned int count,
702                const void *kbuf, const void __user *ubuf)
703 {
704         struct fpu *fpu = &target->thread.fpu;
705         struct user_i387_ia32_struct env;
706         int ret;
707
708         fpu__activate_stopped(fpu);
709         fpstate_sanitize_xstate(fpu);
710
711         if (!static_cpu_has(X86_FEATURE_FPU))
712                 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
713
714         if (!cpu_has_fxsr)
715                 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
716                                           &fpu->state.fsave, 0,
717                                           -1);
718
719         if (pos > 0 || count < sizeof(env))
720                 convert_from_fxsr(&env, target);
721
722         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
723         if (!ret)
724                 convert_to_fxsr(target, &env);
725
726         /*
727          * update the header bit in the xsave header, indicating the
728          * presence of FP.
729          */
730         if (cpu_has_xsave)
731                 fpu->state.xsave.header.xfeatures |= XSTATE_FP;
732         return ret;
733 }
734
735 /*
736  * FPU state for core dumps.
737  * This is only used for a.out dumps now.
738  * It is declared generically using elf_fpregset_t (which is
739  * struct user_i387_struct) but is in fact only used for 32-bit
740  * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
741  */
742 int dump_fpu(struct pt_regs *regs, struct user_i387_struct *ufpu)
743 {
744         struct task_struct *tsk = current;
745         struct fpu *fpu = &tsk->thread.fpu;
746         int fpvalid;
747
748         fpvalid = fpu->fpstate_active;
749         if (fpvalid)
750                 fpvalid = !fpregs_get(tsk, NULL,
751                                       0, sizeof(struct user_i387_ia32_struct),
752                                       ufpu, NULL);
753
754         return fpvalid;
755 }
756 EXPORT_SYMBOL(dump_fpu);
757
758 #endif  /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */
759
760 /*
761  * x87 math exception handling:
762  */
763
764 static inline unsigned short get_fpu_cwd(struct fpu *fpu)
765 {
766         if (cpu_has_fxsr) {
767                 return fpu->state.fxsave.cwd;
768         } else {
769                 return (unsigned short)fpu->state.fsave.cwd;
770         }
771 }
772
773 static inline unsigned short get_fpu_swd(struct fpu *fpu)
774 {
775         if (cpu_has_fxsr) {
776                 return fpu->state.fxsave.swd;
777         } else {
778                 return (unsigned short)fpu->state.fsave.swd;
779         }
780 }
781
782 static inline unsigned short get_fpu_mxcsr(struct fpu *fpu)
783 {
784         if (cpu_has_xmm) {
785                 return fpu->state.fxsave.mxcsr;
786         } else {
787                 return MXCSR_DEFAULT;
788         }
789 }
790
791 int fpu__exception_code(struct fpu *fpu, int trap_nr)
792 {
793         int err;
794
795         if (trap_nr == X86_TRAP_MF) {
796                 unsigned short cwd, swd;
797                 /*
798                  * (~cwd & swd) will mask out exceptions that are not set to unmasked
799                  * status.  0x3f is the exception bits in these regs, 0x200 is the
800                  * C1 reg you need in case of a stack fault, 0x040 is the stack
801                  * fault bit.  We should only be taking one exception at a time,
802                  * so if this combination doesn't produce any single exception,
803                  * then we have a bad program that isn't synchronizing its FPU usage
804                  * and it will suffer the consequences since we won't be able to
805                  * fully reproduce the context of the exception
806                  */
807                 cwd = get_fpu_cwd(fpu);
808                 swd = get_fpu_swd(fpu);
809
810                 err = swd & ~cwd;
811         } else {
812                 /*
813                  * The SIMD FPU exceptions are handled a little differently, as there
814                  * is only a single status/control register.  Thus, to determine which
815                  * unmasked exception was caught we must mask the exception mask bits
816                  * at 0x1f80, and then use these to mask the exception bits at 0x3f.
817                  */
818                 unsigned short mxcsr = get_fpu_mxcsr(fpu);
819                 err = ~(mxcsr >> 7) & mxcsr;
820         }
821
822         if (err & 0x001) {      /* Invalid op */
823                 /*
824                  * swd & 0x240 == 0x040: Stack Underflow
825                  * swd & 0x240 == 0x240: Stack Overflow
826                  * User must clear the SF bit (0x40) if set
827                  */
828                 return FPE_FLTINV;
829         } else if (err & 0x004) { /* Divide by Zero */
830                 return FPE_FLTDIV;
831         } else if (err & 0x008) { /* Overflow */
832                 return FPE_FLTOVF;
833         } else if (err & 0x012) { /* Denormal, Underflow */
834                 return FPE_FLTUND;
835         } else if (err & 0x020) { /* Precision */
836                 return FPE_FLTRES;
837         }
838
839         /*
840          * If we're using IRQ 13, or supposedly even some trap
841          * X86_TRAP_MF implementations, it's possible
842          * we get a spurious trap, which is not an error.
843          */
844         return 0;
845 }