x86, fpu: XSAVE cleanups and sanity checks
cover x86, fpu: XSAVE cleanups and sanity checks
These patches make some updates to the x86 XSAVE code.
There are basically 5 things going on here:
* removal of the LWP (lightweight profiling) code
* naming and type cleanups
* removal of xfeatures_nr variable
* addition of AVX-512 C structures
* new sanity checks of XSAVE buffer sizing
PATCH 1/11 x86, fpu: kill LWP support
From: Dave Hansen <[email protected]>
LightWeight Profiling was evidently an AMD profiling feature that we never got around to implementing. Remove the references to it.
This patch is a *bit* worrisome becuase it will implicitly cause 'struct xregs_state' to shrink. This effectively removes some unused padding that we had in there. It might expose other bugs.
---
b/arch/x86/include/asm/fpu/types.h | 6 ------ 1 file changed, 6 deletions(-)
arch/x86/include/asm/fpu/types.h renamed from arch/x86/include/asm/fpu/types.h~axe-lwp +0 -6
@@ -132,11 +132,6 @@ struct ymmh_struct {u8 ymmh_space[256 ]; }; -/* We don't support LWP yet: */ -struct lwp_struct { -u8 reserved[128 ]; -}; -/* Intel MPX support: */ struct bndreg {u64 lower_bound;
@@ -161,7 +156,6 @@ struct xstate_header {/* New processor state extensions should be added here: */ #define XSTATE_RESERVE (sizeof(struct ymmh_struct) + \ - sizeof(struct lwp_struct) + \ sizeof(struct mpx_struct) )/* * This is our most modern FPU state format, as saved by the XSAVE
_
PATCH 2/11 x86, fpu: rename xfeature_bit
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
The 'xfeature_bit's are at best xfeature bit _numbers_. Calling them bits is at best inconsistent with ending the enum list with 'XFEATURES_NR_MAX'.
This patch renames the enum to be 'xfeature_nr'. These also happen to be what the Intel documentation calls a "state component".
---
b/arch/x86/include/asm/fpu/types.h | 38 ++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-)
arch/x86/include/asm/fpu/types.h renamed from arch/x86/include/asm/fpu/types.h~x86-fpu-rename-xfeature_bit +21 -17
@@ -95,27 +95,31 @@ struct swregs_state {/* * List of XSAVE features Linux knows about: */ -enum xfeature_bit { -XSTATE_BIT_FP , -XSTATE_BIT_SSE , -XSTATE_BIT_YMM , -XSTATE_BIT_BNDREGS , -XSTATE_BIT_BNDCSR , -XSTATE_BIT_OPMASK , - XSTATE_BIT_ZMM_Hi256, - XSTATE_BIT_Hi16_ZMM, +enum xfeature_nr { +XFEATURE_NR_FP , +XFEATURE_NR_SSE , +/* +* Values above here are "legacy states". +* Those below are "extended states". +*/ +XFEATURE_NR_YMM , +XFEATURE_NR_BNDREGS , +XFEATURE_NR_BNDCSR , +XFEATURE_NR_OPMASK , + XFEATURE_NR_ZMM_Hi256, + XFEATURE_NR_Hi16_ZMM,XFEATURES_NR_MAX , }; -#define XSTATE_FP (1 << XSTATE_BIT_FP) -#define XSTATE_SSE (1 << XSTATE_BIT_SSE) -#define XSTATE_YMM (1 << XSTATE_BIT_YMM) -#define XSTATE_BNDREGS (1 << XSTATE_BIT_BNDREGS) -#define XSTATE_BNDCSR (1 << XSTATE_BIT_BNDCSR) -#define XSTATE_OPMASK (1 << XSTATE_BIT_OPMASK) -#define XSTATE_ZMM_Hi256 (1 << XSTATE_BIT_ZMM_Hi256) -#define XSTATE_Hi16_ZMM (1 << XSTATE_BIT_Hi16_ZMM) +#define XSTATE_FP (1 << XFEATURE_NR_FP) +#define XSTATE_SSE (1 << XFEATURE_NR_SSE) +#define XSTATE_YMM (1 << XFEATURE_NR_YMM) +#define XSTATE_BNDREGS (1 << XFEATURE_NR_BNDREGS) +#define XSTATE_BNDCSR (1 << XFEATURE_NR_BNDCSR) +#define XSTATE_OPMASK (1 << XFEATURE_NR_OPMASK) +#define XSTATE_ZMM_Hi256 (1 << XFEATURE_NR_ZMM_Hi256) +#define XSTATE_Hi16_ZMM (1 << XFEATURE_NR_Hi16_ZMM)#define XSTATE_FPSSE (XSTATE_FP | XSTATE_SSE)#define XSTATE_AVX512 (XSTATE_OPMASK | XSTATE_ZMM_Hi256 | XSTATE_Hi16_ZMM)
_
Borislav Petkov
maintainerRe: [PATCH 02/11] x86, fpu: rename xfeature_bit
Quoted 42 lines · Dave Hansen
From: Dave Hansen <[email protected]>
The 'xfeature_bit's are at best xfeature bit _numbers_. Calling them bits is at best inconsistent with ending the enum list with '
XFEATURES_NR_MAX'.This patch renames the enum to be '
xfeature_nr'. These also happen to be what the Intel documentation calls a "state component".Signed-off-by Dave Hansen <[email protected]>Cc Ingo Molnar <[email protected]>Cc Borislav Petkov <[email protected]>Cc Fenghua Yu <[email protected]>Cc Tim Chen <[email protected]>---
b/arch/x86/include/asm/fpu/types.h | 38 ++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-)diff -puN
arch/x86/include/asm/fpu/types.h~x86-fpu-rename-xfeature_bitarch/x86/include/asm/fpu/types.harch/x86/include/asm/fpu/types.h renamed from arch/x86/include/asm/fpu/types.h~x86-fpu-rename-xfeature_bit +3
-9@@ -95,27 +95,31 @@ struct swregs_state {/* * List of XSAVE features Linux knows about: */ -enum xfeature_bit { -XSTATE_BIT_FP , -XSTATE_BIT_SSE , -XSTATE_BIT_YMM , -XSTATE_BIT_BNDREGS , -XSTATE_BIT_BNDCSR , -XSTATE_BIT_OPMASK , - XSTATE_BIT_ZMM_Hi256, - XSTATE_BIT_Hi16_ZMM, +enum xfeature_nr { +XFEATURE_NR_FP , +XFEATURE_NR_SSE ,
arch/x86/include/asm/fpu/types.h struct swregs_state { · line 110
Quoted 10 lines
arch/x86/include/asm/fpu/types.h renamed from arch/x86/include/asm/fpu/types.h~x86-fpu-rename-xfeature_bit +10
-0+/* +* Values above here are "legacy states". +* Those below are "extended states". +*/ +XFEATURE_NR_YMM , +XFEATURE_NR_BNDREGS , +XFEATURE_NR_BNDCSR , +XFEATURE_NR_OPMASK , + XFEATURE_NR_ZMM_Hi256, + XFEATURE_NR_Hi16_ZMM,
Why not simply
s/NR_//
?
I.e., XFEATURE_FP, XFEATURE_SSE and so on...
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Re: [PATCH 02/11] x86, fpu: rename xfeature_bit
Quoted 24 lines · Borislav Petkov
Quoted 14 lines
Quoted 14 lines
arch/x86/include/asm/fpu/types.h renamed from arch/x86/include/asm/fpu/types.h~x86-fpu-rename-xfeature_bit +5
-9-enum xfeature_bit { -XSTATE_BIT_FP , -XSTATE_BIT_SSE , -XSTATE_BIT_YMM , -XSTATE_BIT_BNDREGS , -XSTATE_BIT_BNDCSR , -XSTATE_BIT_OPMASK , - XSTATE_BIT_ZMM_Hi256, - XSTATE_BIT_Hi16_ZMM, +enum xfeature_nr { +XFEATURE_NR_FP , +XFEATURE_NR_SSE , +/* +* Values above here are "legacy states". Quoted 8 lines
arch/x86/include/asm/fpu/types.h struct swregs_state { · line 110
Quoted 8 lines
arch/x86/include/asm/fpu/types.h renamed from arch/x86/include/asm/fpu/types.h~x86-fpu-rename-xfeature_bit +8
-0+* Those below are "extended states". +*/ +XFEATURE_NR_YMM , +XFEATURE_NR_BNDREGS , +XFEATURE_NR_BNDCSR , +XFEATURE_NR_OPMASK , + XFEATURE_NR_ZMM_Hi256, + XFEATURE_NR_Hi16_ZMM,Why not simply
Quoted 5 lines
s/NR_//
?
I.e.,
XFEATURE_FP,XFEATURE_SSEand so on...
I wanted to differentiate them from the
#define XSTATE_FP (1 << XSTATE_BIT_FP)
#define XSTATE_SSE (1 << XSTATE_BIT_SSE)
defines below. Those are really XFEATURE_MASK_... I think this gets a lots more clear if you have a pair of #defines like:
XFEATURE_NR_BNDREGS
XFEATURE_MASK_BNDREGS
It's pretty obvious what's going on there. What we have now is
XSTATE_BIT_BNDREGS
XSTATE_BNDREGS
which I find much less clear.
PATCH 3/11 x86, fpu: rework XSTATE_* macros to remove magic '2'
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
The 'xstate.c' code has a bunch of references to '2'. This is because we have a lot more work to do for the "extended" xstates than the "legacy" ones and state component 2 is the first "extended" state.
This patch replaces all of the instances of '2' with FIRST_EXTENDED_XFEATURE_NR, which clearly explains what is going on.
---
b/arch/x86/include/asm/fpu/types.h | 2 ++ b/arch/x86/kernel/fpu/xstate.c | 13 +++++++------ 2 files changed, 9 insertions(+), 6 deletions(-)
arch/x86/include/asm/fpu/types.h renamed from arch/x86/include/asm/fpu/types.h~remove-hard-coded-values +2 -0
@@ -124,6 +124,8 @@ enum xfeature_nr {#define XSTATE_FPSSE (XSTATE_FP | XSTATE_SSE)#define XSTATE_AVX512 (XSTATE_OPMASK | XSTATE_ZMM_Hi256 | XSTATE_Hi16_ZMM) +#define FIRST_EXTENDED_XFEATURE_NR XFEATURE_NR_YMM +/* * There are 16x 256-bit AVX registers named YMM0-YMM15. * The low 128 bits are aliased to the 16 SSE registers (XMM0-XMM15)
arch/x86/kernel/fpu/xstate.c renamed from arch/x86/kernel/fpu/xstate.c~remove-hard-coded-values +7 -6
@@ -181,7 +181,7 @@ static void __init setup_xstate_features xfeatures_nr =fls64 (xfeatures_mask); -for (leaf =2 ; leaf < xfeatures_nr; leaf++) { +for (leaf =FIRST_EXTENDED_XFEATURE_NR ; leaf < xfeatures_nr; leaf++) {cpuid_count (XSTATE_CPUID , leaf, &eax, &ebx, &ecx, &edx); xstate_offsets[leaf] = ebx;
@@ -233,7 +233,7 @@ static void __init setup_xstate_comp(voi xstate_comp_offsets[1 ] = offsetof(struct fxregs_state , xmm_space);if (!cpu_has_xsaves) { -for (i =2 ; i < xfeatures_nr; i++) { +for (i =FIRST_EXTENDED_XFEATURE_NR ; i < xfeatures_nr; i++) {if (test_bit (i, (unsigned long *)&xfeatures_mask)) { xstate_comp_offsets[i] = xstate_offsets[i]; xstate_comp_sizes[i] = xstate_sizes[i];
@@ -242,15 +242,16 @@ static void __init setup_xstate_comp(voireturn ; } - xstate_comp_offsets[2 ] =FXSAVE_SIZE +XSAVE_HDR_SIZE ; + xstate_comp_offsets[FIRST_EXTENDED_XFEATURE_NR ] = +FXSAVE_SIZE +XSAVE_HDR_SIZE ; -for (i =2 ; i < xfeatures_nr; i++) { +for (i =FIRST_EXTENDED_XFEATURE_NR ; i < xfeatures_nr; i++) {if (test_bit (i, (unsigned long *)&xfeatures_mask)) xstate_comp_sizes[i] = xstate_sizes[i];else xstate_comp_sizes[i] =0 ; -if (i >2 ) +if (i >FIRST_EXTENDED_XFEATURE_NR ) xstate_comp_offsets[i] = xstate_comp_offsets[i-1 ] + xstate_comp_sizes[i-1 ];
@@ -305,7 +306,7 @@ static void __init init_xstate_size(void } xstate_size =FXSAVE_SIZE +XSAVE_HDR_SIZE ; -for (i =2 ; i <64 ; i++) { +for (i =FIRST_EXTENDED_XFEATURE_NR ; i <64 ; i++) {if (test_bit (i, (unsigned long *)&xfeatures_mask)) {cpuid_count (XSTATE_CPUID , i, &eax, &ebx, &ecx, &edx); xstate_size += eax;
_
PATCH 4/11 x86, fpu: remove xfeature_nr
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
xfeature_nr ended up being initialized too late for me to use it in the "xsave size sanity check" patch which is later in the series. I tried to move around its initialization but realized that it was just as easy to get rid of it.
We only have 9 XFEATURES. Instead of dynamically calculating and storing the last feature, just use the compile-time max:XFEATURES_NR_MAX. Note that even with 'xfeatures_nr' we can had "holes" in the xfeatures_mask that we had to deal with.
We also change a 'leaf' variable to be a plain 'i'. Although it is used to grab a cpuid leaf in this one loop, all of the other loops just use an 'i' and I find it much more obvious to keep the naming consistent across all the similar loops.
---
b/arch/x86/kernel/fpu/xstate.c | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-)
arch/x86/kernel/fpu/xstate.c renamed from arch/x86/kernel/fpu/xstate.c~kill-xfeatures_nr +8 -16
@@ -35,9 +35,6 @@ static unsigned int xstate_offsets[XFEATstatic unsigned int xstate_sizes[XFEATURES_NR_MAX ] = { [0 ...XFEATURES_NR_MAX -1 ] =-1 };static unsigned int xstate_comp_offsets[sizeof (xfeatures_mask)*8 ]; -/* The number of supported xfeatures in xfeatures_mask: */ -static unsigned int xfeatures_nr; -/* * Return whether the system supports a given xfeature. *
@@ -171,23 +168,18 @@ void fpu__init_cpu_xstate(void)/* * Record the offsets and sizes of various xstates contained * in the XSAVE state memory layout. -* -* ( Note that certain features might be non-present, for them -* we'll have 0 offset and 0 size. ) */ static void __init setup_xstate_features (void ) { -u32 eax, ebx, ecx, edx, leaf; - - xfeatures_nr =fls64 (xfeatures_mask); +u32 eax, ebx, ecx, edx, i; -for (leaf =FIRST_EXTENDED_XFEATURE_NR ; leaf < xfeatures_nr; leaf++) { -cpuid_count (XSTATE_CPUID , leaf, &eax, &ebx, &ecx, &edx); +for (i =FIRST_EXTENDED_XFEATURE_NR ; i <XFEATURES_NR_MAX ; i++) { +cpuid_count (XSTATE_CPUID , i, &eax, &ebx, &ecx, &edx); - xstate_offsets[leaf] = ebx; - xstate_sizes[leaf] = eax; + xstate_offsets[i] = ebx; + xstate_sizes[i] = eax; -printk (KERN_INFO "x86/fpu: xstate_offset[%d]: %04x, xstate_sizes[%d]: %04x\n" , leaf, ebx, leaf, eax); +printk (KERN_INFO "x86/fpu: xstate_offset[%d]: %04x, xstate_sizes[%d]: %04x\n" , i, ebx, i, eax); } }
@@ -233,7 +225,7 @@ static void __init setup_xstate_comp(voi xstate_comp_offsets[1 ] = offsetof(struct fxregs_state , xmm_space);if (!cpu_has_xsaves) { -for (i =FIRST_EXTENDED_XFEATURE_NR ; i < xfeatures_nr; i++) { +for (i =FIRST_EXTENDED_XFEATURE_NR ; i <XFEATURES_NR_MAX ; i++) {if (test_bit (i, (unsigned long *)&xfeatures_mask)) { xstate_comp_offsets[i] = xstate_offsets[i]; xstate_comp_sizes[i] = xstate_sizes[i];
@@ -245,7 +237,7 @@ static void __init setup_xstate_comp(voi xstate_comp_offsets[FIRST_EXTENDED_XFEATURE_NR ] =FXSAVE_SIZE +XSAVE_HDR_SIZE ; -for (i =FIRST_EXTENDED_XFEATURE_NR ; i < xfeatures_nr; i++) { +for (i =FIRST_EXTENDED_XFEATURE_NR ; i <XFEATURES_NR_MAX ; i++) {if (test_bit (i, (unsigned long *)&xfeatures_mask)) xstate_comp_sizes[i] = xstate_sizes[i];else
_
PATCH 5/11 x86, fpu: add helper xfeature_nr_enabled() instead of test_bit()
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
We currently use test_bit() in a few places to see if an xfeature is enabled. It ends up being a bit ugly because 'xfeatures_mask' is a u64 and test_bit wants an 'unsigned long' so it requires a cast. The *_bit() functions are also techincally atomic, which we have no need for here.
So, remove the test_bit()s and replace with the new xfeature_nr_enabled() helper.
This also provides a central place to add a comment about the future need to support 'system xstates'.
---
b/arch/x86/kernel/fpu/xstate.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
arch/x86/kernel/fpu/xstate.c renamed from arch/x86/kernel/fpu/xstate.c~use-xfeature_enabled +13 -3
@@ -207,6 +207,16 @@ static void __init print_xstate_features }/* +* Note that in the future we will likely need a pair of +* functions here: one for user xstates and the other for +* system xstates. For now, they are the same. +*/ +static int xfeature_nr_enabled (enum xfeature_nr xfeature_nr) +{ +return !!(xfeatures_mask & (1UL << xfeature_nr)); +} + +/* * This function sets up offsets and sizes of all extended states in * xsave area. This supports both standard format and compacted format * of the xsave aread.
@@ -226,7 +236,7 @@ static void __init setup_xstate_comp(voiif (!cpu_has_xsaves) {for (i =FIRST_EXTENDED_XFEATURE_NR ; i <XFEATURES_NR_MAX ; i++) { -if (test_bit (i, (unsigned long *)&xfeatures_mask)) { +if (xfeature_nr_enabled (i)) { xstate_comp_offsets[i] = xstate_offsets[i]; xstate_comp_sizes[i] = xstate_sizes[i]; }
@@ -238,7 +248,7 @@ static void __init setup_xstate_comp(voiFXSAVE_SIZE +XSAVE_HDR_SIZE ;for (i =FIRST_EXTENDED_XFEATURE_NR ; i <XFEATURES_NR_MAX ; i++) { -if (test_bit (i, (unsigned long *)&xfeatures_mask)) +if (xfeature_nr_enabled (i)) xstate_comp_sizes[i] = xstate_sizes[i];else xstate_comp_sizes[i] =0 ;
@@ -299,7 +309,7 @@ static void __init init_xstate_size(void xstate_size =FXSAVE_SIZE +XSAVE_HDR_SIZE ;for (i =FIRST_EXTENDED_XFEATURE_NR ; i <64 ; i++) { -if (test_bit (i, (unsigned long *)&xfeatures_mask)) { +if (xfeature_nr_enabled (i)) {cpuid_count (XSTATE_CPUID , i, &eax, &ebx, &ecx, &edx); xstate_size += eax; }
_
PATCH 6/11 x86, fpu: rework MPX 'xstate' types
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
MPX includes two separate "extended state components". There is no real need to have an 'mpx_struct' because we never really manage the states together.
We also separate out the actual data in 'mpx_bndcsr_state' from the padding. We will shortly be checking the state sizes against our structures and need them to match. For consistency, we also ensure to prefix these types with 'mpx_'.
Lastly, we add some comments to mirror some of the descriptions in the Intel documents (SDM) of the various state components.
---
b/arch/x86/include/asm/fpu/types.h | 36 +++++++++++++++++++++++++++--------- b/arch/x86/include/asm/trace/mpx.h | 7 ++++--- b/arch/x86/kernel/traps.c | 2 +- b/arch/x86/mm/mpx.c | 9 +++++---- 4 files changed, 37 insertions(+), 17 deletions(-)
arch/x86/include/asm/fpu/types.h renamed from arch/x86/include/asm/fpu/types.h~x86-fpu-rework-mpx-types +27 -9
@@ -139,20 +139,37 @@ struct ymmh_struct { };/* Intel MPX support: */ -struct bndreg { + +struct mpx_bndreg {u64 lower_bound;u64 upper_bound; } __packed; +/* +* State component 3 is used for the 4 128-bit bounds registers +*/ +struct mpx_bndreg_state { +struct mpx_bndreg bndreg[4 ]; +} __packed; -struct bndcsr { -u64 bndcfgu; -u64 bndstatus; +/* +* State component 4 is used for the 64-bit user-mode MPX +* configuration register BNDCFGU and the 64-bit MPX status +* register BNDSTATUS. We call the pair "BNDCSR". +*/ +struct mpx_bndcsr { +u64 bndcfgu; +u64 bndstatus; } __packed; -struct mpx_struct { -struct bndreg bndreg[4 ]; -struct bndcsr bndcsr; -}; +/* +* The BNDCSR state is padded out to be 64-bytes in size. +*/ +struct mpx_bndcsr_state { +union { +struct mpx_bndcsr bndcsr; +u8 pad_to_64_bytes[64 ]; + }; +} __packed;struct xstate_header {u64 xfeatures;
@@ -162,7 +179,8 @@ struct xstate_header {/* New processor state extensions should be added here: */ #define XSTATE_RESERVE (sizeof(struct ymmh_struct) + \ - sizeof(struct mpx_struct) ) + sizeof(struct mpx_bndreg_state) + \ + sizeof(struct mpx_bndcsr_state) )/* * This is our most modern FPU state format, as saved by the XSAVE * and restored by the XRSTOR instructions.
arch/x86/include/asm/trace/mpx.h renamed from arch/x86/include/asm/trace/mpx.h~x86-fpu-rework-mpx-types +4 -3
@@ -11,7 +11,7 @@ TRACE_EVENT(mpx_bounds_register_exception, TP_PROTO(void *addr_referenced, - const struct bndreg *bndreg), + const struct mpx_bndreg *bndreg), TP_ARGS(addr_referenced, bndreg), TP_STRUCT__entry(
@@ -44,7 +44,7 @@ TRACE_EVENT(mpx_bounds_register_exceptio TRACE_EVENT(bounds_exception_mpx, - TP_PROTO(const struct bndcsr *bndcsr), + TP_PROTO(const struct mpx_bndcsr *bndcsr), TP_ARGS(bndcsr), TP_STRUCT__entry(
@@ -116,7 +116,8 @@ TRACE_EVENT(mpx_new_bounds_table,/* * This gets used outside of MPX-specific code, so we need a stub. */ -static inline void trace_bounds_exception_mpx (const struct bndcsr *bndcsr) +static inline +void trace_bounds_exception_mpx (const struct mpx_bndcsr *bndcsr) { }
arch/x86/kernel/traps.c renamed from arch/x86/kernel/traps.c~x86-fpu-rework-mpx-types +1 -1
@@ -372,7 +372,7 @@ dotraplinkage void do_double_fault(struc dotraplinkage void do_bounds(struct pt_regs *regs, long error_code) {enum ctx_state prev_state; -const struct bndcsr *bndcsr; +const struct mpx_bndcsr *bndcsr;siginfo_t *info; prev_state =exception_enter ();
arch/x86/mm/mpx.c renamed from arch/x86/mm/mpx.c~x86-fpu-rework-mpx-types +5 -4
@@ -274,7 +274,8 @@ bad_opcode:*/ siginfo_t *mpx_generate_siginfo (struct pt_regs *regs) { -const struct bndreg *bndregs, *bndreg; +const struct mpx_bndreg_state *bndregs; +const struct mpx_bndreg *bndreg;siginfo_t *info =NULL ;struct insn insn;uint8_t bndregno;
@@ -301,7 +302,7 @@ siginfo_t *mpx_generate_siginfo(struct p goto err_out; }/* now go select the individual register in the set of 4 */ - bndreg = &bndregs[bndregno]; + bndreg = &bndregs->bndreg[bndregno]; info =kzalloc (sizeof (*info),GFP_KERNEL );if (!info) {
@@ -343,7 +344,7 @@ err_out:static __user void *mpx_get_bounds_dir (void ) { -const struct bndcsr *bndcsr; +const struct mpx_bndcsr *bndcsr;if (!cpu_feature_enabled (X86_FEATURE_MPX ))return MPX_INVALID_BOUNDS_DIR ;
@@ -526,7 +527,7 @@ out_unmap:static int do_mpx_bt_fault (void ) {unsigned long bd_entry, bd_base; -const struct bndcsr *bndcsr; +const struct mpx_bndcsr *bndcsr;struct mm_struct *mm = current->mm; bndcsr =get_xsave_field_ptr (XSTATE_BNDCSR );
_
PATCH 7/11 x86, fpu: rework YMM definition
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
We are about to rework all of the "extended state" definitions. This makes the 'ymm' naming consistent with the AVX-512 types we will introduce later.
We also add a convenience type: "reg_128_bit" so that we do not have to spell out our arithmetic.
---
b/arch/x86/include/asm/fpu/types.h | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
arch/x86/include/asm/fpu/types.h renamed from arch/x86/include/asm/fpu/types.h~x86-fpu-rework-ymm-types +11 -5
@@ -126,17 +126,23 @@ enum xfeature_nr {#define FIRST_EXTENDED_XFEATURE_NR XFEATURE_NR_YMM +struct reg_128_bit { +u8 regbytes[128 /8 ]; +}; +/* +* State component 2: +* * There are 16x 256-bit AVX registers named YMM0-YMM15. * The low 128 bits are aliased to the 16 SSE registers (XMM0-XMM15) -* and are stored in 'struct fxregs_state::xmm_space[]'. +* and are stored in 'struct fxregs_state::xmm_space[]' in the +* "legacy" area. * -* The high 128 bits are stored here: -* 16x 128 bits == 256 bytes. +* The high 128 bits are stored here. */ struct ymmh_struct { -u8 ymmh_space[256 ]; -}; +struct reg_128_bit hi_ymm[16 ]; +} __packed;/* Intel MPX support: */
_
PATCH 8/11 x86, fpu: add C structures for AVX-512 state components
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
AVX-512 has 3 separate state components:
1. opmask registers
2. zmm upper half of registers 0-15
3. new zmm registers (16-31)
This patch adds C structures for the three components along with a few comments mostly lifted from the SDM to explain what they do. This will allow us to check our structures against what the hardware tells us about the sizes of the components.
---
b/arch/x86/include/asm/fpu/types.h | 43 ++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-)
arch/x86/include/asm/fpu/types.h renamed from arch/x86/include/asm/fpu/types.h~avx-512-structs +40 -3
@@ -129,6 +129,12 @@ enum xfeature_nr {struct reg_128_bit {u8 regbytes[128 /8 ]; }; +struct reg_256_bit { +u8 regbytes[256 /8 ]; +}; +struct reg_512_bit { +u8 regbytes[512 /8 ]; +};/* * State component 2:
@@ -177,6 +183,33 @@ struct mpx_bndcsr_state { }; } __packed; +/* AVX-512 Components: */ + +/* +* State component 5 is used for the 8 64-bit opmask registers +* k0–k7 (opmask state). +*/ +struct avx_512_opmask_state { +u64 opmask_reg[8 ]; +} __packed; + +/* +* State component 6 is used for the upper 256 bits of the +* registers ZMM0–ZMM15. These 16 256-bit values are denoted +* ZMM0_H–ZMM15_H (ZMM_Hi256 state). +*/ +struct avx_512_zmm_uppers_state { +struct reg_256_bit zmm_upper[16 ]; +} __packed; + +/* +* State component 7 is used for the 16 512-bit registers +* ZMM16–ZMM31 (Hi16_ZMM state). +*/ +struct avx_512_hi16_state { +struct reg_512_bit hi16_zmm[16 ]; +} __packed; +struct xstate_header {u64 xfeatures;u64 xcomp_bv;
@@ -184,9 +217,13 @@ struct xstate_header { } __attribute__((packed));/* New processor state extensions should be added here: */ -#define XSTATE_RESERVE (sizeof(struct ymmh_struct) + \ - sizeof(struct mpx_bndreg_state) + \ - sizeof(struct mpx_bndcsr_state) ) +#define XSTATE_RESERVE (sizeof(struct ymmh_struct) + \ + sizeof(struct mpx_bndreg_state) + \ + sizeof(struct mpx_bndcsr_state) + \ + sizeof(struct avx_512_opmask_state) + \ + sizeof(struct avx_512_zmm_uppers_state) + \ + sizeof(struct avx_512_hi16_state)) +/* * This is our most modern FPU state format, as saved by the XSAVE * and restored by the XRSTOR instructions.
_
PATCH 9/11 x86, fpu: correct and check XSAVE xstate size calculations
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
Note: our xsaves support is currently broken and disabled. This patch does not fix it, but it is an incremental improvement. This might be useful to someone backporting the entire set of XSAVES patches at some point, but it should not be backported alone.
Ingo said he wanted something like this (bullets 2 and 3):
http://lkml.kernel.org/r/[email protected]
There are currently two xsave buffer formats: standard and compacted. The standard format is waht 'XSAVE' and 'XSAVEOPT' produce while 'XSAVES' and 'XSAVEC' produce a compacted-formet buffer. (The kernel never uses XSAVEC)
But, the XSAVES buffer *ALSO* contains "system state components" which are never saved by a plain XSAVE. So, XSAVES has two things that might make its buffer differently-sized from an XSAVE-produced one.
The current code assumes that an XSAVES buffer's size is simply the sum of the sizes of the (user) states which are supported. This seems to work in most cases, but it is not consistent with what the SDM says, and it breaks if we 'align' a component in the buffer. The calculation is also unnecessary work since the CPU *tells* us the size of the buffer directly.
This patch just reads the size of the buffer right out of the CPUID leaf instead of trying to derive it.
But, blindly trusting the CPU like this is dangerous. We add a verification pass in do_extra_xstate_size_checks() to ensure that the size we calculate matches with what we see from the hardware. When it comes down to it, we trust but verify the CPU.
---
b/arch/x86/kernel/fpu/xstate.c | 181 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 171 insertions(+), 10 deletions(-)
arch/x86/kernel/fpu/xstate.c renamed from arch/x86/kernel/fpu/xstate.c~fix-xstate_size-calculation +171 -10
@@ -293,27 +293,188 @@ static void __init setup_init_fpu_buf(vocopy_xregs_to_kernel_booting (&init_fpstate.xsave); } +static int xfeature_is_supervisor (int xfeature_nr) +{ +/* +* We currently do not suport supervisor states, but if +* we did, we could find out like this. +* +* SDM says: If state component i is a user state component, +* ECX[0] return 0; if state component i is a supervisor +* state component, ECX[0] returns 1. +u32 eax, ebx, ecx, edx; +cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx); +return !!(ecx & 1); +*/ +return 0 ; +} +/* +static int xfeature_is_user(int xfeature_nr) +{ +return !xfeature_is_supervisor(xfeature_nr); +} +*/ + +/* +* This check is important because it is easy to get XSTATE_* +* confused with XSTATE_BIT_*. +*/ +#define CHECK_XFEATURE_NR (nr) do { \ + WARN_ON(nr < FIRST_EXTENDED_XFEATURE_NR); \ + WARN_ON(nr >= XFEATURES_NR_MAX); \ +} while (0); + +/* +* We could cache this like xstate_size[], but we only use +* it here, so it would be a waste of space. +*/ +static int xfeature_is_aligned (int xfeature_nr) +{ +u32 eax, ebx, ecx, edx; +CHECK_XFEATURE_NR (xfeature_nr); +cpuid_count (XSTATE_CPUID , xfeature_nr, &eax, &ebx, &ecx, &edx); +/* +* The value returned by ECX[1] indicates the alignment +* of state component i when the compacted format +* of the extended region of an XSAVE area is used +*/ +return !!(ecx &2 ); +} + +static int xfeature_uncompacted_offset (int xfeature_nr) +{ +u32 eax, ebx, ecx, edx; +CHECK_XFEATURE_NR (xfeature_nr); +cpuid_count (XSTATE_CPUID , xfeature_nr, &eax, &ebx, &ecx, &edx); +return ebx; +} + +static int xfeature_size (int xfeature_nr) +{ +u32 eax, ebx, ecx, edx; +CHECK_XFEATURE_NR (xfeature_nr); +cpuid_count (XSTATE_CPUID , xfeature_nr, &eax, &ebx, &ecx, &edx); +return eax; +} + +/* +* 'XSAVES' implies two different things: +* 1. saving of supervisor/system state +* 2. using the compacted format +* +* Use this function when dealing with the compacted format so +* that it is obvious which aspect of 'XSAVES' is being handled +* by the calling code. +*/ +static int using_compacted_format (void ) +{ +return cpu_has_xsaves; +} + +static void __xstate_dump_leaves (void ) +{ +int i; +u32 eax, ebx, ecx, edx; +static int dumped_once =0 ; + +if (dumped_once) +return ; + dumped_once =1 ; +/* +* Dump out a few leaves past the ones that we support +* just in case there are some goodies up there +*/ +for (i =0 ; i <XFEATURES_NR_MAX +10 ; i++) { +cpuid_count (XSTATE_CPUID , i, &eax, &ebx, &ecx, &edx); +pr_warn ("CPUID[%02x, %02x]: eax=%08x ebx=%08x ecx=%08x edx=%08x\n" , +XSTATE_CPUID , i, eax, ebx, ecx, edx); + } +} + +#define XSTATE_WARN_ON (x) do { \ + if (WARN_ONCE(x, "XSAVE consistency problem, dumping leaves")) { \ + __xstate_dump_leaves(); \ + } \ +} while (0) + +/* +* This essentially double-checks what the cpu told us about +* how large the XSAVE buffer needs to be. We are recalculating +* it to be safe. +*/ +static void do_extra_xstate_size_checks (void ) +{ +int paranoid_xstate_size =FXSAVE_SIZE +XSAVE_HDR_SIZE ; +int i; + +for (i =FIRST_EXTENDED_XFEATURE_NR ; i <XFEATURES_NR_MAX ; i++) { +if (!xfeature_nr_enabled (i)) +continue ; +/* +* Supervisor state components can be managed only by +* XSAVES, which is compacted-format only. +*/ +if (!using_compacted_format ()) +XSTATE_WARN_ON (xfeature_is_supervisor (i)); + +/* Align from the end of the previous feature */ +if (xfeature_is_aligned (i)) + paranoid_xstate_size =ALIGN (paranoid_xstate_size,64 ); +/* +* The offset of a given state in the non-compacted +* format is given to us in a CPUID leaf. We check +* them for being ordered (increasing offsets) in +* setup_xstate_features(). +*/ +if (!using_compacted_format ()) + paranoid_xstate_size =xfeature_uncompacted_offset (i); +/* +* The compacted-format offset always depends on where +* the previous state ended. +*/ + paranoid_xstate_size +=xfeature_size (i); + } +XSTATE_WARN_ON (paranoid_xstate_size != xstate_size); +} +/* * Calculate total size of enabled xstates in XCR0/xfeatures_mask. +* +* Note the SDM's wording here. "sub-function 0" only enumerates +* the size of the *user* states. If we use it to size a buffer +* that we use 'XSAVES' on, we could potentially overflow the +* buffer because 'XSAVES' saves system states too. +* +* Note that we do not currently set any bits on IA32_XSS so +* 'XCR0 | IA32_XSS == XCR0' for now. */ static void __init init_xstate_size (void ) {unsigned int eax, ebx, ecx, edx; -int i;if (!cpu_has_xsaves) { +/* +* - CPUID function 0DH, sub-function 0: +* EBX enumerates the size (in bytes) required by +* the XSAVE instruction for an XSAVE area +* containing all the *user* state components +* corresponding to bits currently set in XCR0. +*/ cpuid_count (XSTATE_CPUID ,0 , &eax, &ebx, &ecx, &edx); xstate_size = ebx; -return ; - } - - xstate_size =FXSAVE_SIZE +XSAVE_HDR_SIZE ; -for (i =FIRST_EXTENDED_XFEATURE_NR ; i <64 ; i++) { -if (xfeature_nr_enabled (i)) { -cpuid_count (XSTATE_CPUID , i, &eax, &ebx, &ecx, &edx); - xstate_size += eax; - } + }else { +/* +* - CPUID function 0DH, sub-function 1: +* EBX enumerates the size (in bytes) required by +* the XSAVES instruction for an XSAVE area +* containing all the state components +* corresponding to bits currently set in +* XCR0 | IA32_XSS. +*/ +cpuid_count (XSTATE_CPUID ,1 , &eax, &ebx, &ecx, &edx); + xstate_size = ebx; } +do_extra_xstate_size_checks (); }/*
_
PATCH 10/11 x86, fpu: check to ensure increasing-offset xstate offsets
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
The xstate CPUID leaves enumerate where each state component is inside the XSAVE buffer, along with the size of the entire buffer. Our new XSAVE sanity-checking code extrapolates an expected _total_ buffer size by looking at the last component that it encounters.
That method requires that the highest-numbered component also be the one with the highest offset. This is a pretty safe assumption, but let's add some code to ensure it stays true.
To make this check work correctly, we also need to ensure we only consider the offsets from enabled features because the offset register (ebx) will return 0 on unsupported features.
This also means that we will preserve the -1's that we initialized xstate_offsets/sizes[] with. That will help find bugs.
---
b/arch/x86/kernel/fpu/xstate.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-)
arch/x86/kernel/fpu/xstate.c renamed from arch/x86/kernel/fpu/xstate.c~x86-fpu-increasing-offset-assumption +21 -11
@@ -166,18 +166,38 @@ void fpu__init_cpu_xstate(void) }/* +* Note that in the future we will likely need a pair of +* functions here: one for user xstates and the other for +* system xstates. For now, they are the same. +*/ +static int xfeature_nr_enabled (enum xfeature_nr xfeature_nr) +{ +return !!(xfeatures_mask & (1UL << xfeature_nr)); +} + +/* * Record the offsets and sizes of various xstates contained * in the XSAVE state memory layout. */ static void __init setup_xstate_features (void ) {u32 eax, ebx, ecx, edx, i; +unsigned int last_good_offset =-1 ;for (i =FIRST_EXTENDED_XFEATURE_NR ; i <XFEATURES_NR_MAX ; i++) { -cpuid_count (XSTATE_CPUID , i, &eax, &ebx, &ecx, &edx); +if (!xfeature_nr_enabled (i)) +continue ; +cpuid_count (XSTATE_CPUID , i, &eax, &ebx, &ecx, &edx); xstate_offsets[i] = ebx; xstate_sizes[i] = eax; +/* +* In our xstate size checks, we assume that the +* highest-numbered xstate feature has the +* highest offset in the buffer. Ensure it does. +*/ +WARN_ON (last_good_offset > xstate_offsets[i]); + last_good_offset = xstate_offsets[i];printk (KERN_INFO "x86/fpu: xstate_offset[%d]: %04x, xstate_sizes[%d]: %04x\n" , i, ebx, i, eax); }
@@ -207,16 +227,6 @@ static void __init print_xstate_features }/* -* Note that in the future we will likely need a pair of -* functions here: one for user xstates and the other for -* system xstates. For now, they are the same. -*/ -static int xfeature_nr_enabled (enum xfeature_nr xfeature_nr) -{ -return !!(xfeatures_mask & (1UL << xfeature_nr)); -} - -/* * This function sets up offsets and sizes of all extended states in * xsave area. This supports both standard format and compacted format * of the xsave aread.
_
PATCH 11/11 x86, fpu: check CPU-provided sizes against struct declarations
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
We now have C structures defined for each of the XSAVE state components that we support. This patch adds checks during our verification pass to ensure that the CPU-provided data enumerated in CPUID leaves matches our C structures.
If not, we warn and dump all the XSAVE CPUID leaves.
Note: this *actually* found an inconsistency with the MPX 'bndcsr' state. The hardware pads it out differently from our C structures. This patch caught it and warned.
---
b/appendme | 6 +++ b/arch/x86/include/asm/fpu/types.h | 1 b/arch/x86/kernel/fpu/xstate.c | 62 +++++++++++++++++++++++++++++++++++-- 3 files changed, 67 insertions(+), 2 deletions(-)
appendme new file +6 -0
@@ -0,0 +1,6 @@ +Cc: Ingo Molnar <[email protected]> +Cc: [email protected] +Cc: Borislav Petkov <[email protected]> +Cc: Fenghua Yu <[email protected]> +Cc: Tim Chen <[email protected]> +Cc: [email protected]
arch/x86/include/asm/fpu/types.h renamed from arch/x86/include/asm/fpu/types.h~x86-fpu-check-against-struct-declarations +1 -0
@@ -108,6 +108,7 @@ enum xfeature_nr {XFEATURE_NR_OPMASK , XFEATURE_NR_ZMM_Hi256, XFEATURE_NR_Hi16_ZMM, +XFEATURE_NR_PT_UNIMPLEMENTED_SO_FAR , XFEATURES_NR_MAX, };
arch/x86/kernel/fpu/xstate.c renamed from arch/x86/kernel/fpu/xstate.c~x86-fpu-check-against-struct-declarations +60 -2
92 lines not on this page · raw message
_
Re: [PATCH 11/11] x86, fpu: check CPU-provided sizes against struct declarations
Quoted 41 lines · Dave Hansen
From: Dave Hansen <[email protected]>
We now have C structures defined for each of the XSAVE state components that we support. This patch adds checks during our verification pass to ensure that the CPU-provided data enumerated in CPUID leaves matches our C structures.
If not, we warn and dump all the XSAVE CPUID leaves.
Note: this *actually* found an inconsistency with the MPX 'bndcsr' state. The hardware pads it out differently from our C structures. This patch caught it and warned.
Signed-off-by Dave Hansen <[email protected]>Cc Ingo Molnar <[email protected]>Cc Borislav Petkov <[email protected]>Cc Fenghua Yu <[email protected]>Cc Tim Chen <[email protected]>---
b/appendme | 6 +++ b/arch/x86/include/asm/fpu/types.h | 1 b/arch/x86/kernel/fpu/xstate.c | 62 +++++++++++++++++++++++++++++++++++-- 3 files changed, 67 insertions(+), 2 deletions(-)diff -puN
/dev/nullappendmeappendme new file +6
-0@@ -0,0 +1,6 @@ +Cc: Ingo Molnar <[email protected]> +Cc: [email protected] +Cc: Borislav Petkov <[email protected]> +Cc: Fenghua Yu <[email protected]> +Cc: Tim Chen <[email protected]> +Cc: [email protected]diff -puN
arch/x86/include/asm/fpu/types.h~x86-fpu-check-against-struct-declarationsarch/x86/include/asm/fpu/types.h
arch/x86/include/asm/fpu/types.h enum xfeature_nr { · line 112
Quoted 5 lines
arch/x86/include/asm/fpu/types.h renamed from arch/x86/include/asm/fpu/types.h~x86-fpu-check-against-struct-declarations +1
-0@@ -108,6 +108,7 @@ enum xfeature_nr {XFEATURE_NR_OPMASK , XFEATURE_NR_ZMM_Hi256, XFEATURE_NR_Hi16_ZMM, + XFEATURE_NR_PT_UNIMPLEMENTED_SO_FAR,
Maybe move this to a separate patch with a bit more explanation of this XFEATURE?
Tim
Re: [PATCH 11/11] x86, fpu: check CPU-provided sizes against struct declarations
Quoted 11 lines · Tim Chen
Quoted 9 lines
diff -puN
arch/x86/include/asm/fpu/types.h~x86-fpu-check-against-struct-declarationsarch/x86/include/asm/fpu/types.harch/x86/include/asm/fpu/types.h enum xfeature_nr { · line 112
Quoted 8 lines
arch/x86/include/asm/fpu/types.h renamed from arch/x86/include/asm/fpu/types.h~x86-fpu-check-against-struct-declarations +1
-0@@ -108,6 +108,7 @@ enum xfeature_nr {XFEATURE_NR_OPMASK , XFEATURE_NR_ZMM_Hi256, XFEATURE_NR_Hi16_ZMM, + XFEATURE_NR_PT_UNIMPLEMENTED_SO_FAR,Maybe move this to a separate patch with a bit more explanation of this XFEATURE?
Yeah, plus the obvious garbage in my 'appendme' file. Will do.