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