target/i386: Always set leaf 0x1f
PATCH 1/1 target/i386: Always set leaf 0x1f
QEMU does not set 0x1f in case VM does not have extended CPU topology and expects guests to fallback to 0xb. Some versions of windows i.e. windows 10, 11 does not like this behavior and expects this leaf to be populated. This is observed with windows VMs with secure boot, uefi and HyperV role enabled.
Leaf 0x1f is superset of 0xb, so it makes sense to set 0x1f equivalent to 0xb by default and workaround windows issue. This change adds a new property 'cpuid-0x1f-enforce' to set leaf 0x1f equivalent to 0xb in case extended CPU topology is not present and behave as before otherwise.
--- hw/i386/pc.c | 1 + target/i386/cpu.c | 71 +++++++++++++++++++++++++++---------------- target/i386/cpu.h | 5 +++ target/i386/kvm/kvm.c | 4 ++- 4 files changed, 53 insertions(+), 28 deletions(-)
hw/i386/pc.c +1 -0
@@ -85,6 +85,7 @@ GlobalProperty pc_compat_9_0[] = { { TYPE_X86_CPU, "guest-phys-bits", "0" }, { "sev-guest", "legacy-vm-type", "on" }, { TYPE_X86_CPU, "legacy-multi-node", "on" }, + { TYPE_X86_CPU, "cpuid-0x1f-enforce", "false" }, };const size_t pc_compat_9_0_len =G_N_ELEMENTS (pc_compat_9_0);
target/i386/cpu.c +44 -27
95 lines not on this page · raw message
target/i386/cpu.h +5 -0
@@ -2102,6 +2102,11 @@ struct ArchCPU {/* Compatibility bits for old machine types: */ bool enable_cpuid_0xb; +/* Always return values for 0x1f leaf. In cases where extended CPU topology +* is not supported, return values equivalent of leaf 0xb. +*/ +bool enable_cpuid_0x1f_enforce; +/* Enable auto level-increase for all CPUID leaves */ bool full_cpuid_auto_level;
target/i386/kvm/kvm.c +3 -1
@@ -1799,6 +1799,7 @@ static uint32_t kvm_x86_build_cpuid(CPUX86State *env,uint32_t limit, i, j;uint32_t unused;struct kvm_cpuid_entry2 *c; +X86CPU *cpu =env_archcpu (env);cpu_x86_cpuid (env,0 ,0 , &limit, &unused, &unused, &unused);
@@ -1831,7 +1832,8 @@ static uint32_t kvm_x86_build_cpuid(CPUX86State *env,break ; }case 0x1f : -if (!x86_has_extended_topo (env->avail_cpu_topo)) { +if (!x86_has_extended_topo (env->avail_cpu_topo) && + !cpu->enable_cpuid_0x1f_enforce) { cpuid_i--;break ; }
Signature · 1 line
2.22.3
(+Xiaoyao, whose TDX work may also be related with this.)
Hi Manish,
Thanks for your patch! Some comments below.
Quoted 27 lines · manish.mishra
Date: Mon, 22 Jul 2024 10:18:59 +0000
From: "manish.mishra" <[email protected]>
Subject: [PATCH] target/i386: Always set leaf0x1f
X-Mailer: git-send-email 2.22.3QEMU does not set
0x1fin case VM does not have extended CPU topology and expects guests to fallback to0xb. Some versions of windows i.e. windows 10, 11 does not like this behavior and expects this leaf to be populated. This is observed with windows VMs with secure boot, uefi and HyperV role enabled.Leaf
0x1fis superset of0xb, so it makes sense to set0x1fequivalent to0xbby default and workaround windows issue. This change adds a new property 'cpuid-0x1f-enforce' to set leaf0x1fequivalent to0xbin case extended CPU topology is not present and behave as before otherwise.--- hw/i386/pc.c | 1 + target/i386/cpu.c | 71 +++++++++++++++++++++++++++---------------- target/i386/cpu.h | 5 +++ target/i386/kvm/kvm.c | 4 ++- 4 files changed, 53 insertions(+), 28 deletions(-)hw/i386/pc.c +0
-0
hw/i386/pc.c GlobalProperty pc_compat_9_0[] = { · line 90
Quoted 6 lines
hw/i386/pc.c +1
-0@@ -85,6 +85,7 @@ GlobalProperty pc_compat_9_0[] = { { TYPE_X86_CPU, "guest-phys-bits", "0" }, { "sev-guest", "legacy-vm-type", "on" }, { TYPE_X86_CPU, "legacy-multi-node", "on" }, + { TYPE_X86_CPU, "cpuid-0x1f-enforce", "false" }, };const size_t pc_compat_9_0_len =G_N_ELEMENTS (pc_compat_9_0);
Yes, this is needed, but the 9.1 soft freeze is coming close soon, so you may have to add pc_compat_9_1[] if it doesn't get accepted before the soft freeze.
Quoted 85 lines
target/i386/cpu.c +39
-27@@ -416,6 +416,43 @@ static void encode_topo_cpuid1f(CPUX86State *env, uint32_t count,assert (!(*eax & ~0x1f )); } +static void encode_topo_cpuid_b (CPUX86State *env,uint32_t count, +X86CPUTopoInfo *topo_info, +uint32_t threads_per_pkg, +uint32_t *eax,uint32_t *ebx, +uint32_t *ecx,uint32_t *edx) +{ +X86CPU *cpu =env_archcpu (env); + +if (!cpu->enable_cpuid_0xb) { + *eax = *ebx = *ecx = *edx =0 ; +return ; + } + + *ecx = count &0xff ; + *edx = cpu->apic_id; + +switch (count) { +case 0 : + *eax =apicid_core_offset (topo_info); + *ebx = topo_info->threads_per_core; + *ecx |=CPUID_B_ECX_TOPO_LEVEL_SMT <<8 ; +break ; +case 1 : + *eax =apicid_pkg_offset (topo_info); + *ebx = threads_per_pkg; + *ecx |=CPUID_B_ECX_TOPO_LEVEL_CORE <<8 ; +break ; +default : + *eax =0 ; + *ebx =0 ; + *ecx |=CPUID_B_ECX_TOPO_LEVEL_INVALID <<8 ; + } + +assert (!(*eax & ~0x1f )); + *ebx &=0xffff ;/* The count doesn't need to be reliable. */ +} +/* Encode cache info for CPUID[0x80000005].ECX or CPUID[0x80000005].EDX */ static uint32_t encode_cache_cpuid80000005 (CPUCacheInfo *cache) {@@ -6601,33 +6638,8 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,break ;case 0xB :/* Extended Topology Enumeration Leaf */ -if (!cpu->enable_cpuid_0xb) { - *eax = *ebx = *ecx = *edx =0 ; -break ; - } - - *ecx = count &0xff ; - *edx = cpu->apic_id; - -switch (count) { -case 0 : - *eax =apicid_core_offset (&topo_info); - *ebx = topo_info.threads_per_core; - *ecx |=CPUID_B_ECX_TOPO_LEVEL_SMT <<8 ; -break ; -case 1 : - *eax =apicid_pkg_offset (&topo_info); - *ebx = threads_per_pkg; - *ecx |=CPUID_B_ECX_TOPO_LEVEL_CORE <<8 ; -break ; -default : - *eax =0 ; - *ebx =0 ; - *ecx |=CPUID_B_ECX_TOPO_LEVEL_INVALID <<8 ; - } - -assert (!(*eax & ~0x1f )); - *ebx &=0xffff ;/* The count doesn't need to be reliable. */ +encode_topo_cpuid_b (env, count, &topo_info, threads_per_pkg, + eax, ebx, ecx, edx);break ;case 0x1C :if (cpu->enable_pmu && (env->features[FEAT_7_0_EDX ] &CPUID_7_0_EDX_ARCH_LBR )) {
target/i386/cpu.c void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, · line 6657
Quoted 7 lines
target/i386/cpu.c +4
-0@@ -6639,6 +6651,10 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,/* V2 Extended Topology Enumeration Leaf */ if (!x86_has_extended_topo (env->avail_cpu_topo)) { *eax = *ebx = *ecx = *edx =0 ; +if (cpu->enable_cpuid_0x1f_enforce) { +encode_topo_cpuid_b (env, count, &topo_info, threads_per_pkg, + eax, ebx, ecx, edx); + }
encode_topo_cpuid_b() is not necessary since encode_topo_cpuid1f() could encode SMT/core levels with the same type code as 0x0b.
So here we just need tweak the encoding condition:
- if (!x86_has_extended_topo(env->avail_cpu_topo)) {
+ if (!x86_has_extended_topo(env->avail_cpu_topo) &&
+ !cpu->enable_cpuid_0x1f_enforce) {
*eax = *ebx = *ecx = *edx = 0;
break;
}
encode_topo_cpuid1f(env, count, &topo_info, eax, ebx, ecx, edx);
break;Then wrapping encode_topo_cpuid_b() could also be omitted, which keeps the code changes as minor as possible.
Quoted 16 lines
target/i386/cpu.c +1
-0break ; }@@ -8316,6 +8332,7 @@ static Property x86_cpu_properties[] = {DEFINE_PROP_BOOL ("full-cpuid-auto-level" ,X86CPU , full_cpuid_auto_level, true),DEFINE_PROP_STRING ("hv-vendor-id" ,X86CPU , hyperv_vendor),DEFINE_PROP_BOOL ("cpuid-0xb" ,X86CPU , enable_cpuid_0xb, true), +DEFINE_PROP_BOOL ("cpuid-0x1f-enforce" ,X86CPU , enable_cpuid_0x1f_enforce, true),DEFINE_PROP_BOOL ("x-vendor-cpuid-only" ,X86CPU , vendor_cpuid_only, true),DEFINE_PROP_BOOL ("x-amd-topoext-features-only" ,X86CPU , amd_topoext_features_only, true), DEFINE_PROP_BOOL("lmce", X86CPU, enable_lmce, false),target/i386/cpu.h +0
-0
target/i386/cpu.h struct ArchCPU { · line 2106
Quoted 5 lines
target/i386/cpu.h +2
-0@@ -2102,6 +2102,11 @@ struct ArchCPU {/* Compatibility bits for old machine types: */ bool enable_cpuid_0xb; +/* Always return values for 0x1f leaf. In cases where extended CPU topology +* is not supported, return values equivalent of leaf 0xb.
s/is not supported/is not configured/
Quoted 19 lines
target/i386/cpu.h +3
-0+*/ +bool enable_cpuid_0x1f_enforce; +/* Enable auto level-increase for all CPUID leaves */ bool full_cpuid_auto_level;target/i386/kvm/kvm.c +1
-0@@ -1799,6 +1799,7 @@ static uint32_t kvm_x86_build_cpuid(CPUX86State *env,uint32_t limit, i, j;uint32_t unused;struct kvm_cpuid_entry2 *c; +X86CPU *cpu =env_archcpu (env);cpu_x86_cpuid (env,0 ,0 , &limit, &unused, &unused, &unused);
target/i386/kvm/kvm.c static uint32_t kvm_x86_build_cpuid(CPUX86State *env, · line 1839
Quoted 9 lines
target/i386/kvm/kvm.c +2
-1@@ -1831,7 +1832,8 @@ static uint32_t kvm_x86_build_cpuid(CPUX86State *env,break ; }case 0x1f : -if (!x86_has_extended_topo (env->avail_cpu_topo)) { +if (!x86_has_extended_topo (env->avail_cpu_topo) && + !cpu->enable_cpuid_0x1f_enforce) { cpuid_i--;break ; }
In addition to the above changes, we need to adjust the min_level of the CPUID to ensure that 0x1f can be encoded:
@@ -7466,7 +7466,8 @@ void x86_cpu_expand_features(X86CPU *cpu, Error **errp)
* cpu->vendor_cpuid_only has been unset for compatibility with older
* machine types.
*/
- if (x86_has_extended_topo(env->avail_cpu_topo) &&
+ if ((x86_has_extended_topo(env->avail_cpu_topo) ||
+ cpu->enable_cpuid_0x1f_enforce) &&
(IS_INTEL_CPU(env) || !cpu->vendor_cpuid_only)) {
x86_cpu_adjust_level(cpu, &env->cpuid_min_level, 0x1F);
}Thanks,
Zhao
Quoted 1 line · Zhao Liu
(+Xiaoyao, whose TDX work may also be related with this.)
I have a similar patch for TDX because TDX requires CPUID leaf 0x1f to configure topology as a must.
(I haven't post to QEMU community yet. I'm not sure how people want to proceed, refine this patch or I can post my version?)
https://github.com/intel-staging/qemu-tdx/commit/de08fd30926bc9d7997af6bd12cfff1b998da8b7
https://github.com/intel-staging/qemu-tdx/commit/f81d2bcb67e4b01577723cc621099b0c6d558334
Quoted 224 lines
Hi Manish,
Thanks for your patch! Some comments below.
Quoted 27 lines · manish.mishra
Date: Mon, 22 Jul 2024 10:18:59 +0000
From: "manish.mishra" <[email protected]>
Subject: [PATCH] target/i386: Always set leaf0x1f
X-Mailer: git-send-email 2.22.3QEMU does not set
0x1fin case VM does not have extended CPU topology and expects guests to fallback to0xb. Some versions of windows i.e. windows 10, 11 does not like this behavior and expects this leaf to be populated. This is observed with windows VMs with secure boot, uefi and HyperV role enabled.Leaf
0x1fis superset of0xb, so it makes sense to set0x1fequivalent to0xbby default and workaround windows issue. This change adds a new property 'cpuid-0x1f-enforce' to set leaf0x1fequivalent to0xbin case extended CPU topology is not present and behave as before otherwise.--- hw/i386/pc.c | 1 + target/i386/cpu.c | 71 +++++++++++++++++++++++++++---------------- target/i386/cpu.h | 5 +++ target/i386/kvm/kvm.c | 4 ++- 4 files changed, 53 insertions(+), 28 deletions(-)hw/i386/pc.c +0
-0hw/i386/pc.c GlobalProperty pc_compat_9_0[] = { · line 90
Quoted 6 lines
hw/i386/pc.c +1
-0@@ -85,6 +85,7 @@ GlobalProperty pc_compat_9_0[] = { { TYPE_X86_CPU, "guest-phys-bits", "0" }, { "sev-guest", "legacy-vm-type", "on" }, { TYPE_X86_CPU, "legacy-multi-node", "on" }, + { TYPE_X86_CPU, "cpuid-0x1f-enforce", "false" }, };const size_t pc_compat_9_0_len =G_N_ELEMENTS (pc_compat_9_0);Yes, this is needed, but the 9.1 soft freeze is coming close soon, so you may have to add pc_compat_9_1[] if it doesn't get accepted before the soft freeze.
Quoted 85 lines
target/i386/cpu.c +39
-27@@ -416,6 +416,43 @@ static void encode_topo_cpuid1f(CPUX86State *env, uint32_t count,assert (!(*eax & ~0x1f )); } +static void encode_topo_cpuid_b (CPUX86State *env,uint32_t count, +X86CPUTopoInfo *topo_info, +uint32_t threads_per_pkg, +uint32_t *eax,uint32_t *ebx, +uint32_t *ecx,uint32_t *edx) +{ +X86CPU *cpu =env_archcpu (env); + +if (!cpu->enable_cpuid_0xb) { + *eax = *ebx = *ecx = *edx =0 ; +return ; + } + + *ecx = count &0xff ; + *edx = cpu->apic_id; + +switch (count) { +case 0 : + *eax =apicid_core_offset (topo_info); + *ebx = topo_info->threads_per_core; + *ecx |=CPUID_B_ECX_TOPO_LEVEL_SMT <<8 ; +break ; +case 1 : + *eax =apicid_pkg_offset (topo_info); + *ebx = threads_per_pkg; + *ecx |=CPUID_B_ECX_TOPO_LEVEL_CORE <<8 ; +break ; +default : + *eax =0 ; + *ebx =0 ; + *ecx |=CPUID_B_ECX_TOPO_LEVEL_INVALID <<8 ; + } + +assert (!(*eax & ~0x1f )); + *ebx &=0xffff ;/* The count doesn't need to be reliable. */ +} +/* Encode cache info for CPUID[0x80000005].ECX or CPUID[0x80000005].EDX */ static uint32_t encode_cache_cpuid80000005 (CPUCacheInfo *cache) {@@ -6601,33 +6638,8 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,break ;case 0xB :/* Extended Topology Enumeration Leaf */ -if (!cpu->enable_cpuid_0xb) { - *eax = *ebx = *ecx = *edx =0 ; -break ; - } - - *ecx = count &0xff ; - *edx = cpu->apic_id; - -switch (count) { -case 0 : - *eax =apicid_core_offset (&topo_info); - *ebx = topo_info.threads_per_core; - *ecx |=CPUID_B_ECX_TOPO_LEVEL_SMT <<8 ; -break ; -case 1 : - *eax =apicid_pkg_offset (&topo_info); - *ebx = threads_per_pkg; - *ecx |=CPUID_B_ECX_TOPO_LEVEL_CORE <<8 ; -break ; -default : - *eax =0 ; - *ebx =0 ; - *ecx |=CPUID_B_ECX_TOPO_LEVEL_INVALID <<8 ; - } - -assert (!(*eax & ~0x1f )); - *ebx &=0xffff ;/* The count doesn't need to be reliable. */ +encode_topo_cpuid_b (env, count, &topo_info, threads_per_pkg, + eax, ebx, ecx, edx);break ;case 0x1C :if (cpu->enable_pmu && (env->features[FEAT_7_0_EDX ] &CPUID_7_0_EDX_ARCH_LBR )) {target/i386/cpu.c void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, · line 6657
Quoted 7 lines
target/i386/cpu.c +4
-0@@ -6639,6 +6651,10 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,/* V2 Extended Topology Enumeration Leaf */ if (!x86_has_extended_topo (env->avail_cpu_topo)) { *eax = *ebx = *ecx = *edx =0 ; +if (cpu->enable_cpuid_0x1f_enforce) { +encode_topo_cpuid_b (env, count, &topo_info, threads_per_pkg, + eax, ebx, ecx, edx); + }
encode_topo_cpuid_b()is not necessary sinceencode_topo_cpuid1f()could encode SMT/core levels with the same type code as0x0b.So here we just need tweak the encoding condition:
target/i386/kvm/kvm.c +2
-1-if (!x86_has_extended_topo (env->avail_cpu_topo)) { +if (!x86_has_extended_topo (env->avail_cpu_topo) && + !cpu->enable_cpuid_0x1f_enforce) { *eax = *ebx = *ecx = *edx =0 ;break ; }encode_topo_cpuid1f (env, count, &topo_info, eax, ebx, ecx, edx);break ;Then wrapping
encode_topo_cpuid_b()could also be omitted, which keeps the code changes as minor as possible.Quoted 16 lines
target/i386/cpu.c +1
-0break ; }@@ -8316,6 +8332,7 @@ static Property x86_cpu_properties[] = {DEFINE_PROP_BOOL ("full-cpuid-auto-level" ,X86CPU , full_cpuid_auto_level, true),DEFINE_PROP_STRING ("hv-vendor-id" ,X86CPU , hyperv_vendor),DEFINE_PROP_BOOL ("cpuid-0xb" ,X86CPU , enable_cpuid_0xb, true), +DEFINE_PROP_BOOL ("cpuid-0x1f-enforce" ,X86CPU , enable_cpuid_0x1f_enforce, true),DEFINE_PROP_BOOL ("x-vendor-cpuid-only" ,X86CPU , vendor_cpuid_only, true),DEFINE_PROP_BOOL ("x-amd-topoext-features-only" ,X86CPU , amd_topoext_features_only, true), DEFINE_PROP_BOOL("lmce", X86CPU, enable_lmce, false),target/i386/cpu.h +0
-0target/i386/cpu.h struct ArchCPU { · line 2106
Quoted 5 lines
target/i386/cpu.h +2
-0@@ -2102,6 +2102,11 @@ struct ArchCPU {/* Compatibility bits for old machine types: */ bool enable_cpuid_0xb; +/* Always return values for 0x1f leaf. In cases where extended CPU topology +* is not supported, return values equivalent of leaf 0xb. s/is not supported/is not configured/
Quoted 19 lines
target/i386/cpu.h +3
-0+*/ +bool enable_cpuid_0x1f_enforce; +/* Enable auto level-increase for all CPUID leaves */ bool full_cpuid_auto_level;target/i386/kvm/kvm.c +1
-0@@ -1799,6 +1799,7 @@ static uint32_t kvm_x86_build_cpuid(CPUX86State *env,uint32_t limit, i, j;uint32_t unused;struct kvm_cpuid_entry2 *c; +X86CPU *cpu =env_archcpu (env);cpu_x86_cpuid (env,0 ,0 , &limit, &unused, &unused, &unused);target/i386/kvm/kvm.c static uint32_t kvm_x86_build_cpuid(CPUX86State *env, · line 1839
Quoted 9 lines
target/i386/kvm/kvm.c +2
-1@@ -1831,7 +1832,8 @@ static uint32_t kvm_x86_build_cpuid(CPUX86State *env,break ; }case 0x1f : -if (!x86_has_extended_topo (env->avail_cpu_topo)) { +if (!x86_has_extended_topo (env->avail_cpu_topo) && + !cpu->enable_cpuid_0x1f_enforce) { cpuid_i--;break ; }In addition to the above changes, we need to adjust the
min_levelof the CPUID to ensure that0x1fcan be encoded:(quoted diff) +2
-1@@ -7466,7 +7466,8 @@ void x86_cpu_expand_features(X86CPU *cpu, Error **errp) * cpu->vendor_cpuid_only has been unset for compatibility with older * machine types. */ - if (x86_has_extended_topo(env->avail_cpu_topo) && + if ((x86_has_extended_topo(env->avail_cpu_topo) || + cpu->enable_cpuid_0x1f_enforce) && (IS_INTEL_CPU(env) || !cpu->vendor_cpuid_only)) { x86_cpu_adjust_level(cpu, &env->cpuid_min_level, 0x1F); }Thanks,
Zhao
Quoted 7 lines · Xiaoyao Li
Date: Wed, 24 Jul 2024 08:25:12 +0800
From: Xiaoyao Li <[email protected]>
Subject: Re: [PATCH] target/i386: Always set leaf0x1fQuoted 1 line · Zhao Liu
(+Xiaoyao, whose TDX work may also be related with this.)
Quoted 7 lines
I have a similar patch for TDX because TDX requires CPUID leaf
0x1fto configure topology as a must.(I haven't post to QEMU community yet. I'm not sure how people want to proceed, refine this patch or I can post my version?)
https://github.com/intel-staging/qemu-tdx/commit/de08fd30926bc9d7997af6bd12cfff1b998da8b7
Hi Xiaoyao, the logic is similar, if Manish is willing to keep iterating, we can help him improve to cover all the cases we need, then TDX and his case could both benefit.
Quoted 223 lines
https://github.com/intel-staging/qemu-tdx/commit/f81d2bcb67e4b01577723cc621099b0c6d558334
Quoted 218 lines
Hi Manish,
Thanks for your patch! Some comments below.
Quoted 27 lines · manish.mishra
Date: Mon, 22 Jul 2024 10:18:59 +0000
From: "manish.mishra" <[email protected]>
Subject: [PATCH] target/i386: Always set leaf0x1f
X-Mailer: git-send-email 2.22.3QEMU does not set
0x1fin case VM does not have extended CPU topology and expects guests to fallback to0xb. Some versions of windows i.e. windows 10, 11 does not like this behavior and expects this leaf to be populated. This is observed with windows VMs with secure boot, uefi and HyperV role enabled.Leaf
0x1fis superset of0xb, so it makes sense to set0x1fequivalent to0xbby default and workaround windows issue. This change adds a new property 'cpuid-0x1f-enforce' to set leaf0x1fequivalent to0xbin case extended CPU topology is not present and behave as before otherwise.--- hw/i386/pc.c | 1 + target/i386/cpu.c | 71 +++++++++++++++++++++++++++---------------- target/i386/cpu.h | 5 +++ target/i386/kvm/kvm.c | 4 ++- 4 files changed, 53 insertions(+), 28 deletions(-)hw/i386/pc.c +0
-0hw/i386/pc.c GlobalProperty pc_compat_9_0[] = { · line 90
Quoted 6 lines
hw/i386/pc.c +1
-0@@ -85,6 +85,7 @@ GlobalProperty pc_compat_9_0[] = { { TYPE_X86_CPU, "guest-phys-bits", "0" }, { "sev-guest", "legacy-vm-type", "on" }, { TYPE_X86_CPU, "legacy-multi-node", "on" }, + { TYPE_X86_CPU, "cpuid-0x1f-enforce", "false" }, };const size_t pc_compat_9_0_len =G_N_ELEMENTS (pc_compat_9_0);Yes, this is needed, but the 9.1 soft freeze is coming close soon, so you may have to add pc_compat_9_1[] if it doesn't get accepted before the soft freeze.
Quoted 84 lines
target/i386/cpu.c +39
-27@@ -416,6 +416,43 @@ static void encode_topo_cpuid1f(CPUX86State *env, uint32_t count,assert (!(*eax & ~0x1f )); } +static void encode_topo_cpuid_b (CPUX86State *env,uint32_t count, +X86CPUTopoInfo *topo_info, +uint32_t threads_per_pkg, +uint32_t *eax,uint32_t *ebx, +uint32_t *ecx,uint32_t *edx) +{ +X86CPU *cpu =env_archcpu (env); + +if (!cpu->enable_cpuid_0xb) { + *eax = *ebx = *ecx = *edx =0 ; +return ; + } + + *ecx = count &0xff ; + *edx = cpu->apic_id; + +switch (count) { +case 0 : + *eax =apicid_core_offset (topo_info); + *ebx = topo_info->threads_per_core; + *ecx |=CPUID_B_ECX_TOPO_LEVEL_SMT <<8 ; +break ; +case 1 : + *eax =apicid_pkg_offset (topo_info); + *ebx = threads_per_pkg; + *ecx |=CPUID_B_ECX_TOPO_LEVEL_CORE <<8 ; +break ; +default : + *eax =0 ; + *ebx =0 ; + *ecx |=CPUID_B_ECX_TOPO_LEVEL_INVALID <<8 ; + } + +assert (!(*eax & ~0x1f )); + *ebx &=0xffff ;/* The count doesn't need to be reliable. */ +} +/* Encode cache info for CPUID[0x80000005].ECX or CPUID[0x80000005].EDX */ static uint32_t encode_cache_cpuid80000005 (CPUCacheInfo *cache) {@@ -6601,33 +6638,8 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,break ;case 0xB :/* Extended Topology Enumeration Leaf */ -if (!cpu->enable_cpuid_0xb) { - *eax = *ebx = *ecx = *edx =0 ; -break ; - } - - *ecx = count &0xff ; - *edx = cpu->apic_id; - -switch (count) { -case 0 : - *eax =apicid_core_offset (&topo_info); - *ebx = topo_info.threads_per_core; - *ecx |=CPUID_B_ECX_TOPO_LEVEL_SMT <<8 ; -break ; -case 1 : - *eax =apicid_pkg_offset (&topo_info); - *ebx = threads_per_pkg; - *ecx |=CPUID_B_ECX_TOPO_LEVEL_CORE <<8 ; -break ; -default : - *eax =0 ; - *ebx =0 ; - *ecx |=CPUID_B_ECX_TOPO_LEVEL_INVALID <<8 ; - } - -assert (!(*eax & ~0x1f )); - *ebx &=0xffff ;/* The count doesn't need to be reliable. */ +encode_topo_cpuid_b (env, count, &topo_info, threads_per_pkg, + eax, ebx, ecx, edx);break ;case 0x1C :if (cpu->enable_pmu && (env->features[FEAT_7_0_EDX ] &CPUID_7_0_EDX_ARCH_LBR )) {target/i386/cpu.c void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, · line 6657
Quoted 7 lines
target/i386/cpu.c +4
-0@@ -6639,6 +6651,10 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,/* V2 Extended Topology Enumeration Leaf */ if (!x86_has_extended_topo (env->avail_cpu_topo)) { *eax = *ebx = *ecx = *edx =0 ; +if (cpu->enable_cpuid_0x1f_enforce) { +encode_topo_cpuid_b (env, count, &topo_info, threads_per_pkg, + eax, ebx, ecx, edx); + }
encode_topo_cpuid_b()is not necessary sinceencode_topo_cpuid1f()could encode SMT/core levels with the same type code as0x0b.So here we just need tweak the encoding condition:
target/i386/kvm/kvm.c +2
-1-if (!x86_has_extended_topo (env->avail_cpu_topo)) { +if (!x86_has_extended_topo (env->avail_cpu_topo) && + !cpu->enable_cpuid_0x1f_enforce) { *eax = *ebx = *ecx = *edx =0 ;break ; }encode_topo_cpuid1f (env, count, &topo_info, eax, ebx, ecx, edx);break ;Then wrapping
encode_topo_cpuid_b()could also be omitted, which keeps the code changes as minor as possible.Quoted 15 lines
target/i386/cpu.c +1
-0break ; }@@ -8316,6 +8332,7 @@ static Property x86_cpu_properties[] = {DEFINE_PROP_BOOL ("full-cpuid-auto-level" ,X86CPU , full_cpuid_auto_level, true),DEFINE_PROP_STRING ("hv-vendor-id" ,X86CPU , hyperv_vendor),DEFINE_PROP_BOOL ("cpuid-0xb" ,X86CPU , enable_cpuid_0xb, true), +DEFINE_PROP_BOOL ("cpuid-0x1f-enforce" ,X86CPU , enable_cpuid_0x1f_enforce, true),DEFINE_PROP_BOOL ("x-vendor-cpuid-only" ,X86CPU , vendor_cpuid_only, true),DEFINE_PROP_BOOL ("x-amd-topoext-features-only" ,X86CPU , amd_topoext_features_only, true), DEFINE_PROP_BOOL("lmce", X86CPU, enable_lmce, false),target/i386/cpu.h +0
-0target/i386/cpu.h struct ArchCPU { · line 2105
Quoted 4 lines
target/i386/cpu.h +2
-0@@ -2102,6 +2102,11 @@ struct ArchCPU {/* Compatibility bits for old machine types: */ bool enable_cpuid_0xb; +/* Always return values for 0x1f leaf. In cases where extended CPU topology +* is not supported, return values equivalent of leaf 0xb. s/is not supported/is not configured/
Quoted 16 lines
target/i386/cpu.h +3
-0+*/ +bool enable_cpuid_0x1f_enforce; +/* Enable auto level-increase for all CPUID leaves */ bool full_cpuid_auto_level;target/i386/kvm/kvm.c +1
-0@@ -1799,6 +1799,7 @@ static uint32_t kvm_x86_build_cpuid(CPUX86State *env,uint32_t limit, i, j;uint32_t unused;struct kvm_cpuid_entry2 *c; +X86CPU *cpu =env_archcpu (env);cpu_x86_cpuid (env,0 ,0 , &limit, &unused, &unused, &unused);target/i386/kvm/kvm.c static uint32_t kvm_x86_build_cpuid(CPUX86State *env, · line 1839
Quoted 9 lines
target/i386/kvm/kvm.c +2
-1@@ -1831,7 +1832,8 @@ static uint32_t kvm_x86_build_cpuid(CPUX86State *env,break ; }case 0x1f : -if (!x86_has_extended_topo (env->avail_cpu_topo)) { +if (!x86_has_extended_topo (env->avail_cpu_topo) && + !cpu->enable_cpuid_0x1f_enforce) { cpuid_i--;break ; }In addition to the above changes, we need to adjust the
min_levelof the CPUID to ensure that0x1fcan be encoded:(quoted diff) +2
-1@@ -7466,7 +7466,8 @@ void x86_cpu_expand_features(X86CPU *cpu, Error **errp) * cpu->vendor_cpuid_only has been unset for compatibility with older * machine types. */ - if (x86_has_extended_topo(env->avail_cpu_topo) && + if ((x86_has_extended_topo(env->avail_cpu_topo) || + cpu->enable_cpuid_0x1f_enforce) && (IS_INTEL_CPU(env) || !cpu->vendor_cpuid_only)) { x86_cpu_adjust_level(cpu, &env->cpuid_min_level, 0x1F); }Thanks,
Zhao
Thanks Zhao for quick review. I have sent v1 for this. https://patchwork.ozlabs.org/project/qemu-devel/patch/[email protected]/.
Quoted 34 lines · Zhao Liu
!-------------------------------------------------------------------|
CAUTION: External Email|-------------------------------------------------------------------!
(+Xiaoyao, whose TDX work may also be related with this.)
Hi Manish,
Thanks for your patch! Some comments below.
Quoted 22 lines · manish.mishra
Date: Mon, 22 Jul 2024 10:18:59 +0000
From: "manish.mishra" <[email protected]>
Subject: [PATCH] target/i386: Always set leaf0x1f
X-Mailer: git-send-email 2.22.3QEMU does not set
0x1fin case VM does not have extended CPU topology and expects guests to fallback to0xb. Some versions of windows i.e. windows 10, 11 does not like this behavior and expects this leaf to be populated. This is observed with windows VMs with secure boot, uefi and HyperV role enabled.Leaf
0x1fis superset of0xb, so it makes sense to set0x1fequivalent to0xbby default and workaround windows issue. This change adds a new property 'cpuid-0x1f-enforce' to set leaf0x1fequivalent to0xbin case extended CPU topology is not present and behave as before otherwise.--- hw/i386/pc.c | 1 + target/i386/cpu.c | 71 +++++++++++++++++++++++++++---------------- target/i386/cpu.h | 5 +++ target/i386/kvm/kvm.c | 4 ++- 4 files changed, 53 insertions(+), 28 deletions(-)
Quoted 14 lines
hw/i386/pc.c GlobalProperty pc_compat_9_0[] = { · line 90
Quoted 11 lines
hw/i386/pc.c +1
-0@@ -85,6 +85,7 @@ GlobalProperty pc_compat_9_0[] = { { TYPE_X86_CPU, "guest-phys-bits", "0" }, { "sev-guest", "legacy-vm-type", "on" }, { TYPE_X86_CPU, "legacy-multi-node", "on" }, + { TYPE_X86_CPU, "cpuid-0x1f-enforce", "false" }, };const size_t pc_compat_9_0_len =G_N_ELEMENTS (pc_compat_9_0);Yes, this is needed, but the 9.1 soft freeze is coming close soon, so you may have to add pc_compat_9_1[] if it doesn't get accepted before the soft freeze.
Sure, i will update it if required.
Quoted 96 lines
Quoted 86 lines
target/i386/cpu.c +39
-27@@ -416,6 +416,43 @@ static void encode_topo_cpuid1f(CPUX86State *env, uint32_t count,assert (!(*eax & ~0x1f )); } +static void encode_topo_cpuid_b (CPUX86State *env,uint32_t count, +X86CPUTopoInfo *topo_info, +uint32_t threads_per_pkg, +uint32_t *eax,uint32_t *ebx, +uint32_t *ecx,uint32_t *edx) +{ +X86CPU *cpu =env_archcpu (env); + +if (!cpu->enable_cpuid_0xb) { + *eax = *ebx = *ecx = *edx =0 ; +return ; + } + + *ecx = count &0xff ; + *edx = cpu->apic_id; + +switch (count) { +case 0 : + *eax =apicid_core_offset (topo_info); + *ebx = topo_info->threads_per_core; + *ecx |=CPUID_B_ECX_TOPO_LEVEL_SMT <<8 ; +break ; +case 1 : + *eax =apicid_pkg_offset (topo_info); + *ebx = threads_per_pkg; + *ecx |=CPUID_B_ECX_TOPO_LEVEL_CORE <<8 ; +break ; +default : + *eax =0 ; + *ebx =0 ; + *ecx |=CPUID_B_ECX_TOPO_LEVEL_INVALID <<8 ; + } + +assert (!(*eax & ~0x1f )); + *ebx &=0xffff ;/* The count doesn't need to be reliable. */ +} +/* Encode cache info for CPUID[0x80000005].ECX or CPUID[0x80000005].EDX */ static uint32_t encode_cache_cpuid80000005 (CPUCacheInfo *cache) {@@ -6601,33 +6638,8 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,break ;case 0xB :/* Extended Topology Enumeration Leaf */ -if (!cpu->enable_cpuid_0xb) { - *eax = *ebx = *ecx = *edx =0 ; -break ; - } - - *ecx = count &0xff ; - *edx = cpu->apic_id; - -switch (count) { -case 0 : - *eax =apicid_core_offset (&topo_info); - *ebx = topo_info.threads_per_core; - *ecx |=CPUID_B_ECX_TOPO_LEVEL_SMT <<8 ; -break ; -case 1 : - *eax =apicid_pkg_offset (&topo_info); - *ebx = threads_per_pkg; - *ecx |=CPUID_B_ECX_TOPO_LEVEL_CORE <<8 ; -break ; -default : - *eax =0 ; - *ebx =0 ; - *ecx |=CPUID_B_ECX_TOPO_LEVEL_INVALID <<8 ; - } - -assert (!(*eax & ~0x1f )); - *ebx &=0xffff ;/* The count doesn't need to be reliable. */ +encode_topo_cpuid_b (env, count, &topo_info, threads_per_pkg, + eax, ebx, ecx, edx);break ;case 0x1C :if (cpu->enable_pmu && (env->features[FEAT_7_0_EDX ] &CPUID_7_0_EDX_ARCH_LBR )) {target/i386/cpu.c void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, · line 6657
Quoted 7 lines
target/i386/cpu.c +4
-0@@ -6639,6 +6651,10 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,/* V2 Extended Topology Enumeration Leaf */ if (!x86_has_extended_topo (env->avail_cpu_topo)) { *eax = *ebx = *ecx = *edx =0 ; +if (cpu->enable_cpuid_0x1f_enforce) { +encode_topo_cpuid_b (env, count, &topo_info, threads_per_pkg, + eax, ebx, ecx, edx); + }
encode_topo_cpuid_b()is not necessary sinceencode_topo_cpuid1f()could encode SMT/core levels with the same type code as0x0b.
Quoted 1 line
So here we just need tweak the encoding condition:
Done
Quoted 11 lines
target/i386/kvm/kvm.c +2
-1-if (!x86_has_extended_topo (env->avail_cpu_topo)) { +if (!x86_has_extended_topo (env->avail_cpu_topo) && + !cpu->enable_cpuid_0x1f_enforce) { *eax = *ebx = *ecx = *edx =0 ;break ; }encode_topo_cpuid1f (env, count, &topo_info, eax, ebx, ecx, edx);break ;
Quoted 2 lines
Then wrapping
encode_topo_cpuid_b()could also be omitted, which keeps the code changes as minor as possible.
Done
Quoted 20 lines
Quoted 20 lines
target/i386/cpu.c +1
-0break ; }@@ -8316,6 +8332,7 @@ static Property x86_cpu_properties[] = {DEFINE_PROP_BOOL ("full-cpuid-auto-level" ,X86CPU , full_cpuid_auto_level, true),DEFINE_PROP_STRING ("hv-vendor-id" ,X86CPU , hyperv_vendor),DEFINE_PROP_BOOL ("cpuid-0xb" ,X86CPU , enable_cpuid_0xb, true), +DEFINE_PROP_BOOL ("cpuid-0x1f-enforce" ,X86CPU , enable_cpuid_0x1f_enforce, true),DEFINE_PROP_BOOL ("x-vendor-cpuid-only" ,X86CPU , vendor_cpuid_only, true),DEFINE_PROP_BOOL ("x-amd-topoext-features-only" ,X86CPU , amd_topoext_features_only, true), DEFINE_PROP_BOOL("lmce", X86CPU, enable_lmce, false),target/i386/cpu.h +0
-0@@ -2102,6 +2102,11 @@ struct ArchCPU {/* Compatibility bits for old machine types: */ bool enable_cpuid_0xb;
Quoted 3 lines
target/i386/cpu.h struct ArchCPU { · line 2106
Quoted 2 lines
target/i386/cpu.h +2
-0+/* Always return values for 0x1f leaf. In cases where extended CPU topology +* is not supported, return values equivalent of leaf 0xb. s/is not supported/is not configured/
Done
Quoted 33 lines
Quoted 20 lines
target/i386/cpu.h +3
-0+*/ +bool enable_cpuid_0x1f_enforce; +/* Enable auto level-increase for all CPUID leaves */ bool full_cpuid_auto_level;target/i386/kvm/kvm.c +1
-0@@ -1799,6 +1799,7 @@ static uint32_t kvm_x86_build_cpuid(CPUX86State *env,uint32_t limit, i, j;uint32_t unused;struct kvm_cpuid_entry2 *c; +X86CPU *cpu =env_archcpu (env);cpu_x86_cpuid (env,0 ,0 , &limit, &unused, &unused, &unused);target/i386/kvm/kvm.c static uint32_t kvm_x86_build_cpuid(CPUX86State *env, · line 1839
Quoted 9 lines
target/i386/kvm/kvm.c +2
-1@@ -1831,7 +1832,8 @@ static uint32_t kvm_x86_build_cpuid(CPUX86State *env,break ; }case 0x1f : -if (!x86_has_extended_topo (env->avail_cpu_topo)) { +if (!x86_has_extended_topo (env->avail_cpu_topo) && + !cpu->enable_cpuid_0x1f_enforce) { cpuid_i--;break ; }In addition to the above changes, we need to adjust the
min_levelof the CPUID to ensure that0x1fcan be encoded:
Review void x86_cpu_expand_features(X86CPU *cpu, Error **errp) · line 7473
Quoted 9 lines
(quoted diff) +2
-1@@ -7466,7 +7466,8 @@ void x86_cpu_expand_features(X86CPU *cpu, Error **errp) * cpu->vendor_cpuid_only has been unset for compatibility with older * machine types. */ - if (x86_has_extended_topo(env->avail_cpu_topo) && + if ((x86_has_extended_topo(env->avail_cpu_topo) || + cpu->enable_cpuid_0x1f_enforce) && (IS_INTEL_CPU(env) || !cpu->vendor_cpuid_only)) { x86_cpu_adjust_level(cpu, &env->cpuid_min_level, 0x1F); }
Done
Quoted 4 lines
Thanks,
Zhao
Thanks
Manish Mishra
Quoted 3 lines · On Mon, 22 Jul 2024 10:18:59 +0000 "manish.mishra" <[email protected]>…
QEMU does not set
0x1fin case VM does not have extended CPU topology and expects guests to fallback to0xb. Some versions of windows i.e. windows 10, 11 does not like this behavior and expects this leaf to be
^^^^^^^^^^^^^
be more clear about
Quoted 2 lines
populated. This is observed with windows VMs with secure boot, uefi and HyperV role enabled.
add here exact QME CLI and necessary guest OS details to reproduce the issue.
Quoted 5 lines
Leaf
0x1fis superset of0xb, so it makes sense to set0x1fequivalent to0xbby default and workaround windows issue. This change adds a new property 'cpuid-0x1f-enforce' to set leaf0x1fequivalent to0xbin case extended CPU topology is not present and behave as before otherwise.
---
maybe instead of adding workaround it would be better to enable 1F leaf on CPU models that supposed to have it?
Quoted 159 lines
hw/i386/pc.c | 1 + target/i386/cpu.c | 71 +++++++++++++++++++++++++++---------------- target/i386/cpu.h | 5 +++ target/i386/kvm/kvm.c | 4 ++- 4 files changed, 53 insertions(+), 28 deletions(-)hw/i386/pc.c +1
-0@@ -85,6 +85,7 @@ GlobalProperty pc_compat_9_0[] = { { TYPE_X86_CPU, "guest-phys-bits", "0" }, { "sev-guest", "legacy-vm-type", "on" }, { TYPE_X86_CPU, "legacy-multi-node", "on" }, + { TYPE_X86_CPU, "cpuid-0x1f-enforce", "false" }, };const size_t pc_compat_9_0_len =G_N_ELEMENTS (pc_compat_9_0);target/i386/cpu.c +44
-27@@ -416,6 +416,43 @@ static void encode_topo_cpuid1f(CPUX86State *env, uint32_t count,assert (!(*eax & ~0x1f )); } +static void encode_topo_cpuid_b (CPUX86State *env,uint32_t count, +X86CPUTopoInfo *topo_info, +uint32_t threads_per_pkg, +uint32_t *eax,uint32_t *ebx, +uint32_t *ecx,uint32_t *edx) +{ +X86CPU *cpu =env_archcpu (env); + +if (!cpu->enable_cpuid_0xb) { + *eax = *ebx = *ecx = *edx =0 ; +return ; + } + + *ecx = count &0xff ; + *edx = cpu->apic_id; + +switch (count) { +case 0 : + *eax =apicid_core_offset (topo_info); + *ebx = topo_info->threads_per_core; + *ecx |=CPUID_B_ECX_TOPO_LEVEL_SMT <<8 ; +break ; +case 1 : + *eax =apicid_pkg_offset (topo_info); + *ebx = threads_per_pkg; + *ecx |=CPUID_B_ECX_TOPO_LEVEL_CORE <<8 ; +break ; +default : + *eax =0 ; + *ebx =0 ; + *ecx |=CPUID_B_ECX_TOPO_LEVEL_INVALID <<8 ; + } + +assert (!(*eax & ~0x1f )); + *ebx &=0xffff ;/* The count doesn't need to be reliable. */ +} +/* Encode cache info for CPUID[0x80000005].ECX or CPUID[0x80000005].EDX */ static uint32_t encode_cache_cpuid80000005 (CPUCacheInfo *cache) {@@ -6601,33 +6638,8 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,break ;case 0xB :/* Extended Topology Enumeration Leaf */ -if (!cpu->enable_cpuid_0xb) { - *eax = *ebx = *ecx = *edx =0 ; -break ; - } - - *ecx = count &0xff ; - *edx = cpu->apic_id; - -switch (count) { -case 0 : - *eax =apicid_core_offset (&topo_info); - *ebx = topo_info.threads_per_core; - *ecx |=CPUID_B_ECX_TOPO_LEVEL_SMT <<8 ; -break ; -case 1 : - *eax =apicid_pkg_offset (&topo_info); - *ebx = threads_per_pkg; - *ecx |=CPUID_B_ECX_TOPO_LEVEL_CORE <<8 ; -break ; -default : - *eax =0 ; - *ebx =0 ; - *ecx |=CPUID_B_ECX_TOPO_LEVEL_INVALID <<8 ; - } - -assert (!(*eax & ~0x1f )); - *ebx &=0xffff ;/* The count doesn't need to be reliable. */ +encode_topo_cpuid_b (env, count, &topo_info, threads_per_pkg, + eax, ebx, ecx, edx);break ;case 0x1C :if (cpu->enable_pmu && (env->features[FEAT_7_0_EDX ] &CPUID_7_0_EDX_ARCH_LBR )) {@@ -6639,6 +6651,10 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,/* V2 Extended Topology Enumeration Leaf */ if (!x86_has_extended_topo (env->avail_cpu_topo)) { *eax = *ebx = *ecx = *edx =0 ; +if (cpu->enable_cpuid_0x1f_enforce) { +encode_topo_cpuid_b (env, count, &topo_info, threads_per_pkg, + eax, ebx, ecx, edx); + }break ; }@@ -8316,6 +8332,7 @@ static Property x86_cpu_properties[] = {DEFINE_PROP_BOOL ("full-cpuid-auto-level" ,X86CPU , full_cpuid_auto_level, true),DEFINE_PROP_STRING ("hv-vendor-id" ,X86CPU , hyperv_vendor),DEFINE_PROP_BOOL ("cpuid-0xb" ,X86CPU , enable_cpuid_0xb, true), +DEFINE_PROP_BOOL ("cpuid-0x1f-enforce" ,X86CPU , enable_cpuid_0x1f_enforce, true),DEFINE_PROP_BOOL ("x-vendor-cpuid-only" ,X86CPU , vendor_cpuid_only, true),DEFINE_PROP_BOOL ("x-amd-topoext-features-only" ,X86CPU , amd_topoext_features_only, true), DEFINE_PROP_BOOL("lmce", X86CPU, enable_lmce, false),target/i386/cpu.h +5
-0@@ -2102,6 +2102,11 @@ struct ArchCPU {/* Compatibility bits for old machine types: */ bool enable_cpuid_0xb; +/* Always return values for 0x1f leaf. In cases where extended CPU topology +* is not supported, return values equivalent of leaf 0xb. +*/ +bool enable_cpuid_0x1f_enforce; +/* Enable auto level-increase for all CPUID leaves */ bool full_cpuid_auto_level;target/i386/kvm/kvm.c +3
-1@@ -1799,6 +1799,7 @@ static uint32_t kvm_x86_build_cpuid(CPUX86State *env,uint32_t limit, i, j;uint32_t unused;struct kvm_cpuid_entry2 *c; +X86CPU *cpu =env_archcpu (env);cpu_x86_cpuid (env,0 ,0 , &limit, &unused, &unused, &unused);@@ -1831,7 +1832,8 @@ static uint32_t kvm_x86_build_cpuid(CPUX86State *env,break ; }case 0x1f : -if (!x86_has_extended_topo (env->avail_cpu_topo)) { +if (!x86_has_extended_topo (env->avail_cpu_topo) && + !cpu->enable_cpuid_0x1f_enforce) { cpuid_i--;break ; }
Thanks Zhao, Igor and Xiaoyao, I have sent v1 for this https://patchwork.ozlabs.org/project/qemu-devel/patch/[email protected]/.
On 23/07/24 8:33 pm, Igor Mammedov wrote:
!-------------------------------------------------------------------|
CAUTION: External Email
|-------------------------------------------------------------------!
On Mon, 22 Jul 2024 10:18:59 +0000
"manish.mishra" <[email protected]><mailto:[email protected]> wrote:
QEMU does not set 0x1f in case VM does not have extended CPU topology
and expects guests to fallback to 0xb. Some versions of windows i.e.
windows 10, 11 does not like this behavior and expects this leaf to be
^^^^^^^^^^^^^
be more clear about
Done now, please let me know if i need to add more details.
populated. This is observed with windows VMs with secure boot, uefi
and HyperV role enabled.
add here exact QME CLI and necessary guest OS details to reproduce the issue.
Done
Leaf 0x1f is superset of 0xb, so it makes sense to set 0x1f equivalent
to 0xb by default and workaround windows issue. This change adds a
new property 'cpuid-0x1f-enforce' to set leaf 0x1f equivalent to 0xb in
case extended CPU topology is not present and behave as before otherwise.
---
maybe instead of adding workaround it would be better to enable
1F leaf on CPU models that supposed to have it?
Windows expects it only when we have set max cpuid level greater than or equal to 0x1f. I mean if it is exposed it should not be all zeros. SapphireRapids CPU definition raised cpuid level to 0x20, so we starting seeing it with SapphireRapids.
hw/i386/pc.c | 1 +
target/i386/cpu.c | 71 +++++++++++++++++++++++++++----------------
target/i386/cpu.h | 5 +++
target/i386/kvm/kvm.c | 4 ++-
4 files changed, 53 insertions(+), 28 deletions(-)
hw/i386/pc.c +0 -0
index c74931d577..4cab04e443 100644
--- a/hw/i386/pc.c
+++ b/hw/i386/pc.c
@@ -85,6 +85,7 @@ GlobalProperty pc_compat_9_0[] = {
{ TYPE_X86_CPU, "guest-phys-bits", "0" },
{ "sev-guest", "legacy-vm-type", "on" },
{ TYPE_X86_CPU, "legacy-multi-node", "on" },+ { TYPE_X86_CPU, "cpuid-0x1f-enforce", "false" },
};
const size_t pc_compat_9_0_len = G_N_ELEMENTS(pc_compat_9_0);
target/i386/cpu.c +0 -0
index 4688d140c2..f89b2ef335 100644
--- a/target/i386/cpu.c
+++ b/target/i386/cpu.c
@@ -416,6 +416,43 @@ static void encode_topo_cpuid1f(CPUX86State *env, uint32_t count,
assert(!(*eax & ~0x1f));
}
+static void encode_topo_cpuid_b(CPUX86State *env, uint32_t count,
+ X86CPUTopoInfo *topo_info,
+ uint32_t threads_per_pkg,
+ uint32_t *eax, uint32_t *ebx,
+ uint32_t *ecx, uint32_t *edx)
+{
+ X86CPU *cpu = env_archcpu(env);
+
+ if (!cpu->enable_cpuid_0xb) {
+ *eax = *ebx = *ecx = *edx = 0;
+ return;
+ }
+
+ *ecx = count & 0xff;
+ *edx = cpu->apic_id;
+
+ switch (count) {
+ case 0:
+ *eax = apicid_core_offset(topo_info);
+ *ebx = topo_info->threads_per_core;
+ *ecx |= CPUID_B_ECX_TOPO_LEVEL_SMT << 8;
+ break;
+ case 1:
+ *eax = apicid_pkg_offset(topo_info);
+ *ebx = threads_per_pkg;
+ *ecx |= CPUID_B_ECX_TOPO_LEVEL_CORE << 8;
+ break;
+ default:
+ *eax = 0;
+ *ebx = 0;
+ *ecx |= CPUID_B_ECX_TOPO_LEVEL_INVALID << 8;
+ }
+
+ assert(!(*eax & ~0x1f));
+ *ebx &= 0xffff; /* The count doesn't need to be reliable. */
+}
+
/* Encode cache info for CPUID[0x80000005].ECX or CPUID[0x80000005].EDX */
static uint32_t encode_cache_cpuid80000005(CPUCacheInfo *cache)
{
@@ -6601,33 +6638,8 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
break;
case 0xB:
/* Extended Topology Enumeration Leaf */- if (!cpu->enable_cpuid_0xb) {
- *eax = *ebx = *ecx = *edx = 0;
- break;
- }
-
- *ecx = count & 0xff;
- *edx = cpu->apic_id;
-
- switch (count) {
- case 0:
- *eax = apicid_core_offset(&topo_info);
- *ebx = topo_info.threads_per_core;
- *ecx |= CPUID_B_ECX_TOPO_LEVEL_SMT << 8;
- break;
- case 1:
- *eax = apicid_pkg_offset(&topo_info);
- *ebx = threads_per_pkg;
- *ecx |= CPUID_B_ECX_TOPO_LEVEL_CORE << 8;
- break;
- default:
- *eax = 0;
- *ebx = 0;
- *ecx |= CPUID_B_ECX_TOPO_LEVEL_INVALID << 8;
- }
-
- assert(!(*eax & ~0x1f));
- *ebx &= 0xffff; /* The count doesn't need to be reliable. */
+ encode_topo_cpuid_b(env, count, &topo_info, threads_per_pkg,
+ eax, ebx, ecx, edx);
break;
case 0x1C:
if (cpu->enable_pmu && (env->features[FEAT_7_0_EDX] & CPUID_7_0_EDX_ARCH_LBR)) {@@ -6639,6 +6651,10 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
/* V2 Extended Topology Enumeration Leaf */
if (!x86_has_extended_topo(env->avail_cpu_topo)) {
*eax = *ebx = *ecx = *edx = 0;+ if (cpu->enable_cpuid_0x1f_enforce) {
+ encode_topo_cpuid_b(env, count, &topo_info, threads_per_pkg,
+ eax, ebx, ecx, edx);
+ }
break;
}@@ -8316,6 +8332,7 @@ static Property x86_cpu_properties[] = {
DEFINE_PROP_BOOL("full-cpuid-auto-level", X86CPU, full_cpuid_auto_level, true),
DEFINE_PROP_STRING("hv-vendor-id", X86CPU, hyperv_vendor),
DEFINE_PROP_BOOL("cpuid-0xb", X86CPU, enable_cpuid_0xb, true),
+ DEFINE_PROP_BOOL("cpuid-0x1f-enforce", X86CPU, enable_cpuid_0x1f_enforce, true),
DEFINE_PROP_BOOL("x-vendor-cpuid-only", X86CPU, vendor_cpuid_only, true),
DEFINE_PROP_BOOL("x-amd-topoext-features-only", X86CPU, amd_topoext_features_only, true),
DEFINE_PROP_BOOL("lmce", X86CPU, enable_lmce, false),
target/i386/cpu.h +0 -0
index 1e121acef5..718b9f2b0b 100644
--- a/target/i386/cpu.h
+++ b/target/i386/cpu.h
@@ -2102,6 +2102,11 @@ struct ArchCPU {
/* Compatibility bits for old machine types: */
bool enable_cpuid_0xb;+ /* Always return values for 0x1f leaf. In cases where extended CPU topology
+ * is not supported, return values equivalent of leaf 0xb.
+ */
+ bool enable_cpuid_0x1f_enforce;
+
/* Enable auto level-increase for all CPUID leaves */
bool full_cpuid_auto_level;target/i386/kvm/kvm.c +0 -0
index becca2efa5..a9c6f02900 100644
--- a/target/i386/kvm/kvm.c
+++ b/target/i386/kvm/kvm.c
@@ -1799,6 +1799,7 @@ static uint32_t kvm_x86_build_cpuid(CPUX86State *env,
uint32_t limit, i, j;
uint32_t unused;
struct kvm_cpuid_entry2 *c;+ X86CPU *cpu = env_archcpu(env);
cpu_x86_cpuid(env, 0, 0, &limit, &unused, &unused, &unused);
@@ -1831,7 +1832,8 @@ static uint32_t kvm_x86_build_cpuid(CPUX86State *env,
break;
}
case 0x1f:- if (!x86_has_extended_topo(env->avail_cpu_topo)) {
+ if (!x86_has_extended_topo(env->avail_cpu_topo) &&
+ !cpu->enable_cpuid_0x1f_enforce) {
cpuid_i--;
break;
}