i386/cpu: Add the Intel PT capabilities checking before extend the CPUID level
PATCH 1/2 i386/cpu: Add the Intel PT capabilities checking before extend the CPUID level
The current implementation will extend the CPUID level to 0x14 if Intel PT is enabled in the guest(in x86_cpu_expand_features()) and the Intel PT will be disabled if it can't pass the capabilities checking later(in x86_cpu_filter_features()). In this case, the level of CPUID will be still 0x14 and the CPUID values from leaf 0xe to 0x14 are all zero.
This patch moves the capabilities checking before setting the level of the CPUID.
--- target/i386/cpu.c | 63 ++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 31 deletions(-)
target/i386/cpu.c +32 -31
@@ -6401,12 +6401,40 @@ static void x86_cpu_expand_features(X86CPU *cpu, Error **errp)/* Intel Processor Trace requires CPUID[0x14] */ if ((env->features[FEAT_7_0_EBX ] &CPUID_7_0_EBX_INTEL_PT )) { -if (cpu->intel_pt_auto_level) { -x86_cpu_adjust_level (cpu, &cpu->env.cpuid_min_level,0x14 ); - }else if (cpu->env.cpuid_min_level <0x14 ) { +uint32_t eax_0, ebx_0, ecx_0, eax_1, ebx_1; + + eax_0 =kvm_arch_get_supported_cpuid (kvm_state,0x14 ,0 ,R_EAX ); + ebx_0 =kvm_arch_get_supported_cpuid (kvm_state,0x14 ,0 ,R_EBX ); + ecx_0 =kvm_arch_get_supported_cpuid (kvm_state,0x14 ,0 ,R_ECX ); + eax_1 =kvm_arch_get_supported_cpuid (kvm_state,0x14 ,1 ,R_EAX ); + ebx_1 =kvm_arch_get_supported_cpuid (kvm_state,0x14 ,1 ,R_EBX ); + +if (eax_0 && + ((ebx_0 &INTEL_PT_MINIMAL_EBX ) ==INTEL_PT_MINIMAL_EBX ) && + ((ecx_0 &INTEL_PT_MINIMAL_ECX ) ==INTEL_PT_MINIMAL_ECX ) && + ((eax_1 &INTEL_PT_MTC_BITMAP ) ==INTEL_PT_MTC_BITMAP ) && + ((eax_1 &INTEL_PT_ADDR_RANGES_NUM_MASK ) >= +INTEL_PT_ADDR_RANGES_NUM ) && + ((ebx_1 & (INTEL_PT_PSB_BITMAP |INTEL_PT_CYCLE_BITMAP )) == + (INTEL_PT_PSB_BITMAP |INTEL_PT_CYCLE_BITMAP )) && + !(ecx_0 &INTEL_PT_IP_LIP )) { +if (cpu->intel_pt_auto_level) { +x86_cpu_adjust_level (cpu, &cpu->env.cpuid_min_level,0x14 ); + }else if (cpu->env.cpuid_min_level <0x14 ) { +mark_unavailable_features (cpu,FEAT_7_0_EBX , +CPUID_7_0_EBX_INTEL_PT , +"Intel PT need CPUID leaf 0x14, please set by \"-cpu ...,+intel-pt,min-level=0x14\"" ); + } + }else { +/* +* Processor Trace capabilities aren't configurable, so if the +* host can't emulate the capabilities we report on +* cpu_x86_cpuid(), intel-pt can't be enabled on the current +* host. +*/ mark_unavailable_features (cpu,FEAT_7_0_EBX ,CPUID_7_0_EBX_INTEL_PT , -"Intel PT need CPUID leaf 0x14, please set by \"-cpu ...,+intel-pt,min-level=0x14\"" ); +"host Intel PT features doesn't satisfy the guest request." ); } }
@@ -6466,33 +6494,6 @@ static void x86_cpu_filter_features(X86CPU *cpu, bool verbose)uint64_t unavailable_features = requested_features & ~host_feat;mark_unavailable_features (cpu, w, unavailable_features, prefix); } - -if ((env->features[FEAT_7_0_EBX ] &CPUID_7_0_EBX_INTEL_PT ) && -kvm_enabled ()) { -KVMState *s =CPU (cpu)->kvm_state; -uint32_t eax_0 =kvm_arch_get_supported_cpuid (s,0x14 ,0 ,R_EAX ); -uint32_t ebx_0 =kvm_arch_get_supported_cpuid (s,0x14 ,0 ,R_EBX ); -uint32_t ecx_0 =kvm_arch_get_supported_cpuid (s,0x14 ,0 ,R_ECX ); -uint32_t eax_1 =kvm_arch_get_supported_cpuid (s,0x14 ,1 ,R_EAX ); -uint32_t ebx_1 =kvm_arch_get_supported_cpuid (s,0x14 ,1 ,R_EBX ); - -if (!eax_0 || - ((ebx_0 &INTEL_PT_MINIMAL_EBX ) !=INTEL_PT_MINIMAL_EBX ) || - ((ecx_0 &INTEL_PT_MINIMAL_ECX ) !=INTEL_PT_MINIMAL_ECX ) || - ((eax_1 &INTEL_PT_MTC_BITMAP ) !=INTEL_PT_MTC_BITMAP ) || - ((eax_1 &INTEL_PT_ADDR_RANGES_NUM_MASK ) < -INTEL_PT_ADDR_RANGES_NUM ) || - ((ebx_1 & (INTEL_PT_PSB_BITMAP |INTEL_PT_CYCLE_BITMAP )) != - (INTEL_PT_PSB_BITMAP |INTEL_PT_CYCLE_BITMAP )) || - (ecx_0 &INTEL_PT_IP_LIP )) { -/* -* Processor Trace capabilities aren't configurable, so if the -* host can't emulate the capabilities we report on -* cpu_x86_cpuid(), intel-pt can't be enabled on the current host. -*/ -mark_unavailable_features (cpu,FEAT_7_0_EBX ,CPUID_7_0_EBX_INTEL_PT , prefix); - } - } }static void x86_cpu_realizefn (DeviceState *dev,Error **errp)
Signature · 1 line
2.18.4
PATCH 2/2 i386/cpu: Make the Intel PT LIP feature configurable
The current implementation will disable the guest Intel PT feature if the Intel PT LIP feature is supported on the host, but the LIP feature is comming soon(e.g. SnowRidge and later).
This patch will make the guest LIP feature configurable and Intel PT feature can be enabled in guest when the guest LIP status same with the host.
--- target/i386/cpu.c | 29 +++++++++++++++++++++++++++-- target/i386/cpu.h | 4 ++++ 2 files changed, 31 insertions(+), 2 deletions(-)
target/i386/cpu.c +27 -2
@@ -672,6 +672,7 @@ static void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1,#define TCG_XSAVE_FEATURES (CPUID_XSAVE_XSAVEOPT | CPUID_XSAVE_XGETBV1)/* missing: CPUID_XSAVE_XSAVEC, CPUID_XSAVE_XSAVES */ +#define TCG_14_0_ECX_FEATURES 0 typedef enum FeatureWordType { CPUID_FEATURE_WORD,
@@ -1301,6 +1302,26 @@ static FeatureWordInfo feature_word_info[FEATURE_WORDS] = { } }, + [FEAT_14_0_ECX ] = { + .type =CPUID_FEATURE_WORD , + .feat_names = { +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,"intel-pt-lip" , + }, + .cpuid = { + .eax =0x14 , + .needs_ecx = true, .ecx =0 , + .reg =R_ECX , + }, + .tcg_features =TCG_14_0_ECX_FEATURES , + }, + }; typedef struct FeatureMask {
@@ -5743,6 +5764,9 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *eax =INTEL_PT_MAX_SUBLEAF ; *ebx =INTEL_PT_MINIMAL_EBX ; *ecx =INTEL_PT_MINIMAL_ECX ; +if (env->features[FEAT_14_0_ECX ] &CPUID_14_0_ECX_LIP ) { + *ecx |=CPUID_14_0_ECX_LIP ; + } }else if (count ==1 ) { *eax =INTEL_PT_MTC_BITMAP |INTEL_PT_ADDR_RANGES_NUM ; *ebx =INTEL_PT_PSB_BITMAP |INTEL_PT_CYCLE_BITMAP ;
@@ -6416,8 +6440,9 @@ static void x86_cpu_expand_features(X86CPU *cpu, Error **errp) ((eax_1 &INTEL_PT_ADDR_RANGES_NUM_MASK ) >=INTEL_PT_ADDR_RANGES_NUM ) && ((ebx_1 & (INTEL_PT_PSB_BITMAP |INTEL_PT_CYCLE_BITMAP )) == - (INTEL_PT_PSB_BITMAP |INTEL_PT_CYCLE_BITMAP )) && - !(ecx_0 & INTEL_PT_IP_LIP)) { + (INTEL_PT_PSB_BITMAP |INTEL_PT_CYCLE_BITMAP )) && + ((ecx_0 &CPUID_14_0_ECX_LIP ) == + (env->features[FEAT_14_0_ECX] & CPUID_14_0_ECX_LIP))) { if (cpu->intel_pt_auto_level) {x86_cpu_adjust_level (cpu, &cpu->env.cpuid_min_level,0x14 ); } else if (cpu->env.cpuid_min_level < 0x14) {
target/i386/cpu.h +4 -0
@@ -541,6 +541,7 @@ typedef enum FeatureWord {FEAT_VMX_EPT_VPID_CAPS ,FEAT_VMX_BASIC ,FEAT_VMX_VMFUNC , +FEAT_14_0_ECX ,FEATURE_WORDS , } FeatureWord;
@@ -797,6 +798,9 @@ typedef uint64_t FeatureWordArray[FEATURE_WORDS];/* AVX512 BFloat16 Instruction */ #define CPUID_7_1_EAX_AVX512_BF16 (1U << 5) +/* Packets which contain IP payload have LIP values */ +#define CPUID_14_0_ECX_LIP (1U << 31) +/* CLZERO instruction */ #define CPUID_8000_0008_EBX_CLZERO (1U << 0)/* Always save/restore FP error pointers */
Signature · 1 line
2.18.4
RE: [PATCH 2/2] i386/cpu: Make the Intel PT LIP feature configurable
Quoted 7 lines
-----Original Message-----
From: Kang, Luwei <[email protected]>
Sent: Wednesday, October 14, 2020 4:05 PM
To: [email protected]; [email protected]; [email protected]
Cc: [email protected]; Kang, Luwei <[email protected]>
Subject: [PATCH 2/2] i386/cpu: Make the Intel PT LIP feature configurable
Quoted 6 lines
The current implementation will disable the guest Intel PT feature if the Intel PT LIP feature is supported on the host, but the LIP feature is comming soon(e.g. SnowRidge and later).
This patch will make the guest LIP feature configurable and Intel PT feature can be enabled in guest when the guest LIP status same with the host.
Ping. 😊
Thanks,
Luwei Kang
Quoted 95 lines
Signed-off-by Luwei Kang <[email protected]>---
target/i386/cpu.c| 29 +++++++++++++++++++++++++++--target/i386/cpu.h
| 4 ++++2 files changed, 31 insertions(+), 2 deletions(-)target/i386/cpu.c index renamed from target/i386/cpu.c +0
-0
24644abfd4..aeabdd5bd4100644target/i386/cpu.c +0
-0@@ -672,6 +672,7 @@ static void x86_cpu_vendor_words2str(char *dst,
uint32_tvendor1, #defineTCG_XSAVE_FEATURES(CPUID_XSAVE_XSAVEOPT|CPUID_XSAVE_XGETBV1)
/* missing:
CPUID_XSAVE_XSAVEC,CPUID_XSAVE_XSAVES*/target/i386/cpu.c +21
-0+#define TCG_14_0_ECX_FEATURES 0 typedef enum FeatureWordType { CPUID_FEATURE_WORD,@@ -1301,6 +1302,26 @@ static FeatureWordInfo feature_word_info[FEATURE_WORDS] = { } }, + [FEAT_14_0_ECX ] = { + .type =CPUID_FEATURE_WORD , + .feat_names = { +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,"intel-pt-lip" , + }, + .cpuid = { + .eax =0x14 , + .needs_ecx = true, .ecx =0 , + .reg =R_ECX , + }, + .tcg_features =TCG_14_0_ECX_FEATURES , + }, + };typedef struct FeatureMask {
target/i386/cpu.c +3
-0@@ -5743,6 +5764,9 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *eax =INTEL_PT_MAX_SUBLEAF ; *ebx =INTEL_PT_MINIMAL_EBX ; *ecx =INTEL_PT_MINIMAL_ECX ; +if (env->features[FEAT_14_0_ECX ] &CPUID_14_0_ECX_LIP ) { + *ecx |=CPUID_14_0_ECX_LIP ; + } }else if (count ==1 ) { *eax =INTEL_PT_MTC_BITMAP |INTEL_PT_ADDR_RANGES_NUM ;*ebx = INTEL_PT_PSB_BITMAP | INTEL_PT_CYCLE_BITMAP; @@ -6416,8 +6440,9 @@ static void x86_cpu_expand_features(X86CPU *cpu, Error **errp) ((eax_1 & INTEL_PT_ADDR_RANGES_NUM_MASK) >= INTEL_PT_ADDR_RANGES_NUM) && ((ebx_1 & (INTEL_PT_PSB_BITMAP | INTEL_PT_CYCLE_BITMAP)) ==target/i386/cpu.c +4
-2- (INTEL_PT_PSB_BITMAP | INTEL_PT_CYCLE_BITMAP)) && - !(ecx_0 & INTEL_PT_IP_LIP)) { + (INTEL_PT_PSB_BITMAP | INTEL_PT_CYCLE_BITMAP)) && + ((ecx_0 & CPUID_14_0_ECX_LIP) == + (env->features[FEAT_14_0_ECX] & + CPUID_14_0_ECX_LIP))) {if (cpu->intel_pt_auto_level) {x86_cpu_adjust_level (cpu, &cpu->env.cpuid_min_level,0x14 ); } else if (cpu->env.cpuid_min_level < 0x14) { diff --git
a/target/i386/cpu.hb/target/i386/cpu.hindex51c1d5f60a..1fcd93e39a100644target/i386/cpu.h +4
-1@@ -541,6 +541,7 @@ typedef enum FeatureWord {FEAT_VMX_EPT_VPID_CAPS ,FEAT_VMX_BASIC ,FEAT_VMX_VMFUNC , +FEAT_14_0_ECX ,FEATURE_WORDS , } FeatureWord;@@ -797,6 +798,9 @@ typedef uint64_t FeatureWordArray[FEATURE_WORDS];/* AVX512 BFloat16 Instruction */ #define CPUID_7_1_EAX_AVX512_BF16 (1U << 5) +/* Packets which contain IP payload have LIP values */ +#define CPUID_14_0_ECX_LIP (1U << 31) +/* CLZERO instruction */ #define CPUID_8000_0008_EBX_CLZERO (1U << 0)/* Always save/restore FP error pointers */ --2.18.4
Re: [PATCH 2/2] i386/cpu: Make the Intel PT LIP feature configurable
In reply to Luwei KangQuoted 9 lines · Luwei Kang
The current implementation will disable the guest Intel PT feature if the Intel PT LIP feature is supported on the host, but the LIP feature is comming soon(e.g. SnowRidge and later).
This patch will make the guest LIP feature configurable and Intel PT feature can be enabled in guest when the guest LIP status same with the host.
Signed-off-by Luwei Kang <[email protected]>
The patch looks good, but I would like to confirm if the feature works as expected if patch 1/2 is not applied.
I have manually fixed it up to make patch 1/1 not required, but I would like to confirm it really works on both Intel hosts with LIP=1 and hosts with LIP=0. Could you please test the following version of the patch?
-------
From 8a2c9fdbe50f7ffd9900c67e72f53c8dc85ff3fd Mon Sep 17 00:00:00 2001
From: Luwei Kang <[email protected]>
Date: Wed, 14 Oct 2020 16:04:43 +0800
Subject: [PATCH] i386/cpu: Make the Intel PT LIP feature configurable
The current implementation will disable the guest Intel PT feature if the Intel PT LIP feature is supported on the host, but the LIP feature is comming soon(e.g. SnowRidge and later).
This patch will make the guest LIP feature configurable and Intel PT feature can be enabled in guest when the guest LIP status same with the host.
Message-Id: <[email protected]>
--- target/i386/cpu.h | 4 ++++ target/i386/cpu.c | 27 ++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-)
target/i386/cpu.h +4 -0
@@ -541,6 +541,7 @@ typedef enum FeatureWord {FEAT_VMX_EPT_VPID_CAPS ,FEAT_VMX_BASIC ,FEAT_VMX_VMFUNC , +FEAT_14_0_ECX ,FEATURE_WORDS , } FeatureWord;
@@ -797,6 +798,9 @@ typedef uint64_t FeatureWordArray[FEATURE_WORDS];/* AVX512 BFloat16 Instruction */ #define CPUID_7_1_EAX_AVX512_BF16 (1U << 5) +/* Packets which contain IP payload have LIP values */ +#define CPUID_14_0_ECX_LIP (1U << 31) +/* CLZERO instruction */ #define CPUID_8000_0008_EBX_CLZERO (1U << 0)/* Always save/restore FP error pointers */
target/i386/cpu.c +26 -1
@@ -672,6 +672,7 @@ static void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1,#define TCG_XSAVE_FEATURES (CPUID_XSAVE_XSAVEOPT | CPUID_XSAVE_XGETBV1)/* missing: CPUID_XSAVE_XSAVEC, CPUID_XSAVE_XSAVES */ +#define TCG_14_0_ECX_FEATURES 0 typedef enum FeatureWordType { CPUID_FEATURE_WORD,
@@ -1301,6 +1302,26 @@ static FeatureWordInfo feature_word_info[FEATURE_WORDS] = { } }, + [FEAT_14_0_ECX ] = { + .type =CPUID_FEATURE_WORD , + .feat_names = { +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,"intel-pt-lip" , + }, + .cpuid = { + .eax =0x14 , + .needs_ecx = true, .ecx =0 , + .reg =R_ECX , + }, + .tcg_features =TCG_14_0_ECX_FEATURES , + }, + }; typedef struct FeatureMask {
@@ -5752,6 +5773,9 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *eax =INTEL_PT_MAX_SUBLEAF ; *ebx =INTEL_PT_MINIMAL_EBX ; *ecx =INTEL_PT_MINIMAL_ECX ; +if (env->features[FEAT_14_0_ECX ] &CPUID_14_0_ECX_LIP ) { + *ecx |=CPUID_14_0_ECX_LIP ; + } }else if (count ==1 ) { *eax =INTEL_PT_MTC_BITMAP |INTEL_PT_ADDR_RANGES_NUM ; *ebx =INTEL_PT_PSB_BITMAP |INTEL_PT_CYCLE_BITMAP ;
@@ -6498,7 +6522,8 @@ static void x86_cpu_filter_features(X86CPU *cpu, bool verbose) INTEL_PT_ADDR_RANGES_NUM) || ((ebx_1 & (INTEL_PT_PSB_BITMAP |INTEL_PT_CYCLE_BITMAP )) != (INTEL_PT_PSB_BITMAP |INTEL_PT_CYCLE_BITMAP )) || - (ecx_0 & INTEL_PT_IP_LIP)) { + ((ecx_0 &CPUID_14_0_ECX_LIP ) != + (env->features[FEAT_14_0_ECX] & CPUID_14_0_ECX_LIP))) {/* * Processor Trace capabilities aren't configurable, so if the * host can't emulate the capabilities we report on
Signature · 4 lines
2.28.0 -- Eduardo
RE: [PATCH 2/2] i386/cpu: Make the Intel PT LIP feature configurable
Quoted 10 lines
Quoted 9 lines
The current implementation will disable the guest Intel PT feature if the Intel PT LIP feature is supported on the host, but the LIP feature is comming soon(e.g. SnowRidge and later).
This patch will make the guest LIP feature configurable and Intel PT feature can be enabled in guest when the guest LIP status same with the host.
Signed-off-by Luwei Kang <[email protected]>
Quoted 7 lines
The patch looks good, but I would like to confirm if the feature works as expected if patch 1/2 is not applied.
I have manually fixed it up to make patch 1/1 not required, but I would like to confirm it really works on both Intel hosts with
LIP=1 and hosts with LIP=0. Could you please test the following version of the patch?
Hi Eduardo,
Thanks for your reply. I sent out a new version(V2) which adds a dependency flag based on your modification. And all the test cases and results include HW w/ and w/o LIP list in version 2.
Thanks,
Luwei Kang
Quoted 110 lines
-------
From8a2c9fdbe50f7ffd9900c67e72f53c8dc85ff3fdMon Sep 17 00:00:00 2001
From: Luwei Kang <[email protected]>
Date: Wed, 14 Oct 2020 16:04:43 +0800
Subject: [PATCH] i386/cpu: Make the Intel PT LIP feature configurableThe current implementation will disable the guest Intel PT feature if the Intel PT LIP feature is supported on the host, but the LIP feature is comming soon(e.g. SnowRidge and later).
This patch will make the guest LIP feature configurable and Intel PT feature can be enabled in guest when the guest LIP status same with the host.
Signed-off-by Luwei Kang <[email protected]>Message-Id: <[email protected]>
Signed-off-by Eduardo Habkost <[email protected]>--- target/i386/cpu.h | 4 ++++ target/i386/cpu.c | 27 ++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-)target/i386/cpu.h index 88e8586f8f..c4a49c06a8 renamed from target/i386/cpu.h +0
-0100644
target/i386/cpu.h +4
-0@@ -541,6 +541,7 @@ typedef enum FeatureWord {FEAT_VMX_EPT_VPID_CAPS ,FEAT_VMX_BASIC ,FEAT_VMX_VMFUNC , +FEAT_14_0_ECX ,FEATURE_WORDS , } FeatureWord;@@ -797,6 +798,9 @@ typedef uint64_t FeatureWordArray[FEATURE_WORDS];/* AVX512 BFloat16 Instruction */ #define CPUID_7_1_EAX_AVX512_BF16 (1U << 5) +/* Packets which contain IP payload have LIP values */ +#define CPUID_14_0_ECX_LIP (1U << 31) +/* CLZERO instruction */ #define CPUID_8000_0008_EBX_CLZERO (1U << 0) /* Always save/restore FP error pointers */ diff --git a/target/i386/cpu.c
b/target/i386/cpu.cindex5a8c96072e..db9e48033d100644target/i386/cpu.c +0
-0@@ -672,6 +672,7 @@ static void x86_cpu_vendor_words2str(char *dst,
uint32_tvendor1, #defineTCG_XSAVE_FEATURES(CPUID_XSAVE_XSAVEOPT|CPUID_XSAVE_XGETBV1)
/* missing:
CPUID_XSAVE_XSAVEC,CPUID_XSAVE_XSAVES*/target/i386/cpu.c +21
-0+#define TCG_14_0_ECX_FEATURES 0 typedef enum FeatureWordType { CPUID_FEATURE_WORD,@@ -1301,6 +1302,26 @@ static FeatureWordInfo feature_word_info[FEATURE_WORDS] = { } }, + [FEAT_14_0_ECX ] = { + .type =CPUID_FEATURE_WORD , + .feat_names = { +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,NULL , +NULL ,NULL ,NULL ,"intel-pt-lip" , + }, + .cpuid = { + .eax =0x14 , + .needs_ecx = true, .ecx =0 , + .reg =R_ECX , + }, + .tcg_features =TCG_14_0_ECX_FEATURES , + }, + };typedef struct FeatureMask {
target/i386/cpu.c +3
-0@@ -5752,6 +5773,9 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *eax =INTEL_PT_MAX_SUBLEAF ; *ebx =INTEL_PT_MINIMAL_EBX ; *ecx =INTEL_PT_MINIMAL_ECX ; +if (env->features[FEAT_14_0_ECX ] &CPUID_14_0_ECX_LIP ) { + *ecx |=CPUID_14_0_ECX_LIP ; + } }else if (count ==1 ) { *eax =INTEL_PT_MTC_BITMAP |INTEL_PT_ADDR_RANGES_NUM ;*ebx = INTEL_PT_PSB_BITMAP | INTEL_PT_CYCLE_BITMAP; @@ -6498,7 +6522,8 @@ static void x86_cpu_filter_features(X86CPU *cpu, bool verbose) INTEL_PT_ADDR_RANGES_NUM) || ((ebx_1 & (INTEL_PT_PSB_BITMAP | INTEL_PT_CYCLE_BITMAP)) != (INTEL_PT_PSB_BITMAP | INTEL_PT_CYCLE_BITMAP)) ||target/i386/cpu.c +2
-2- (ecx_0 & INTEL_PT_IP_LIP)) { + ((ecx_0 & CPUID_14_0_ECX_LIP) != + (env->features[FEAT_14_0_ECX] & CPUID_14_0_ECX_LIP))) {/* * Processor Trace capabilities aren't configurable, so if the * host can't emulate the capabilities we report on -- 2.28.0
--
Eduardo
Hi,
Sorry for the long delay in reviewing this. Now that 5.2 is about to be released, we can try to merge this.
Comments below:
Quoted 9 lines · Luwei Kang
The current implementation will extend the CPUID level to
0x14if Intel PT is enabled in theguest(in x86_cpu_expand_features())and the Intel PT will be disabled if it can't pass the capabilities checkinglater(in x86_cpu_filter_features()). In this case, the level of CPUID will be still0x14and the CPUID values from leaf0xeto0x14are all zero.This patch moves the capabilities checking before setting the level of the CPUID.
Why is this patch necessary and what problem does it fix? Is it a nice to have feature, or a bug fix?
If you still want to change how the x86_cpu_adjust_level() code behaves, it should apply to all features filtered by x86_cpu_filter_features(), not just intel-pt, shouldn't it?
Quoted 92 lines
Signed-off-by Luwei Kang <[email protected]>--- target/i386/cpu.c | 63 ++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 31 deletions(-)target/i386/cpu.c +32
-31@@ -6401,12 +6401,40 @@ static void x86_cpu_expand_features(X86CPU *cpu, Error **errp)/* Intel Processor Trace requires CPUID[0x14] */ if ((env->features[FEAT_7_0_EBX ] &CPUID_7_0_EBX_INTEL_PT )) { -if (cpu->intel_pt_auto_level) { -x86_cpu_adjust_level (cpu, &cpu->env.cpuid_min_level,0x14 ); - }else if (cpu->env.cpuid_min_level <0x14 ) { +uint32_t eax_0, ebx_0, ecx_0, eax_1, ebx_1; + + eax_0 =kvm_arch_get_supported_cpuid (kvm_state,0x14 ,0 ,R_EAX ); + ebx_0 =kvm_arch_get_supported_cpuid (kvm_state,0x14 ,0 ,R_EBX ); + ecx_0 =kvm_arch_get_supported_cpuid (kvm_state,0x14 ,0 ,R_ECX ); + eax_1 =kvm_arch_get_supported_cpuid (kvm_state,0x14 ,1 ,R_EAX ); + ebx_1 =kvm_arch_get_supported_cpuid (kvm_state,0x14 ,1 ,R_EBX ); + +if (eax_0 && + ((ebx_0 &INTEL_PT_MINIMAL_EBX ) ==INTEL_PT_MINIMAL_EBX ) && + ((ecx_0 &INTEL_PT_MINIMAL_ECX ) ==INTEL_PT_MINIMAL_ECX ) && + ((eax_1 &INTEL_PT_MTC_BITMAP ) ==INTEL_PT_MTC_BITMAP ) && + ((eax_1 &INTEL_PT_ADDR_RANGES_NUM_MASK ) >= +INTEL_PT_ADDR_RANGES_NUM ) && + ((ebx_1 & (INTEL_PT_PSB_BITMAP |INTEL_PT_CYCLE_BITMAP )) == + (INTEL_PT_PSB_BITMAP |INTEL_PT_CYCLE_BITMAP )) && + !(ecx_0 &INTEL_PT_IP_LIP )) { +if (cpu->intel_pt_auto_level) { +x86_cpu_adjust_level (cpu, &cpu->env.cpuid_min_level,0x14 ); + }else if (cpu->env.cpuid_min_level <0x14 ) { +mark_unavailable_features (cpu,FEAT_7_0_EBX , +CPUID_7_0_EBX_INTEL_PT , +"Intel PT need CPUID leaf 0x14, please set by \"-cpu ...,+intel-pt,min-level=0x14\"" ); + } + }else { +/* +* Processor Trace capabilities aren't configurable, so if the +* host can't emulate the capabilities we report on +* cpu_x86_cpuid(), intel-pt can't be enabled on the current +* host. +*/ mark_unavailable_features (cpu,FEAT_7_0_EBX ,CPUID_7_0_EBX_INTEL_PT , -"Intel PT need CPUID leaf 0x14, please set by \"-cpu ...,+intel-pt,min-level=0x14\"" ); +"host Intel PT features doesn't satisfy the guest request." ); } }@@ -6466,33 +6494,6 @@ static void x86_cpu_filter_features(X86CPU *cpu, bool verbose)uint64_t unavailable_features = requested_features & ~host_feat;mark_unavailable_features (cpu, w, unavailable_features, prefix); } - -if ((env->features[FEAT_7_0_EBX ] &CPUID_7_0_EBX_INTEL_PT ) && -kvm_enabled ()) { -KVMState *s =CPU (cpu)->kvm_state; -uint32_t eax_0 =kvm_arch_get_supported_cpuid (s,0x14 ,0 ,R_EAX ); -uint32_t ebx_0 =kvm_arch_get_supported_cpuid (s,0x14 ,0 ,R_EBX ); -uint32_t ecx_0 =kvm_arch_get_supported_cpuid (s,0x14 ,0 ,R_ECX ); -uint32_t eax_1 =kvm_arch_get_supported_cpuid (s,0x14 ,1 ,R_EAX ); -uint32_t ebx_1 =kvm_arch_get_supported_cpuid (s,0x14 ,1 ,R_EBX ); - -if (!eax_0 || - ((ebx_0 &INTEL_PT_MINIMAL_EBX ) !=INTEL_PT_MINIMAL_EBX ) || - ((ecx_0 &INTEL_PT_MINIMAL_ECX ) !=INTEL_PT_MINIMAL_ECX ) || - ((eax_1 &INTEL_PT_MTC_BITMAP ) !=INTEL_PT_MTC_BITMAP ) || - ((eax_1 &INTEL_PT_ADDR_RANGES_NUM_MASK ) < -INTEL_PT_ADDR_RANGES_NUM ) || - ((ebx_1 & (INTEL_PT_PSB_BITMAP |INTEL_PT_CYCLE_BITMAP )) != - (INTEL_PT_PSB_BITMAP |INTEL_PT_CYCLE_BITMAP )) || - (ecx_0 &INTEL_PT_IP_LIP )) { -/* -* Processor Trace capabilities aren't configurable, so if the -* host can't emulate the capabilities we report on -* cpu_x86_cpuid(), intel-pt can't be enabled on the current host. -*/ -mark_unavailable_features (cpu,FEAT_7_0_EBX ,CPUID_7_0_EBX_INTEL_PT , prefix); - } - } }static void x86_cpu_realizefn (DeviceState *dev,Error **errp)Signature · 1 line
2.18.4
Signature · 1 line
Eduardo
Quoted 26 lines
-----Original Message-----
From: Eduardo Habkost <[email protected]>
Sent: Wednesday, December 2, 2020 5:12 AM
To: Kang, Luwei <[email protected]>
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 1/2] i386/cpu: Add the Intel PT capabilities checking before extend the CPUID levelHi,
Sorry for the long delay in reviewing this. Now that 5.2 is about to be released, we can try to merge this.
Comments below:
Quoted 9 lines · Luwei Kang
The current implementation will extend the CPUID level to
0x14if Intel PT is enabled in theguest(in x86_cpu_expand_features())and the Intel PT will be disabled if it can't pass the capabilities checkinglater(in x86_cpu_filter_features()). In this case, the level of CPUID will be still0x14and the CPUID values from leaf0xeto0x14are all zero.This patch moves the capabilities checking before setting the level of the CPUID.
Quoted 6 lines
Why is this patch necessary and what problem does it fix? Is it a nice to have feature, or a bug fix?
If you still want to change how the
x86_cpu_adjust_level()code behaves, it should apply to all features filtered byx86_cpu_filter_features(), not just intel- pt, shouldn't it?
Hi Eduardo,
Let me clarify the issue.
The cpuid level is 0xd if create a VM(cpu model qemu64) w/o intel pt feature on Snowridge HW.
CMD: qemu-system-x86_64 -cpu qemu64 ... (Intel PT is disabled by default)
The cpuid level will be extended to 0x14 if create a VM(cpu model qemu64) w/ intel pt feature on Snowridage.
CMD: qemu-system-x86_64 -cpu qemu64,+intel-pt ...
But the current software implementation will mask off intel pt if the host has LIP, and Snowridge support it. So cpuid level has been extended to 0x14(x86_cpu_expand_features) and Intel PT is disabled later(x86_cpu_filter_features), and the guest CPUID will include some zero items like this. 0x0000000e 0x00: eax=0x00000000 ebx=0x00000000 ecx=0x00000000 edx=0x00000000 0x0000000f 0x00: eax=0x00000000 ebx=0x00000000 ecx=0x00000000 edx=0x00000000 0x00000010 0x00: eax=0x00000000 ebx=0x00000000 ecx=0x00000000 edx=0x00000000 0x00000011 0x00: eax=0x00000000 ebx=0x00000000 ecx=0x00000000 edx=0x00000000 0x00000012 0x00: eax=0x00000000 ebx=0x00000000 ecx=0x00000000 edx=0x00000000 0x00000013 0x00: eax=0x00000000 ebx=0x00000000 ecx=0x00000000 edx=0x00000000
0x00000014 0x00: eax=0x00000000 ebx=0x00000000 ecx=0x00000000 edx=0x00000000 (Intel PT feature)
x86_cpu_realizefn()
|-> x86_cpu_expand_features()
| IF has Intel PT feature
| Then extended the cpuid level to 0x14;
|-> x86_cpu_filter_features() IF has Intel PT feature & has LIP
THEN mask off the guest Intel PT feature,
*but* the cpuid level still 0x14.
Expect result and how to:
Don't impact the cpuid level if Intel PT can't be supported in the guest. So this patch moves the intel PT capabilities check before extending the cpuid level.
Thanks,
Luwei Kang
Quoted 106 lines
Quoted 21 lines
Signed-off-by Luwei Kang <[email protected]>--- target/i386/cpu.c | 63 ++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 31 deletions(-)target/i386/cpu.c index renamed from target/i386/cpu.c +0
-0
9eafbe3690..24644abfd4100644target/i386/cpu.c +1
-3@@ -6401,12 +6401,40 @@ static void x86_cpu_expand_features(X86CPU *cpu, Error **errp)/* Intel Processor Trace requires CPUID[0x14] */ if ((env->features[FEAT_7_0_EBX ] &CPUID_7_0_EBX_INTEL_PT )) { -if (cpu->intel_pt_auto_level) { -x86_cpu_adjust_level (cpu, &cpu->env.cpuid_min_level,0x14 ); - }else if (cpu->env.cpuid_min_level <0x14 ) { +uint32_t eax_0, ebx_0, ecx_0, eax_1, ebx_1;target/i386/cpu.c static void x86_cpu_expand_features(X86CPU · line 6415
Quoted 10 lines
target/i386/cpu.c +10
-0+ + eax_0 =kvm_arch_get_supported_cpuid (kvm_state,0x14 ,0 ,R_EAX ); + ebx_0 =kvm_arch_get_supported_cpuid (kvm_state,0x14 ,0 ,R_EBX ); + ecx_0 =kvm_arch_get_supported_cpuid (kvm_state,0x14 ,0 ,R_ECX ); + eax_1 =kvm_arch_get_supported_cpuid (kvm_state,0x14 ,1 ,R_EAX ); + ebx_1 =kvm_arch_get_supported_cpuid (kvm_state,0x14 ,1 , +R_EBX ); + +if (eax_0 && + ((ebx_0 & INTEL_PT_MINIMAL_EBX) == INTEL_PT_MINIMAL_EBX)&&
target/i386/cpu.c static void x86_cpu_expand_features(X86CPU · line 6416
Quoted 1 line
target/i386/cpu.c +1
-0+ ((ecx_0 &INTEL_PT_MINIMAL_ECX ) ==INTEL_PT_MINIMAL_ECX )&&
target/i386/cpu.c static void x86_cpu_expand_features(X86CPU *cpu, Error **errp)
Quoted 12 lines
target/i386/cpu.c +12
-0+ ((eax_1 &INTEL_PT_MTC_BITMAP ) ==INTEL_PT_MTC_BITMAP ) && + ((eax_1 &INTEL_PT_ADDR_RANGES_NUM_MASK ) >= +INTEL_PT_ADDR_RANGES_NUM ) && + ((ebx_1 & (INTEL_PT_PSB_BITMAP |INTEL_PT_CYCLE_BITMAP )) == + (INTEL_PT_PSB_BITMAP |INTEL_PT_CYCLE_BITMAP )) && + !(ecx_0 & INTEL_PT_IP_LIP)) { + if (cpu->intel_pt_auto_level) { +x86_cpu_adjust_level (cpu, &cpu->env.cpuid_min_level,0x14 ); + } else if (cpu->env.cpuid_min_level < 0x14) { + mark_unavailable_features(cpu, FEAT_7_0_EBX, + CPUID_7_0_EBX_INTEL_PT, + "Intel PT need CPUID leaf 0x14, please set by \"-cpu ...,+intel-pt,min-level=0x14\"");
target/i386/cpu.c static void x86_cpu_expand_features(X86CPU *cpu, Error **errp)
Quoted 11 lines
target/i386/cpu.c +8
-1+ } + }else { +/* +* Processor Trace capabilities aren't configurable, so if the +* host can't emulate the capabilities we report on +* cpu_x86_cpuid(), intel-pt can't be enabled on the current +* host. +*/ mark_unavailable_features(cpu, FEAT_7_0_EBX, CPUID_7_0_EBX_INTEL_PT, - "Intel PT need CPUID leaf 0x14, please set by \"-cpu ...,+intel-pt,min-level=0x14\"");
target/i386/cpu.c static void x86_cpu_filter_features(X86CPU *cpu,
Quoted 6 lines
target/i386/cpu.c +2
-0+ "host Intel PT features doesn't satisfy the guest + request."); } }@@ -6466,33 +6494,6 @@ static void x86_cpu_filter_features(X86CPU *cpu,bool verbose)
Quoted 18 lines
target/i386/cpu.c +0
-15uint64_t unavailable_features = requested_features & ~host_feat;mark_unavailable_features (cpu, w, unavailable_features, prefix); } - -if ((env->features[FEAT_7_0_EBX ] &CPUID_7_0_EBX_INTEL_PT ) && -kvm_enabled ()) { -KVMState *s =CPU (cpu)->kvm_state; -uint32_t eax_0 =kvm_arch_get_supported_cpuid (s,0x14 ,0 ,R_EAX ); -uint32_t ebx_0 =kvm_arch_get_supported_cpuid (s,0x14 ,0 ,R_EBX ); -uint32_t ecx_0 =kvm_arch_get_supported_cpuid (s,0x14 ,0 ,R_ECX ); -uint32_t eax_1 =kvm_arch_get_supported_cpuid (s,0x14 ,1 ,R_EAX ); -uint32_t ebx_1 =kvm_arch_get_supported_cpuid (s,0x14 ,1 ,R_EBX ); - - if (!eax_0 || - ((ebx_0 & INTEL_PT_MINIMAL_EBX) != INTEL_PT_MINIMAL_EBX) || - ((ecx_0 & INTEL_PT_MINIMAL_ECX) != INTEL_PT_MINIMAL_ECX) || - ((eax_1 & INTEL_PT_MTC_BITMAP) != INTEL_PT_MTC_BITMAP) || - ((eax_1 & INTEL_PT_ADDR_RANGES_NUM_MASK) <target/i386/cpu.c static void x86_cpu_filter_features(X86CPU *cpu, bool verbose)
Quoted 10 lines
target/i386/cpu.c +0
-10- INTEL_PT_ADDR_RANGES_NUM) || - ((ebx_1 & (INTEL_PT_PSB_BITMAP | INTEL_PT_CYCLE_BITMAP)) != - (INTEL_PT_PSB_BITMAP | INTEL_PT_CYCLE_BITMAP)) || - (ecx_0 & INTEL_PT_IP_LIP)) { -/* -* Processor Trace capabilities aren't configurable, so if the -* host can't emulate the capabilities we report on -* cpu_x86_cpuid(), intel-pt can't be enabled on the current host. -*/ - mark_unavailable_features(cpu, FEAT_7_0_EBX,
CPUID_7_0_EBX_INTEL_PT, prefix);Quoted 8 lines
target/i386/cpu.c +0
-3- } - } }static void x86_cpu_realizefn (DeviceState *dev,Error **errp) --2.18.4
--
Eduardo