Skip to content

Commit 524bb73

Browse files
yyu-intel-comsuryasaimadhu
authored andcommitted
x86/fpu/xstate: Separate user and supervisor xfeatures mask
Before the introduction of XSAVES supervisor states, 'xfeatures_mask' is used at various places to determine XSAVE buffer components and XCR0 bits. It contains only user xstates. To support supervisor xstates, it is necessary to separate user and supervisor xstates: - First, change 'xfeatures_mask' to 'xfeatures_mask_all', which represents the full set of bits that should ever be set in a kernel XSAVE buffer. - Introduce xfeatures_mask_supervisor() and xfeatures_mask_user() to extract relevant xfeatures from xfeatures_mask_all. Co-developed-by: Fenghua Yu <[email protected]> Signed-off-by: Fenghua Yu <[email protected]> Signed-off-by: Yu-cheng Yu <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Reviewed-by: Dave Hansen <[email protected]> Reviewed-by: Tony Luck <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 8ab2280 commit 524bb73

File tree

4 files changed

+67
-37
lines changed

4 files changed

+67
-37
lines changed

arch/x86/include/asm/fpu/internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static inline void fpstate_init_xstate(struct xregs_state *xsave)
9292
* XRSTORS requires these bits set in xcomp_bv, or it will
9393
* trigger #GP:
9494
*/
95-
xsave->header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT | xfeatures_mask;
95+
xsave->header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT | xfeatures_mask_all;
9696
}
9797

9898
static inline void fpstate_init_fxstate(struct fxregs_state *fx)

arch/x86/include/asm/fpu/xstate.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,18 @@
5151
#define REX_PREFIX
5252
#endif
5353

54-
extern u64 xfeatures_mask;
54+
extern u64 xfeatures_mask_all;
55+
56+
static inline u64 xfeatures_mask_supervisor(void)
57+
{
58+
return xfeatures_mask_all & XFEATURE_MASK_SUPERVISOR_SUPPORTED;
59+
}
60+
61+
static inline u64 xfeatures_mask_user(void)
62+
{
63+
return xfeatures_mask_all & XFEATURE_MASK_USER_SUPPORTED;
64+
}
65+
5566
extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
5667

