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