target/i386: Add AMD LBR Extension v2 support
v2 cover target/i386: Add AMD LBR Extension v2 support
This QEMU series exposes AMD's Last Branch Record Extension Version 2 (LbrExtV2) and the dependent LBR/PMC freeze feature to guests, so that they can use the LBRv2 virtualization provided by KVM.
LbrExtV2, introduced on Zen 4, is a branch sampling facility. It records the last taken branches in a 16-entry stack of dedicated MSRs:
* MSR 0xC000010E - Last Branch Stack Select (LBR_SELECT)
* MSR 0xC000010F - Debug Extension Control (DBG_EXTN_CFG)
* MSR 0xC0010300..0xC001031F - 16 LBR FROM/TO MSR pairs
v1: https://lore.kernel.org/qemu-devel/[email protected]/
Regards,
Shivansh
---
Changelog:
v1 -> v2:
* Rebased on v11.1.2.
* New patch to read and write the LbrExtV2 MSRs with KVM_GET_MSRS and KVM_SET_MSRS (patch 1).
* New patch to migrate the LbrExtV2 MSRs in a new VMState subsection (patch 2).
* Moved the CPUID_8000_0022_EAX_LBREXT_V2 definition to patch 1.
---
Shivansh Dhiman (4):
target/i386: Add MSR access interface for AMD LbrExtV2 target/i386: Add AMD LbrExtV2 states to vmstate target/i386: Add support for AMD LbrExtV2 target/i386: Add support for AMD LBR/PMC freeze
target/i386/cpu.c | 26 +++++++++++++++++++++++--- target/i386/cpu.h | 15 ++++++++++++++- target/i386/kvm/kvm.c | 31 +++++++++++++++++++++++++++++++ target/i386/machine.c | 24 ++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 4 deletions(-)
base-commit: 4fc49f46dc95d4a27de2509e7fceb2931e91faeb
Signature · 1 line
2.43.0
PATCH v2 1/4 target/i386: Add MSR access interface for AMD LbrExtV2
Read and write the LbrExtV2 MSRs with KVM_GET_MSRS and KVM_SET_MSRS. These MSRs are the Debug Extension Control MSR, the Last Branch Stack Select MSR and the 32 MSRs of the 16-entry LBR stack. Do this only when KVM is enabled, the vCPU has a PMU and LbrExtV2 is set.
---
Changes in v2:
* New patch.
--- target/i386/cpu.h | 12 ++++++++++++ target/i386/kvm/kvm.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+)
target/i386/cpu.h +12 -0
@@ -566,10 +566,16 @@ typedef enum X86Seg {#define MSR_AMD64_TSC_RATIO_DEFAULT 0x100000000ULL +#define MSR_AMD64_LBR_SELECT 0xc000010e +#define MSR_AMD_DBG_EXTN_CFG 0xc000010f +#define MSR_K7_HWCR 0xc0010015#define MSR_VM_HSAVE_PA 0xc0010117 +#define MSR_AMD_SAMP_BR_FROM 0xc0010300 +#define AMD_LBR_V2_NR_ENTRIES 16 +#define MSR_IA32_XFD 0x000001c4#define MSR_IA32_XFD_ERR 0x000001c5
@@ -1224,6 +1230,7 @@ uint64_t x86_cpu_get_supported_feature_word(X86CPU *cpu, FeatureWord w);/* Performance Monitoring Version 2 */ #define CPUID_8000_0022_EAX_PERFMON_V2 (1U << 0) +#define CPUID_8000_0022_EAX_LBREXT_V2 (1U << 1)#define CPUID_XSAVE_XSAVEOPT (1U << 0)#define CPUID_XSAVE_XSAVEC (1U << 1)
@@ -2176,6 +2183,11 @@ typedef struct CPUArchState {uint64_t msr_lbr_depth;LBREntry lbr_records[ARCH_LBR_NR_ENTRIES ]; +/* Per-VCPU AMD LbrExtV2 MSRs */ +uint64_t msr_amd_dbg_extn_cfg; +uint64_t msr_amd_lbr_select; +uint64_t msr_amd_lbr_stack[AMD_LBR_V2_NR_ENTRIES *2 ]; +/* AMD MSRC001_0015 Hardware Configuration */ uint64_t msr_hwcr;
target/i386/kvm/kvm.c +31 -0
@@ -4517,6 +4517,19 @@ static int kvm_put_msrs(X86CPU *cpu, KvmPutState level) } } +if (kvm_enabled () && cpu->enable_pmu && + (env->features[FEAT_8000_0022_EAX ] & +CPUID_8000_0022_EAX_LBREXT_V2 )) { +kvm_msr_entry_add (cpu,MSR_AMD_DBG_EXTN_CFG , + env->msr_amd_dbg_extn_cfg); +kvm_msr_entry_add (cpu,MSR_AMD64_LBR_SELECT , + env->msr_amd_lbr_select); +for (i =0 ; i <AMD_LBR_V2_NR_ENTRIES *2 ; i++) { +kvm_msr_entry_add (cpu,MSR_AMD_SAMP_BR_FROM + i, + env->msr_amd_lbr_stack[i]); + } + } +/* Note: MSR_IA32_FEATURE_CONTROL is written separately, see * kvm_put_msr_feature_control. */ }
@@ -5017,6 +5030,15 @@ static int kvm_get_msrs(X86CPU *cpu) } } +if (kvm_enabled () && cpu->enable_pmu && + (env->features[FEAT_8000_0022_EAX ] &CPUID_8000_0022_EAX_LBREXT_V2 )) { +kvm_msr_entry_add (cpu,MSR_AMD_DBG_EXTN_CFG ,0 ); +kvm_msr_entry_add (cpu,MSR_AMD64_LBR_SELECT ,0 ); +for (i =0 ; i <AMD_LBR_V2_NR_ENTRIES *2 ; i++) { +kvm_msr_entry_add (cpu,MSR_AMD_SAMP_BR_FROM + i,0 ); + } + } +if (env->features[FEAT_7_0_ECX ] &CPUID_7_0_ECX_CET_SHSTK || env->features[FEAT_7_0_EDX ] &CPUID_7_0_EDX_CET_IBT ) {kvm_msr_entry_add (cpu,MSR_IA32_U_CET ,0 );
@@ -5400,6 +5422,15 @@ static int kvm_get_msrs(X86CPU *cpu) case MSR_ARCH_LBR_INFO_0 ... MSR_ARCH_LBR_INFO_0 + 31: env->lbr_records[index -MSR_ARCH_LBR_INFO_0 ].info = msrs[i].data;break ; +case MSR_AMD_DBG_EXTN_CFG : + env->msr_amd_dbg_extn_cfg = msrs[i].data; +break ; +case MSR_AMD64_LBR_SELECT : + env->msr_amd_lbr_select = msrs[i].data; +break ; + case MSR_AMD_SAMP_BR_FROM ... MSR_AMD_SAMP_BR_FROM + 31: + env->msr_amd_lbr_stack[index -MSR_AMD_SAMP_BR_FROM ] = msrs[i].data; +break ;case MSR_IA32_U_CET : env->u_cet = msrs[i].data;break ;
Signature · 1 line
2.43.0
PATCH v2 2/4 target/i386: Add AMD LbrExtV2 states to vmstate
In reply to Shivansh DhimanMigrate the LbrExtV2 control and LBR stack MSRs in a new VMState subsection, so that the guest's LBR configuration and branch records survive live migration. Do it only when the vCPU has a PMU and LbrExtV2 is set.
---
Changes in v2:
* New patch.
--- target/i386/machine.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
target/i386/machine.c +24 -0
@@ -1648,6 +1648,29 @@ static const VMStateDescription vmstate_arch_lbr = { } }; +static bool amd_lbr_v2_needed (void *opaque) +{ +X86CPU *cpu = opaque; +CPUX86State *env = &cpu->env; + +return cpu->enable_pmu && + (env->features[FEAT_8000_0022_EAX ] &CPUID_8000_0022_EAX_LBREXT_V2 ); +} + +static const VMStateDescription vmstate_amd_lbr_v2 = { + .name ="cpu/amd_lbr_v2" , + .version_id =1 , + .minimum_version_id =1 , + .needed = amd_lbr_v2_needed, + .fields = (VMStateField []) { +VMSTATE_UINT64 (env.msr_amd_dbg_extn_cfg,X86CPU ), +VMSTATE_UINT64 (env.msr_amd_lbr_select,X86CPU ), +VMSTATE_UINT64_ARRAY (env.msr_amd_lbr_stack,X86CPU , +AMD_LBR_V2_NR_ENTRIES *2 ), +VMSTATE_END_OF_LIST () + } +}; +static bool triple_fault_needed (void *opaque) {X86CPU *cpu = opaque;
@@ -1910,6 +1933,7 @@ const VMStateDescription vmstate_x86_cpu = { &vmstate_amx_xtile, #endif &vmstate_arch_lbr, + &vmstate_amd_lbr_v2, &vmstate_triple_fault, &vmstate_pl0_ssp, &vmstate_cet,
Signature · 1 line
2.43.0
PATCH v2 3/4 target/i386: Add support for AMD LbrExtV2
In reply to Shivansh DhimanAdd the "lbrext-v2" CPU feature (CPUID 0x80000022.EAX[1]) and report the 16-entry LBR stack size in EBX[9:4], allowing guests to use AMD's Last Branch Record Extension v2 when KVM virtualizes it.
---
Changes in v2:
* Moved the CPUID_8000_0022_EAX_LBREXT_V2 definition to patch 1.
--- target/i386/cpu.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-)
target/i386/cpu.c +15 -3
@@ -1468,7 +1468,7 @@ FeatureWordInfo feature_word_info[FEATURE_WORDS] = { [FEAT_8000_0022_EAX] = { .type = CPUID_FEATURE_WORD, .feat_names = { -"perfmon-v2" ,NULL ,NULL ,NULL , +"perfmon-v2" ,"lbrext-v2" ,NULL ,NULL ,NULL ,NULL ,NULL ,NULL ,NULL ,NULL ,NULL ,NULL , NULL, NULL, NULL, NULL,
@@ -2048,6 +2048,10 @@ static FeatureDep feature_dependencies[] = { .from = { FEAT_7_1_EAX, CPUID_7_1_EAX_FRED }, .to = { FEAT_VMX_ENTRY_CTLS, VMX_VM_ENTRY_LOAD_IA32_FRED }, }, + { + .from = { FEAT_8000_0022_EAX, CPUID_8000_0022_EAX_PERFMON_V2 }, + .to = { FEAT_8000_0022_EAX, CPUID_8000_0022_EAX_LBREXT_V2 }, + }, }; typedef struct X86RegisterInfo32 {
@@ -9306,12 +9310,20 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,case 0x80000022 : *eax = *ebx = *ecx = *edx =0 ;/* AMD Extended Performance Monitoring and Debug */ -if (kvm_enabled () && cpu->enable_pmu && - (env->features[FEAT_8000_0022_EAX ] &CPUID_8000_0022_EAX_PERFMON_V2 )) { +if (!kvm_enabled () || !cpu->enable_pmu) { +break ; + } +if (env->features[FEAT_8000_0022_EAX ] &CPUID_8000_0022_EAX_PERFMON_V2 ) { *eax |=CPUID_8000_0022_EAX_PERFMON_V2 ; *ebx |=kvm_arch_get_supported_cpuid (cs->kvm_state, index, count,R_EBX ) &0xf ; } +/* Dependency PERFMON_V2 => LBREXT_V2 enforced via feature_dependencies[] */ +if (env->features[FEAT_8000_0022_EAX ] &CPUID_8000_0022_EAX_LBREXT_V2 ) { + *eax |=CPUID_8000_0022_EAX_LBREXT_V2 ; + *ebx |=kvm_arch_get_supported_cpuid (cs->kvm_state, index, count, +R_EBX ) &0x3f0 ;/* EBX[9:4] */ + }break ;case 0xC0000000 : *eax = env->cpuid_xlevel2;
Signature · 1 line
2.43.0
PATCH v2 4/4 target/i386: Add support for AMD LBR/PMC freeze
In reply to Shivansh DhimanAdd the "lbr-pmc-freeze" CPU feature (CPUID 0x80000022.EAX[2]), which lets a guest freeze the LBR stack and performance counters on a PMI. As it operates on the LBR v2 stack, make it depend on lbrext-v2.
---
Changes in v2:
* Rebased, no changes.
--- target/i386/cpu.c | 10 +++++++++- target/i386/cpu.h | 5 +++-- 2 files changed, 12 insertions(+), 3 deletions(-)
target/i386/cpu.c +9 -1
@@ -1468,7 +1468,7 @@ FeatureWordInfo feature_word_info[FEATURE_WORDS] = { [FEAT_8000_0022_EAX] = { .type = CPUID_FEATURE_WORD, .feat_names = { -"perfmon-v2" ,"lbrext-v2" ,NULL ,NULL , +"perfmon-v2" ,"lbrext-v2" ,"lbr-pmc-freeze" ,NULL ,NULL ,NULL ,NULL ,NULL ,NULL ,NULL ,NULL ,NULL , NULL, NULL, NULL, NULL,
@@ -2052,6 +2052,10 @@ static FeatureDep feature_dependencies[] = { .from = { FEAT_8000_0022_EAX, CPUID_8000_0022_EAX_PERFMON_V2 }, .to = { FEAT_8000_0022_EAX, CPUID_8000_0022_EAX_LBREXT_V2 }, }, + { + .from = { FEAT_8000_0022_EAX, CPUID_8000_0022_EAX_LBREXT_V2 }, + .to = { FEAT_8000_0022_EAX, CPUID_8000_0022_EAX_LBR_PMC_FREEZE }, + }, }; typedef struct X86RegisterInfo32 {
@@ -9324,6 +9328,10 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *ebx |=kvm_arch_get_supported_cpuid (cs->kvm_state, index, count,R_EBX ) &0x3f0 ;/* EBX[9:4] */ } +/* Dependency LBREXT_V2 => LBRPMC_FREEZE enforced via feature_dependencies[] */ +if (env->features[FEAT_8000_0022_EAX ] &CPUID_8000_0022_EAX_LBR_PMC_FREEZE ) { + *eax |=CPUID_8000_0022_EAX_LBR_PMC_FREEZE ; + }break ;case 0xC0000000 : *eax = env->cpuid_xlevel2;
target/i386/cpu.h +3 -2
@@ -1229,8 +1229,9 @@ uint64_t x86_cpu_get_supported_feature_word(X86CPU *cpu, FeatureWord w);#define CPUID_8000_0021_ECX_TSA_L1_NO (1U << 2)/* Performance Monitoring Version 2 */ -#define CPUID_8000_0022_EAX_PERFMON_V2 (1U << 0) -#define CPUID_8000_0022_EAX_LBREXT_V2 (1U << 1) +#define CPUID_8000_0022_EAX_PERFMON_V2 (1U << 0) +#define CPUID_8000_0022_EAX_LBREXT_V2 (1U << 1) +#define CPUID_8000_0022_EAX_LBR_PMC_FREEZE (1U << 2)#define CPUID_XSAVE_XSAVEOPT (1U << 0)#define CPUID_XSAVE_XSAVEC (1U << 1)
Signature · 1 line
2.43.0