Introduce Architectural LBR for vPMU
Subsystems (4)
v6 cover Introduce Architectural LBR for vPMU
The Architectural Last Branch Records (LBRs) is published in the 319433-040 release of Intel Architecture Instruction Set Extensions and Future Features Programming Reference[0].
The main advantages of Arch LBR are [1]:
- Faster context switching due to XSAVES support and faster reset of LBR MSRs via the new DEPTH MSR
- Faster LBR read for a non-PEBS event due to XSAVES support, which lowers the overhead of the NMI handler.
- Linux kernel can support the LBR features without knowing the model number of the current CPU.
From end user's point of view, the usage of Arch LBR is the same as the Legacy LBR that has been merged in the mainline.
Note, there's one limitations for current guest Arch LBR implementation:
Guest can only use the same LBR record depth as host, this is due to the special behavior of MSR_ARCH_LBR_DEPTH: a) On write to the MSR, it'll reset all Arch LBR recording MSRs to 0s. b) XRSTORS will reset all recording MSRs to 0s if the saved depth mismatches MSR_ARCH_LBR_DEPTH.
But this limitation won't impact guest perf tool usage.
Regarding the MSR_ARCH_LBR_CTL handling in SMM and #DB, pending details inquiry to spec. owner, will enclose related patches if necessary.
[0] https://software.intel.com/content/www/us/en/develop/download/intel-architecture-instruction-set-extensions-and-future-features-programming-reference.html
[1] https://lore.kernel.org/lkml/[email protected]/
Previous version:
v5: https://lkml.kernel.org/kvm/[email protected]/
Changes in v6:
1. Removed host MSR_ARCH_LBR_CTL save/restore patch as it won't benefit host Arch LBR profiling. <Jim M.>
2. Removed arch_lbr_reset flag as it'll make things complicated/broken. <Jim M.>
3. Fixed a few issues in setting Arch LBR MSRs. <Jim M.>
4. Explicitly mask the Arch LBR depth in guest CPUID so that guest only sees the supported value.
5. Other minor fixes per Jim's review feedback.
Like Xu (6):
perf/x86/intel: Fix the comment about guest LBR support on KVM perf/x86/lbr: Simplify the exposure check for the LBR_INFO registers
KVM: vmx/pmu: Emulate MSR_ARCH_LBR_DEPTH for guest Arch LBR
KVM: vmx/pmu: Emulate MSR_ARCH_LBR_CTL for guest Arch LBR
KVM: x86: Refine the matching and clearing logic for supported_xss
KVM: x86: Add XSAVE Support for Architectural LBR
Sean Christopherson (1):
KVM: x86: Report XSS as an MSR to be saved if there are supported
features
Yang Weijiang (5):
KVM: x86: Add arch LBR MSRs to msrs_to_save_all list
KVM: x86/pmu: Refactor code to support guest Arch LBR
KVM: x86: Refresh CPUID on writes to MSR_IA32_XSS
KVM: x86/vmx: Check Arch LBR config when return perf capabilities
KVM: x86/cpuid: Advise Arch LBR feature in CPUID
arch/x86/events/intel/core.c | 3 +- arch/x86/events/intel/lbr.c | 6 +- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/asm/msr-index.h | 1 + arch/x86/include/asm/vmx.h | 4 ++ arch/x86/kvm/cpuid.c | 54 ++++++++++++-- arch/x86/kvm/vmx/capabilities.h | 25 ++++--- arch/x86/kvm/vmx/pmu_intel.c | 120 +++++++++++++++++++++++++++---- arch/x86/kvm/vmx/vmx.c | 27 +++++-- arch/x86/kvm/x86.c | 24 ++++++- 10 files changed, 226 insertions(+), 39 deletions(-)
Signature · 1 line
2.21.1
PATCH v6 1/12 perf/x86/intel: Fix the comment about guest LBR support on KVM
From: Like Xu <[email protected]>
Starting from v5.12, KVM reports guest LBR and extra_regs support when the host has relevant support. Just delete this part of the comment and fix a typo incidentally.
--- arch/x86/events/intel/core.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/events/intel/core.c +1 -2
@@ -6249,8 +6249,7 @@ __init int intel_pmu_init(void) x86_pmu.intel_ctrl);/* * Access LBR MSR may cause #GP under certain circumstances. -* E.g. KVM doesn't support LBR MSR -* Check all LBT MSR here. +* Check all LBR MSR here. * Disable LBR access if any LBR MSRs can not be accessed. */ if (x86_pmu.lbr_tos && !check_msr (x86_pmu.lbr_tos,0x3UL ))
Signature · 1 line
2.21.1
PATCH v6 2/12 perf/x86/lbr: Simplify the exposure check for the LBR_INFO registers
In reply to Yang WeijiangFrom: Like Xu <[email protected]>
The x86_pmu.lbr_info is 0 unless explicitly initialized, so there's no point checking x86_pmu.intel_cap.lbr_format.
--- arch/x86/events/intel/lbr.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
arch/x86/events/intel/lbr.c +1 -3
@@ -1848,12 +1848,10 @@ void __init intel_pmu_arch_lbr_init(void)*/ int x86_perf_get_lbr (struct x86_pmu_lbr *lbr) { -int lbr_fmt = x86_pmu.intel_cap.lbr_format; - lbr->nr = x86_pmu.lbr_nr; lbr->from = x86_pmu.lbr_from; lbr->to = x86_pmu.lbr_to; - lbr->info = (lbr_fmt ==LBR_FORMAT_INFO ) ? x86_pmu.lbr_info :0 ; + lbr->info = x86_pmu.lbr_info;return 0 ; }
Signature · 1 line
2.21.1
PATCH v6 3/12 KVM: x86: Add Arch LBR MSRs to msrs_to_save_all list
In reply to Yang WeijiangArch LBR MSR_ARCH_LBR_DEPTH and MSR_ARCH_LBR_CTL are {saved|restored} by userspace application if they're available.
--- arch/x86/kvm/x86.c | 6 ++++++ 1 file changed, 6 insertions(+)
arch/x86/kvm/x86.c +6 -0
@@ -1298,6 +1298,7 @@ static const u32 msrs_to_save_all[] = {MSR_ARCH_PERFMON_EVENTSEL0 +12 ,MSR_ARCH_PERFMON_EVENTSEL0 +13 ,MSR_ARCH_PERFMON_EVENTSEL0 +14 ,MSR_ARCH_PERFMON_EVENTSEL0 +15 ,MSR_ARCH_PERFMON_EVENTSEL0 +16 ,MSR_ARCH_PERFMON_EVENTSEL0 +17 , + MSR_ARCH_LBR_CTL, MSR_ARCH_LBR_DEPTH, };static u32 msrs_to_save[ARRAY_SIZE (msrs_to_save_all)];
@@ -6049,6 +6050,11 @@ static void kvm_init_msr_list(void) min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp)) continue;break ; +case MSR_ARCH_LBR_DEPTH : +case MSR_ARCH_LBR_CTL : +if (!kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR )) +continue ; +break ;default :break ; }
Signature · 1 line
2.21.1
PATCH v6 4/12 KVM: vmx/pmu: Emulate MSR_ARCH_LBR_DEPTH for guest Arch LBR
In reply to Yang WeijiangFrom: Like Xu <[email protected]>
The number of Arch LBR entries available is determined by the value in host MSR_ARCH_LBR_DEPTH.DEPTH. The supported LBR depth values are enumerated in CPUID.(EAX=01CH, ECX=0):EAX[7:0]. For each bit "n" set in this field, the MSR_ARCH_LBR_DEPTH.DEPTH value of "8*(n+1)" is supported.
On a guest write to MSR_ARCH_LBR_DEPTH, all LBR entries are reset to 0. KVM writes guest requested value to the native ARCH_LBR_DEPTH MSR (this is safe because the two values will be the same) when the Arch LBR records MSRs are pass-through to the guest.
--- arch/x86/kvm/vmx/pmu_intel.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/pmu_intel.c +34 -1
@@ -211,7 +211,7 @@ static bool intel_pmu_is_valid_lbr_msr(struct kvm_vcpu *vcpu, u32 index)static bool intel_is_valid_msr (struct kvm_vcpu *vcpu,u32 msr) {struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); -int ret; +int ret =0 ;switch (msr) {case MSR_CORE_PERF_FIXED_CTR_CTRL :
@@ -220,6 +220,10 @@ static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)case MSR_CORE_PERF_GLOBAL_OVF_CTRL : ret = pmu->version >1 ;break ; +case MSR_ARCH_LBR_DEPTH : +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR )) + ret =guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR ); +break ;default : ret =get_gp_pmc (pmu, msr,MSR_IA32_PERFCTR0 ) || get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0) ||
@@ -348,10 +352,28 @@ static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu,return true; } +/* +* Check if the requested depth value the same as that of host. +* When guest/host depth are different, the handling would be tricky, +* so now only max depth is supported for both host and guest. +*/ +static bool arch_lbr_depth_is_valid (struct kvm_vcpu *vcpu,u64 depth) +{ +unsigned int eax, ebx, ecx, edx; + +if (!kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR )) +return false; + +cpuid_count (0x1c ,0 , &eax, &ebx, &ecx, &edx); + +return (depth ==fls (eax &0xff ) *8 ); +} +static int intel_pmu_get_msr (struct kvm_vcpu *vcpu,struct msr_data *msr_info) {struct kvm_pmu *pmu =vcpu_to_pmu (vcpu);struct kvm_pmc *pmc; +struct lbr_desc *lbr_desc =vcpu_to_lbr_desc (vcpu);u32 msr = msr_info->index;switch (msr) {
@@ -367,6 +389,9 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case MSR_CORE_PERF_GLOBAL_OVF_CTRL : msr_info->data = pmu->global_ovf_ctrl;return 0 ; +case MSR_ARCH_LBR_DEPTH : + msr_info->data = lbr_desc->records.nr; +return 0 ;default :if ((pmc =get_gp_pmc (pmu, msr,MSR_IA32_PERFCTR0 )) || (pmc =get_gp_pmc (pmu, msr,MSR_IA32_PMC0 ))) {
@@ -393,6 +418,7 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) {struct kvm_pmu *pmu =vcpu_to_pmu (vcpu);struct kvm_pmc *pmc; +struct lbr_desc *lbr_desc =vcpu_to_lbr_desc (vcpu);u32 msr = msr_info->index;u64 data = msr_info->data;
@@ -427,6 +453,13 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)return 0 ; }break ; +case MSR_ARCH_LBR_DEPTH : +if (!arch_lbr_depth_is_valid (vcpu, data)) +return 1 ; + lbr_desc->records.nr = data; +if (!msr_info->host_initiated) +wrmsrl (MSR_ARCH_LBR_DEPTH , lbr_desc->records.nr); +return 0 ;default :if ((pmc =get_gp_pmc (pmu, msr,MSR_IA32_PERFCTR0 )) || (pmc =get_gp_pmc (pmu, msr,MSR_IA32_PMC0 ))) {
Signature · 1 line
2.21.1
Re: [PATCH v6 04/12] KVM: vmx/pmu: Emulate MSR_ARCH_LBR_DEPTH for guest Arch LBR
Quoted 9 lines · Yang Weijiang
From: Like Xu <[email protected]>
The number of Arch LBR entries available is determined by the value in host MSR_ARCH_LBR_DEPTH.DEPTH. The supported LBR depth values are enumerated in CPUID.(EAX=01CH, ECX=0):EAX[7:0]. For each bit "n" set in this field, the MSR_ARCH_LBR_DEPTH.DEPTH value of "8*(n+1)" is supported.
Quoted 7 lines
On a guest write to
MSR_ARCH_LBR_DEPTH, all LBR entries are reset to 0. KVM writes guest requested value to the nativeARCH_LBR_DEPTHMSR (this is safe because the two values will be the same) when the Arch LBR records MSRs are pass-through to the guest.Signed-off-by Like Xu <[email protected]>Signed-off-by Yang Weijiang <[email protected]>
It might be worth noting that KVM_SET_MSRS cannot be used to emulate a wrmsr instruction in the guest, but maybe that's already implicit.
Re: [PATCH v6 04/12] KVM: vmx/pmu: Emulate MSR_ARCH_LBR_DEPTH for guest Arch LBR
Quoted 15 lines · Jim Mattson
Quoted 14 lines · Yang Weijiang
From: Like Xu <[email protected]>
The number of Arch LBR entries available is determined by the value in host MSR_ARCH_LBR_DEPTH.DEPTH. The supported LBR depth values are enumerated in CPUID.(EAX=01CH, ECX=0):EAX[7:0]. For each bit "n" set in this field, the MSR_ARCH_LBR_DEPTH.DEPTH value of "8*(n+1)" is supported.
On a guest write to
MSR_ARCH_LBR_DEPTH, all LBR entries are reset to 0. KVM writes guest requested value to the nativeARCH_LBR_DEPTHMSR (this is safe because the two values will be the same) when the Arch LBR records MSRs are pass-through to the guest.
Quoted 7 lines
Quoted 2 lines
Signed-off-by Like Xu <[email protected]>Signed-off-by Yang Weijiang <[email protected]>It might be worth noting that
KVM_SET_MSRScannot be used to emulate a wrmsr instruction in the guest, but maybe that's already implicit.Reviewed-by Jim Mattson <[email protected]>
Thanks Jim. Yes, guest wrmsr read/write emulation shares none-host initiated path.
PATCH v6 5/12 KVM: vmx/pmu: Emulate MSR_ARCH_LBR_CTL for guest Arch LBR
In reply to Yang WeijiangFrom: Like Xu <[email protected]>
Arch LBRs are enabled by setting MSR_ARCH_LBR_CTL.LBREn to 1. A new guest state field named "Guest IA32_LBR_CTL" is added to enhance guest LBR usage. When guest Arch LBR is enabled, a guest LBR event will be created like the model-specific LBR does. Clear guest LBR enable bit on host PMI handling so guest can see expected config.
On processors that support Arch LBR, MSR_IA32_DEBUGCTLMSR[bit 0] has no meaning. It can be written to 0 or 1, but reads will always return 0. Like IA32_DEBUGCTL, IA32_ARCH_LBR_CTL msr is also preserved on INIT.
Regardless of the Arch LBR or legacy LBR, when the LBR_EN bit 0 of the corresponding control MSR is set to 1, LBR recording will be enabled.
--- arch/x86/events/intel/lbr.c | 2 -- arch/x86/include/asm/msr-index.h | 1 + arch/x86/include/asm/vmx.h | 2 ++ arch/x86/kvm/vmx/pmu_intel.c | 48 ++++++++++++++++++++++++++++---- arch/x86/kvm/vmx/vmx.c | 9 ++++++ 5 files changed, 55 insertions(+), 7 deletions(-)
arch/x86/events/intel/lbr.c +0 -2
@@ -168,8 +168,6 @@ enum { ARCH_LBR_RETURN |\ ARCH_LBR_OTHER_BRANCH) -#define ARCH_LBR_CTL_MASK 0x7f000e -static void intel_pmu_lbr_filter (struct cpu_hw_events *cpuc); static __always_inline bool is_lbr_call_stack_bit_set(u64 config)
arch/x86/include/asm/msr-index.h +1 -0
@@ -169,6 +169,7 @@#define LBR_INFO_BR_TYPE (0xfull << LBR_INFO_BR_TYPE_OFFSET)#define MSR_ARCH_LBR_CTL 0x000014ce +#define ARCH_LBR_CTL_MASK 0x7f000e#define ARCH_LBR_CTL_LBREN BIT(0)#define ARCH_LBR_CTL_CPL_OFFSET 1#define ARCH_LBR_CTL_CPL (0x3ull << ARCH_LBR_CTL_CPL_OFFSET)
arch/x86/include/asm/vmx.h +2 -0
@@ -245,6 +245,8 @@ enum vmcs_field {GUEST_BNDCFGS_HIGH =0x00002813 ,GUEST_IA32_RTIT_CTL =0x00002814 ,GUEST_IA32_RTIT_CTL_HIGH =0x00002815 , +GUEST_IA32_LBR_CTL =0x00002816 , +GUEST_IA32_LBR_CTL_HIGH =0x00002817 ,HOST_IA32_PAT =0x00002c00 ,HOST_IA32_PAT_HIGH =0x00002c01 , HOST_IA32_EFER = 0x00002c02,
arch/x86/kvm/vmx/pmu_intel.c +43 -5
85 lines not on this page · raw message
arch/x86/kvm/vmx/vmx.c +9 -0
@@ -2088,6 +2088,13 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) VM_EXIT_SAVE_DEBUG_CONTROLS) get_vmcs12(vcpu)->guest_ia32_debugctl = data; + /* + * For Arch LBR, IA32_DEBUGCTL[bit 0] has no meaning. + * It can be written to 0 or 1, but reads will always return 0. + */ + if (guest_cpuid_has(vcpu, X86_FEATURE_ARCH_LBR)) + data &= ~DEBUGCTLMSR_LBR; + vmcs_write64(GUEST_IA32_DEBUGCTL, data); if (intel_pmu_lbr_is_enabled(vcpu) && !to_vmx(vcpu)->lbr_desc.event && (data & DEBUGCTLMSR_LBR))
@@ -4527,6 +4534,8 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)vmcs_writel (GUEST_SYSENTER_ESP ,0 );vmcs_writel (GUEST_SYSENTER_EIP ,0 );vmcs_write64 (GUEST_IA32_DEBUGCTL ,0 ); +if (static_cpu_has (X86_FEATURE_ARCH_LBR )) +vmcs_write64 (GUEST_IA32_LBR_CTL ,0 ); }kvm_set_rflags (vcpu,X86_EFLAGS_FIXED );
Signature · 1 line
2.21.1
PATCH v6 6/12 KVM: x86/pmu: Refactor code to support guest Arch LBR
In reply to Yang WeijiangTake account of Arch LBR when do sanity checks before program vPMU for guest. Pass through Arch LBR recording MSRs to guest to gain better performance. Note, Arch LBR and Legacy LBR support are mutually exclusive, i.e., they're not both available on one platform.
--- arch/x86/kvm/vmx/pmu_intel.c | 37 +++++++++++++++++++++++++++++------- arch/x86/kvm/vmx/vmx.c | 3 +++ 2 files changed, 33 insertions(+), 7 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +30 -7
@@ -176,12 +176,16 @@ static inline struct kvm_pmc *get_fw_gp_pmc(struct kvm_pmu *pmu, u32 msr)bool intel_pmu_lbr_is_compatible (struct kvm_vcpu *vcpu) { +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR )) +return guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR ); +/* * As a first step, a guest could only enable LBR feature if its * cpu model is the same as the host because the LBR registers * would be pass-through to the guest and they're model specific. */ -return boot_cpu_data.x86_model ==guest_cpuid_model (vcpu); +return !boot_cpu_has (X86_FEATURE_ARCH_LBR ) && + boot_cpu_data.x86_model ==guest_cpuid_model (vcpu); }bool intel_pmu_lbr_is_enabled (struct kvm_vcpu *vcpu)
@@ -199,12 +203,19 @@ static bool intel_pmu_is_valid_lbr_msr(struct kvm_vcpu *vcpu, u32 index)if (!intel_pmu_lbr_is_enabled (vcpu))return ret; - ret = (index ==MSR_LBR_SELECT ) || (index ==MSR_LBR_TOS ) || - (index >= records->from && index < records->from + records->nr) || - (index >= records->to && index < records->to + records->nr); +if (!guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR )) + ret = (index ==MSR_LBR_SELECT ) || (index ==MSR_LBR_TOS ); + +if (!ret) { + ret = (index >= records->from && + index < records->from + records->nr) || + (index >= records->to && + index < records->to + records->nr); + }if (!ret && records->info) - ret = (index >= records->info && index < records->info + records->nr); + ret = (index >= records->info && + index < records->info + records->nr);return ret; }
@@ -706,6 +717,9 @@ static void vmx_update_intercept_for_lbr_msrs(struct kvm_vcpu *vcpu, bool set)vmx_set_intercept_for_msr (vcpu, lbr->info + i,MSR_TYPE_RW , set); } +if (guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR )) +return ; +vmx_set_intercept_for_msr (vcpu,MSR_LBR_SELECT ,MSR_TYPE_RW , set);vmx_set_intercept_for_msr (vcpu,MSR_LBR_TOS ,MSR_TYPE_RW , set); }
@@ -746,10 +760,13 @@ void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu) {struct kvm_pmu *pmu =vcpu_to_pmu (vcpu);struct lbr_desc *lbr_desc =vcpu_to_lbr_desc (vcpu); +bool lbr_enable =guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR ) ? + (vmcs_read64 (GUEST_IA32_LBR_CTL ) &ARCH_LBR_CTL_LBREN ) : + (vmcs_read64 (GUEST_IA32_DEBUGCTL ) &DEBUGCTLMSR_LBR );if (!lbr_desc->event) {vmx_disable_lbr_msrs_passthrough (vcpu); -if (vmcs_read64 (GUEST_IA32_DEBUGCTL ) &DEBUGCTLMSR_LBR ) +if (lbr_enable) goto warn;if (test_bit (INTEL_PMC_IDX_FIXED_VLBR , pmu->pmc_in_use)) goto warn;
@@ -766,13 +783,19 @@ void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu)return ; warn: +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR )) +wrmsrl (MSR_ARCH_LBR_DEPTH , lbr_desc->records.nr);pr_warn_ratelimited ("kvm: vcpu-%d: fail to passthrough LBR.\n" , vcpu->vcpu_id); }static void intel_pmu_cleanup (struct kvm_vcpu *vcpu) { -if (!(vmcs_read64 (GUEST_IA32_DEBUGCTL ) &DEBUGCTLMSR_LBR )) +bool lbr_enable =guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR ) ? + (vmcs_read64 (GUEST_IA32_LBR_CTL ) &ARCH_LBR_CTL_LBREN ) : + (vmcs_read64 (GUEST_IA32_DEBUGCTL ) &DEBUGCTLMSR_LBR ); + +if (!lbr_enable)intel_pmu_release_guest_lbr_event (vcpu); }
arch/x86/kvm/vmx/vmx.c +3 -0
@@ -671,6 +671,9 @@ static bool is_valid_passthrough_msr(u32 msr) case MSR_LBR_NHM_TO ... MSR_LBR_NHM_TO + 31: case MSR_LBR_CORE_FROM ... MSR_LBR_CORE_FROM + 8: case MSR_LBR_CORE_TO ... MSR_LBR_CORE_TO + 8: + case MSR_ARCH_LBR_FROM_0 ... MSR_ARCH_LBR_FROM_0 + 31: + case MSR_ARCH_LBR_TO_0 ... MSR_ARCH_LBR_TO_0 + 31: + case MSR_ARCH_LBR_INFO_0 ... MSR_ARCH_LBR_INFO_0 + 31:/* LBR MSRs. These are handled in vmx_update_intercept_for_lbr_msrs() */ return true; }
Signature · 1 line
2.21.1
PATCH v6 7/12 KVM: x86: Refresh CPUID on writes to MSR_IA32_XSS
In reply to Yang WeijiangUpdated CPUID.0xD.0x1, which reports the current required storage size of all features enabled via XCR0 | XSS, when the guest's XSS is modified.
Note, KVM does not yet support any XSS based features, i.e. supported_xss is guaranteed to be zero at this time.
Message-Id: <[email protected]>
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/cpuid.c | 21 ++++++++++++++++++--- arch/x86/kvm/x86.c | 7 +++++-- 3 files changed, 24 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -651,6 +651,7 @@ struct kvm_vcpu_arch {u64 xcr0;u64 guest_supported_xcr0; +u64 guest_supported_xss;struct kvm_pio_request pio;void *pio_data;
arch/x86/kvm/cpuid.c +18 -3
@@ -131,9 +131,24 @@ void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu) best->ebx =xstate_required_size (vcpu->arch.xcr0, false); best =kvm_find_cpuid_entry (vcpu,0xD ,1 ); -if (best && (cpuid_entry_has (best,X86_FEATURE_XSAVES ) || -cpuid_entry_has (best,X86_FEATURE_XSAVEC ))) - best->ebx =xstate_required_size (vcpu->arch.xcr0, true); +if (best) { +if (cpuid_entry_has (best,X86_FEATURE_XSAVES ) || +cpuid_entry_has (best,X86_FEATURE_XSAVEC )) { +u64 xstate = vcpu->arch.xcr0 | vcpu->arch.ia32_xss; + + best->ebx =xstate_required_size (xstate, true); + } + +if (!cpuid_entry_has (best,X86_FEATURE_XSAVES )) { + best->ecx =0 ; + best->edx =0 ; + } + vcpu->arch.guest_supported_xss = + (((u64 )best->edx <<32 ) | best->ecx) & supported_xss; + + }else { + vcpu->arch.guest_supported_xss =0 ; + } best =kvm_find_cpuid_entry (vcpu,KVM_CPUID_FEATURES ,0 ); if (kvm_hlt_in_guest(vcpu->kvm) && best &&
arch/x86/kvm/x86.c +5 -2
@@ -3265,9 +3265,12 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)* IA32_XSS[bit 8]. Guests have to use RDMSR/WRMSR rather than * XSAVES/XRSTORS to save/restore PT MSRs. */ -if (data & ~supported_xss) +if (data & ~vcpu->arch.guest_supported_xss)return 1 ; - vcpu->arch.ia32_xss = data; +if (vcpu->arch.ia32_xss != data) { + vcpu->arch.ia32_xss = data; +kvm_update_cpuid_runtime (vcpu); + }break ;case MSR_SMI_COUNT :if (!msr_info->host_initiated)
Signature · 1 line
2.21.1
PATCH v6 8/12 KVM: x86: Report XSS as an MSR to be saved if there are supported features
In reply to Yang WeijiangFrom: Sean Christopherson <[email protected]>
Add MSR_IA32_XSS to the list of MSRs reported to userspace if supported_xss is non-zero, i.e. KVM supports at least one XSS based feature.
Message-Id: <[email protected]>
--- arch/x86/kvm/x86.c | 5 +++++ 1 file changed, 5 insertions(+)
arch/x86/kvm/x86.c +5 -0
@@ -1299,6 +1299,7 @@ static const u32 msrs_to_save_all[] = {MSR_ARCH_PERFMON_EVENTSEL0 +14 ,MSR_ARCH_PERFMON_EVENTSEL0 +15 ,MSR_ARCH_PERFMON_EVENTSEL0 +16 ,MSR_ARCH_PERFMON_EVENTSEL0 +17 ,MSR_ARCH_LBR_CTL ,MSR_ARCH_LBR_DEPTH , + MSR_IA32_XSS, };static u32 msrs_to_save[ARRAY_SIZE (msrs_to_save_all)];
@@ -6058,6 +6059,10 @@ static void kvm_init_msr_list(void)if (!kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR ))continue ;break ; +case MSR_IA32_XSS : +if (!supported_xss) +continue ; +break ;default :break ; }
Signature · 1 line
2.21.1
PATCH v6 9/12 KVM: x86: Refine the matching and clearing logic for supported_xss
In reply to Yang WeijiangFrom: Like Xu <[email protected]>
Refine the code path of the existing clearing of supported_xss in this way:
initialize the supported_xss with the filter of KVM_SUPPORTED_XSS mask and update its value in a bit clear manner (rather than bit setting).
--- arch/x86/kvm/vmx/vmx.c | 5 +++-- arch/x86/kvm/x86.c | 6 +++++- 2 files changed, 8 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/vmx.c +3 -2
@@ -7326,9 +7326,10 @@ static __init void vmx_set_cpu_caps(void)kvm_cpu_cap_set (X86_FEATURE_UMIP );/* CPUID 0xD.1 */ - supported_xss =0 ; -if (!cpu_has_vmx_xsaves ()) +if (!cpu_has_vmx_xsaves ()) {kvm_cpu_cap_clear (X86_FEATURE_XSAVES ); + supported_xss =0 ; + }/* CPUID 0x80000001 and 0x7 (RDPID) */ if (!cpu_has_vmx_rdtscp ()) {
arch/x86/kvm/x86.c +5 -1
@@ -203,6 +203,8 @@ static struct kvm_user_return_msrs __percpu *user_return_msrs; | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \ | XFEATURE_MASK_PKRU) +#define KVM_SUPPORTED_XSS 0 +u64 __read_mostly host_efer;EXPORT_SYMBOL_GPL (host_efer);
@@ -10654,8 +10656,10 @@ int kvm_arch_hardware_setup(void *opaque)rdmsrl_safe (MSR_EFER , &host_efer); -if (boot_cpu_has (X86_FEATURE_XSAVES )) +if (boot_cpu_has (X86_FEATURE_XSAVES )) {rdmsrl (MSR_IA32_XSS , host_xss); + supported_xss = host_xss &KVM_SUPPORTED_XSS ; + } r = ops->hardware_setup ();if (r !=0 )
Signature · 1 line
2.21.1
PATCH v6 10/12 KVM: x86: Add XSAVE Support for Architectural LBR
In reply to Yang WeijiangFrom: Like Xu <[email protected]>
On processors supporting XSAVES and XRSTORS, Architectural LBR XSAVE support is enumerated from CPUID.(EAX=0DH, ECX=1):ECX[bit 15]. The detailed sub-leaf for Arch LBR is enumerated in CPUID.(0DH, 0FH).
XSAVES provides a faster means than RDMSR for guest to read all LBRs. When guest IA32_XSS[bit 15] is set, the Arch LBR state can be saved using XSAVES and restored by XRSTORS with the appropriate RFBM.
--- arch/x86/kvm/vmx/vmx.c | 4 ++++ arch/x86/kvm/x86.c | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/vmx.c +4 -0
@@ -7314,6 +7314,10 @@ static __init void vmx_set_cpu_caps(void)kvm_cpu_cap_clear (X86_FEATURE_INVPCID );if (vmx_pt_mode_is_host_guest ())kvm_cpu_cap_check_and_set (X86_FEATURE_INTEL_PT ); +if (!cpu_has_vmx_arch_lbr ()) { +kvm_cpu_cap_clear (X86_FEATURE_ARCH_LBR ); + supported_xss &= ~XFEATURE_MASK_LBR ; + }if (!enable_sgx) {kvm_cpu_cap_clear (X86_FEATURE_SGX );
arch/x86/kvm/x86.c +1 -1
@@ -203,7 +203,7 @@ static struct kvm_user_return_msrs __percpu *user_return_msrs; | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \ | XFEATURE_MASK_PKRU) -#define KVM_SUPPORTED_XSS 0 +#define KVM_SUPPORTED_XSS XFEATURE_MASK_LBRu64 __read_mostly host_efer;EXPORT_SYMBOL_GPL (host_efer);
Signature · 1 line
2.21.1
PATCH v6 11/12 KVM: x86/vmx: Check Arch LBR config when return perf capabilities
In reply to Yang WeijiangTwo new bit fields(VM_EXIT_CLEAR_IA32_LBR_CTL, VM_ENTRY_LOAD_IA32_LBR_CTL) are added to support guest Arch LBR. These two bits should be set in order to make Arch LBR workable in both guest and host.
--- arch/x86/include/asm/vmx.h | 2 ++ arch/x86/kvm/vmx/capabilities.h | 25 +++++++++++++++++-------- arch/x86/kvm/vmx/vmx.c | 6 ++++-- 3 files changed, 23 insertions(+), 10 deletions(-)
arch/x86/include/asm/vmx.h +2 -0
@@ -95,6 +95,7 @@#define VM_EXIT_CLEAR_BNDCFGS 0x00800000#define VM_EXIT_PT_CONCEAL_PIP 0x01000000#define VM_EXIT_CLEAR_IA32_RTIT_CTL 0x02000000 +#define VM_EXIT_CLEAR_IA32_LBR_CTL 0x04000000#define VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR 0x00036dff
@@ -108,6 +109,7 @@#define VM_ENTRY_LOAD_BNDCFGS 0x00010000#define VM_ENTRY_PT_CONCEAL_PIP 0x00020000#define VM_ENTRY_LOAD_IA32_RTIT_CTL 0x00040000 +#define VM_ENTRY_LOAD_IA32_LBR_CTL 0x00200000#define VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR 0x000011ff
arch/x86/kvm/vmx/capabilities.h +17 -8
@@ -377,20 +377,29 @@ static inline bool vmx_pt_mode_is_host_guest(void)return pt_mode ==PT_MODE_HOST_GUEST ; } -static inline u64 vmx_get_perf_capabilities (void ) +static inline bool cpu_has_vmx_arch_lbr (void ) { -u64 perf_cap =0 ; - -if (boot_cpu_has (X86_FEATURE_PDCM )) -rdmsrl (MSR_IA32_PERF_CAPABILITIES , perf_cap); - - perf_cap &=PMU_CAP_LBR_FMT ; +return (vmcs_config.vmexit_ctrl &VM_EXIT_CLEAR_IA32_LBR_CTL ) && + (vmcs_config.vmentry_ctrl &VM_ENTRY_LOAD_IA32_LBR_CTL ); +} +static inline u64 vmx_get_perf_capabilities (void ) +{/* * Since counters are virtualized, KVM would support full * width counting unconditionally, even if the host lacks it. */ -return PMU_CAP_FW_WRITES | perf_cap; +u64 perf_cap =PMU_CAP_FW_WRITES ; +u64 host_perf_cap =0 ; + +if (boot_cpu_has (X86_FEATURE_PDCM )) +rdmsrl (MSR_IA32_PERF_CAPABILITIES , host_perf_cap); + + perf_cap |= host_perf_cap &PMU_CAP_LBR_FMT ; +if (boot_cpu_has (X86_FEATURE_ARCH_LBR ) && !cpu_has_vmx_arch_lbr ()) + perf_cap &= ~PMU_CAP_LBR_FMT ; + +return perf_cap; }static inline u64 vmx_supported_debugctl (void )
arch/x86/kvm/vmx/vmx.c +4 -2
@@ -2599,7 +2599,8 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf,VM_EXIT_LOAD_IA32_EFER |VM_EXIT_CLEAR_BNDCFGS |VM_EXIT_PT_CONCEAL_PIP | -VM_EXIT_CLEAR_IA32_RTIT_CTL ; +VM_EXIT_CLEAR_IA32_RTIT_CTL | +VM_EXIT_CLEAR_IA32_LBR_CTL ;if (adjust_vmx_controls (min, opt,MSR_IA32_VMX_EXIT_CTLS , &_vmexit_control) <0 )return -EIO ;
@@ -2623,7 +2624,8 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf,VM_ENTRY_LOAD_IA32_EFER |VM_ENTRY_LOAD_BNDCFGS |VM_ENTRY_PT_CONCEAL_PIP | -VM_ENTRY_LOAD_IA32_RTIT_CTL ; +VM_ENTRY_LOAD_IA32_RTIT_CTL | +VM_ENTRY_LOAD_IA32_LBR_CTL ;if (adjust_vmx_controls (min, opt,MSR_IA32_VMX_ENTRY_CTLS , &_vmentry_control) <0 )return -EIO ;
Signature · 1 line
2.21.1
PATCH v6 12/12 KVM: x86/cpuid: Advise Arch LBR feature in CPUID
In reply to Yang WeijiangAdd Arch LBR feature bit in CPU cap-mask to expose the feature. Currently only max LBR depth is available for guest, and it's consistent with host Arch LBR settings.
--- arch/x86/kvm/cpuid.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +32 -1
@@ -202,6 +202,16 @@ static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) best->ecx |=XFEATURE_MASK_FPSSE ; } + best =kvm_find_cpuid_entry (vcpu,0x1c ,0 ); +if (best) { +unsigned int eax, ebx, ecx, edx; + +/* Make sure guest only sees the supported arch lbr depth. */ +cpuid_count (0x1c ,0 , &eax, &ebx, &ecx, &edx); + best->eax &= ~0xff ; + best->eax |=BIT (fls (eax &0xff ) -1 ); + } +kvm_update_pv_runtime (vcpu); vcpu->arch.maxphyaddr =cpuid_query_maxphyaddr (vcpu);
@@ -490,7 +500,7 @@ void kvm_set_cpu_caps(void)F (AVX512_4VNNIW ) |F (AVX512_4FMAPS ) |F (SPEC_CTRL ) |F (SPEC_CTRL_SSBD ) |F (ARCH_CAPABILITIES ) |F (INTEL_STIBP ) |F (MD_CLEAR ) |F (AVX512_VP2INTERSECT ) |F (FSRM ) | -F (SERIALIZE ) |F (TSXLDTRK ) |F (AVX512_FP16 ) +F (SERIALIZE ) |F (TSXLDTRK ) |F (AVX512_FP16 ) |F (ARCH_LBR ) );/* TSC_ADJUST and ARCH_CAPABILITIES are emulated in software. */
@@ -902,6 +912,27 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) goto out; }break ; +/* Architectural LBR */ +case 0x1c : { +u32 lbr_depth_mask = entry->eax &0xff ; + +if (!lbr_depth_mask || + !kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR )) { + entry->eax = entry->ebx = entry->ecx = entry->edx =0 ; +break ; + } +/* +* KVM only exposes the maximum supported depth, which is the +* fixed value used on the host side. +* KVM doesn't allow VMM userspace to adjust LBR depth because +* guest LBR emulation depends on the configuration of host LBR +* driver. +*/ + lbr_depth_mask =BIT ((fls (lbr_depth_mask) -1 )); + entry->eax &= ~0xff ; + entry->eax |= lbr_depth_mask; +break ; + }case KVM_CPUID_SIGNATURE : {static const char signature[12 ] ="KVMKVMKVM\0\0" ;const u32 *sigptr = (const u32 *)signature;
Signature · 1 line
2.21.1