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