Merge tag 'iwlwifi-next-for-kalle-2016-03-30' of https://git.kernel.org/pub/scm/linux...
[sfrench/cifs-2.6.git] / arch / x86 / include / asm / fpu / internal.h
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  * x86-64 work by Andi Kleen 2002
8  */
9
10 #ifndef _ASM_X86_FPU_INTERNAL_H
11 #define _ASM_X86_FPU_INTERNAL_H
12
13 #include <linux/compat.h>
14 #include <linux/sched.h>
15 #include <linux/slab.h>
16
17 #include <asm/user.h>
18 #include <asm/fpu/api.h>
19 #include <asm/fpu/xstate.h>
20 #include <asm/cpufeature.h>
21
22 /*
23  * High level FPU state handling functions:
24  */
25 extern void fpu__activate_curr(struct fpu *fpu);
26 extern void fpu__activate_fpstate_read(struct fpu *fpu);
27 extern void fpu__activate_fpstate_write(struct fpu *fpu);
28 extern void fpu__current_fpstate_write_begin(void);
29 extern void fpu__current_fpstate_write_end(void);
30 extern void fpu__save(struct fpu *fpu);
31 extern void fpu__restore(struct fpu *fpu);
32 extern int  fpu__restore_sig(void __user *buf, int ia32_frame);
33 extern void fpu__drop(struct fpu *fpu);
34 extern int  fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu);
35 extern void fpu__clear(struct fpu *fpu);
36 extern int  fpu__exception_code(struct fpu *fpu, int trap_nr);
37 extern int  dump_fpu(struct pt_regs *ptregs, struct user_i387_struct *fpstate);
38
39 /*
40  * Boot time FPU initialization functions:
41  */
42 extern void fpu__init_cpu(void);
43 extern void fpu__init_system_xstate(void);
44 extern void fpu__init_cpu_xstate(void);
45 extern void fpu__init_system(struct cpuinfo_x86 *c);
46 extern void fpu__init_check_bugs(void);
47 extern void fpu__resume_cpu(void);
48 extern u64 fpu__get_supported_xfeatures_mask(void);
49
50 /*
51  * Debugging facility:
52  */
53 #ifdef CONFIG_X86_DEBUG_FPU
54 # define WARN_ON_FPU(x) WARN_ON_ONCE(x)
55 #else
56 # define WARN_ON_FPU(x) ({ (void)(x); 0; })
57 #endif
58
59 /*
60  * FPU related CPU feature flag helper routines:
61  */
62 static __always_inline __pure bool use_eager_fpu(void)
63 {
64         return static_cpu_has(X86_FEATURE_EAGER_FPU);
65 }
66
67 static __always_inline __pure bool use_xsaveopt(void)
68 {
69         return static_cpu_has(X86_FEATURE_XSAVEOPT);
70 }
71
72 static __always_inline __pure bool use_xsave(void)
73 {
74         return static_cpu_has(X86_FEATURE_XSAVE);
75 }
76
77 static __always_inline __pure bool use_fxsr(void)
78 {
79         return static_cpu_has(X86_FEATURE_FXSR);
80 }
81
82 /*
83  * fpstate handling functions:
84  */
85
86 extern union fpregs_state init_fpstate;
87
88 extern void fpstate_init(union fpregs_state *state);
89 #ifdef CONFIG_MATH_EMULATION
90 extern void fpstate_init_soft(struct swregs_state *soft);
91 #else
92 static inline void fpstate_init_soft(struct swregs_state *soft) {}
93 #endif
94 static inline void fpstate_init_fxstate(struct fxregs_state *fx)
95 {
96         fx->cwd = 0x37f;
97         fx->mxcsr = MXCSR_DEFAULT;
98 }
99 extern void fpstate_sanitize_xstate(struct fpu *fpu);
100
101 #define user_insn(insn, output, input...)                               \
102 ({                                                                      \
103         int err;                                                        \
104         asm volatile(ASM_STAC "\n"                                      \
105                      "1:" #insn "\n\t"                                  \
106                      "2: " ASM_CLAC "\n"                                \
107                      ".section .fixup,\"ax\"\n"                         \
108                      "3:  movl $-1,%[err]\n"                            \
109                      "    jmp  2b\n"                                    \
110                      ".previous\n"                                      \
111                      _ASM_EXTABLE(1b, 3b)                               \
112                      : [err] "=r" (err), output                         \
113                      : "0"(0), input);                                  \
114         err;                                                            \
115 })
116
117 #define check_insn(insn, output, input...)                              \
118 ({                                                                      \
119         int err;                                                        \
120         asm volatile("1:" #insn "\n\t"                                  \
121                      "2:\n"                                             \
122                      ".section .fixup,\"ax\"\n"                         \
123                      "3:  movl $-1,%[err]\n"                            \
124                      "    jmp  2b\n"                                    \
125                      ".previous\n"                                      \
126                      _ASM_EXTABLE(1b, 3b)                               \
127                      : [err] "=r" (err), output                         \
128                      : "0"(0), input);                                  \
129         err;                                                            \
130 })
131
132 static inline int copy_fregs_to_user(struct fregs_state __user *fx)
133 {
134         return user_insn(fnsave %[fx]; fwait,  [fx] "=m" (*fx), "m" (*fx));
135 }
136
137 static inline int copy_fxregs_to_user(struct fxregs_state __user *fx)
138 {
139         if (config_enabled(CONFIG_X86_32))
140                 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
141         else if (config_enabled(CONFIG_AS_FXSAVEQ))
142                 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
143
144         /* See comment in copy_fxregs_to_kernel() below. */
145         return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
146 }
147
148 static inline void copy_kernel_to_fxregs(struct fxregs_state *fx)
149 {
150         int err;
151
152         if (config_enabled(CONFIG_X86_32)) {
153                 err = check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
154         } else {
155                 if (config_enabled(CONFIG_AS_FXSAVEQ)) {
156                         err = check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
157                 } else {
158                         /* See comment in copy_fxregs_to_kernel() below. */
159                         err = check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx), "m" (*fx));
160                 }
161         }
162         /* Copying from a kernel buffer to FPU registers should never fail: */
163         WARN_ON_FPU(err);
164 }
165
166 static inline int copy_user_to_fxregs(struct fxregs_state __user *fx)
167 {
168         if (config_enabled(CONFIG_X86_32))
169                 return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
170         else if (config_enabled(CONFIG_AS_FXSAVEQ))
171                 return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
172
173         /* See comment in copy_fxregs_to_kernel() below. */
174         return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
175                           "m" (*fx));
176 }
177
178 static inline void copy_kernel_to_fregs(struct fregs_state *fx)
179 {
180         int err = check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
181
182         WARN_ON_FPU(err);
183 }
184
185 static inline int copy_user_to_fregs(struct fregs_state __user *fx)
186 {
187         return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
188 }
189
190 static inline void copy_fxregs_to_kernel(struct fpu *fpu)
191 {
192         if (config_enabled(CONFIG_X86_32))
193                 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state.fxsave));
194         else if (config_enabled(CONFIG_AS_FXSAVEQ))
195                 asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state.fxsave));
196         else {
197                 /* Using "rex64; fxsave %0" is broken because, if the memory
198                  * operand uses any extended registers for addressing, a second
199                  * REX prefix will be generated (to the assembler, rex64
200                  * followed by semicolon is a separate instruction), and hence
201                  * the 64-bitness is lost.
202                  *
203                  * Using "fxsaveq %0" would be the ideal choice, but is only
204                  * supported starting with gas 2.16.
205                  *
206                  * Using, as a workaround, the properly prefixed form below
207                  * isn't accepted by any binutils version so far released,
208                  * complaining that the same type of prefix is used twice if
209                  * an extended register is needed for addressing (fix submitted
210                  * to mainline 2005-11-21).
211                  *
212                  *  asm volatile("rex64/fxsave %0" : "=m" (fpu->state.fxsave));
213                  *
214                  * This, however, we can work around by forcing the compiler to
215                  * select an addressing mode that doesn't require extended
216                  * registers.
217                  */
218                 asm volatile( "rex64/fxsave (%[fx])"
219                              : "=m" (fpu->state.fxsave)
220                              : [fx] "R" (&fpu->state.fxsave));
221         }
222 }
223
224 /* These macros all use (%edi)/(%rdi) as the single memory argument. */
225 #define XSAVE           ".byte " REX_PREFIX "0x0f,0xae,0x27"
226 #define XSAVEOPT        ".byte " REX_PREFIX "0x0f,0xae,0x37"
227 #define XSAVES          ".byte " REX_PREFIX "0x0f,0xc7,0x2f"
228 #define XRSTOR          ".byte " REX_PREFIX "0x0f,0xae,0x2f"
229 #define XRSTORS         ".byte " REX_PREFIX "0x0f,0xc7,0x1f"
230
231 #define XSTATE_OP(op, st, lmask, hmask, err)                            \
232         asm volatile("1:" op "\n\t"                                     \
233                      "xor %[err], %[err]\n"                             \
234                      "2:\n\t"                                           \
235                      ".pushsection .fixup,\"ax\"\n\t"                   \
236                      "3: movl $-2,%[err]\n\t"                           \
237                      "jmp 2b\n\t"                                       \
238                      ".popsection\n\t"                                  \
239                      _ASM_EXTABLE(1b, 3b)                               \
240                      : [err] "=r" (err)                                 \
241                      : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)    \
242                      : "memory")
243
244 /*
245  * If XSAVES is enabled, it replaces XSAVEOPT because it supports a compact
246  * format and supervisor states in addition to modified optimization in
247  * XSAVEOPT.
248  *
249  * Otherwise, if XSAVEOPT is enabled, XSAVEOPT replaces XSAVE because XSAVEOPT
250  * supports modified optimization which is not supported by XSAVE.
251  *
252  * We use XSAVE as a fallback.
253  *
254  * The 661 label is defined in the ALTERNATIVE* macros as the address of the
255  * original instruction which gets replaced. We need to use it here as the
256  * address of the instruction where we might get an exception at.
257  */
258 #define XSTATE_XSAVE(st, lmask, hmask, err)                             \
259         asm volatile(ALTERNATIVE_2(XSAVE,                               \
260                                    XSAVEOPT, X86_FEATURE_XSAVEOPT,      \
261                                    XSAVES,   X86_FEATURE_XSAVES)        \
262                      "\n"                                               \
263                      "xor %[err], %[err]\n"                             \
264                      "3:\n"                                             \
265                      ".pushsection .fixup,\"ax\"\n"                     \
266                      "4: movl $-2, %[err]\n"                            \
267                      "jmp 3b\n"                                         \
268                      ".popsection\n"                                    \
269                      _ASM_EXTABLE(661b, 4b)                             \
270                      : [err] "=r" (err)                                 \
271                      : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)    \
272                      : "memory")
273
274 /*
275  * Use XRSTORS to restore context if it is enabled. XRSTORS supports compact
276  * XSAVE area format.
277  */
278 #define XSTATE_XRESTORE(st, lmask, hmask, err)                          \
279         asm volatile(ALTERNATIVE(XRSTOR,                                \
280                                  XRSTORS, X86_FEATURE_XSAVES)           \
281                      "\n"                                               \
282                      "xor %[err], %[err]\n"                             \
283                      "3:\n"                                             \
284                      ".pushsection .fixup,\"ax\"\n"                     \
285                      "4: movl $-2, %[err]\n"                            \
286                      "jmp 3b\n"                                         \
287                      ".popsection\n"                                    \
288                      _ASM_EXTABLE(661b, 4b)                             \
289                      : [err] "=r" (err)                                 \
290                      : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)    \
291                      : "memory")
292
293 /*
294  * This function is called only during boot time when x86 caps are not set
295  * up and alternative can not be used yet.
296  */
297 static inline void copy_xregs_to_kernel_booting(struct xregs_state *xstate)
298 {
299         u64 mask = -1;
300         u32 lmask = mask;
301         u32 hmask = mask >> 32;
302         int err;
303
304         WARN_ON(system_state != SYSTEM_BOOTING);
305
306         if (static_cpu_has(X86_FEATURE_XSAVES))
307                 XSTATE_OP(XSAVES, xstate, lmask, hmask, err);
308         else
309                 XSTATE_OP(XSAVE, xstate, lmask, hmask, err);
310
311         /* We should never fault when copying to a kernel buffer: */
312         WARN_ON_FPU(err);
313 }
314
315 /*
316  * This function is called only during boot time when x86 caps are not set
317  * up and alternative can not be used yet.
318  */
319 static inline void copy_kernel_to_xregs_booting(struct xregs_state *xstate)
320 {
321         u64 mask = -1;
322         u32 lmask = mask;
323         u32 hmask = mask >> 32;
324         int err;
325
326         WARN_ON(system_state != SYSTEM_BOOTING);
327
328         if (static_cpu_has(X86_FEATURE_XSAVES))
329                 XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
330         else
331                 XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
332
333         /* We should never fault when copying from a kernel buffer: */
334         WARN_ON_FPU(err);
335 }
336
337 /*
338  * Save processor xstate to xsave area.
339  */
340 static inline void copy_xregs_to_kernel(struct xregs_state *xstate)
341 {
342         u64 mask = -1;
343         u32 lmask = mask;
344         u32 hmask = mask >> 32;
345         int err;
346
347         WARN_ON(!alternatives_patched);
348
349         XSTATE_XSAVE(xstate, lmask, hmask, err);
350
351         /* We should never fault when copying to a kernel buffer: */
352         WARN_ON_FPU(err);
353 }
354
355 /*
356  * Restore processor xstate from xsave area.
357  */
358 static inline void copy_kernel_to_xregs(struct xregs_state *xstate, u64 mask)
359 {
360         u32 lmask = mask;
361         u32 hmask = mask >> 32;
362         int err;
363
364         XSTATE_XRESTORE(xstate, lmask, hmask, err);
365
366         /* We should never fault when copying from a kernel buffer: */
367         WARN_ON_FPU(err);
368 }
369
370 /*
371  * Save xstate to user space xsave area.
372  *
373  * We don't use modified optimization because xrstor/xrstors might track
374  * a different application.
375  *
376  * We don't use compacted format xsave area for
377  * backward compatibility for old applications which don't understand
378  * compacted format of xsave area.
379  */
380 static inline int copy_xregs_to_user(struct xregs_state __user *buf)
381 {
382         int err;
383
384         /*
385          * Clear the xsave header first, so that reserved fields are
386          * initialized to zero.
387          */
388         err = __clear_user(&buf->header, sizeof(buf->header));
389         if (unlikely(err))
390                 return -EFAULT;
391
392         stac();
393         XSTATE_OP(XSAVE, buf, -1, -1, err);
394         clac();
395
396         return err;
397 }
398
399 /*
400  * Restore xstate from user space xsave area.
401  */
402 static inline int copy_user_to_xregs(struct xregs_state __user *buf, u64 mask)
403 {
404         struct xregs_state *xstate = ((__force struct xregs_state *)buf);
405         u32 lmask = mask;
406         u32 hmask = mask >> 32;
407         int err;
408
409         stac();
410         XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
411         clac();
412
413         return err;
414 }
415
416 /*
417  * These must be called with preempt disabled. Returns
418  * 'true' if the FPU state is still intact and we can
419  * keep registers active.
420  *
421  * The legacy FNSAVE instruction cleared all FPU state
422  * unconditionally, so registers are essentially destroyed.
423  * Modern FPU state can be kept in registers, if there are
424  * no pending FP exceptions.
425  */
426 static inline int copy_fpregs_to_fpstate(struct fpu *fpu)
427 {
428         if (likely(use_xsave())) {
429                 copy_xregs_to_kernel(&fpu->state.xsave);
430                 return 1;
431         }
432
433         if (likely(use_fxsr())) {
434                 copy_fxregs_to_kernel(fpu);
435                 return 1;
436         }
437
438         /*
439          * Legacy FPU register saving, FNSAVE always clears FPU registers,
440          * so we have to mark them inactive:
441          */
442         asm volatile("fnsave %[fp]; fwait" : [fp] "=m" (fpu->state.fsave));
443
444         return 0;
445 }
446
447 static inline void __copy_kernel_to_fpregs(union fpregs_state *fpstate)
448 {
449         if (use_xsave()) {
450                 copy_kernel_to_xregs(&fpstate->xsave, -1);
451         } else {
452                 if (use_fxsr())
453                         copy_kernel_to_fxregs(&fpstate->fxsave);
454                 else
455                         copy_kernel_to_fregs(&fpstate->fsave);
456         }
457 }
458
459 static inline void copy_kernel_to_fpregs(union fpregs_state *fpstate)
460 {
461         /*
462          * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
463          * pending. Clear the x87 state here by setting it to fixed values.
464          * "m" is a random variable that should be in L1.
465          */
466         if (unlikely(static_cpu_has_bug(X86_BUG_FXSAVE_LEAK))) {
467                 asm volatile(
468                         "fnclex\n\t"
469                         "emms\n\t"
470                         "fildl %P[addr]"        /* set F?P to defined value */
471                         : : [addr] "m" (fpstate));
472         }
473
474         __copy_kernel_to_fpregs(fpstate);
475 }
476
477 extern int copy_fpstate_to_sigframe(void __user *buf, void __user *fp, int size);
478
479 /*
480  * FPU context switch related helper methods:
481  */
482
483 DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
484
485 /*
486  * Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx,
487  * on this CPU.
488  *
489  * This will disable any lazy FPU state restore of the current FPU state,
490  * but if the current thread owns the FPU, it will still be saved by.
491  */
492 static inline void __cpu_disable_lazy_restore(unsigned int cpu)
493 {
494         per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
495 }
496
497 static inline int fpu_want_lazy_restore(struct fpu *fpu, unsigned int cpu)
498 {
499         return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
500 }
501
502
503 /*
504  * Wrap lazy FPU TS handling in a 'hw fpregs activation/deactivation'
505  * idiom, which is then paired with the sw-flag (fpregs_active) later on:
506  */
507
508 static inline void __fpregs_activate_hw(void)
509 {
510         if (!use_eager_fpu())
511                 clts();
512 }
513
514 static inline void __fpregs_deactivate_hw(void)
515 {
516         if (!use_eager_fpu())
517                 stts();
518 }
519
520 /* Must be paired with an 'stts' (fpregs_deactivate_hw()) after! */
521 static inline void __fpregs_deactivate(struct fpu *fpu)
522 {
523         WARN_ON_FPU(!fpu->fpregs_active);
524
525         fpu->fpregs_active = 0;
526         this_cpu_write(fpu_fpregs_owner_ctx, NULL);
527 }
528
529 /* Must be paired with a 'clts' (fpregs_activate_hw()) before! */
530 static inline void __fpregs_activate(struct fpu *fpu)
531 {
532         WARN_ON_FPU(fpu->fpregs_active);
533
534         fpu->fpregs_active = 1;
535         this_cpu_write(fpu_fpregs_owner_ctx, fpu);
536 }
537
538 /*
539  * The question "does this thread have fpu access?"
540  * is slightly racy, since preemption could come in
541  * and revoke it immediately after the test.
542  *
543  * However, even in that very unlikely scenario,
544  * we can just assume we have FPU access - typically
545  * to save the FP state - we'll just take a #NM
546  * fault and get the FPU access back.
547  */
548 static inline int fpregs_active(void)
549 {
550         return current->thread.fpu.fpregs_active;
551 }
552
553 /*
554  * Encapsulate the CR0.TS handling together with the
555  * software flag.
556  *
557  * These generally need preemption protection to work,
558  * do try to avoid using these on their own.
559  */
560 static inline void fpregs_activate(struct fpu *fpu)
561 {
562         __fpregs_activate_hw();
563         __fpregs_activate(fpu);
564 }
565
566 static inline void fpregs_deactivate(struct fpu *fpu)
567 {
568         __fpregs_deactivate(fpu);
569         __fpregs_deactivate_hw();
570 }
571
572 /*
573  * FPU state switching for scheduling.
574  *
575  * This is a two-stage process:
576  *
577  *  - switch_fpu_prepare() saves the old state and
578  *    sets the new state of the CR0.TS bit. This is
579  *    done within the context of the old process.
580  *
581  *  - switch_fpu_finish() restores the new state as
582  *    necessary.
583  */
584 typedef struct { int preload; } fpu_switch_t;
585
586 static inline fpu_switch_t
587 switch_fpu_prepare(struct fpu *old_fpu, struct fpu *new_fpu, int cpu)
588 {
589         fpu_switch_t fpu;
590
591         /*
592          * If the task has used the math, pre-load the FPU on xsave processors
593          * or if the past 5 consecutive context-switches used math.
594          */
595         fpu.preload = static_cpu_has(X86_FEATURE_FPU) &&
596                       new_fpu->fpstate_active &&
597                       (use_eager_fpu() || new_fpu->counter > 5);
598
599         if (old_fpu->fpregs_active) {
600                 if (!copy_fpregs_to_fpstate(old_fpu))
601                         old_fpu->last_cpu = -1;
602                 else
603                         old_fpu->last_cpu = cpu;
604
605                 /* But leave fpu_fpregs_owner_ctx! */
606                 old_fpu->fpregs_active = 0;
607
608                 /* Don't change CR0.TS if we just switch! */
609                 if (fpu.preload) {
610                         new_fpu->counter++;
611                         __fpregs_activate(new_fpu);
612                         prefetch(&new_fpu->state);
613                 } else {
614                         __fpregs_deactivate_hw();
615                 }
616         } else {
617                 old_fpu->counter = 0;
618                 old_fpu->last_cpu = -1;
619                 if (fpu.preload) {
620                         new_fpu->counter++;
621                         if (fpu_want_lazy_restore(new_fpu, cpu))
622                                 fpu.preload = 0;
623                         else
624                                 prefetch(&new_fpu->state);
625                         fpregs_activate(new_fpu);
626                 }
627         }
628         return fpu;
629 }
630
631 /*
632  * Misc helper functions:
633  */
634
635 /*
636  * By the time this gets called, we've already cleared CR0.TS and
637  * given the process the FPU if we are going to preload the FPU
638  * state - all we need to do is to conditionally restore the register
639  * state itself.
640  */
641 static inline void switch_fpu_finish(struct fpu *new_fpu, fpu_switch_t fpu_switch)
642 {
643         if (fpu_switch.preload)
644                 copy_kernel_to_fpregs(&new_fpu->state);
645 }
646
647 /*
648  * Needs to be preemption-safe.
649  *
650  * NOTE! user_fpu_begin() must be used only immediately before restoring
651  * the save state. It does not do any saving/restoring on its own. In
652  * lazy FPU mode, it is just an optimization to avoid a #NM exception,
653  * the task can lose the FPU right after preempt_enable().
654  */
655 static inline void user_fpu_begin(void)
656 {
657         struct fpu *fpu = &current->thread.fpu;
658
659         preempt_disable();
660         if (!fpregs_active())
661                 fpregs_activate(fpu);
662         preempt_enable();
663 }
664
665 /*
666  * MXCSR and XCR definitions:
667  */
668
669 extern unsigned int mxcsr_feature_mask;
670
671 #define XCR_XFEATURE_ENABLED_MASK       0x00000000
672
673 static inline u64 xgetbv(u32 index)
674 {
675         u32 eax, edx;
676
677         asm volatile(".byte 0x0f,0x01,0xd0" /* xgetbv */
678                      : "=a" (eax), "=d" (edx)
679                      : "c" (index));
680         return eax + ((u64)edx << 32);
681 }
682
683 static inline void xsetbv(u32 index, u64 value)
684 {
685         u32 eax = value;
686         u32 edx = value >> 32;
687
688         asm volatile(".byte 0x0f,0x01,0xd1" /* xsetbv */
689                      : : "a" (eax), "d" (edx), "c" (index));
690 }
691
692 #endif /* _ASM_X86_FPU_INTERNAL_H */