5768
extern void __init update_regset_xstate_info(unsigned int size,

arch/x86/kernel/fpu/signal.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,17 @@ sanitize_restored_xstate(union fpregs_state *state,
252252
*/
253253
static int copy_user_to_fpregs_zeroing(void __user *buf, u64 xbv, int fx_only)
254254
{
255+
u64 init_bv;
256+
255257
if (use_xsave()) {
256258
if (fx_only) {
257-
u64 init_bv = xfeatures_mask & ~XFEATURE_MASK_FPSSE;
259+
init_bv = xfeatures_mask_user() & ~XFEATURE_MASK_FPSSE;
260+
258261
copy_kernel_to_xregs(&init_fpstate.xsave, init_bv);
259262
return copy_user_to_fxregs(buf);
260263
} else {
261-
u64 init_bv = xfeatures_mask & ~xbv;
264+
init_bv = xfeatures_mask_user() & ~xbv;
265+
262266
if (unlikely(init_bv))
263267
copy_kernel_to_xregs(&init_fpstate.xsave, init_bv);
264268
return copy_user_to_xregs(buf, xbv);
@@ -358,7 +362,7 @@ static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size)
358362

359363

360364
if (use_xsave() && !fx_only) {
361-
u64 init_bv = xfeatures_mask & ~xfeatures;
365+
u64 init_bv = xfeatures_mask_user() & ~xfeatures;
362366

363367
if (using_compacted_format()) {
364368
ret = copy_user_to_xstate(&fpu->state.xsave, buf_fx);
@@ -389,7 +393,9 @@ static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size)
389393

390394
fpregs_lock();
391395
if (use_xsave()) {
392-
u64 init_bv = xfeatures_mask & ~XFEATURE_MASK_FPSSE;
396+
u64 init_bv;
397+
398+
init_bv = xfeatures_mask_user() & ~XFEATURE_MASK_FPSSE;
393399
copy_kernel_to_xregs(&init_fpstate.xsave, init_bv);
394400
}
395401

@@ -465,7 +471,7 @@ void fpu__init_prepare_fx_sw_frame(void)
465471

466472
fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
467473
fx_sw_reserved.extended_size = size;
468-
fx_sw_reserved.xfeatures = xfeatures_mask;
474+
fx_sw_reserved.xfeatures = xfeatures_mask_user();
469475
fx_sw_reserved.xstate_size = fpu_user_xstate_size;
470476

471477
if (IS_ENABLED(CONFIG_IA32_EMULATION) ||

arch/x86/kernel/fpu/xstate.c

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ static short xsave_cpuid_features[] __initdata = {
5454
};
5555

5656
/*
57-
* Mask of xstate features supported by the CPU and the kernel:
57+
* This represents the full set of bits that should ever be set in a kernel
58+
* XSAVE buffer, both supervisor and user xstates.
5859
*/
59-
u64 xfeatures_mask __read_mostly;
60+
u64 xfeatures_mask_all __read_mostly;
6061

6162
static unsigned int xstate_offsets[XFEATURE_MAX] = { [ 0 ... XFEATURE_MAX - 1] = -1};
6263
static unsigned int xstate_sizes[XFEATURE_MAX] = { [ 0 ... XFEATURE_MAX - 1] = -1};
@@ -76,7 +77,7 @@ unsigned int fpu_user_xstate_size;
7677
*/
7778
int cpu_has_xfeatures(u64 xfeatures_needed, const char **feature_name)
7879
{
79-
u64 xfeatures_missing = xfeatures_needed & ~xfeatures_mask;
80+
u64 xfeatures_missing = xfeatures_needed & ~xfeatures_mask_all;
8081

8182
if (unlikely(feature_name)) {
8283
long xfeature_idx, max_idx;
@@ -150,7 +151,7 @@ void fpstate_sanitize_xstate(struct fpu *fpu)
150151
* None of the feature bits are in init state. So nothing else
151152
* to do for us, as the memory layout is up to date.
152153
*/
153-
if ((xfeatures & xfeatures_mask) == xfeatures_mask)
154+
if ((xfeatures & xfeatures_mask_all) == xfeatures_mask_all)
154155
return;
155156

156157
/*
@@ -177,7 +178,7 @@ void fpstate_sanitize_xstate(struct fpu *fpu)
177178
* in a special way already:
178179
*/
179180
feature_bit = 0x2;
180-
xfeatures = (xfeatures_mask & ~xfeatures) >> 2;
181+
xfeatures = (xfeatures_mask_user() & ~xfeatures) >> 2;
181182

182183
/*
183184
* Update all the remaining memory layouts according to their
@@ -205,29 +206,38 @@ void fpstate_sanitize_xstate(struct fpu *fpu)
205206
*/
206207
void fpu__init_cpu_xstate(void)
207208
{
208-
if (!boot_cpu_has(X86_FEATURE_XSAVE) || !xfeatures_mask)
209+
u64 unsup_bits;
210+
211+
if (!boot_cpu_has(X86_FEATURE_XSAVE) || !xfeatures_mask_all)
209212
return;
210213
/*
211214
* Unsupported supervisor xstates should not be found in
212215
* the xfeatures mask.
213216
*/
214-
WARN_ONCE((xfeatures_mask & XFEATURE_MASK_SUPERVISOR_UNSUPPORTED),
215-
"x86/fpu: Found unsupported supervisor xstates.\n");
217+
unsup_bits = xfeatures_mask_all & XFEATURE_MASK_SUPERVISOR_UNSUPPORTED;
218+
WARN_ONCE(unsup_bits, "x86/fpu: Found unsupported supervisor xstates: 0x%llx\n",
219+
unsup_bits);
216220

217-
xfeatures_mask &= ~XFEATURE_MASK_SUPERVISOR_UNSUPPORTED;
221+
xfeatures_mask_all &= ~XFEATURE_MASK_SUPERVISOR_UNSUPPORTED;
218222

219223
cr4_set_bits(X86_CR4_OSXSAVE);
220-
xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask);
224+
225+
/*
226+
* XCR_XFEATURE_ENABLED_MASK (aka. XCR0) sets user features
227+
* managed by XSAVE{C, OPT, S} and XRSTOR{S}. Only XSAVE user
228+
* states can be set here.
229+
*/
230+
xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask_user());
221231
}
222232

223233
/*
224234
* Note that in the future we will likely need a pair of
225235
* functions here: one for user xstates and the other for
226236
* system xstates. For now, they are the same.
227237
*/
228-
static int xfeature_enabled(enum xfeature xfeature)
238+
static bool xfeature_enabled(enum xfeature xfeature)
229239
{
230-
return !!(xfeatures_mask & (1UL << xfeature));
240+
return xfeatures_mask_all & BIT_ULL(xfeature);
231241
}
232242

233243
/*
@@ -414,7 +424,7 @@ static void __init setup_init_fpu_buf(void)
414424

415425
if (boot_cpu_has(X86_FEATURE_XSAVES))
416426
init_fpstate.xsave.header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT |
417-
xfeatures_mask;
427+
xfeatures_mask_all;
418428

419429
/*
420430
* Init all the features state with header.xfeatures being 0x0
@@ -474,7 +484,7 @@ int using_compacted_format(void)
474484
int validate_user_xstate_header(const struct xstate_header *hdr)
475485
{
476486
/* No unknown or supervisor features may be set */
477-
if (hdr->xfeatures & ~(xfeatures_mask & XFEATURE_MASK_USER_SUPPORTED))
487+
if (hdr->xfeatures & ~xfeatures_mask_user())
478488
return -EINVAL;
479489

480490
/* Userspace must use the uncompacted format */
@@ -609,7 +619,7 @@ static void do_extra_xstate_size_checks(void)
609619

610620

611621
/*
612-
* Get total size of enabled xstates in XCR0/xfeatures_mask.
622+
* Get total size of enabled xstates in XCR0 | IA32_XSS.
613623
*
614624
* Note the SDM's wording here. "sub-function 0" only enumerates
615625
* the size of the *user* states. If we use it to size a buffer
@@ -699,7 +709,7 @@ static int __init init_xstate_size(void)
699709
*/
700710
static void fpu__init_disable_system_xstate(void)
701711
{
702-
xfeatures_mask = 0;
712+
xfeatures_mask_all = 0;
703713
cr4_clear_bits(X86_CR4_OSXSAVE);
704714
setup_clear_cpu_cap(X86_FEATURE_XSAVE);
705715
}
@@ -734,16 +744,21 @@ void __init fpu__init_system_xstate(void)
734744
return;
735745
}
736746

747+
/*
748+
* Find user xstates supported by the processor.
749+
*/
737750
cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
738-
xfeatures_mask = eax + ((u64)edx << 32);
751+
xfeatures_mask_all = eax + ((u64)edx << 32);
739752

740-
if ((xfeatures_mask & XFEATURE_MASK_FPSSE) != XFEATURE_MASK_FPSSE) {
753+
/* Place supervisor features in xfeatures_mask_all here */
754+
if ((xfeatures_mask_user() & XFEATURE_MASK_FPSSE) != XFEATURE_MASK_FPSSE) {
741755
/*
742756
* This indicates that something really unexpected happened
743757
* with the enumeration. Disable XSAVE and try to continue
744758
* booting without it. This is too early to BUG().
745759
*/
746-
pr_err("x86/fpu: FP/SSE not present amongst the CPU's xstate features: 0x%llx.\n", xfeatures_mask);
760+
pr_err("x86/fpu: FP/SSE not present amongst the CPU's xstate features: 0x%llx.\n",
761+
xfeatures_mask_all);
747762
goto out_disable;
748763
}
749764

@@ -752,10 +767,10 @@ void __init fpu__init_system_xstate(void)
752767
*/
753768
for (i = 0; i < ARRAY_SIZE(xsave_cpuid_features); i++) {
754769
if (!boot_cpu_has(xsave_cpuid_features[i]))
755-
xfeatures_mask &= ~BIT(i);
770+
xfeatures_mask_all &= ~BIT_ULL(i);
756771
}
757772

758-
xfeatures_mask &= fpu__get_supported_xfeatures_mask();
773+
xfeatures_mask_all &= fpu__get_supported_xfeatures_mask();
759774

760775
/* Enable xstate instructions to be able to continue with initialization: */
761776
fpu__init_cpu_xstate();
@@ -767,16 +782,15 @@ void __init fpu__init_system_xstate(void)
767782
* Update info used for ptrace frames; use standard-format size and no
768783
* supervisor xstates:
769784
*/
770-
update_regset_xstate_info(fpu_user_xstate_size,
771-
xfeatures_mask & XFEATURE_MASK_USER_SUPPORTED);
785+
update_regset_xstate_info(fpu_user_xstate_size, xfeatures_mask_user());
772786

773787
fpu__init_prepare_fx_sw_frame();
774788
setup_init_fpu_buf();
775789
setup_xstate_comp_offsets();
776790
print_xstate_offset_size();
777791

778792
pr_info("x86/fpu: Enabled xstate features 0x%llx, context size is %d bytes, using '%s' format.\n",
779-
xfeatures_mask,
793+
xfeatures_mask_all,
780794
fpu_kernel_xstate_size,
781795
boot_cpu_has(X86_FEATURE_XSAVES) ? "compacted" : "standard");
782796
return;
@@ -795,7 +809,7 @@ void fpu__resume_cpu(void)
795809
* Restore XCR0 on xsave capable CPUs:
796810
*/
797811
if (boot_cpu_has(X86_FEATURE_XSAVE))
798-
xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask);
812+
xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask_user());
799813
}
800814

801815
/*
@@ -840,10 +854,9 @@ void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr)
840854

841855
/*
842856
* We should not ever be requesting features that we
843-
* have not enabled. Remember that xfeatures_mask is
844-
* what we write to the XCR0 register.
857+
* have not enabled.
845858
*/
846-
WARN_ONCE(!(xfeatures_mask & BIT_ULL(xfeature_nr)),
859+
WARN_ONCE(!(xfeatures_mask_all & BIT_ULL(xfeature_nr)),
847860
"get of unsupported state");
848861
/*
849862
* This assumes the last 'xsave*' instruction to
@@ -996,7 +1009,7 @@ int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int of
9961009
*/
9971010
memset(&header, 0, sizeof(header));
9981011
header.xfeatures = xsave->header.xfeatures;
999-
header.xfeatures &= XFEATURE_MASK_USER_SUPPORTED;
1012+
header.xfeatures &= xfeatures_mask_user();
10001013

10011014
/*
10021015
* Copy xregs_state->header:
@@ -1080,7 +1093,7 @@ int copy_xstate_to_user(void __user *ubuf, struct xregs_state *xsave, unsigned i
10801093
*/
10811094
memset(&header, 0, sizeof(header));
10821095
header.xfeatures = xsave->header.xfeatures;
1083-
header.xfeatures &= XFEATURE_MASK_USER_SUPPORTED;
1096+
header.xfeatures &= xfeatures_mask_user();
10841097

10851098
/*
10861099
* Copy xregs_state->header:

0 commit comments

Comments
 (0)