Virtualize architectural LBRs
v2 cover Virtualize architectural LBRs
Intel model-specific last branch records (LBRs) were replaced by architectural LBRs (see Chapter 20 of Intel SDM volume 3B). This patchset implements virtual LBRs for HVM guests using Intel's "load guest IA32_LBR_CTL" and "clear IA32_LBR_CTL" VMX controls. It dynamically intercepts accesses to LBR state to translate between linear and effective IP depending on the current host CPU core type.
The v2 patchset implements LBR state support in Xen's xstate handling. Additionally, it adds XSAVES/XRSTORS support to the x86 emulator. Finally, migration is handled by adding a new HVM save code CPU_XSAVES_CODE containing a vCPU's compacted xstates as written by XSAVES.
I'm looking for feedback on emulator handling of XSAVES/XRSTORS, especially concerning FPU bits as it's not clear to me what should be done in these cases.
Tu Dinh (10):
x86: Add architectural LBR definitions
x86: Define arch LBR feature bits
tools: Add arch LBR feature bits
x86: Calculate arch LBR CPUID policies
x86: Keep a copy of XSAVE area size
x86: Enable XSTATE save/restore for arch LBR x86/hvm: Don't count XSS bits in XSAVE size x86/emulate: Implement XSAVES/XRSTORS for arch LBR x86/vmx: Implement arch LBR
x86/hvm: Enable XSAVES LBR save/restore
tools/libs/light/libxl_cpuid.c | 3 + tools/misc/xen-cpuid.c | 3 + tools/tests/x86_emulator/x86-emulate.h | 2 + xen/arch/x86/cpu-policy.c | 28 +++ xen/arch/x86/cpu/common.c | 7 + xen/arch/x86/domain.c | 7 + xen/arch/x86/hvm/emulate.c | 11 + xen/arch/x86/hvm/hvm.c | 70 +++++- xen/arch/x86/hvm/vmx/vmcs.c | 11 +- xen/arch/x86/hvm/vmx/vmx.c | 203 +++++++++++++-- xen/arch/x86/include/asm/cpufeature.h | 5 + xen/arch/x86/include/asm/domain.h | 1 + xen/arch/x86/include/asm/hvm/hvm.h | 3 + xen/arch/x86/include/asm/hvm/vmx/vmcs.h | 11 + xen/arch/x86/include/asm/msr-index.h | 12 + xen/arch/x86/include/asm/msr.h | 5 + xen/arch/x86/include/asm/xstate.h | 22 +- xen/arch/x86/msr.c | 89 ++++++- xen/arch/x86/x86_emulate/0fc7.c | 260 ++++++++++++++------ xen/arch/x86/x86_emulate/blk.c | 142 +++++++++++ xen/arch/x86/x86_emulate/private.h | 8 + xen/arch/x86/x86_emulate/util-xen.c | 14 ++ xen/arch/x86/x86_emulate/x86_emulate.c | 19 ++ xen/arch/x86/x86_emulate/x86_emulate.h | 33 +++ xen/arch/x86/xstate.c | 83 +++++-- xen/include/public/arch-x86/cpufeatureset.h | 28 ++- xen/include/public/arch-x86/hvm/save.h | 4 +- xen/include/xen/lib/x86/cpu-policy.h | 51 +++- xen/lib/x86/cpuid.c | 6 + 29 files changed, 1013 insertions(+), 128 deletions(-)
Signature · 9 lines
2.43.0 Ngoc Tu Dinh | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech
RFC PATCH v2 1/10 x86: Add architectural LBR definitions
--- xen/arch/x86/include/asm/msr-index.h | 12 ++++++++++++ 1 file changed, 12 insertions(+)
xen/arch/x86/include/asm/msr-index.h +12 -0
@@ -112,6 +112,8 @@#define MCU_OPT_CTRL_GDS_MIT_DIS (_AC(1, ULL) << 4)#define MCU_OPT_CTRL_GDS_MIT_LOCK (_AC(1, ULL) << 5) +#define MSR_LER_INFO 0x000001e0 +#define MSR_RTIT_OUTPUT_BASE 0x00000560#define MSR_RTIT_OUTPUT_MASK 0x00000561#define MSR_RTIT_CTL 0x00000570
@@ -193,6 +195,16 @@#define MSR_UARCH_MISC_CTRL 0x00001b01#define UARCH_CTRL_DOITM (_AC(1, ULL) << 0) +/* Architectural LBR state MSRs */ +#define MSR_LBR_INFO (n) (0x00001200 + (n)) +#define MSR_LBR_CTL 0x000014ce +#define LBR_CTL_VALID _AC(0x7f000f, ULL) +#define MSR_LBR_DEPTH 0x000014cf +#define MSR_LBR_FROM_IP (n) (0x00001500 + (n)) +#define MSR_LBR_TO_IP (n) (0x00001600 + (n)) +/* Must be updated along with XSTATE LBR state size */ +#define NUM_MSR_ARCH_LBR_FROM_TO 32 +#define MSR_EFER _AC(0xc0000080, U)/* Extended Feature Enable Register */ #define EFER_SCE (_AC(1, ULL) << 0)/* SYSCALL Enable */ #define EFER_LME (_AC(1, ULL) << 8)/* Long Mode Enable */
Signature · 9 lines
2.43.0 Ngoc Tu Dinh | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech
Re: [RFC PATCH v2 01/10] x86: Add architectural LBR definitions
Quoted 12 lines · Tu Dinh
xen/arch/x86/include/asm/msr-index.h +2
-0@@ -112,6 +112,8 @@#define MCU_OPT_CTRL_GDS_MIT_DIS (_AC(1, ULL) << 4)#define MCU_OPT_CTRL_GDS_MIT_LOCK (_AC(1, ULL) << 5) +#define MSR_LER_INFO 0x000001e0 +#define MSR_RTIT_OUTPUT_BASE 0x00000560#define MSR_RTIT_OUTPUT_MASK 0x00000561#define MSR_RTIT_CTL 0x00000570
xen/arch/x86/include/asm/msr-index.h line 201
Quoted 7 lines
xen/arch/x86/include/asm/msr-index.h +4
-0@@ -193,6 +195,16 @@#define MSR_UARCH_MISC_CTRL 0x00001b01#define UARCH_CTRL_DOITM (_AC(1, ULL) << 0) +/* Architectural LBR state MSRs */ +#define MSR_LBR_INFO (n) (0x00001200 + (n)) +#define MSR_LBR_CTL 0x000014ce +#define LBR_CTL_VALID _AC(0x7f000f, ULL)
While I can see that such a value may be useful at some point, I think it wants introducing when needed and composing of definitions for the individual bits.
Jan
RFC PATCH v2 2/10 x86: Define arch LBR feature bits
In reply to Tu DinhAdd three featureset words corresponding to the 3 CPUID words in leaf 0x1c.
Intel SDM states that CPUID may indicate a LBR depth of up to 64. However, since XSAVE LBR state only goes up to 32 LBRs, don't expose the other CPUID depth bits for now.
--- xen/arch/x86/include/asm/cpufeature.h | 5 ++ xen/include/public/arch-x86/cpufeatureset.h | 28 ++++++++++- xen/include/xen/lib/x86/cpu-policy.h | 51 ++++++++++++++++++++- xen/lib/x86/cpuid.c | 6 +++ 4 files changed, 88 insertions(+), 2 deletions(-)
xen/arch/x86/include/asm/cpufeature.h +5 -0
@@ -219,6 +219,11 @@ static inline bool boot_cpu_has(unsigned int feat)#define cpu_has_rfds_no boot_cpu_has(X86_FEATURE_RFDS_NO)#define cpu_has_rfds_clear boot_cpu_has(X86_FEATURE_RFDS_CLEAR) +/* CPUID level 0x0000001c.eax */ + +#define current_cpu_has_lbr_lip cpu_has(¤t_cpu_data, \ + X86_FEATURE_LBR_LIP); +/* Synthesized. */ #define cpu_has_arch_perfmon boot_cpu_has(X86_FEATURE_ARCH_PERFMON)#define cpu_has_cpuid_faulting boot_cpu_has(X86_FEATURE_CPUID_FAULTING)
xen/include/public/arch-x86/cpufeatureset.h +27 -1
@@ -284,7 +284,7 @@ XEN_CPUFEATURE(SERIALIZE, 9*32+14) /*A SERIALIZE insn */ XEN_CPUFEATURE(HYBRID, 9*32+15) /* Heterogeneous platform */ XEN_CPUFEATURE(TSXLDTRK, 9*32+16) /*a TSX load tracking suspend/resume insns */ XEN_CPUFEATURE(PCONFIG, 9*32+18) /* PCONFIG instruction */ -XEN_CPUFEATURE(ARCH_LBR, 9*32+19) /* Architectural Last Branch Record */ +XEN_CPUFEATURE(ARCH_LBR, 9*32+19) /*s Architectural Last Branch Record */ XEN_CPUFEATURE(CET_IBT, 9*32+20) /* CET - Indirect Branch Tracking */ XEN_CPUFEATURE(AMX_BF16, 9*32+22) /* AMX BFloat16 instruction */XEN_CPUFEATURE (AVX512_FP16 ,9 *32 +23 )/*A AVX512 FP16 instructions */
@@ -379,6 +379,32 @@ XEN_CPUFEATURE(RFDS_CLEAR, 16*32+28) /*!A| Register File(s) cleared by V/* Intel-defined CPU features, MSR_ARCH_CAPS 0x10a.edx, word 17 */ +/* Intel-defined CPU features, CPUID level 0x0000001c.eax, word 18 */ +XEN_CPUFEATURE(LBR_DEPTH_8, 18*32+ 0) /*s Depth 8 */ +XEN_CPUFEATURE(LBR_DEPTH_16, 18*32+ 1) /*s Depth 16 */ +XEN_CPUFEATURE(LBR_DEPTH_24, 18*32+ 2) /*s Depth 24 */ +XEN_CPUFEATURE(LBR_DEPTH_32, 18*32+ 3) /*s Depth 32 */ +XEN_CPUFEATURE(LBR_DEPTH_40, 18*32+ 4) /* Depth 40 */ +XEN_CPUFEATURE(LBR_DEPTH_48, 18*32+ 5) /* Depth 48 */ +XEN_CPUFEATURE(LBR_DEPTH_56, 18*32+ 6) /* Depth 56 */ +XEN_CPUFEATURE(LBR_DEPTH_64, 18*32+ 7) /* Depth 64 */ +XEN_CPUFEATURE(LBR_DCST_RST, 18*32+30) /*s Deep C-state reset */ +XEN_CPUFEATURE (LBR_LIP ,18 *32 +31 )/*! IP is linear IP */ + +/* Intel-defined CPU features, CPUID level 0x0000001c.ebx, word 19 */ +XEN_CPUFEATURE(LBR_CPL_FILTER, 19*32+ 0) /*s CPL filtering */ +XEN_CPUFEATURE(LBR_BR_FILTER, 19*32+ 1) /*s Branch filtering */ +XEN_CPUFEATURE (LBR_CALL_STACK ,19 *32 +2 )/*s Call stack mode */ + +/* Intel-defined CPU features, CPUID level 0x0000001c.ecx, word 20 */ +XEN_CPUFEATURE(LBR_MISPRED, 20*32+ 0) /*s Mispredict mode */ +XEN_CPUFEATURE(LBR_TIMED, 20*32+ 1) /*s Timed mode */ +XEN_CPUFEATURE(LBR_BR_TYPE, 20*32+ 2) /*s Branch type */ +XEN_CPUFEATURE(LBR_EVENT_LOG_0, 20*32+16) /*s Event logging for counter 0 */ +XEN_CPUFEATURE(LBR_EVENT_LOG_1, 20*32+17) /*s Event logging for counter 1 */ +XEN_CPUFEATURE(LBR_EVENT_LOG_2, 20*32+18) /*s Event logging for counter 2 */ +XEN_CPUFEATURE(LBR_EVENT_LOG_3, 20*32+19) /*s Event logging for counter 3 */ +#endif /* XEN_CPUFEATURE */ /* Clean up from a default include. Close the enum (for C). */
xen/include/xen/lib/x86/cpu-policy.h +50 -1
69 lines not on this page · raw message
xen/lib/x86/cpuid.c +6 -0
@@ -81,6 +81,9 @@ void x86_cpu_policy_to_featureset( fs[FEATURESET_7d1] = p->feat._7d1; fs[FEATURESET_m10Al] = p->arch_caps.lo; fs[FEATURESET_m10Ah] = p->arch_caps.hi; + fs[FEATURESET_1Ca] = p->basic._1Ca; + fs[FEATURESET_1Cb] = p->basic._1Cb; + fs[FEATURESET_1Cc] = p->basic._1Cc; } void x86_cpu_featureset_to_policy(
@@ -104,6 +107,9 @@ void x86_cpu_featureset_to_policy( p->feat._7d1 = fs[FEATURESET_7d1]; p->arch_caps.lo = fs[FEATURESET_m10Al]; p->arch_caps.hi = fs[FEATURESET_m10Ah]; + p->basic._1Ca = fs[FEATURESET_1Ca]; + p->basic._1Cb = fs[FEATURESET_1Cb]; + p->basic._1Cc = fs[FEATURESET_1Cc]; }void x86_cpu_policy_recalc_synth (struct cpu_policy *p)
Signature · 9 lines
2.43.0 Ngoc Tu Dinh | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech
Re: [RFC PATCH v2 02/10] x86: Define arch LBR feature bits
xen/arch/x86/include/asm/cpufeature.h static inline bool boot_cpu_has(unsigned int feat) · line 225
Quoted 10 lines · Tu Dinh
xen/arch/x86/include/asm/cpufeature.h +4
-0@@ -219,6 +219,11 @@ static inline bool boot_cpu_has(unsigned int feat)#define cpu_has_rfds_no boot_cpu_has(X86_FEATURE_RFDS_NO)#define cpu_has_rfds_clear boot_cpu_has(X86_FEATURE_RFDS_CLEAR) +/* CPUID level 0x0000001c.eax */ + +#define current_cpu_has_lbr_lip cpu_has(¤t_cpu_data, \ + X86_FEATURE_LBR_LIP);
Why would this, unlike all other similar constructs, need to use current_cpu_data rather than boot_cpu_has()? If there is a reason, it almost certainly wants naming in the description.
xen/include/public/arch-x86/cpufeatureset.h XEN_CPUFEATURE(SERIALIZE, 9*32+14) /*A SERIALIZE insn */ · line 287
Quoted 8 lines
xen/include/public/arch-x86/cpufeatureset.h +1
-1@@ -284,7 +284,7 @@ XEN_CPUFEATURE(SERIALIZE, 9*32+14) /*A SERIALIZE insn */ XEN_CPUFEATURE(HYBRID, 9*32+15) /* Heterogeneous platform */ XEN_CPUFEATURE(TSXLDTRK, 9*32+16) /*a TSX load tracking suspend/resume insns */ XEN_CPUFEATURE(PCONFIG, 9*32+18) /* PCONFIG instruction */ -XEN_CPUFEATURE (ARCH_LBR ,9 *32 +19 )/* Architectural Last Branch Record */ +XEN_CPUFEATURE (ARCH_LBR ,9 *32 +19 )/*s Architectural Last Branch Record */
The 's' here (and below) may only be added once all of the respective handling is complete.
Quoted 13 lines
xen/include/xen/lib/x86/cpu-policy.h +3
-0@@ -22,6 +22,9 @@#define FEATURESET_7d1 15/* 0x00000007:1.edx */ #define FEATURESET_m10Al 16/* 0x0000010a.eax */ #define FEATURESET_m10Ah 17/* 0x0000010a.edx */ +#define FEATURESET_1Ca 18/* 0x0000001c.eax */ +#define FEATURESET_1Cb 19/* 0x0000001c.ebx */ +#define FEATURESET_1Cc 20/* 0x0000001c.ecx */ struct cpuid_leaf {
xen/include/xen/lib/x86/cpu-policy.h unsigned int x86_cpuid_lookup_vendor(uint32_t ebx, uint32_t ecx, uint32_t edx); · line 91
Quoted 5 lines
xen/include/xen/lib/x86/cpu-policy.h +1
-1@@ -85,7 +88,7 @@ unsigned int x86_cpuid_lookup_vendor(uint32_t ebx, uint32_t ecx, uint32_t edx);*/ const char *x86_cpuid_vendor_to_str (unsigned int vendor); -#define CPUID_GUEST_NR_BASIC (0xdu + 1) +#define CPUID_GUEST_NR_BASIC (0x1cu + 1)
Are you sure this can be done with no other prep work? I've been sitting on AMX and AVX10 patches where I need to bump this, too. Yet I continue to think that something along the lines of the 3-patch series at [1] is necessary up front.
Quoted 14 lines
xen/include/xen/lib/x86/cpu-policy.h +10
-0@@ -158,6 +161,52 @@ struct cpu_policy uint64_t :64, :64; /* Leaf 0xb - Topology. */ uint64_t :64, :64; /* Leaf 0xc - rsvd */ uint64_t :64, :64; /* Leaf 0xd - XSTATE. */ + + uint64_t :64, :64; /* Leaf 0xe - rsvd */ + uint64_t :64, :64; /* Leaf 0xf - rsvd */ + uint64_t :64, :64; /* Leaf 0x10 - rsvd */ + uint64_t :64, :64; /* Leaf 0x11 - rsvd */ + uint64_t :64, :64; /* Leaf 0x12 - rsvd */ + uint64_t :64, :64; /* Leaf 0x13 - rsvd */ + uint64_t :64, :64; /* Leaf 0x14 - rsvd */ + uint64_t :64, :64; /* Leaf 0x15 - rsvd */ + uint64_t :64, :64; /* Leaf 0x16 - rsvd */
xen/include/xen/lib/x86/cpu-policy.h struct cpu_policy · line 183
Quoted 10 lines
xen/include/xen/lib/x86/cpu-policy.h +10
-0+ uint64_t :64, :64; /* Leaf 0x17 - rsvd */ + uint64_t :64, :64; /* Leaf 0x18 - rsvd */ + uint64_t :64, :64; /* Leaf 0x19 - rsvd */ + uint64_t :64, :64; /* Leaf 0x1a - rsvd */ + uint64_t :64, :64; /* Leaf 0x1b - rsvd */ + +union { +uint32_t _1Ca; +struct { +uint32_t supported_depths:8 ;
According to XEN_CPUFEATURE(LBR_DEPTH_...) further up these are 8 individual bits. Further, is there a reason you don't use here what the additions there produce in the generated header, but you rather re-define the fields from scratch?
Jan
[1] https://lists.xen.org/archives/html/xen-devel/2024-08/msg00591.html
RFC PATCH v2 3/10 tools: Add arch LBR feature bits
In reply to Tu Dinh--- tools/libs/light/libxl_cpuid.c | 3 +++ tools/misc/xen-cpuid.c | 3 +++ 2 files changed, 6 insertions(+)
tools/libs/light/libxl_cpuid.c +3 -0
@@ -342,6 +342,9 @@ int libxl_cpuid_parse_config(libxl_cpuid_policy_list *policy, const char* str)CPUID_ENTRY (0x00000007 ,1 ,CPUID_REG_EDX ),MSR_ENTRY (0x10a ,CPUID_REG_EAX ),MSR_ENTRY (0x10a ,CPUID_REG_EDX ), +CPUID_ENTRY (0x0000001C ,NA ,CPUID_REG_EAX ), +CPUID_ENTRY (0x0000001C ,NA ,CPUID_REG_EBX ), +CPUID_ENTRY (0x0000001C ,NA ,CPUID_REG_ECX ), #undef MSR_ENTRY#undef CPUID_ENTRY };
tools/misc/xen-cpuid.c +3 -0
@@ -37,6 +37,9 @@ static const struct { { "CPUID 0x00000007:1.edx", "7d1" }, { "MSR_ARCH_CAPS.lo", "m10Al" }, { "MSR_ARCH_CAPS.hi", "m10Ah" }, + { "CPUID 0x0000001c.eax", "1Ca" }, + { "CPUID 0x0000001c.ebx", "1Cb" }, + { "CPUID 0x0000001c.ecx", "1Cc" }, };#define COL_ALIGN "24"
Signature · 9 lines
2.43.0 Ngoc Tu Dinh | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech
Re: [RFC PATCH v2 03/10] tools: Add arch LBR feature bits
Quoted 25 lines · Tu Dinh
Signed-off-by Tu Dinh <[email protected]>--- tools/libs/light/libxl_cpuid.c | 3 +++ tools/misc/xen-cpuid.c | 3 +++ 2 files changed, 6 insertions(+)tools/libs/light/libxl_cpuid.c +3
-0@@ -342,6 +342,9 @@ int libxl_cpuid_parse_config(libxl_cpuid_policy_list *policy, const char* str)CPUID_ENTRY (0x00000007 ,1 ,CPUID_REG_EDX ),MSR_ENTRY (0x10a ,CPUID_REG_EAX ),MSR_ENTRY (0x10a ,CPUID_REG_EDX ), +CPUID_ENTRY (0x0000001C ,NA ,CPUID_REG_EAX ), +CPUID_ENTRY (0x0000001C ,NA ,CPUID_REG_EBX ), +CPUID_ENTRY (0x0000001C ,NA ,CPUID_REG_ECX ), #undef MSR_ENTRY#undef CPUID_ENTRY };tools/misc/xen-cpuid.c +0
-0
tools/misc/xen-cpuid.c static const struct { · line 45
Quoted 9 lines
tools/misc/xen-cpuid.c +3
-0@@ -37,6 +37,9 @@ static const struct { { "CPUID 0x00000007:1.edx", "7d1" }, { "MSR_ARCH_CAPS.lo", "m10Al" }, { "MSR_ARCH_CAPS.hi", "m10Ah" }, + { "CPUID 0x0000001c.eax", "1Ca" }, + { "CPUID 0x0000001c.ebx", "1Cb" }, + { "CPUID 0x0000001c.ecx", "1Cc" }, };#define COL_ALIGN "24"
I think this needs folding into patch 2. These tables want to stay in sync at all times with what the public interface has.
Jan
RFC PATCH v2 4/10 x86: Calculate arch LBR CPUID policies
In reply to Tu DinhEnsure that the arch LBR feature and its dependents are disabled if any prerequisites are not available.
--- xen/arch/x86/cpu-policy.c | 28 ++++++++++++++++++++++++++++ xen/arch/x86/cpu/common.c | 7 +++++++ 2 files changed, 35 insertions(+)
xen/arch/x86/cpu-policy.c +28 -0
@@ -190,6 +190,16 @@ static void sanitise_featureset(uint32_t *fs) } } +static void recalculate_arch_lbr (struct cpu_policy *p) +{ +if ( p->basic.max_leaf <0x1c || + !(cpu_policy_xstates (&host_cpu_policy) &X86_XSS_LBR ) || + p->basic.lbr_1Ca.supported_depths ==0 ) + p->feat.arch_lbr =0 ; +if ( !p->feat.arch_lbr ) + p->basic.raw[0x1c ] =EMPTY_LEAF ; +} +static void recalculate_xstate (struct cpu_policy *p) {uint64_t xstates =XSTATE_FP_SSE ;
@@ -219,6 +229,9 @@ static void recalculate_xstate(struct cpu_policy *p)if ( p->feat.amx_tile ) xstates |=X86_XCR0_TILE_CFG |X86_XCR0_TILE_DATA ; +if ( p->feat.arch_lbr ) + xstates |=X86_XSS_LBR ; +/* Subleaf 0 */ p->xstate.max_size =xstate_uncompressed_size (xstates & ~XSTATE_XSAVES_ONLY );
@@ -271,6 +284,8 @@ static void recalculate_misc(struct cpu_policy *p) p->basic.raw[0xc ] =EMPTY_LEAF ; +zero_leaves (p->basic.raw,0xe ,0x1b ); + p->extd.e1d &= ~CPUID_COMMON_1D_FEATURES ;/* Most of Power/RAS hidden from guests. */
@@ -630,6 +645,7 @@ static void __init calculate_pv_max_policy(void)sanitise_featureset (fs);x86_cpu_featureset_to_policy (fs, p); +recalculate_arch_lbr (p);recalculate_xstate (p); p->extd.raw[0xa ] =EMPTY_LEAF ;/* No SVM for PV guests. */
@@ -670,6 +686,7 @@ static void __init calculate_pv_def_policy(void) }x86_cpu_featureset_to_policy (fs, p); +recalculate_arch_lbr (p);recalculate_xstate (p); }
@@ -755,6 +772,14 @@ static void __init calculate_hvm_max_policy(void)if ( !cpu_has_vmx_xsaves )__clear_bit (X86_FEATURE_XSAVES , fs); + +/* +* VMX bitmap is needed for passing through LBR info MSRs. +* Require it for virtual arch LBR. +*/ +if ( !cpu_has_vmx_guest_lbr_ctl || !cpu_has_vmx_msr_bitmap || + !cpu_has_vmx_xsaves ) +__clear_bit (X86_FEATURE_ARCH_LBR , fs); }/*
@@ -787,6 +812,7 @@ static void __init calculate_hvm_max_policy(void)sanitise_featureset (fs);x86_cpu_featureset_to_policy (fs, p); +recalculate_arch_lbr (p);recalculate_xstate (p);/* It's always possible to emulate CPUID faulting for HVM guests */
@@ -839,6 +865,7 @@ static void __init calculate_hvm_def_policy(void) }x86_cpu_featureset_to_policy (fs, p); +recalculate_arch_lbr (p);recalculate_xstate (p); }
@@ -971,6 +998,7 @@ void recalculate_cpuid_policy(struct domain *d) p->extd.maxlinaddr = p->extd.lm ?48 :32 ; +recalculate_arch_lbr (p);recalculate_xstate (p);recalculate_misc (p);
xen/arch/x86/cpu/common.c +7 -0
@@ -505,6 +505,13 @@ static void generic_identify(struct cpuinfo_x86 *c) &c->x86_capability[FEATURESET_Da1], &tmp, &tmp, &tmp); +if (c->cpuid_level >=0x1c ) +cpuid (0x1c , + &c->x86_capability[FEATURESET_1Ca], + &c->x86_capability[FEATURESET_1Cb], + &c->x86_capability[FEATURESET_1Cc], + &tmp); +if (test_bit (X86_FEATURE_ARCH_CAPS , c->x86_capability)) rdmsr(MSR_ARCH_CAPABILITIES, c->x86_capability[FEATURESET_m10Al],
Signature · 9 lines
2.43.0 Ngoc Tu Dinh | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech
Re: [RFC PATCH v2 04/10] x86: Calculate arch LBR CPUID policies
xen/arch/x86/cpu-policy.c static void sanitise_featureset(uint32_t *fs) · line 196
Quoted 10 lines · Tu Dinh
xen/arch/x86/cpu-policy.c +4
-0@@ -190,6 +190,16 @@ static void sanitise_featureset(uint32_t *fs) } } +static void recalculate_arch_lbr (struct cpu_policy *p) +{ + if ( p->basic.max_leaf < 0x1c || + !(cpu_policy_xstates(&host_cpu_policy) & X86_XSS_LBR) ||
A reference to the host policy looks questionable here. If you ...
Quoted 29 lines
xen/arch/x86/cpu-policy.c +11
-0+ p->basic.lbr_1Ca.supported_depths == 0) + p->feat.arch_lbr =0 ; +if ( !p->feat.arch_lbr ) + p->basic.raw[0x1c ] =EMPTY_LEAF ; +} +static void recalculate_xstate (struct cpu_policy *p) {uint64_t xstates =XSTATE_FP_SSE ;@@ -219,6 +229,9 @@ static void recalculate_xstate(struct cpu_policy *p)if ( p->feat.amx_tile ) xstates |=X86_XCR0_TILE_CFG |X86_XCR0_TILE_DATA ; +if ( p->feat.arch_lbr ) + xstates |=X86_XSS_LBR ; +/* Subleaf 0 */ p->xstate.max_size =xstate_uncompressed_size (xstates & ~XSTATE_XSAVES_ONLY );@@ -271,6 +284,8 @@ static void recalculate_misc(struct cpu_policy *p) p->basic.raw[0xc ] =EMPTY_LEAF ; +zero_leaves (p->basic.raw,0xe ,0x1b ); + p->extd.e1d &= ~CPUID_COMMON_1D_FEATURES ;/* Most of Power/RAS hidden from guests. */
xen/arch/x86/cpu-policy.c static void __init calculate_pv_max_policy(void) · line 649
Quoted 5 lines
xen/arch/x86/cpu-policy.c +1
-0@@ -630,6 +645,7 @@ static void __init calculate_pv_max_policy(void)sanitise_featureset (fs);x86_cpu_featureset_to_policy (fs, p); +recalculate_arch_lbr (p);recalculate_xstate (p);
... flipped the order of these calls, couldn't you use the respective bit from the correct policy? Then again you need p->feat.arch_lbr to actually set X86_XSS_LBR. Still the aspect in recalculate_arch_lbr() wants sorting somehow.
Jan
RFC PATCH v2 5/10 x86: Keep a copy of XSAVE area size
In reply to Tu Dinh--- xen/arch/x86/include/asm/domain.h | 1 + xen/arch/x86/xstate.c | 1 + 2 files changed, 2 insertions(+)
xen/arch/x86/include/asm/domain.h +1 -0
@@ -638,6 +638,7 @@ struct arch_vcpu* #NM handler, we XRSTOR the states we XSAVE-ed; */ struct xsave_struct *xsave_area; +unsigned int xsave_area_size;uint64_t xcr0;/* Accumulated eXtended features mask for using XSAVE/XRESTORE by Xen * itself, as we can never know whether guest OS depends on content
xen/arch/x86/xstate.c +1 -0
@@ -550,6 +550,7 @@ int xstate_alloc_save_area(struct vcpu *v) save_area->fpu_sse.mxcsr =MXCSR_DEFAULT ; v->arch.xsave_area = save_area; + v->arch.xsave_area_size = size; v->arch.xcr0 =0 ; v->arch.xcr0_accum =0 ;
Signature · 9 lines
2.43.0 Ngoc Tu Dinh | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech
Re: [RFC PATCH v2 05/10] x86: Keep a copy of XSAVE area size
Quoted 1 line · Tu Dinh
Signed-off-by Tu Dinh <[email protected]>
This needs a non-empty description to clarify why this would be needed.
Jan
Quoted 22 lines
xen/arch/x86/include/asm/domain.h +1
-0@@ -638,6 +638,7 @@ struct arch_vcpu* #NM handler, we XRSTOR the states we XSAVE-ed; */ struct xsave_struct *xsave_area; +unsigned int xsave_area_size;uint64_t xcr0;/* Accumulated eXtended features mask for using XSAVE/XRESTORE by Xen * itself, as we can never know whether guest OS depends on content xen/arch/x86/xstate.c +1
-0@@ -550,6 +550,7 @@ int xstate_alloc_save_area(struct vcpu *v) save_area->fpu_sse.mxcsr =MXCSR_DEFAULT ; v->arch.xsave_area = save_area; + v->arch.xsave_area_size = size; v->arch.xcr0 =0 ; v->arch.xcr0_accum =0 ;
RFC PATCH v2 6/10 x86: Enable XSTATE save/restore for arch LBR
In reply to Tu DinhAdd a function get_xstate_component_comp() to allow fetching a specific XSTATE component from a compressed image.
Also add LBR state declarations in xstate.h.
--- xen/arch/x86/include/asm/xstate.h | 22 ++++++++- xen/arch/x86/msr.c | 3 +- xen/arch/x86/xstate.c | 79 +++++++++++++++++++++++-------- 3 files changed, 79 insertions(+), 25 deletions(-)
xen/arch/x86/include/asm/xstate.h +20 -2
@@ -33,13 +33,13 @@ extern uint32_t mxcsr_mask;#define XSTATE_FP_SSE (X86_XCR0_X87 | X86_XCR0_SSE)#define XCNTXT_MASK (X86_XCR0_X87 | X86_XCR0_SSE | X86_XCR0_YMM | \ X86_XCR0_OPMASK | X86_XCR0_ZMM | X86_XCR0_HI_ZMM | \ - XSTATE_NONLAZY) + XSTATE_NONLAZY | XSTATE_XSAVES_ONLY)#define XSTATE_ALL (~(1ULL << 63))#define XSTATE_NONLAZY (X86_XCR0_BNDREGS | X86_XCR0_BNDCSR | X86_XCR0_PKRU | \ X86_XCR0_TILE_CFG | X86_XCR0_TILE_DATA)#define XSTATE_LAZY (XSTATE_ALL & ~XSTATE_NONLAZY) -#define XSTATE_XSAVES_ONLY 0 +#define XSTATE_XSAVES_ONLY (X86_XSS_LBR)#define XSTATE_COMPACTION_ENABLED (1ULL << 63)#define XSTATE_XSS (1U << 0)
@@ -91,6 +91,21 @@ struct xstate_bndcsr {uint64_t bndstatus; }; +struct xstate_lbr_entry { +uint64_t lbr_from_ip; +uint64_t lbr_to_ip; +uint64_t lbr_info; +}; + +struct xstate_lbr { +uint64_t lbr_ctl; +uint64_t lbr_depth; +uint64_t ler_from_ip; +uint64_t ler_to_ip; +uint64_t ler_info; +struct xstate_lbr_entry entries[32 ]; +}; +/* extended state operations */ bool __must_check set_xcr0 (u64 xfeatures);uint64_t get_xcr0 (void );
@@ -114,6 +129,9 @@ int xstate_alloc_save_area(struct vcpu *v);void xstate_init (struct cpuinfo_x86 *c);unsigned int xstate_uncompressed_size (uint64_t xcr0);unsigned int xstate_compressed_size (uint64_t xstates); +void *get_xstate_component_comp (struct xsave_struct *xstate, +unsigned int size, +uint64_t component);static inline uint64_t xgetbv (unsigned int index) {
xen/arch/x86/msr.c +1 -2
@@ -522,8 +522,7 @@ int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val)if ( !cp->xstate.xsaves ) goto gp_fault; -/* No XSS features currently supported for guests */ -if ( val !=0 ) +if ( val & ~(uint64_t )XSTATE_XSAVES_ONLY ) goto gp_fault; msrs->xss.raw = val;
xen/arch/x86/xstate.c +58 -21
157 lines not on this page · raw message
Signature · 9 lines
2.43.0 Ngoc Tu Dinh | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech
Re: [RFC PATCH v2 06/10] x86: Enable XSTATE save/restore for arch LBR
xen/arch/x86/include/asm/xstate.h extern uint32_t mxcsr_mask; · line 36
Quoted 8 lines · Tu Dinh
xen/arch/x86/include/asm/xstate.h +1
-1@@ -33,13 +33,13 @@ extern uint32_t mxcsr_mask;#define XSTATE_FP_SSE (X86_XCR0_X87 | X86_XCR0_SSE)#define XCNTXT_MASK (X86_XCR0_X87 | X86_XCR0_SSE | X86_XCR0_YMM | \ X86_XCR0_OPMASK | X86_XCR0_ZMM | X86_XCR0_HI_ZMM | \ - XSTATE_NONLAZY) + XSTATE_NONLAZY | XSTATE_XSAVES_ONLY)
This is odd - in the sole pre-existing place where the #define is used you then remove X86_XSS_STATES again. Plus please see also
https://lists.xen.org/archives/html/xen-devel/2021-04/msg01336.html.
Quoted 17 lines
xen/arch/x86/include/asm/xstate.h +5
-1#define XSTATE_ALL (~(1ULL << 63))#define XSTATE_NONLAZY (X86_XCR0_BNDREGS | X86_XCR0_BNDCSR | X86_XCR0_PKRU | \ X86_XCR0_TILE_CFG | X86_XCR0_TILE_DATA)#define XSTATE_LAZY (XSTATE_ALL & ~XSTATE_NONLAZY) -#define XSTATE_XSAVES_ONLY 0 +#define XSTATE_XSAVES_ONLY (X86_XSS_LBR)#define XSTATE_COMPACTION_ENABLED (1ULL << 63)#define XSTATE_XSS (1U << 0)@@ -91,6 +91,21 @@ struct xstate_bndcsr {uint64_t bndstatus; }; +struct xstate_lbr_entry { +uint64_t lbr_from_ip; +uint64_t lbr_to_ip; +uint64_t lbr_info;
xen/arch/x86/include/asm/xstate.h struct xstate_bndcsr { · line 107
Quoted 10 lines
xen/arch/x86/include/asm/xstate.h +10
-0+}; + +struct xstate_lbr { +uint64_t lbr_ctl; +uint64_t lbr_depth; +uint64_t ler_from_ip; +uint64_t ler_to_ip; +uint64_t ler_info; +struct xstate_lbr_entry entries[32 ]; +};
Doesn't this 32 appear in an earlier patch as well? They need tying together via a #define then.
Also nit: Hard tabs slipped in.
Quoted 15 lines
xen/arch/x86/include/asm/xstate.h +3
-0@@ -114,6 +129,9 @@ int xstate_alloc_save_area(struct vcpu *v);void xstate_init (struct cpuinfo_x86 *c);unsigned int xstate_uncompressed_size (uint64_t xcr0);unsigned int xstate_compressed_size (uint64_t xstates); +void *get_xstate_component_comp (struct xsave_struct *xstate, +unsigned int size, +uint64_t component);static inline uint64_t xgetbv (unsigned int index) {xen/arch/x86/msr.c +0
-0
xen/arch/x86/msr.c int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val) · line 526
Quoted 7 lines
xen/arch/x86/msr.c +1
-2@@ -522,8 +522,7 @@ int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val)if ( !cp->xstate.xsaves ) goto gp_fault; -/* No XSS features currently supported for guests */ -if ( val !=0 ) +if ( val & ~(uint64_t )XSTATE_XSAVES_ONLY ) goto gp_fault;
Imo we'd be better off arranging for casts like this to not be required. It's too easy to leave one out unintentionally.
Quoted 10 lines
xen/arch/x86/msr.c +1
-1@@ -210,7 +214,7 @@ void expand_xsave_states(const struct vcpu *v, void *dest, unsigned int size)* non-compacted offset. */ src = xstate; - valid = xstate_bv & ~XSTATE_FP_SSE ; + valid = xstate_bv & ~XSTATE_FP_SSE & ~X86_XSS_STATES ;while ( valid ) {u64 feature = valid & -valid;
xen/arch/x86/msr.c void compress_xsave_states(struct vcpu *v, const void *src, unsigned int size) · line 286
Quoted 8 lines
xen/arch/x86/msr.c +1
-1@@ -276,7 +280,7 @@ void compress_xsave_states(struct vcpu *v, const void *src, unsigned int size)* possibly compacted offset. */ dest = xstate; - valid = xstate_bv & ~XSTATE_FP_SSE ; + valid = xstate_bv & ~XSTATE_FP_SSE & ~X86_XSS_STATES ;while ( valid ) {u64 feature = valid & -valid;
I can't figure why these two changes are needed, and the description isn't of any help here either.
Quoted 17 lines
xen/arch/x86/msr.c +13
-0@@ -1072,6 +1085,30 @@ void xstate_set_init(uint64_t mask)BUG (); } +void *get_xstate_component_comp (struct xsave_struct *xstate, +unsigned int size, +uint64_t component) +{ +uint16_t comp_offsets[sizeof (xfeature_mask) *8 ]; +uint16_t offset; +unsigned int i =ilog2 (component); + +ASSERT (generic_hweightl (component) ==1 ); + + if ( !(xstate->xsave_hdr.xcomp_bv & XSTATE_COMPACTION_ENABLED) || + i >= xstate_features || + xstate_sizes[i] == 0 ||
xen/arch/x86/msr.c void xstate_set_init(uint64_t mask) · line 1110
Quoted 10 lines
xen/arch/x86/msr.c +10
-0+ !(xstate->xsave_hdr.xcomp_bv & component) ) +return NULL ; + +setup_xstate_comp (comp_offsets, xstate->xsave_hdr.xcomp_bv); + offset = comp_offsets[i]; +if ( xstate_sizes[i] + offset > size ) +return NULL ; + +return (void *)xstate + offset; +}
The function is unused at this point. Besides this being a Misra violation (unreachable code), it also means it's left unclear what the purpose is. That would, for example, influence whether there should be some "const" applied. I find it particularly worrying that the function returns a pointer to non-const, thus permitting the caller to fiddle with the contents. Similarly it's left unclear what the "size" parameter is for.
Jan
RFC PATCH v2 7/10 x86/hvm: Don't count XSS bits in XSAVE size
In reply to Tu DinhHVM vCPU state images are uncompressed and therefore can't contain XSS states.
--- xen/arch/x86/hvm/hvm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
xen/arch/x86/hvm/hvm.c +2 -1
@@ -1208,7 +1208,8 @@ HVM_REGISTER_SAVE_RESTORE(CPU, hvm_save_cpu_ctxt, NULL, hvm_load_cpu_ctxt, 1,#define HVM_CPU_XSAVE_SIZE (xcr0) (offsetof(struct hvm_hw_cpu_xsave, \ save_area) + \ - xstate_uncompressed_size(xcr0)) + xstate_uncompressed_size(xcr0 & \ + ~X86_XSS_STATES)) static int cf_check hvm_save_cpu_xsave_states(struct vcpu *v,hvm_domain_context_t *h)
Signature · 9 lines
2.43.0 Ngoc Tu Dinh | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech
Re: [RFC PATCH v2 07/10] x86/hvm: Don't count XSS bits in XSAVE size
Quoted 2 lines · Tu Dinh
HVM vCPU state images are uncompressed and therefore can't contain XSS states.
Then again ...
xen/arch/x86/hvm/hvm.c HVM_REGISTER_SAVE_RESTORE(CPU, hvm_save_cpu_ctxt, NULL, hvm_load_cpu_ctxt, 1, · line 1212
Quoted 9 lines
xen/arch/x86/hvm/hvm.c +2
-1@@ -1208,7 +1208,8 @@ HVM_REGISTER_SAVE_RESTORE(CPU, hvm_save_cpu_ctxt, NULL, hvm_load_cpu_ctxt, 1,#define HVM_CPU_XSAVE_SIZE (xcr0) (offsetof(struct hvm_hw_cpu_xsave, \ save_area) + \ - xstate_uncompressed_size(xcr0)) + xstate_uncompressed_size(xcr0 & \ + ~X86_XSS_STATES))
... a variable / parameter named "xcr0" shouldn't include any XSS bits anyway. IOW this perhaps needs sorting at the use sites of the macros.
Jan
RFC PATCH v2 8/10 x86/emulate: Implement XSAVES/XRSTORS for arch LBR
In reply to Tu DinhAdd new set_lbr_depth HVM function and emulate ops to support LBR XSAVES/XRSTORS emulation.
Add the appropriate instruction hooks to 0fc7.c. Translate LBR registers using cs.base within a large block emulator operation.
--- tools/tests/x86_emulator/x86-emulate.h | 2 + xen/arch/x86/hvm/emulate.c | 11 ++ xen/arch/x86/include/asm/hvm/hvm.h | 3 + xen/arch/x86/x86_emulate/0fc7.c | 260 ++++++++++++++++++------- xen/arch/x86/x86_emulate/blk.c | 142 ++++++++++++++ xen/arch/x86/x86_emulate/private.h | 8 + xen/arch/x86/x86_emulate/util-xen.c | 14 ++ xen/arch/x86/x86_emulate/x86_emulate.c | 19 ++ xen/arch/x86/x86_emulate/x86_emulate.h | 33 ++++ 9 files changed, 422 insertions(+), 70 deletions(-)
tools/tests/x86_emulator/x86-emulate.h +2 -0
@@ -218,6 +218,8 @@ void wrpkru(unsigned int val);#define cpu_has_fma4 (cpu_policy.extd.fma4 && xcr0_mask(6))#define cpu_has_tbm cpu_policy.extd.tbm +#define current_cpu_has_lbr_lip cpu_policy.basic.lbr_1Ca.ip_contains_lip + int emul_test_cpuid( uint32_t leaf, uint32_t subleaf,
xen/arch/x86/hvm/emulate.c +11 -0
@@ -2562,6 +2562,16 @@ static int cf_check hvmemul_vmfunc(return rc; } +static int cf_check hvmemul_set_lbr_depth( +uint32_t depth, +struct x86_emulate_ctxt *ctxt) +{ +if ( !hvm_funcs.set_lbr_depth ) +return X86EMUL_UNHANDLEABLE ; +alternative_vcall (hvm_funcs.set_lbr_depth, current, depth); +return X86EMUL_OKAY ; +} +static const struct x86_emulate_ops hvm_emulate_ops = { .read = hvmemul_read, .insn_fetch = hvmemul_insn_fetch,
@@ -2590,6 +2600,7 @@ static const struct x86_emulate_ops hvm_emulate_ops = { .get_fpu = hvmemul_get_fpu, .put_fpu = hvmemul_put_fpu, .vmfunc = hvmemul_vmfunc, + .set_lbr_depth = hvmemul_set_lbr_depth, };static const struct x86_emulate_ops hvm_emulate_ops_no_write = {
xen/arch/x86/include/asm/hvm/hvm.h +3 -0
@@ -238,6 +238,9 @@ struct hvm_function_table {int (*vmtrace_get_option)(struct vcpu *v,uint64_t key,uint64_t *value);int (*vmtrace_reset)(struct vcpu *v); +/* Arch LBR */ +void (*set_lbr_depth)(struct vcpu *v,uint32_t depth); +uint64_t (*get_reg)(struct vcpu *v,unsigned int reg);void (*set_reg)(struct vcpu *v,unsigned int reg,uint64_t val);
xen/arch/x86/x86_emulate/0fc7.c +190 -70
283 lines not on this page · raw message
xen/arch/x86/x86_emulate/blk.c +142 -0
154 lines not on this page · raw message
xen/arch/x86/x86_emulate/private.h +8 -0
@@ -295,6 +295,10 @@ struct x86_emulate_state { blk_fxsave, #endif blk_movdir, +#ifndef X86EMUL_NO_FPU + blk_xrstors, + blk_xsaves, +#endif } blk; uint8_t modrm, modrm_mod, modrm_reg, modrm_rm; uint8_t sib_index, sib_scale;
@@ -537,6 +541,7 @@ amd_like(const struct x86_emulate_ctxt *ctxt)#define vcpu_has_avx () (ctxt->cpuid->basic.avx)#define vcpu_has_f16c () (ctxt->cpuid->basic.f16c)#define vcpu_has_rdrand () (ctxt->cpuid->basic.rdrand) +#define vcpu_has_lbr_lip () (ctxt->cpuid->basic.lbr_1Ca.ip_contains_lip)#define vcpu_has_mmxext () (ctxt->cpuid->extd.mmxext || vcpu_has_sse())#define vcpu_has_3dnow_ext () (ctxt->cpuid->extd._3dnowext)
@@ -587,6 +592,7 @@ amd_like(const struct x86_emulate_ctxt *ctxt)#define vcpu_has_avx512_vp2intersect () (ctxt->cpuid->feat.avx512_vp2intersect)#define vcpu_has_serialize () (ctxt->cpuid->feat.serialize)#define vcpu_has_tsxldtrk () (ctxt->cpuid->feat.tsxldtrk) +#define vcpu_has_arch_lbr () (ctxt->cpuid->feat.arch_lbr)#define vcpu_has_avx512_fp16 () (ctxt->cpuid->feat.avx512_fp16)#define vcpu_has_sha512 () (ctxt->cpuid->feat.sha512)#define vcpu_has_sm3 () (ctxt->cpuid->feat.sm3)
@@ -600,6 +606,8 @@ amd_like(const struct x86_emulate_ctxt *ctxt)#define vcpu_has_avx_ne_convert () (ctxt->cpuid->feat.avx_ne_convert)#define vcpu_has_avx_vnni_int16 () (ctxt->cpuid->feat.avx_vnni_int16) +#define vcpu_has_xsaves () (ctxt->cpuid->xstate.xsaves) +#define vcpu_must_have (feat) \ generate_exception_if(!vcpu_has_##feat(), X86_EXC_UD)
xen/arch/x86/x86_emulate/util-xen.c +14 -0
@@ -96,6 +96,20 @@ bool cf_check x86_insn_is_cr_access(const struct x86_emulate_state *s,return false; } +bool cf_check x86_insn_is_xsaves(const struct x86_emulate_state *s, +const struct x86_emulate_ctxt *ctxt) +{ +return ctxt->opcode ==X86EMUL_OPC (0x0f ,0xc7 ) && s->ea.type ==OP_MEM && + (s->modrm_reg &7 ) ==5 ; +} + +bool cf_check x86_insn_is_xrstors(const struct x86_emulate_state *s, +const struct x86_emulate_ctxt *ctxt) +{ +return ctxt->opcode ==X86EMUL_OPC (0x0f ,0xc7 ) && s->ea.type ==OP_MEM && + (s->modrm_reg &7 ) ==3 ; +} +unsigned long x86_insn_immediate (const struct x86_emulate_state *s,unsigned int nr) {
xen/arch/x86/x86_emulate/x86_emulate.c +19 -0
@@ -8592,6 +8592,25 @@ int x86_emul_rmw( }#undef stub_exn +uint64_t x86_get_lbr_cs_offset (const struct cpu_policy *cp, +bool is_long_mode, +const struct segment_register *cs, +bool saving) +{ +bool host_lip, guest_lip; + + host_lip = current_cpu_has_lbr_lip; + guest_lip = !!cp->basic.lbr_1Ca.ip_contains_lip; + +if ( host_lip == guest_lip || is_long_mode ) +return 0 ; +else if ( (host_lip && !guest_lip && saving) || + (!host_lip && guest_lip && !saving) ) +return -cs->base; +else +return cs->base; +} +static void __init __maybe_unused build_assertions (void ) {/* Check the values against SReg3 encoding in opcode/ModRM bytes. */
xen/arch/x86/x86_emulate/x86_emulate.h +33 -0
@@ -534,6 +534,10 @@ struct x86_emulate_ops/* vmfunc: Emulate VMFUNC via given set of EAX ECX inputs */ int (*vmfunc)(struct x86_emulate_ctxt *ctxt); + +int (*set_lbr_depth)( +uint32_t depth, +struct x86_emulate_ctxt *ctxt); };struct cpu_user_regs ;
@@ -572,6 +576,22 @@ struct x86_emulate_ctxt/* Long mode active? */ bool lma; +struct { +/* In */ +uint64_t rfbm; +unsigned int size; +/* Inout */ +/* +* MSR_LBR_{CTL,DEPTH} don't match guest state, so we need to pass +* them to the emulator. +*/ +uint64_t lbr_ctl; +uint64_t lbr_depth; +/* Out */ +/* XRSTORS */ +uint64_t xstate_bv; + } xsaves; +/* * Output-only state: */
@@ -763,6 +783,14 @@ bool cf_check x86_insn_is_cr_access(const struct x86_emulate_state *s, const struct x86_emulate_ctxt *ctxt); +bool cf_check +x86_insn_is_xsaves (const struct x86_emulate_state *s, +const struct x86_emulate_ctxt *ctxt); + +bool cf_check +x86_insn_is_xrstors (const struct x86_emulate_state *s, +const struct x86_emulate_ctxt *ctxt); +#if !defined(__XEN__) || defined(NDEBUG )static inline void x86_emulate_free_state (struct x86_emulate_state *s) {}#else
@@ -802,6 +830,11 @@ x86_emul_blk(struct x86_emulate_state *s, struct x86_emulate_ctxt *ctxt); +uint64_t x86_get_lbr_cs_offset (const struct cpu_policy *cp, +bool is_long_mode, +const struct segment_register *cs, +bool reading); +static inline void x86_emul_hw_exception (unsigned int vector,int error_code,struct x86_emulate_ctxt *ctxt) {
Signature · 9 lines
2.43.0 Ngoc Tu Dinh | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech
Re: [RFC PATCH v2 08/10] x86/emulate: Implement XSAVES/XRSTORS for arch LBR
Quoted 2 lines · Tu Dinh
Add new
set_lbr_depthHVM function and emulate ops to support LBR XSAVES/XRSTORS emulation.
What about any of the other state components, like BNDCSR?
Quoted 4 lines
Add the appropriate instruction hooks to
0fc7.c. Translate LBR registers using cs.base within a large block emulator operation.Signed-off-by Tu Dinh <[email protected]>
I fear this needs splitting; the x86_emulate/* changes want to be separate, and I'm having a hard time seeing why we'd gain XSAVES/XRSTORS support there without first/also gaining XSAVE/XRSTOR/XSAVEOPT/XSAVEC support.
Various comments further down will hopefully give you an idea of why this enabling wasn't done so far.
xen/arch/x86/include/asm/hvm/hvm.h struct hvm_function_table { · line 242
Quoted 8 lines
xen/arch/x86/include/asm/hvm/hvm.h +2
-0@@ -238,6 +238,9 @@ struct hvm_function_table {int (*vmtrace_get_option)(struct vcpu *v,uint64_t key,uint64_t *value);int (*vmtrace_reset)(struct vcpu *v); +/* Arch LBR */ +void (*set_lbr_depth)(struct vcpu *v,uint32_t depth);
I don't see why "depth" would need to be fixed-width. "unsigned int" will surely do? See also ./CODING_STYLE.
Quoted 159 lines
xen/arch/x86/x86_emulate/0fc7.c +81
-60@@ -10,6 +10,10 @@#include "private.h" +#if defined(__XEN__) && !defined(X86EMUL_NO_FPU ) +# include <asm/xstate.h> +#endif +/* Avoid namespace pollution. */ #undef cmpxchg@@ -107,87 +111,203 @@ int x86emul_0fc7(struct x86_emulate_state *s, }else { -union { -uint32_t u32[2 ]; -uint64_t u64[2 ]; - } *old, *aux; - -/* cmpxchg8b/cmpxchg16b */ -generate_exception_if ((s->modrm_reg &7 ) !=1 ,X86_EXC_UD ); -fail_if (!ops->cmpxchg); -if ( s->rex_prefix &REX_W ) - { -host_and_vcpu_must_have (cx16); -generate_exception_if (!is_aligned (s->ea.mem.seg, s->ea.mem.off,16 , - ctxt, ops), -X86_EXC_GP ,0 ); - s->op_bytes =16 ; - } -else +switch ( s->modrm_reg &7 ) { -vcpu_must_have (cx8); - s->op_bytes =8 ; - } +default : +return X86EMUL_UNRECOGNIZED ; - old =container_of (&mmvalp->ymm[0 ],typeof (*old), u64[0 ]); - aux =container_of (&mmvalp->ymm[2 ],typeof (*aux), u64[0 ]); +case 1 :/* cmpxchg8b/cmpxchg16b */ + { +union { +uint32_t u32[2 ]; +uint64_t u64[2 ]; + } *old, *aux; -/* Get actual old value. */ -if ( (rc = ops->read (s->ea.mem.seg, s->ea.mem.off, old, s->op_bytes, - ctxt)) !=X86EMUL_OKAY ) - goto done; +fail_if (!ops->cmpxchg); +if ( s->rex_prefix &REX_W ) + { +host_and_vcpu_must_have (cx16); +generate_exception_if (!is_aligned (s->ea.mem.seg, s->ea.mem.off,16 , + ctxt, ops), +X86_EXC_GP ,0 ); + s->op_bytes =16 ; + } +else + { +vcpu_must_have (cx8); + s->op_bytes =8 ; + } -/* Get expected value. */ -if ( s->op_bytes ==8 ) - { - aux->u32[0 ] = regs->eax; - aux->u32[1 ] = regs->edx; - } -else - { - aux->u64[0 ] = regs->r (ax); - aux->u64[1 ] = regs->r (dx); - } + old =container_of (&mmvalp->ymm[0 ],typeof (*old), u64[0 ]); + aux =container_of (&mmvalp->ymm[2 ],typeof (*aux), u64[0 ]); -if (memcmp (old, aux, s->op_bytes) ) - { - cmpxchgNb_failed: -/* Expected != actual: store actual to rDX:rAX and clear ZF. */ - regs->r (ax) = s->op_bytes ==8 ? old->u32[0 ] : old->u64[0 ]; - regs->r (dx) = s->op_bytes ==8 ? old->u32[1 ] : old->u64[1 ]; - regs->eflags &= ~X86_EFLAGS_ZF ; - } -else - { -/* -* Expected == actual: Get proposed value, attempt atomic cmpxchg -* and set ZF if successful. -*/ -if ( s->op_bytes ==8 ) - { - aux->u32[0 ] = regs->ebx; - aux->u32[1 ] = regs->ecx; - } -else - { - aux->u64[0 ] = regs->r (bx); - aux->u64[1 ] = regs->r (cx); +/* Get actual old value. */ +if ( (rc = ops->read (s->ea.mem.seg, s->ea.mem.off, old, s->op_bytes, + ctxt)) !=X86EMUL_OKAY ) + goto done; + +/* Get expected value. */ +if ( s->op_bytes ==8 ) + { + aux->u32[0 ] = regs->eax; + aux->u32[1 ] = regs->edx; + } +else + { + aux->u64[0 ] = regs->r (ax); + aux->u64[1 ] = regs->r (dx); + } + +if (memcmp (old, aux, s->op_bytes) ) + { + cmpxchgNb_failed: +/* Expected != actual: store actual to rDX:rAX and clear ZF. */ + regs->r (ax) = s->op_bytes ==8 ? old->u32[0 ] : old->u64[0 ]; + regs->r (dx) = s->op_bytes ==8 ? old->u32[1 ] : old->u64[1 ]; + regs->eflags &= ~X86_EFLAGS_ZF ; + } +else + { +/* +* Expected == actual: Get proposed value, attempt atomic cmpxchg +* and set ZF if successful. +*/ +if ( s->op_bytes ==8 ) + { + aux->u32[0 ] = regs->ebx; + aux->u32[1 ] = regs->ecx; + } +else + { + aux->u64[0 ] = regs->r (bx); + aux->u64[1 ] = regs->r (cx); + } + +switch ( rc = ops->cmpxchg (s->ea.mem.seg, s->ea.mem.off, old, aux, + s->op_bytes, s->lock_prefix, ctxt) ) + { +case X86EMUL_OKAY : + regs->eflags |=X86_EFLAGS_ZF ; +break ; + +case X86EMUL_CMPXCHG_FAILED : + rc =X86EMUL_OKAY ; + goto cmpxchgNb_failed;
xen/arch/x86/x86_emulate/0fc7.c int x86emul_0fc7(struct x86_emulate_state *s, · line 178
Quoted 10 lines
xen/arch/x86/x86_emulate/0fc7.c +6
-2+ +default : + goto done; + } + } +break ; } - switch ( rc = ops->cmpxchg(s->ea.mem.seg, s->ea.mem.off, old, aux, - s->op_bytes, s->lock_prefix, ctxt) )
Since it's a significant chunk of diff, this re-arrangement (mostly re- indentation) likely would better be split out to a separate patch, too. This will then also help readability of the diff for the actual addition you're making.
xen/arch/x86/x86_emulate/0fc7.c int x86emul_0fc7(struct x86_emulate_state *s, · line 204
Quoted 1 line
xen/arch/x86/x86_emulate/0fc7.c +1
-0+#if defined(__XEN__) && !defined(X86EMUL_NO_FPU )
I'm sorry, but no, it is not an option to confine this to __XEN__. This very certainly needs wiring up in the testharness as well, just like we have it for FXSAVE/FXRSTOR.
I'm further unconvinced that X86EMUL_NO_FPU is appropriate to use here. This is different from FXSAVE/FXRSTOR, the more that you likely have seen that NO_SIMD and NO_MMX are checked there as well (only FSAVE/FRSTOR have solely NO_FPU as a check). XSAVE etc are about more than just FPU / SIMD state, after all (arch LBR being one of them), expressed by the lack of an F prefix on their mnemonics.
Quoted 40 lines
xen/arch/x86/x86_emulate/0fc7.c +29
-8+case 3 :/* xrstors */ +case 5 :/* xsaves */ { -case X86EMUL_OKAY : - regs->eflags |=X86_EFLAGS_ZF ; -break ; +unsigned long cr0, cr4; +unsigned long xcr0, xss; +struct segment_register cs; -case X86EMUL_CMPXCHG_FAILED : - rc =X86EMUL_OKAY ; - goto cmpxchgNb_failed; +generate_exception_if (s->vex.pfx,X86_EXC_UD ); +host_and_vcpu_must_have (xsaves); +generate_exception_if (s->ea.type !=OP_MEM ,X86_EXC_UD ); +generate_exception_if (!is_aligned (s->ea.mem.seg, s->ea.mem.off, +64 , ctxt, ops), +X86_EXC_GP ,0 ); +fail_if (!ops->blk || !ops->read_cr || !ops->read_xcr || + !ops->read_msr || !ops->write_msr || + !ops->read_segment); +fail_if (vcpu_has_arch_lbr () && !ops->set_lbr_depth); -default : - goto done; +if ( ops->read_cr (4 , &cr4, ctxt) !=X86EMUL_OKAY || + ops->read_cr (0 , &cr0, ctxt) !=X86EMUL_OKAY ) + cr0 = cr4 =0 ; +generate_exception_if (!(cr4 &X86_CR4_OSXSAVE ),X86_EXC_UD ); +generate_exception_if (cr0 &X86_CR0_TS ,X86_EXC_NM ); +generate_exception_if (!mode_ring0 (),X86_EXC_GP ,0 ); + +if ( (rc = ops->read_segment (x86_seg_cs, + &cs, ctxt)) !=X86EMUL_OKAY || + (rc = ops->read_xcr (0 , &xcr0, ctxt)) !=X86EMUL_OKAY || + (rc = ops->read_msr (MSR_IA32_XSS , + &xss, ctxt)) !=X86EMUL_OKAY ) + goto done; +
xen/arch/x86/x86_emulate/0fc7.c int x86emul_0fc7(struct x86_emulate_state *s, · line 246
Quoted 10 lines
xen/arch/x86/x86_emulate/0fc7.c +10
-0+if (vcpu_has_arch_lbr () && + ((rc = ops->read_msr (MSR_LBR_CTL , &ctxt->xsaves.lbr_ctl, + ctxt)) !=X86EMUL_OKAY || + (rc = ops->read_msr (MSR_LBR_DEPTH , &ctxt->xsaves.lbr_depth, + ctxt)) !=X86EMUL_OKAY ) ) + goto done; + + s->blk = (s->modrm_reg &7 ) ==3 ? blk_xrstors : blk_xsaves; + ctxt->xsaves.rfbm = ((uint64_t )regs->edx <<32 ) | regs->eax; + ctxt->xsaves.rfbm &= xcr0 | xss;
I'm unconvinced that this is a field that needs recording / passing. The callee has all necessary pieces available afaic (the hookm after all, should know what its sibling hook functions are).
xen/arch/x86/x86_emulate/0fc7.c int x86emul_0fc7(struct x86_emulate_state *s, · line 251
Quoted 4 lines
xen/arch/x86/x86_emulate/0fc7.c +4
-0+if ( s->blk == blk_xrstors ) + { +struct xsave_struct hdr; +int i;
Please don't ever use plain int for variables only ever holding non- negative values.
Quoted 31 lines
xen/arch/x86/x86_emulate/0fc7.c +31
-0+if ( (rc = ops->read (s->ea.mem.seg, s->ea.mem.off, &hdr, +sizeof (hdr), ctxt)) !=X86EMUL_OKAY ) + goto done; +/* +* We must validate xcomp_bv at this stage to get a +* stable XSAVE area size. +*/ +generate_exception_if (!xsave_area_compressed (&hdr), +X86_EXC_GP ,0 ); +generate_exception_if (hdr.xsave_hdr.xcomp_bv & + ~XSTATE_COMPACTION_ENABLED & + ~(xcr0 | xss), +X86_EXC_GP ,0 ); +generate_exception_if (hdr.xsave_hdr.xstate_bv & + ~hdr.xsave_hdr.xcomp_bv, +X86_EXC_GP ,0 ); +for ( i =0 ; i <ARRAY_SIZE (hdr.xsave_hdr.reserved); i++ ) +generate_exception_if (hdr.xsave_hdr.reserved[i], +X86_EXC_GP ,0 ); + ctxt->xsaves.size =xstate_compressed_size ( + hdr.xsave_hdr.xcomp_bv & + ~XSTATE_COMPACTION_ENABLED ); + } + else + { + ctxt->xsaves.size = +xstate_compressed_size (ctxt->xsaves.rfbm); + } + + if ( (rc = ops->blk(s->ea.mem.seg, s->ea.mem.off, NULL, + ctxt->xsaves.size, ®s->eflags,
xen/arch/x86/x86_emulate/0fc7.c int x86emul_0fc7(struct x86_emulate_state *s, · line 293
Quoted 10 lines
xen/arch/x86/x86_emulate/0fc7.c +10
-0+ s, ctxt)) != X86EMUL_OKAY ) + goto done; + +if ( s->blk == blk_xrstors &&vcpu_has_arch_lbr () ) + { +if ( (rc = ops->write_msr (MSR_LBR_CTL , + ctxt->xsaves.lbr_ctl, + ctxt)) !=X86EMUL_OKAY || + (rc = ops->set_lbr_depth (ctxt->xsaves.lbr_depth, + ctxt)) !=X86EMUL_OKAY )
This is the sole place where this new emulator hook is used. Why is the hook necessary? IOW why isn't this ->write_msr(MSR_LBR_DEPTH, ...)?
xen/arch/x86/x86_emulate/0fc7.c int x86emul_0fc7(struct x86_emulate_state *s, · line 299
Quoted 6 lines
xen/arch/x86/x86_emulate/0fc7.c +6
-0+ goto done; +/* +* Even if xstate_bv[LBR]=0, XRSTORS will still clear +* LBR/LER MSRs. +*/ + if ( !(ctxt->xsaves.xstate_bv & X86_XSS_LBR) &&
Comment and code aren't quite correct, are they? The clearing won't happen if the bit wasn't set in %eax. Plus I'd expect this to happen by way of calling ->blk() (through the XSTRORS insn there) anyway.
xen/arch/x86/x86_emulate/0fc7.c int x86emul_0fc7(struct x86_emulate_state *s, · line 306
Quoted 7 lines
xen/arch/x86/x86_emulate/0fc7.c +7
-0+ ((rc = ops->write_msr (MSR_IA32_LASTINTFROMIP ,0 , + ctxt)) !=X86EMUL_OKAY || + (rc = ops->write_msr (MSR_IA32_LASTINTTOIP ,0 , + ctxt)) !=X86EMUL_OKAY || + (rc = ops->write_msr (MSR_LER_INFO ,0 , + ctxt)) != X86EMUL_OKAY) ) + goto done;
If one of the later operations fails, in principle we'd need to roll back earlier operations. As that's hard, at least a comment is needed to clarify that not doing so is intentional.
Quoted 6 lines
xen/arch/x86/x86_emulate/blk.c +0
-0@@ -17,6 +17,29 @@# endif #endif
xen/arch/x86/x86_emulate/blk.c line 29
Quoted 10 lines
xen/arch/x86/x86_emulate/blk.c +10
-0+#if defined(__XEN__) && !defined(X86EMUL_NO_FPU ) +static struct xstate_lbr * +x86_translate_lbr (struct x86_emulate_state *s, +struct x86_emulate_ctxt *ctxt, +const struct segment_register *cs, +bool saving, +struct xstate_lbr *lbr) +{ +uint64_t cs_offset; +uint32_t i;
As per the earlier comment: While for cs_offset a fixed width type may be justifiable (I would expect it to be a signed type, though), for i it pretty certainly isn't.
xen/arch/x86/x86_emulate/blk.c line 30
Quoted 1 line
xen/arch/x86/x86_emulate/blk.c +1
-0+
Nit: No trailing blanks anywhere, please.
xen/arch/x86/x86_emulate/blk.c line 31
Quoted 1 line
xen/arch/x86/x86_emulate/blk.c +1
-0+ cs_offset =x86_get_lbr_cs_offset (ctxt->cpuid,mode_64bit (), cs, saving);
Both callers avoid calling here when mode_64bit() is true. Why would you nevertheless invoke that function here again? (See also the other, related comment.)
Quoted 15 lines
xen/arch/x86/x86_emulate/blk.c +11
-0+ lbr->ler_from_ip += cs_offset; + lbr->ler_to_ip += cs_offset; +for ( i =0 ; i < ctxt->xsaves.lbr_depth; i++ ) + { + lbr->entries[i].lbr_from_ip += cs_offset; + lbr->entries[i].lbr_to_ip += cs_offset; + } +return lbr; +} +#endif + int x86_emul_blk( void *ptr, void *data,
xen/arch/x86/x86_emulate/blk.c int x86_emul_blk( · line 403
Quoted 8 lines
xen/arch/x86/x86_emulate/blk.c +5
-0@@ -373,6 +396,125 @@ int x86_emul_blk( }break ; +/* +* XSAVES/XRSTORS emulation uses the host's XSS instructions and therefore +* can't be used in an usermode emulator. +*/ +#if defined(__XEN__) && !defined(X86EMUL_NO_FPU )
Seeing the comment here, to add to my related comment elsewhere: While you can't invoke XSAVES/XRSTORS in usermode code, you can still mimic enough of them using XSAVEC/XRSTOR to facilitate a reasonable level of testing. If need be you can even (partly) deal with the new enumerators in test_x86_emulator.c's blk() directly, rather than leaving (all of) it to x86_emul_blk(). This may (see my comments on the commit message) even end up easier if you have proper support for XSAVEC/XRSTOR first.
Quoted 15 lines
xen/arch/x86/x86_emulate/blk.c +15
-0+#define _xrstors (pfx, mem, eax, edx, faulted) \ + asm volatile ( "1: .byte " pfx "0x0f,0xc7,0x1f\n" \ + "3:\n" \ + " .section .fixup,\"ax\"\n" \ + "2: incl %[faulted]\n" \ + " jmp 3b\n" \ + " .previous\n" \ + _ASM_EXTABLE(1b, 2b) \ + : "+m" (*mem), [faulted] "+g" (faulted) \ + : "a" (eax), "d" (edx), "D" (mem) ) +#define _xsaves (pfx, mem, eax, edx, faulted) \ + asm volatile ( "1: .byte " pfx "0x0f,0xc7,0x2f\n" \ + "3:\n" \ + " .section .fixup,\"ax\"\n" \ + "2: incl %[faulted]\n" \
xen/arch/x86/x86_emulate/blk.c int x86_emul_blk( · line 429
Quoted 10 lines
xen/arch/x86/x86_emulate/blk.c +10
-0+" jmp 3b\n" \ +" .previous\n" \ + _ASM_EXTABLE(1b ,2b ) \ + : "+m" (*mem), [faulted] "+g" (faulted) \ + : "a" (eax), "d" (edx), "D" (mem) ) + + case blk_xrstors: + { + struct xsave_struct *xstate = ptr; +unsigned int faulted;
bool?
Quoted 28 lines
xen/arch/x86/x86_emulate/blk.c +28
-0+ASSERT (!data); + +if ( ctxt->xsaves.rfbm & xstate->xsave_hdr.xcomp_bv &X86_XSS_LBR ) + { +struct xstate_lbr *lbr; + + lbr =get_xstate_component_comp (xstate, bytes,X86_XSS_LBR ); +generate_exception_if (!lbr,X86_EXC_GP ,0 ); +if ( xstate->xsave_hdr.xstate_bv &X86_XSS_LBR ) + { +generate_exception_if (lbr->lbr_ctl & ~LBR_CTL_VALID , +X86_EXC_GP ,0 ); +generate_exception_if (lbr->lbr_depth ==0 || + lbr->lbr_depth > +NUM_MSR_ARCH_LBR_FROM_TO || + lbr->lbr_depth %8 !=0 , +X86_EXC_GP ,0 ); + +if ( !mode_64bit () ) +x86_translate_lbr (s, ctxt, data, false, lbr); + ctxt->xsaves.lbr_ctl = lbr->lbr_ctl; + ctxt->xsaves.lbr_depth = lbr->lbr_depth; + lbr->lbr_ctl =0 ; + } +else + { + ctxt->xsaves.lbr_ctl =0 ; +/* Don't touch the previous LBR depth */
xen/arch/x86/x86_emulate/blk.c int x86_emul_blk( · line 468
Quoted 10 lines
xen/arch/x86/x86_emulate/blk.c +10
-0+ } + } + + faulted =0 ; +if ( s->rex_prefix &REX_W ) +_xrstors ("0x48," , xstate, ctxt->xsaves.rfbm &UINT32_MAX , + ctxt->xsaves.rfbm >>32 , faulted); +else +_xrstors ("" , xstate, ctxt->xsaves.rfbm &UINT32_MAX , + ctxt->xsaves.rfbm >>32 , faulted);
Personally I don't think UINT32_MAX is suitable for use as a mask. Imo this wants to be a cast to uint32_t, which will also permit the compiler to generate marginally better code.
xen/arch/x86/x86_emulate/blk.c int x86_emul_blk( · line 467
Quoted 1 line
xen/arch/x86/x86_emulate/blk.c +1
-0+generate_exception_if (faulted,X86_EXC_GP ,0 );
This ought to be only a safety net; you want to check _all_ fault conditions (for all enabled components) in software.
Quoted 14 lines
xen/arch/x86/x86_emulate/private.h +4
-0@@ -295,6 +295,10 @@ struct x86_emulate_state { blk_fxsave, #endif blk_movdir, +#ifndef X86EMUL_NO_FPU + blk_xrstors, + blk_xsaves, +#endif } blk; uint8_t modrm, modrm_mod, modrm_reg, modrm_rm; uint8_t sib_index, sib_scale;
xen/arch/x86/x86_emulate/private.h amd_like(const struct x86_emulate_ctxt *ctxt) · line 544
Quoted 4 lines
xen/arch/x86/x86_emulate/private.h +1
-0@@ -537,6 +541,7 @@ amd_like(const struct x86_emulate_ctxt *ctxt)#define vcpu_has_avx () (ctxt->cpuid->basic.avx)#define vcpu_has_f16c () (ctxt->cpuid->basic.f16c)#define vcpu_has_rdrand () (ctxt->cpuid->basic.rdrand) +#define vcpu_has_lbr_lip () (ctxt->cpuid->basic.lbr_1Ca.ip_contains_lip)
This reads odd, likely indicating a problem earlier in the series (I may even have commented there): With leaf 1c being the LBR leaf, the above ought to read
#define vcpu_has_lbr_lip() (ctxt->cpuid->lbr.ip_contains_lip)
imo.
Quoted 9 lines
xen/arch/x86/x86_emulate/util-xen.c +3
-0@@ -96,6 +96,20 @@ bool cf_check x86_insn_is_cr_access(const struct x86_emulate_state *s,return false; } +bool cf_check x86_insn_is_xsaves(const struct x86_emulate_state *s, +const struct x86_emulate_ctxt *ctxt) +{
xen/arch/x86/x86_emulate/util-xen.c bool cf_check x86_insn_is_cr_access(const struct x86_emulate_state *s, · line 111
Quoted 10 lines
xen/arch/x86/x86_emulate/util-xen.c +10
-0+return ctxt->opcode ==X86EMUL_OPC (0x0f ,0xc7 ) && s->ea.type ==OP_MEM && + (s->modrm_reg &7 ) ==5 ; +} + +bool cf_check x86_insn_is_xrstors(const struct x86_emulate_state *s, +const struct x86_emulate_ctxt *ctxt) +{ +return ctxt->opcode ==X86EMUL_OPC (0x0f ,0xc7 ) && s->ea.type ==OP_MEM && + (s->modrm_reg &7 ) ==3 ; +}
I can't spot any use of these two functions. Why are they being added here? (Recall that unused functions constitute Misra violations. Plus we surely don't want cf_check functions - i.e. more ENDBR instances - when they aren't used.)
Quoted 14 lines
xen/arch/x86/x86_emulate/x86_emulate.c +8
-0@@ -8592,6 +8592,25 @@ int x86_emul_rmw( }#undef stub_exn +uint64_t x86_get_lbr_cs_offset (const struct cpu_policy *cp, +bool is_long_mode, +const struct segment_register *cs, +bool saving) +{ +bool host_lip, guest_lip; + + host_lip = current_cpu_has_lbr_lip;
xen/arch/x86/x86_emulate/x86_emulate.c int x86_emul_rmw( · line 8612
Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +10
-0+ guest_lip = !!cp->basic.lbr_1Ca.ip_contains_lip; + +if ( host_lip == guest_lip || is_long_mode ) +return 0 ; +else if ( (host_lip && !guest_lip && saving) || + (!host_lip && guest_lip && !saving) ) +return -cs->base; +else +return cs->base; +}
This function is pretty certainly misplaced here. In fact I don't see why it would need living under x86_emulate/ at all.
Also can you please avoid the use of "else" in cases like these (where the earlier if() ends in a control flow statement)?
And then I question the is_long_mode parameter: Wouldn't it make sense to have callers simply avoid the call for long mode environments? Yet then the doc says nothing about long mode being special. Calls here may be avoided for 64-bit mode environments, but afaict not for compat mode ones.
Finally - no need for !! when the lhs is of type bool.
Quoted 22 lines
xen/arch/x86/x86_emulate/x86_emulate.h +9
-0@@ -534,6 +534,10 @@ struct x86_emulate_ops/* vmfunc: Emulate VMFUNC via given set of EAX ECX inputs */ int (*vmfunc)(struct x86_emulate_ctxt *ctxt); + +int (*set_lbr_depth)( +uint32_t depth, +struct x86_emulate_ctxt *ctxt); };struct cpu_user_regs ;@@ -572,6 +576,22 @@ struct x86_emulate_ctxt/* Long mode active? */ bool lma; +struct { +/* In */ +uint64_t rfbm; +unsigned int size; +/* Inout */
xen/arch/x86/x86_emulate/x86_emulate.h struct x86_emulate_ctxt · line 593
Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.h +10
-0+/* +* MSR_LBR_{CTL,DEPTH} don't match guest state, so we need to pass +* them to the emulator. +*/ +uint64_t lbr_ctl; +uint64_t lbr_depth; +/* Out */ +/* XRSTORS */ +uint64_t xstate_bv; + } xsaves;
If any of this is really needed (see at least one comment further up), this is the wrong structure to put the data in. Anything here is input / output for callers of x86_emulate(). The interface to the ->blk() hook, otoh, is internal to the emulator, and hence any associated data wants to go into struct x86_emulate_state.
Yet even then a question would be whether adding to that structure is actually necessary. There is the "data" parameter of the hook, after all, which right now is unused for the two new enumerators.
All of the criticism aside, let me say that I'm impressed that you dared to touch the emulator code, even more so for a rather complicated area.
Jan
RFC PATCH v2 9/10 x86/vmx: Implement arch LBR
In reply to Tu DinhUse guest LBR_CTL in VMCS to limit LBR operation per guest.
Use MSR bitmap to disable interception of arch LBR MSRs. Reconfigure bitmap on each valid LBR depth write.
--- xen/arch/x86/domain.c | 7 + xen/arch/x86/hvm/vmx/vmcs.c | 11 +- xen/arch/x86/hvm/vmx/vmx.c | 203 ++++++++++++++++++++++-- xen/arch/x86/include/asm/hvm/vmx/vmcs.h | 11 ++ xen/arch/x86/include/asm/msr.h | 5 + xen/arch/x86/msr.c | 86 ++++++++++ 6 files changed, 307 insertions(+), 16 deletions(-)
xen/arch/x86/domain.c +7 -0
@@ -2021,6 +2021,13 @@ static void __context_switch(void)if ( cpu_has_xsaves &&is_hvm_vcpu (n) )set_msr_xss (n->arch.msrs->xss.raw); } +#ifdef CONFIG_HVM +/* XRSTORS LBR state behavior depends on MSR_LBR_DEPTH */ +if (using_vmx () && +is_hvm_vcpu (n) && + n->domain->arch.cpu_policy->feat.arch_lbr ) +wrmsrl (MSR_LBR_DEPTH , n->arch.msrs->lbr_depth.raw); +#endif vcpu_restore_fpu_nonlazy (n, false); nd->arch.ctxt_switch->to (n); }
xen/arch/x86/hvm/vmx/vmcs.c +9 -2
@@ -203,6 +203,7 @@ static void __init vmx_display_features(void)P (cpu_has_vmx_bus_lock_detection,"Bus Lock Detection" );P (cpu_has_vmx_notify_vm_exiting,"Notify VM Exit" );P (cpu_has_vmx_virt_spec_ctrl,"Virtualize SPEC_CTRL" ); +P (cpu_has_vmx_guest_lbr_ctl,"Architectural LBR virtualization" );#undef Pif ( !printed )
@@ -448,7 +449,8 @@ static int vmx_init_vmcs_config(bool bsp) min =VM_EXIT_ACK_INTR_ON_EXIT ; opt = (VM_EXIT_SAVE_GUEST_PAT |VM_EXIT_LOAD_HOST_PAT | -VM_EXIT_LOAD_HOST_EFER |VM_EXIT_CLEAR_BNDCFGS ); +VM_EXIT_LOAD_HOST_EFER |VM_EXIT_CLEAR_BNDCFGS | +VM_EXIT_CLEAR_GUEST_LBR_CTL ); min |=VM_EXIT_IA32E_MODE ; _vmx_vmexit_control =adjust_vmx_controls ("VMExit Control" , min, opt,MSR_IA32_VMX_EXIT_CTLS , &mismatch);
@@ -489,7 +491,7 @@ static int vmx_init_vmcs_config(bool bsp) min =0 ; opt = (VM_ENTRY_LOAD_GUEST_PAT |VM_ENTRY_LOAD_GUEST_EFER | -VM_ENTRY_LOAD_BNDCFGS ); +VM_ENTRY_LOAD_BNDCFGS |VM_ENTRY_LOAD_GUEST_LBR_CTL ); _vmx_vmentry_control =adjust_vmx_controls ("VMEntry Control" , min, opt,MSR_IA32_VMX_ENTRY_CTLS , &mismatch);
@@ -1329,6 +1331,9 @@ static int construct_vmcs(struct vcpu *v) | (paging_mode_hap(d) ? 0 : (1U << X86_EXC_PF)) | (v->arch.fully_eager_fpu ?0 : (1U <<X86_EXC_NM )); +if ( cpu_has_vmx_guest_lbr_ctl ) +__vmwrite (GUEST_LBR_CTL ,0 ); +if ( cpu_has_vmx_notify_vm_exiting )__vmwrite (NOTIFY_WINDOW , vm_notify_window);
@@ -2087,6 +2092,8 @@ void vmcs_dump_vcpu(struct vcpu *v) vmr32(GUEST_PREEMPTION_TIMER), vmr32(GUEST_SMBASE));printk ("DebugCtl = 0x%016lx DebugExceptions = 0x%016lx\n" ,vmr (GUEST_IA32_DEBUGCTL ),vmr (GUEST_PENDING_DBG_EXCEPTIONS )); +if ( vmentry_ctl &VM_ENTRY_LOAD_GUEST_LBR_CTL ) +printk ("LbrCtl = 0x%016lx\n" ,vmr (GUEST_LBR_CTL ));if ( vmentry_ctl & (VM_ENTRY_LOAD_PERF_GLOBAL_CTRL |VM_ENTRY_LOAD_BNDCFGS ) )printk ("PerfGlobCtl = 0x%016lx BndCfgS = 0x%016lx\n" ,vmr (GUEST_PERF_GLOBAL_CTRL ),vmr (GUEST_BNDCFGS ));
xen/arch/x86/hvm/vmx/vmx.c +189 -14
278 lines not on this page · raw message
xen/arch/x86/include/asm/hvm/vmx/vmcs.h +11 -0
@@ -154,6 +154,9 @@ struct vmx_vcpu {/* Are we emulating rather than VMENTERing? */ uint8_t vmx_emulate; +/* If the vCPU last ran on a host CPU with XEN_X86_FEATURE_LBR_LIP */ +bool last_host_lip; +/* For legacy LBR only */ uint8_t lbr_flags;/* Bitmask of segments that we can't safely use in virtual 8086 mode */
@@ -229,6 +232,7 @@ extern u32 vmx_pin_based_exec_control;#define VM_EXIT_LOAD_HOST_EFER 0x00200000#define VM_EXIT_SAVE_PREEMPT_TIMER 0x00400000#define VM_EXIT_CLEAR_BNDCFGS 0x00800000 +#define VM_EXIT_CLEAR_GUEST_LBR_CTL 0x04000000extern u32 vmx_vmexit_control;#define VM_ENTRY_IA32E_MODE 0x00000200
@@ -238,6 +242,7 @@ extern u32 vmx_vmexit_control;#define VM_ENTRY_LOAD_GUEST_PAT 0x00004000#define VM_ENTRY_LOAD_GUEST_EFER 0x00008000#define VM_ENTRY_LOAD_BNDCFGS 0x00010000 +#define VM_ENTRY_LOAD_GUEST_LBR_CTL 0x00200000extern u32 vmx_vmentry_control;#define SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES 0x00000001U
@@ -391,6 +396,10 @@ extern u64 vmx_ept_vpid_cap;#define cpu_has_vmx_notify_vm_exiting \ (IS_ENABLED(CONFIG_INTEL_VMX) && \ vmx_secondary_exec_control & SECONDARY_EXEC_NOTIFY_VM_EXITING) +#define cpu_has_vmx_guest_lbr_ctl \ + (IS_ENABLED(CONFIG_INTEL_VMX) && \ + (vmx_vmexit_control & VM_EXIT_CLEAR_GUEST_LBR_CTL) && \ + (vmx_vmentry_control & VM_ENTRY_LOAD_GUEST_LBR_CTL))#define VMCS_RID_TYPE_MASK 0x80000000U
@@ -480,6 +489,8 @@ enum vmcs_field {GUEST_PDPTE0 =0x0000280a , #define GUEST_PDPTE(n) (GUEST_PDPTE0 + (n) * 2) /* n = 0...3 */GUEST_BNDCFGS =0x00002812 , +GUEST_RTIT_CTL =0x00002814 , +GUEST_LBR_CTL =0x00002816 ,HOST_PAT =0x00002c00 ,HOST_EFER =0x00002c02 , HOST_PERF_GLOBAL_CTRL = 0x00002c04,
xen/arch/x86/include/asm/msr.h +5 -0
@@ -389,6 +389,11 @@ struct vcpu_msrsuint64_t raw; } xss; +/* 0x000014cf - MSR_LBR_DEPTH */ +struct { +uint64_t raw; + } lbr_depth; +/* * 0xc0000103 - MSR_TSC_AUX *
xen/arch/x86/msr.c +86 -0
98 lines not on this page · raw message
Signature · 9 lines
2.43.0 Ngoc Tu Dinh | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech
Re: [RFC PATCH v2 09/10] x86/vmx: Implement arch LBR
Quoted 6 lines · Tu Dinh
xen/arch/x86/domain.c +0
-0@@ -2021,6 +2021,13 @@ static void __context_switch(void)if ( cpu_has_xsaves &&is_hvm_vcpu (n) )set_msr_xss (n->arch.msrs->xss.raw); }
xen/arch/x86/domain.c static void __context_switch(void) · line 2033
Quoted 10 lines
xen/arch/x86/domain.c +7
-0+#ifdef CONFIG_HVM +/* XRSTORS LBR state behavior depends on MSR_LBR_DEPTH */ +if (using_vmx () && +is_hvm_vcpu (n) && + n->domain->arch.cpu_policy->feat.arch_lbr ) +wrmsrl (MSR_LBR_DEPTH , n->arch.msrs->lbr_depth.raw); +#endif vcpu_restore_fpu_nonlazy (n, false); nd->arch.ctxt_switch->to (n); }
Why the #ifdef? That's (indirectly) included in using_vmx() already, isn't it?
And why the is_hvm_vcpu()? Non-HVM ones shouldn't ever have ->feat.arch_lbr set. In fact using_vmx() ought to be redundant with the CPU policy check, too.
Quoted 11 lines
xen/arch/x86/hvm/vmx/vmx.c +1
-0@@ -48,6 +48,7 @@#include <asm/monitor.h> #include <asm/prot-key.h> #include <asm/spec_ctrl.h> +#include <asm/xstate.h> #include <public/arch-x86/cpuid.h> static bool __initdata opt_force_ept;
xen/arch/x86/hvm/vmx/vmx.c void vmx_update_exception_bitmap(struct vcpu *v) · line 782
Quoted 9 lines
xen/arch/x86/hvm/vmx/vmx.c +6
-0@@ -773,6 +774,67 @@ void vmx_update_exception_bitmap(struct vcpu *v)__vmwrite (EXCEPTION_BITMAP , bitmap); } +static void cf_check vmx_set_lbr_depth(struct vcpu *v, +uint32_t depth) +{ +struct cpu_policy *cp = v->domain->arch.cpu_policy; +bool host_lip, guest_lip; +uint32_t i;
unsigned int
xen/arch/x86/hvm/vmx/vmx.c void vmx_update_exception_bitmap(struct vcpu *v) · line 788
Quoted 5 lines
xen/arch/x86/hvm/vmx/vmx.c +5
-0+if ( !cp->feat.arch_lbr ) +return ; + + ASSERT(depth != 0 && + depth <= NUM_MSR_ARCH_LBR_FROM_TO &&
See comments on the respective guest_wrmsr() additions.
xen/arch/x86/hvm/vmx/vmx.c void vmx_update_exception_bitmap(struct vcpu *v) · line 793
Quoted 5 lines
xen/arch/x86/hvm/vmx/vmx.c +5
-0+ depth % 8 == 0); +ASSERT (cp->basic.lbr_1Ca.supported_depths & ((1u << (depth /8 )) -1 )); + + host_lip = current_cpu_has_lbr_lip; + guest_lip = !!cp->basic.lbr_1Ca.ip_contains_lip;
As already indicated elsewhere in the series: No need for !! in cases like this one. I wonder though whether the local variables are actually needed. They look to be used only ...
xen/arch/x86/hvm/vmx/vmx.c void vmx_update_exception_bitmap(struct vcpu *v) · line 799
Quoted 5 lines
xen/arch/x86/hvm/vmx/vmx.c +5
-0+if ( v->arch.msrs->lbr_depth.raw == depth && + v->arch.hvm.vmx.last_host_lip == host_lip ) +return ; + +if ( host_lip != guest_lip )
... here (the XSS_EXIT_BITMAP update could be folded into this if() and its "else").
Quoted 21 lines
xen/arch/x86/hvm/vmx/vmx.c +21
-0+ { +for ( i =0 ; i < depth; i++ ) + { +vmx_set_msr_intercept (v,MSR_LBR_FROM_IP (i),VMX_MSR_RW ); +vmx_set_msr_intercept (v,MSR_LBR_TO_IP (i),VMX_MSR_RW ); + } +vmx_set_msr_intercept (v,MSR_IA32_LASTINTFROMIP ,VMX_MSR_RW ); +vmx_set_msr_intercept (v,MSR_IA32_LASTINTTOIP ,VMX_MSR_RW ); + } + else + { +for ( i =0 ; i < depth; i++ ) + { +vmx_clear_msr_intercept (v,MSR_LBR_FROM_IP (i),VMX_MSR_RW ); +vmx_clear_msr_intercept (v,MSR_LBR_TO_IP (i),VMX_MSR_RW ); + } +vmx_clear_msr_intercept (v,MSR_IA32_LASTINTFROMIP ,VMX_MSR_RW ); +vmx_clear_msr_intercept (v,MSR_IA32_LASTINTTOIP ,VMX_MSR_RW ); + } + +/* MSR_{LBR,LER}_INFO don't need translating */
xen/arch/x86/hvm/vmx/vmx.c void vmx_update_exception_bitmap(struct vcpu *v) · line 830
Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +10
-0+for ( i =0 ; i < depth; i++ ) +vmx_clear_msr_intercept (v,MSR_LBR_INFO (i),VMX_MSR_RW ); +vmx_clear_msr_intercept (v,MSR_LER_INFO ,VMX_MSR_RW ); +/* MSRs beyond LBR_DEPTH always need #GP */ +for ( i = depth; i <NUM_MSR_ARCH_LBR_FROM_TO ; i++ ) + { +vmx_set_msr_intercept (v,MSR_LBR_INFO (i),VMX_MSR_RW ); +vmx_set_msr_intercept (v,MSR_LBR_FROM_IP (i),VMX_MSR_RW ); +vmx_set_msr_intercept (v,MSR_LBR_TO_IP (i),VMX_MSR_RW ); + }
While I agree with the comment ahead of the loop, wouldn't hardware take care of this when the intercept is disabled?
Further, do you really need to fiddle with the entire [0,NUM_MSR_ARCH_LBR_FROM_TO) range for all three groups? Isn't is enough to cover
[min(depth, v->arch.msrs->lbr_depth), max(depth, v->arch.msrs->lbr_depth)) when "host_lip != guest_lip" didn't change (which, aiui, on some [many?] systems will never be the case)?
xen/arch/x86/hvm/vmx/vmx.c static void cf_check vmx_cpuid_policy_changed(struct vcpu *v) · line 938
Quoted 7 lines
xen/arch/x86/hvm/vmx/vmx.c +3
-0@@ -871,6 +933,16 @@ static void cf_check vmx_cpuid_policy_changed(struct vcpu *v)else vmx_set_msr_intercept (v,MSR_PKRS ,VMX_MSR_RW ); +if ( cp->feat.arch_lbr && v->arch.msrs->lbr_depth.raw ==0 ) + { +uint32_t max_depth;
unsigned int
xen/arch/x86/hvm/vmx/vmx.c static uint64_t cf_check vmx_get_reg(struct vcpu *v, unsigned int reg) · line 2761
Quoted 11 lines
xen/arch/x86/hvm/vmx/vmx.c +7
-0@@ -2679,6 +2752,48 @@ static uint64_t cf_check vmx_get_reg(struct vcpu *v, unsigned int reg)__vmread (GUEST_BNDCFGS , &val);break ; +case MSR_LBR_CTL : +__vmread (GUEST_LBR_CTL , &val); +break ; + +case MSR_LBR_DEPTH : + val = v->arch.msrs->lbr_depth.raw; +break ;
This doesn't look to be VMX-specific (beyond the question whether AMD will ever implement ARCH-LBR), and hence perhaps does't belong here.
Quoted 23 lines
xen/arch/x86/hvm/vmx/vmx.c +23
-0+case MSR_LER_INFO : + case MSR_LBR_INFO(0)...MSR_LBR_INFO(NUM_MSR_ARCH_LBR_FROM_TO - 1): +if ( v != curr ) + { + val =0 ; +break ; + } +rdmsrl (reg, val); +break ; + +case MSR_IA32_LASTINTFROMIP : +case MSR_IA32_LASTINTTOIP : + case MSR_LBR_FROM_IP(0)...MSR_LBR_FROM_IP(NUM_MSR_ARCH_LBR_FROM_TO - 1): + case MSR_LBR_TO_IP(0)...MSR_LBR_TO_IP(NUM_MSR_ARCH_LBR_FROM_TO - 1): + { +struct segment_register cs; +int mode_64bit; +uint64_t offset; + +if ( v != curr ) + { + val =0 ; +break ;
xen/arch/x86/hvm/vmx/vmx.c static uint64_t cf_check vmx_get_reg(struct vcpu *v, unsigned int reg) · line 2795
Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +10
-0+ } + + mode_64bit =vmx_guest_x86_mode (v) ==X86_MODE_64BIT ; +hvm_get_segment_register (v, x86_seg_cs, &cs); + offset =x86_get_lbr_cs_offset (cp, mode_64bit, &cs, true); + +rdmsrl (reg, val); + val += offset; +break ; + }
Same for all of these as it seems. And then similarly for set-reg.
Quoted 8 lines
xen/arch/x86/hvm/vmx/vmx.c +4
-0@@ -4055,6 +4197,36 @@ static void undo_nmis_unblocked_by_iret(void) guest_info | VMX_INTR_SHADOW_NMI); } +static void vmx_handle_xsaves_xrstors (bool saving) +{ +struct hvm_emulate_ctxt ctxt; +int rc;
xen/arch/x86/hvm/vmx/vmx.c static void undo_nmis_unblocked_by_iret(void) · line 4213
Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +10
-0+ +if ( saving ) +hvm_emulate_init_once (&ctxt, x86_insn_is_xsaves,guest_cpu_user_regs ()); +else +hvm_emulate_init_once (&ctxt, x86_insn_is_xrstors,guest_cpu_user_regs ()); + +switch ( rc =hvm_emulate_one (&ctxt, VIO_no_completion) ) + { +case X86EMUL_UNHANDLEABLE : +hvm_dump_emulation_state (XENLOG_G_WARNING ,"XSAVES/XRSTORS" , &ctxt, rc);
Can the strings passed here and ...
xen/arch/x86/hvm/vmx/vmx.c static void undo_nmis_unblocked_by_iret(void) · line 4218
Quoted 5 lines
xen/arch/x86/hvm/vmx/vmx.c +5
-0+hvm_inject_hw_exception (X86_EXC_UD ,0 ); +return ; + +case X86EMUL_UNRECOGNIZED : +hvm_dump_emulation_state (XENLOG_G_WARNING ,"XSAVES/XRSTORS" , &ctxt, rc);
... here please properly reflect "saving"?
xen/arch/x86/hvm/vmx/vmx.c static void undo_nmis_unblocked_by_iret(void) · line 4219
Quoted 1 line
xen/arch/x86/hvm/vmx/vmx.c +1
-0+hvm_inject_hw_exception (X86_EXC_UD ,X86_EVENT_NO_EC );
You correctly have X86_EVENT_NO_EC here, but not in the earlier invocation of the function.
xen/arch/x86/hvm/vmx/vmx.c static void undo_nmis_unblocked_by_iret(void) · line 4225
Quoted 6 lines
xen/arch/x86/hvm/vmx/vmx.c +6
-0+break ; + +case X86EMUL_EXCEPTION : +hvm_inject_event (&ctxt.ctxt.event); +break ; + }
This switch lacks the handling of other X86EMUL_* cases (I realize you may have found this pattern elsewhere). Actually, why aren't you using hvm_emulate_one_insn() in the first place? After all this code isn't VMX- specific at all.
xen/arch/x86/include/asm/hvm/vmx/vmcs.h struct vmx_vcpu { · line 158
Quoted 8 lines
xen/arch/x86/include/asm/hvm/vmx/vmcs.h +2
-0@@ -154,6 +154,9 @@ struct vmx_vcpu {/* Are we emulating rather than VMENTERing? */ uint8_t vmx_emulate; +/* If the vCPU last ran on a host CPU with XEN_X86_FEATURE_LBR_LIP */ +bool last_host_lip;
I assume this is for asymmetric configurations (e.g. E-cores vs P-cores)? Yet then - doesn't this field also need migrating (which I first thought it's for, considering the "host" in its name)?
Quoted 9 lines
xen/arch/x86/include/asm/hvm/vmx/vmcs.h +1
-0@@ -229,6 +232,7 @@ extern u32 vmx_pin_based_exec_control;#define VM_EXIT_LOAD_HOST_EFER 0x00200000#define VM_EXIT_SAVE_PREEMPT_TIMER 0x00400000#define VM_EXIT_CLEAR_BNDCFGS 0x00800000 +#define VM_EXIT_CLEAR_GUEST_LBR_CTL 0x04000000extern u32 vmx_vmexit_control;#define VM_ENTRY_IA32E_MODE 0x00000200
xen/arch/x86/include/asm/hvm/vmx/vmcs.h extern u32 vmx_vmexit_control; · line 246
Quoted 5 lines
xen/arch/x86/include/asm/hvm/vmx/vmcs.h +1
-0@@ -238,6 +242,7 @@ extern u32 vmx_vmexit_control;#define VM_ENTRY_LOAD_GUEST_PAT 0x00004000#define VM_ENTRY_LOAD_GUEST_EFER 0x00008000#define VM_ENTRY_LOAD_BNDCFGS 0x00010000 +#define VM_ENTRY_LOAD_GUEST_LBR_CTL 0x00200000extern u32 vmx_vmentry_control;
As you can see from context (BNDCFGS) we don't use a _GUEST_ infix for these. It's technically wrong in the former case (as it's the real MSR that's to be cleared, not the guest view thereof).
xen/arch/x86/include/asm/hvm/vmx/vmcs.h enum vmcs_field { · line 496
Quoted 9 lines
xen/arch/x86/include/asm/hvm/vmx/vmcs.h +2
-0@@ -480,6 +489,8 @@ enum vmcs_field {GUEST_PDPTE0 =0x0000280a , #define GUEST_PDPTE(n) (GUEST_PDPTE0 + (n) * 2) /* n = 0...3 */GUEST_BNDCFGS =0x00002812 , +GUEST_RTIT_CTL =0x00002814 , +GUEST_LBR_CTL =0x00002816 ,HOST_PAT =0x00002c00 ,HOST_EFER =0x00002c02 , HOST_PERF_GLOBAL_CTRL = 0x00002c04,
While I don't mind the GUEST_RTIT_CTL addition here, it's unrelated. Such things, even if minor, are normally mentioned in the patch description (if nothing else, then to indicate they're actually deliberate).
xen/arch/x86/include/asm/msr.h struct vcpu_msrs · line 395
Quoted 10 lines
xen/arch/x86/include/asm/msr.h +4
-0@@ -389,6 +389,11 @@ struct vcpu_msrsuint64_t raw; } xss; +/* 0x000014cf - MSR_LBR_DEPTH */ +struct { +uint64_t raw; + } lbr_depth;
Why is this needed? The value is part of xstate, and hence saved/restored that way, and also accessible there. You may of course have chosen to put it here for other reasons, but then please comment this entry accordingly. The comment would presumably also want to clarify that this is a HVM-only field (alternatively this could be expressed by enclosing in #ifdef CONFIG_HVM, provided the result actually builds okay).
I'm also uncertain about this wanting to be a struct. There are no fields within the MSR (except for the partitioning between valid and reserved bits).
Quoted 15 lines
xen/arch/x86/msr.c +9
-0@@ -193,6 +193,38 @@ int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val) goto gp_fault; goto get_reg; +case MSR_LBR_CTL : +case MSR_LBR_DEPTH : +case MSR_LER_INFO : +if ( !cp->feat.arch_lbr ) + goto gp_fault; + + goto get_reg; + + case MSR_LBR_INFO(0)...MSR_LBR_INFO(NUM_MSR_ARCH_LBR_FROM_TO - 1):
xen/arch/x86/msr.c int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val) · line 214
Quoted 10 lines
xen/arch/x86/msr.c +10
-0+if ( !cp->feat.arch_lbr ) + goto gp_fault; + +if ( msr -MSR_LBR_INFO (0 ) >= msrs->lbr_depth.raw ) + goto gp_fault; + + goto get_reg; + +case MSR_IA32_LASTINTFROMIP : +case MSR_IA32_LASTINTTOIP :
I don't think you can wire these two ...
xen/arch/x86/msr.c int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val) · line 218
Quoted 4 lines
xen/arch/x86/msr.c +4
-0+ case MSR_LBR_FROM_IP(0)...MSR_LBR_FROM_IP(NUM_MSR_ARCH_LBR_FROM_TO - 1): + case MSR_LBR_TO_IP(0)...MSR_LBR_TO_IP(NUM_MSR_ARCH_LBR_FROM_TO - 1): +if ( !cp->feat.arch_lbr ) + goto gp_fault;
... this way. When called from hvm_msr_read_intercept() they need to be permitted through to vmx_msr_read_intercept() in the !ARCH_LBR case.
xen/arch/x86/msr.c int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val) · line 224
Quoted 5 lines
xen/arch/x86/msr.c +5
-0+if ( (msr >=MSR_LBR_FROM_IP (msrs->lbr_depth.raw) && + msr <=MSR_LBR_FROM_IP (NUM_MSR_ARCH_LBR_FROM_TO -1 )) || + (msr >=MSR_LBR_TO_IP (msrs->lbr_depth.raw) && + msr <=MSR_LBR_TO_IP (NUM_MSR_ARCH_LBR_FROM_TO -1 )) ) + goto gp_fault;
Much like you did for INFO, having the two ranges separately will simplify the conditional(s) here.
Both comments obviously apply to the write path, too.
Quoted 9 lines
xen/arch/x86/msr.c +5
-0@@ -516,6 +548,60 @@ int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val) } goto set_reg; + +case MSR_LBR_CTL : +if ( !cp->feat.arch_lbr ) + goto gp_fault; +
xen/arch/x86/msr.c int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val) · line 565
Quoted 10 lines
xen/arch/x86/msr.c +10
-0+if ( val & ~LBR_CTL_VALID ) + goto gp_fault; + + goto set_reg; + +case MSR_LBR_DEPTH : +if ( !cp->feat.arch_lbr ) + goto gp_fault; + + if ( val == 0 ||
I wasn't able to find an indication that 0 is an illegal value here. It looks as if this simply means "no LBRs other than the LER ones".
xen/arch/x86/msr.c int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val) · line 566
Quoted 1 line
xen/arch/x86/msr.c +1
-0+ val > NUM_MSR_ARCH_LBR_FROM_TO ||
Isn't this redundant with ...
xen/arch/x86/msr.c int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val) · line 572
Quoted 6 lines
xen/arch/x86/msr.c +6
-0+ val % 8 != 0 ) + goto gp_fault; + +if ( !(cp->basic.lbr_1Ca.supported_depths & + ((1u << (val /8 )) -1 )) ) + goto gp_fault;
... this?
Jan
RFC PATCH v2 10/10 x86/hvm: Enable XSAVES LBR save/restore
In reply to Tu DinhAdd a new save code type CPU_XSAVES_CODE containing a compressed XSAVES image.
--- xen/arch/x86/hvm/hvm.c | 67 +++++++++++++++++++++----- xen/arch/x86/xstate.c | 3 +- xen/include/public/arch-x86/hvm/save.h | 4 +- 3 files changed, 60 insertions(+), 14 deletions(-)
xen/arch/x86/hvm/hvm.c +56 -11
@@ -1238,6 +1238,36 @@ static int cf_check hvm_save_cpu_xsave_states(return 0 ; } +#define HVM_CPU_XSAVES_SIZE (xcr0) (offsetof(struct hvm_hw_cpu_xsave, \ + save_area) + \ + xstate_compressed_size(xcr0)) + +static int cf_check hvm_save_cpu_xsaves_states( +struct vcpu *v,hvm_domain_context_t *h) +{ +struct hvm_hw_cpu_xsave *ctxt; +unsigned int size; +int err; + +if ( !xsave_enabled (v) ) +return 0 ;/* do nothing */ + + size =HVM_CPU_XSAVES_SIZE (v->arch.xcr0_accum); + err =_hvm_init_entry (h,CPU_XSAVES_CODE , v->vcpu_id, size); +if ( err ) +return err; + + ctxt = (struct hvm_hw_cpu_xsave *)&h->data[h->cur]; + h->cur += size; + ctxt->xfeature_mask = xfeature_mask; + ctxt->xcr0 = v->arch.xcr0; + ctxt->xcr0_accum = v->arch.xcr0_accum; + +memcpy (&ctxt->save_area, v->arch.xsave_area, size); + +return 0 ; +} +/* * Structure layout conformity checks, documenting correctness of the cast in * the invocation of validate_xstate() below.
@@ -1311,6 +1341,10 @@ static int cf_check hvm_load_cpu_xsave_states( ctxt = (struct hvm_hw_cpu_xsave *)&h->data[h->cur]; h->cur += desc->length; +if ( !cpu_has_xsaves && +xsave_area_compressed ((const void *)&ctxt->save_area) ) +return -EOPNOTSUPP ; + err =validate_xstate (d, ctxt->xcr0, ctxt->xcr0_accum, (const void *)&ctxt->save_area.xsave_hdr);if ( err )
@@ -1322,7 +1356,10 @@ static int cf_check hvm_load_cpu_xsave_states( ctxt->xcr0, ctxt->save_area.xsave_hdr.xstate_bv, err);return err; } - size =HVM_CPU_XSAVE_SIZE (ctxt->xcr0_accum); +if (xsave_area_compressed ((const void *)&ctxt->save_area) ) + size =HVM_CPU_XSAVES_SIZE (ctxt->xcr0_accum); +else + size =HVM_CPU_XSAVE_SIZE (ctxt->xcr0_accum); desc_length = desc->length;if ( desc_length > size ) {
@@ -1348,14 +1385,7 @@ static int cf_check hvm_load_cpu_xsave_states( desc_length = size; } -if (xsave_area_compressed ((const void *)&ctxt->save_area) ) - { -printk (XENLOG_G_WARNING -"HVM%d.%u restore: compressed xsave state not supported\n" , - d->domain_id, vcpuid); -return -EOPNOTSUPP ; - } -else if ( desc_length != size ) +if ( desc_length != size ) { printk(XENLOG_G_WARNING "HVM%d.%u restore mismatch: xsave length %#x != %#x\n",
@@ -1367,8 +1397,13 @@ static int cf_check hvm_load_cpu_xsave_states( v->arch.xcr0 = ctxt->xcr0; v->arch.xcr0_accum = ctxt->xcr0_accum; v->arch.nonlazy_xstate_used = ctxt->xcr0_accum &XSTATE_NONLAZY ; -compress_xsave_states (v, &ctxt->save_area, - size - offsetof(struct hvm_hw_cpu_xsave , save_area)); +if (xsave_area_compressed ((const void *)&ctxt->save_area) ) +memcpy (v->arch.xsave_area, &ctxt->save_area, + size - offsetof(struct hvm_hw_cpu_xsave , save_area)); +else +compress_xsave_states (v, &ctxt->save_area, + size - offsetof(struct hvm_hw_cpu_xsave , + save_area));return 0 ; }
@@ -1385,6 +1420,7 @@ static const uint32_t msrs_to_send[] = {MSR_AMD64_DR1_ADDRESS_MASK ,MSR_AMD64_DR2_ADDRESS_MASK ,MSR_AMD64_DR3_ADDRESS_MASK , + MSR_LBR_DEPTH, }; static int cf_check hvm_save_cpu_msrs(struct vcpu *v, hvm_domain_context_t *h)
@@ -1572,6 +1608,15 @@ static int __init cf_check hvm_register_CPU_save_and_restore(void)sizeof (struct hvm_save_descriptor ), HVMSR_PER_VCPU); +hvm_register_savevm (CPU_XSAVES_CODE , +"CPU_XSAVES" , + hvm_save_cpu_xsaves_states, +NULL , + hvm_load_cpu_xsave_states, +HVM_CPU_XSAVES_SIZE (xfeature_mask) + +sizeof (struct hvm_save_descriptor ), +HVMSR_PER_VCPU ); + hvm_register_savevm(CPU_MSR_CODE, "CPU_MSR", hvm_save_cpu_msrs,
xen/arch/x86/xstate.c +1 -2
@@ -946,8 +946,7 @@ int validate_xstate(const struct domain *d, uint64_t xcr0, uint64_t xcr0_accum, !valid_xcr0(xcr0_accum) )return -EINVAL ; -if ( (xcr0_accum & ~xfeature_mask) || - hdr->xcomp_bv ) +if ( xcr0_accum & ~xfeature_mask )return -EOPNOTSUPP ;for ( i =0 ; i <ARRAY_SIZE (hdr->reserved); ++i )
xen/include/public/arch-x86/hvm/save.h +3 -1
@@ -624,12 +624,14 @@ struct hvm_msr {#define CPU_MSR_CODE 20 +#define CPU_XSAVES_CODE 21 +/* Range 22 - 34 (inclusive) reserved for Amazon */ /* * Largest type-code in use */ -#define HVM_SAVE_CODE_MAX 20 +#define HVM_SAVE_CODE_MAX 21#endif /* __XEN_PUBLIC_HVM_SAVE_X86_H__ */
Signature · 9 lines
2.43.0 Ngoc Tu Dinh | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech
Re: [RFC PATCH v2 10/10] x86/hvm: Enable XSAVES LBR save/restore
Quoted 4 lines · Tu Dinh
Add a new save code type
CPU_XSAVES_CODEcontaining a compressed XSAVES image.Signed-off-by Tu Dinh <[email protected]>
I'm afraid this way too little of a description here. First unanswered question would be why it is that we need a new save code in the first place. Second question then would be what the interaction is when both old and new save records are present. After all aiui ...
Quoted 25 lines
xen/arch/x86/hvm/hvm.c +19
-0@@ -1238,6 +1238,36 @@ static int cf_check hvm_save_cpu_xsave_states(return 0 ; } +#define HVM_CPU_XSAVES_SIZE (xcr0) (offsetof(struct hvm_hw_cpu_xsave, \ + save_area) + \ + xstate_compressed_size(xcr0)) + +static int cf_check hvm_save_cpu_xsaves_states( +struct vcpu *v,hvm_domain_context_t *h) +{ +struct hvm_hw_cpu_xsave *ctxt; +unsigned int size; +int err; + +if ( !xsave_enabled (v) ) +return 0 ;/* do nothing */ + + size =HVM_CPU_XSAVES_SIZE (v->arch.xcr0_accum); + err =_hvm_init_entry (h,CPU_XSAVES_CODE , v->vcpu_id, size); +if ( err ) +return err; +
xen/arch/x86/hvm/hvm.c static int cf_check hvm_save_cpu_xsave_states( · line 1269
Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10
-0+ ctxt = (struct hvm_hw_cpu_xsave *)&h->data[h->cur]; + h->cur += size; + ctxt->xfeature_mask = xfeature_mask; + ctxt->xcr0 = v->arch.xcr0; + ctxt->xcr0_accum = v->arch.xcr0_accum; + +memcpy (&ctxt->save_area, v->arch.xsave_area, size); + +return 0 ; +}
... you save all states under this new code, not just the XSS-controlled ones. Plus you're going through all of this even if there are no XSS- controlled components, i.e. in particular also when there's no XSAVES support in hardware. This way you then end up saving twice the exact same data, just differently arranged.
xen/arch/x86/xstate.c int validate_xstate(const struct domain *d, uint64_t xcr0, uint64_t xcr0_accum, · line 952
Quoted 12 lines
xen/arch/x86/xstate.c +1
-2@@ -946,8 +946,7 @@ int validate_xstate(const struct domain *d, uint64_t xcr0, uint64_t xcr0_accum, !valid_xcr0(xcr0_accum) )return -EINVAL ; -if ( (xcr0_accum & ~xfeature_mask) || - hdr->xcomp_bv ) +if ( xcr0_accum & ~xfeature_mask )return -EOPNOTSUPP ;for ( i =0 ; i <ARRAY_SIZE (hdr->reserved); ++i )
Can you really merely delete the check? Don't you need to validate non-zero ->xcomp_bv then instead?
Jan