2 * xsave/xrstor support.
4 * Author: Suresh Siddha <suresh.b.siddha@intel.com>
6 #include <linux/compat.h>
9 #include <asm/fpu/api.h>
10 #include <asm/fpu/internal.h>
11 #include <asm/fpu/signal.h>
12 #include <asm/fpu/regset.h>
13 #include <asm/sigframe.h>
14 #include <asm/tlbflush.h>
16 static const char *xfeature_names
[] =
18 "x87 floating point registers" ,
21 "MPX bounds registers" ,
26 "unknown xstate feature" ,
30 * Mask of xstate features supported by the CPU and the kernel:
32 u64 xfeatures_mask __read_mostly
;
34 static struct _fpx_sw_bytes fx_sw_reserved
, fx_sw_reserved_ia32
;
35 static unsigned int xstate_offsets
[XFEATURES_NR_MAX
], xstate_sizes
[XFEATURES_NR_MAX
];
36 static unsigned int xstate_comp_offsets
[sizeof(xfeatures_mask
)*8];
38 /* The number of supported xfeatures in xfeatures_mask: */
39 static unsigned int xfeatures_nr
;
42 * Return whether the system supports a given xfeature.
44 * Also return the name of the (most advanced) feature that the caller requested:
46 int cpu_has_xfeatures(u64 xfeatures_needed
, const char **feature_name
)
48 u64 xfeatures_missing
= xfeatures_needed
& ~xfeatures_mask
;
50 if (unlikely(feature_name
)) {
51 long xfeature_idx
, max_idx
;
54 * So we use FLS here to be able to print the most advanced
55 * feature that was requested but is missing. So if a driver
56 * asks about "XSTATE_SSE | XSTATE_YMM" we'll print the
57 * missing AVX feature - this is the most informative message
60 if (xfeatures_missing
)
61 xfeatures_print
= xfeatures_missing
;
63 xfeatures_print
= xfeatures_needed
;
65 xfeature_idx
= fls64(xfeatures_print
)-1;
66 max_idx
= ARRAY_SIZE(xfeature_names
)-1;
67 xfeature_idx
= min(xfeature_idx
, max_idx
);
69 *feature_name
= xfeature_names
[xfeature_idx
];
72 if (xfeatures_missing
)
77 EXPORT_SYMBOL_GPL(cpu_has_xfeatures
);
80 * When executing XSAVEOPT (optimized XSAVE), if a processor implementation
81 * detects that an FPU state component is still (or is again) in its
82 * initialized state, it may clear the corresponding bit in the header.xfeatures
83 * field, and can skip the writeout of registers to the corresponding memory layout.
85 * This means that when the bit is zero, the state component might still contain
86 * some previous - non-initialized register state.
88 * Before writing xstate information to user-space we sanitize those components,
89 * to always ensure that the memory layout of a feature will be in the init state
90 * if the corresponding header bit is zero. This is to ensure that user-space doesn't
91 * see some stale state in the memory layout during signal handling, debugging etc.
93 void fpstate_sanitize_xstate(struct fpu
*fpu
)
95 struct i387_fxsave_struct
*fx
= &fpu
->state
.fxsave
;
102 xfeatures
= fpu
->state
.xsave
.header
.xfeatures
;
105 * None of the feature bits are in init state. So nothing else
106 * to do for us, as the memory layout is up to date.
108 if ((xfeatures
& xfeatures_mask
) == xfeatures_mask
)
112 * FP is in init state
114 if (!(xfeatures
& XSTATE_FP
)) {
121 memset(&fx
->st_space
[0], 0, 128);
125 * SSE is in init state
127 if (!(xfeatures
& XSTATE_SSE
))
128 memset(&fx
->xmm_space
[0], 0, 256);
131 * First two features are FPU and SSE, which above we handled
132 * in a special way already:
135 xfeatures
= (xfeatures_mask
& ~xfeatures
) >> 2;
138 * Update all the remaining memory layouts according to their
139 * standard xstate layout, if their header bit is in the init
143 if (xfeatures
& 0x1) {
144 int offset
= xstate_offsets
[feature_bit
];
145 int size
= xstate_sizes
[feature_bit
];
147 memcpy((void *)fx
+ offset
,
148 (void *)&init_fpstate
.xsave
+ offset
,
158 * Check for the presence of extended state information in the
159 * user fpstate pointer in the sigcontext.
161 static inline int check_for_xstate(struct i387_fxsave_struct __user
*buf
,
162 void __user
*fpstate
,
163 struct _fpx_sw_bytes
*fx_sw
)
165 int min_xstate_size
= sizeof(struct i387_fxsave_struct
) +
166 sizeof(struct xstate_header
);
169 if (__copy_from_user(fx_sw
, &buf
->sw_reserved
[0], sizeof(*fx_sw
)))
172 /* Check for the first magic field and other error scenarios. */
173 if (fx_sw
->magic1
!= FP_XSTATE_MAGIC1
||
174 fx_sw
->xstate_size
< min_xstate_size
||
175 fx_sw
->xstate_size
> xstate_size
||
176 fx_sw
->xstate_size
> fx_sw
->extended_size
)
180 * Check for the presence of second magic word at the end of memory
181 * layout. This detects the case where the user just copied the legacy
182 * fpstate layout with out copying the extended state information
183 * in the memory layout.
185 if (__get_user(magic2
, (__u32 __user
*)(fpstate
+ fx_sw
->xstate_size
))
186 || magic2
!= FP_XSTATE_MAGIC2
)
193 * Signal frame handlers.
195 static inline int save_fsave_header(struct task_struct
*tsk
, void __user
*buf
)
198 struct xsave_struct
*xsave
= &tsk
->thread
.fpu
.state
.xsave
;
199 struct user_i387_ia32_struct env
;
200 struct _fpstate_ia32 __user
*fp
= buf
;
202 convert_from_fxsr(&env
, tsk
);
204 if (__copy_to_user(buf
, &env
, sizeof(env
)) ||
205 __put_user(xsave
->i387
.swd
, &fp
->status
) ||
206 __put_user(X86_FXSR_MAGIC
, &fp
->magic
))
209 struct i387_fsave_struct __user
*fp
= buf
;
211 if (__get_user(swd
, &fp
->swd
) || __put_user(swd
, &fp
->status
))
218 static inline int save_xstate_epilog(void __user
*buf
, int ia32_frame
)
220 struct xsave_struct __user
*x
= buf
;
221 struct _fpx_sw_bytes
*sw_bytes
;
225 /* Setup the bytes not touched by the [f]xsave and reserved for SW. */
226 sw_bytes
= ia32_frame
? &fx_sw_reserved_ia32
: &fx_sw_reserved
;
227 err
= __copy_to_user(&x
->i387
.sw_reserved
, sw_bytes
, sizeof(*sw_bytes
));
232 err
|= __put_user(FP_XSTATE_MAGIC2
, (__u32
*)(buf
+ xstate_size
));
235 * Read the xfeatures which we copied (directly from the cpu or
236 * from the state in task struct) to the user buffers.
238 err
|= __get_user(xfeatures
, (__u32
*)&x
->header
.xfeatures
);
241 * For legacy compatible, we always set FP/SSE bits in the bit
242 * vector while saving the state to the user context. This will
243 * enable us capturing any changes(during sigreturn) to
244 * the FP/SSE bits by the legacy applications which don't touch
245 * xfeatures in the xsave header.
247 * xsave aware apps can change the xfeatures in the xsave
248 * header as well as change any contents in the memory layout.
249 * xrestore as part of sigreturn will capture all the changes.
251 xfeatures
|= XSTATE_FPSSE
;
253 err
|= __put_user(xfeatures
, (__u32
*)&x
->header
.xfeatures
);
258 static inline int copy_fpregs_to_sigframe(struct xsave_struct __user
*buf
)
263 err
= copy_xregs_to_user(buf
);
265 err
= copy_fxregs_to_user((struct i387_fxsave_struct __user
*) buf
);
267 err
= copy_fregs_to_user((struct i387_fsave_struct __user
*) buf
);
269 if (unlikely(err
) && __clear_user(buf
, xstate_size
))
275 * Save the fpu, extended register state to the user signal frame.
277 * 'buf_fx' is the 64-byte aligned pointer at which the [f|fx|x]save
279 * 'buf' points to the 'buf_fx' or to the fsave header followed by 'buf_fx'.
281 * buf == buf_fx for 64-bit frames and 32-bit fsave frame.
282 * buf != buf_fx for 32-bit frames with fxstate.
284 * If the fpu, extended register state is live, save the state directly
285 * to the user frame pointed by the aligned pointer 'buf_fx'. Otherwise,
286 * copy the thread's fpu state to the user frame starting at 'buf_fx'.
288 * If this is a 32-bit frame with fxstate, put a fsave header before
289 * the aligned state at 'buf_fx'.
291 * For [f]xsave state, update the SW reserved fields in the [f]xsave frame
292 * indicating the absence/presence of the extended state to the user.
294 int copy_fpstate_to_sigframe(void __user
*buf
, void __user
*buf_fx
, int size
)
296 struct xsave_struct
*xsave
= ¤t
->thread
.fpu
.state
.xsave
;
297 struct task_struct
*tsk
= current
;
298 int ia32_fxstate
= (buf
!= buf_fx
);
300 ia32_fxstate
&= (config_enabled(CONFIG_X86_32
) ||
301 config_enabled(CONFIG_IA32_EMULATION
));
303 if (!access_ok(VERIFY_WRITE
, buf
, size
))
306 if (!static_cpu_has(X86_FEATURE_FPU
))
307 return fpregs_soft_get(current
, NULL
, 0,
308 sizeof(struct user_i387_ia32_struct
), NULL
,
309 (struct _fpstate_ia32 __user
*) buf
) ? -1 : 1;
311 if (fpregs_active()) {
312 /* Save the live register state to the user directly. */
313 if (copy_fpregs_to_sigframe(buf_fx
))
315 /* Update the thread's fxstate to save the fsave header. */
317 copy_fxregs_to_kernel(&tsk
->thread
.fpu
);
319 fpstate_sanitize_xstate(&tsk
->thread
.fpu
);
320 if (__copy_to_user(buf_fx
, xsave
, xstate_size
))
324 /* Save the fsave header for the 32-bit frames. */
325 if ((ia32_fxstate
|| !use_fxsr()) && save_fsave_header(tsk
, buf
))
328 if (use_fxsr() && save_xstate_epilog(buf_fx
, ia32_fxstate
))
335 sanitize_restored_xstate(struct task_struct
*tsk
,
336 struct user_i387_ia32_struct
*ia32_env
,
337 u64 xfeatures
, int fx_only
)
339 struct xsave_struct
*xsave
= &tsk
->thread
.fpu
.state
.xsave
;
340 struct xstate_header
*header
= &xsave
->header
;
343 /* These bits must be zero. */
344 memset(header
->reserved
, 0, 48);
347 * Init the state that is not present in the memory
348 * layout and not enabled by the OS.
351 header
->xfeatures
= XSTATE_FPSSE
;
353 header
->xfeatures
&= (xfeatures_mask
& xfeatures
);
358 * mscsr reserved bits must be masked to zero for security
361 xsave
->i387
.mxcsr
&= mxcsr_feature_mask
;
363 convert_to_fxsr(tsk
, ia32_env
);
368 * Restore the extended state if present. Otherwise, restore the FP/SSE state.
370 static inline int copy_user_to_fpregs_zeroing(void __user
*buf
, u64 xbv
, int fx_only
)
373 if ((unsigned long)buf
% 64 || fx_only
) {
374 u64 init_bv
= xfeatures_mask
& ~XSTATE_FPSSE
;
375 copy_kernel_to_xregs(&init_fpstate
.xsave
, init_bv
);
376 return copy_user_to_fxregs(buf
);
378 u64 init_bv
= xfeatures_mask
& ~xbv
;
379 if (unlikely(init_bv
))
380 copy_kernel_to_xregs(&init_fpstate
.xsave
, init_bv
);
381 return copy_user_to_xregs(buf
, xbv
);
383 } else if (use_fxsr()) {
384 return copy_user_to_fxregs(buf
);
386 return copy_user_to_fregs(buf
);
389 static int __fpu__restore_sig(void __user
*buf
, void __user
*buf_fx
, int size
)
391 int ia32_fxstate
= (buf
!= buf_fx
);
392 struct task_struct
*tsk
= current
;
393 struct fpu
*fpu
= &tsk
->thread
.fpu
;
394 int state_size
= xstate_size
;
398 ia32_fxstate
&= (config_enabled(CONFIG_X86_32
) ||
399 config_enabled(CONFIG_IA32_EMULATION
));
406 if (!access_ok(VERIFY_READ
, buf
, size
))
409 fpu__activate_curr(fpu
);
411 if (!static_cpu_has(X86_FEATURE_FPU
))
412 return fpregs_soft_set(current
, NULL
,
413 0, sizeof(struct user_i387_ia32_struct
),
417 struct _fpx_sw_bytes fx_sw_user
;
418 if (unlikely(check_for_xstate(buf_fx
, buf_fx
, &fx_sw_user
))) {
420 * Couldn't find the extended state information in the
421 * memory layout. Restore just the FP/SSE and init all
422 * the other extended state.
424 state_size
= sizeof(struct i387_fxsave_struct
);
427 state_size
= fx_sw_user
.xstate_size
;
428 xfeatures
= fx_sw_user
.xfeatures
;
434 * For 32-bit frames with fxstate, copy the user state to the
435 * thread's fpu state, reconstruct fxstate from the fsave
436 * header. Sanitize the copied state etc.
438 struct fpu
*fpu
= &tsk
->thread
.fpu
;
439 struct user_i387_ia32_struct env
;
443 * Drop the current fpu which clears fpu->fpstate_active. This ensures
444 * that any context-switch during the copy of the new state,
445 * avoids the intermediate state from getting restored/saved.
446 * Thus avoiding the new restored state from getting corrupted.
447 * We will be ready to restore/save the state only after
448 * fpu->fpstate_active is again set.
452 if (__copy_from_user(&fpu
->state
.xsave
, buf_fx
, state_size
) ||
453 __copy_from_user(&env
, buf
, sizeof(env
))) {
454 fpstate_init(&fpu
->state
);
457 sanitize_restored_xstate(tsk
, &env
, xfeatures
, fx_only
);
460 fpu
->fpstate_active
= 1;
461 if (use_eager_fpu()) {
470 * For 64-bit frames and 32-bit fsave frames, restore the user
471 * state to the registers directly (with exceptions handled).
474 if (copy_user_to_fpregs_zeroing(buf_fx
, xfeatures
, fx_only
)) {
483 static inline int xstate_sigframe_size(void)
485 return use_xsave() ? xstate_size
+ FP_XSTATE_MAGIC2_SIZE
: xstate_size
;
489 * Restore FPU state from a sigframe:
491 int fpu__restore_sig(void __user
*buf
, int ia32_frame
)
493 void __user
*buf_fx
= buf
;
494 int size
= xstate_sigframe_size();
496 if (ia32_frame
&& use_fxsr()) {
497 buf_fx
= buf
+ sizeof(struct i387_fsave_struct
);
498 size
+= sizeof(struct i387_fsave_struct
);
501 return __fpu__restore_sig(buf
, buf_fx
, size
);
505 fpu__alloc_mathframe(unsigned long sp
, int ia32_frame
,
506 unsigned long *buf_fx
, unsigned long *size
)
508 unsigned long frame_size
= xstate_sigframe_size();
510 *buf_fx
= sp
= round_down(sp
- frame_size
, 64);
511 if (ia32_frame
&& use_fxsr()) {
512 frame_size
+= sizeof(struct i387_fsave_struct
);
513 sp
-= sizeof(struct i387_fsave_struct
);
521 * Prepare the SW reserved portion of the fxsave memory layout, indicating
522 * the presence of the extended state information in the memory layout
523 * pointed by the fpstate pointer in the sigcontext.
524 * This will be saved when ever the FP and extended state context is
525 * saved on the user stack during the signal handler delivery to the user.
527 static void prepare_fx_sw_frame(void)
529 int fsave_header_size
= sizeof(struct i387_fsave_struct
);
530 int size
= xstate_size
+ FP_XSTATE_MAGIC2_SIZE
;
532 if (config_enabled(CONFIG_X86_32
))
533 size
+= fsave_header_size
;
535 fx_sw_reserved
.magic1
= FP_XSTATE_MAGIC1
;
536 fx_sw_reserved
.extended_size
= size
;
537 fx_sw_reserved
.xfeatures
= xfeatures_mask
;
538 fx_sw_reserved
.xstate_size
= xstate_size
;
540 if (config_enabled(CONFIG_IA32_EMULATION
)) {
541 fx_sw_reserved_ia32
= fx_sw_reserved
;
542 fx_sw_reserved_ia32
.extended_size
+= fsave_header_size
;
547 * Enable the extended processor state save/restore feature.
548 * Called once per CPU onlining.
550 void fpu__init_cpu_xstate(void)
552 if (!cpu_has_xsave
|| !xfeatures_mask
)
555 cr4_set_bits(X86_CR4_OSXSAVE
);
556 xsetbv(XCR_XFEATURE_ENABLED_MASK
, xfeatures_mask
);
560 * Record the offsets and sizes of different state managed by the xsave
563 static void __init
setup_xstate_features(void)
565 int eax
, ebx
, ecx
, edx
, leaf
= 0x2;
567 xfeatures_nr
= fls64(xfeatures_mask
);
570 cpuid_count(XSTATE_CPUID
, leaf
, &eax
, &ebx
, &ecx
, &edx
);
575 xstate_offsets
[leaf
] = ebx
;
576 xstate_sizes
[leaf
] = eax
;
582 static void print_xstate_feature(u64 xstate_mask
)
584 const char *feature_name
;
586 if (cpu_has_xfeatures(xstate_mask
, &feature_name
))
587 pr_info("x86/fpu: Supporting XSAVE feature 0x%02Lx: '%s'\n", xstate_mask
, feature_name
);
591 * Print out all the supported xstate features:
593 static void print_xstate_features(void)
595 print_xstate_feature(XSTATE_FP
);
596 print_xstate_feature(XSTATE_SSE
);
597 print_xstate_feature(XSTATE_YMM
);
598 print_xstate_feature(XSTATE_BNDREGS
);
599 print_xstate_feature(XSTATE_BNDCSR
);
600 print_xstate_feature(XSTATE_OPMASK
);
601 print_xstate_feature(XSTATE_ZMM_Hi256
);
602 print_xstate_feature(XSTATE_Hi16_ZMM
);
606 * This function sets up offsets and sizes of all extended states in
607 * xsave area. This supports both standard format and compacted format
608 * of the xsave aread.
613 void setup_xstate_comp(void)
615 unsigned int xstate_comp_sizes
[sizeof(xfeatures_mask
)*8];
619 * The FP xstates and SSE xstates are legacy states. They are always
620 * in the fixed offsets in the xsave area in either compacted form
623 xstate_comp_offsets
[0] = 0;
624 xstate_comp_offsets
[1] = offsetof(struct i387_fxsave_struct
, xmm_space
);
626 if (!cpu_has_xsaves
) {
627 for (i
= 2; i
< xfeatures_nr
; i
++) {
628 if (test_bit(i
, (unsigned long *)&xfeatures_mask
)) {
629 xstate_comp_offsets
[i
] = xstate_offsets
[i
];
630 xstate_comp_sizes
[i
] = xstate_sizes
[i
];
636 xstate_comp_offsets
[2] = FXSAVE_SIZE
+ XSAVE_HDR_SIZE
;
638 for (i
= 2; i
< xfeatures_nr
; i
++) {
639 if (test_bit(i
, (unsigned long *)&xfeatures_mask
))
640 xstate_comp_sizes
[i
] = xstate_sizes
[i
];
642 xstate_comp_sizes
[i
] = 0;
645 xstate_comp_offsets
[i
] = xstate_comp_offsets
[i
-1]
646 + xstate_comp_sizes
[i
-1];
652 * setup the xstate image representing the init state
654 static void setup_init_fpu_buf(void)
659 setup_xstate_features();
660 print_xstate_features();
662 if (cpu_has_xsaves
) {
663 init_fpstate
.xsave
.header
.xcomp_bv
= (u64
)1 << 63 | xfeatures_mask
;
664 init_fpstate
.xsave
.header
.xfeatures
= xfeatures_mask
;
668 * Init all the features state with header_bv being 0x0
670 copy_kernel_to_xregs_booting(&init_fpstate
.xsave
, -1);
673 * Dump the init state again. This is to identify the init state
674 * of any feature which is not represented by all zero's.
676 copy_xregs_to_kernel_booting(&init_fpstate
.xsave
);
680 * Calculate total size of enabled xstates in XCR0/xfeatures_mask.
682 static void __init
init_xstate_size(void)
684 unsigned int eax
, ebx
, ecx
, edx
;
687 if (!cpu_has_xsaves
) {
688 cpuid_count(XSTATE_CPUID
, 0, &eax
, &ebx
, &ecx
, &edx
);
693 xstate_size
= FXSAVE_SIZE
+ XSAVE_HDR_SIZE
;
694 for (i
= 2; i
< 64; i
++) {
695 if (test_bit(i
, (unsigned long *)&xfeatures_mask
)) {
696 cpuid_count(XSTATE_CPUID
, i
, &eax
, &ebx
, &ecx
, &edx
);
703 * Enable and initialize the xsave feature.
704 * Called once per system bootup.
706 * ( Not marked __init because of false positive section warnings. )
708 void fpu__init_system_xstate(void)
710 unsigned int eax
, ebx
, ecx
, edx
;
712 if (!cpu_has_xsave
) {
713 pr_info("x86/fpu: Legacy x87 FPU detected.\n");
717 if (boot_cpu_data
.cpuid_level
< XSTATE_CPUID
) {
718 WARN(1, "x86/fpu: XSTATE_CPUID missing!\n");
722 cpuid_count(XSTATE_CPUID
, 0, &eax
, &ebx
, &ecx
, &edx
);
723 xfeatures_mask
= eax
+ ((u64
)edx
<< 32);
725 if ((xfeatures_mask
& XSTATE_FPSSE
) != XSTATE_FPSSE
) {
726 pr_err("x86/fpu: FP/SSE not present amongst the CPU's xstate features: 0x%llx.\n", xfeatures_mask
);
731 * Support only the state known to OS.
733 xfeatures_mask
= xfeatures_mask
& XCNTXT_MASK
;
735 /* Enable xstate instructions to be able to continue with initialization: */
736 fpu__init_cpu_xstate();
739 * Recompute the context size for enabled features
743 update_regset_xstate_info(xstate_size
, xfeatures_mask
);
744 prepare_fx_sw_frame();
745 setup_init_fpu_buf();
747 pr_info("x86/fpu: Enabled xstate features 0x%llx, context size is 0x%x bytes, using '%s' format.\n",
750 cpu_has_xsaves
? "compacted" : "standard");
754 * Restore minimal FPU state after suspend:
756 void fpu__resume_cpu(void)
759 * Restore XCR0 on xsave capable CPUs:
762 xsetbv(XCR_XFEATURE_ENABLED_MASK
, xfeatures_mask
);
766 * Given the xsave area and a state inside, this function returns the
767 * address of the state.
769 * This is the API that is called to get xstate address in either
770 * standard format or compacted format of xsave area.
773 * xsave: base address of the xsave area;
774 * xstate: state which is defined in xsave.h (e.g. XSTATE_FP, XSTATE_SSE,
777 * address of the state in the xsave area.
779 void *get_xsave_addr(struct xsave_struct
*xsave
, int xstate
)
781 int feature
= fls64(xstate
) - 1;
782 if (!test_bit(feature
, (unsigned long *)&xfeatures_mask
))
785 return (void *)xsave
+ xstate_comp_offsets
[feature
];
787 EXPORT_SYMBOL_GPL(get_xsave_addr
);