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