target/i386: Avoid cpu number overflow in legacy topology
PATCH v2 1/1 target/i386: Avoid cpu number overflow in legacy topology
The legacy topology enumerated by CPUID.1.EBX[23:16] is defined in SDM Vol2:
Bits 23-16: Maximum number of addressable IDs for logical processors in this physical package.
When launching the VM with -smp 256, the value written to EBX[23:16] is 0 because of data overflow. If the guest only supports legacy topology, without V2 Extended Topology enumerated by CPUID.0x1f or Extended Topology enumerated by CPUID.0x0b to support over 255 CPUs, the return of the kernel invoking cpu_smt_allowed() is false and AP's bring-up will fail. Then only CPU 0 is online, and others are offline.
To avoid this issue caused by overflow, limit the max value written to EBX[23:16] to 255.
---
Changes v1 -> v2:
- Revise the commit message and comment to more clearer.
- Rebased to v8.1.0-rc2.
--- target/i386/cpu.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
target/i386/cpu.c +14 -2
@@ -6008,6 +6008,7 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,uint32_t die_offset;uint32_t limit;uint32_t signature[3 ]; +uint32_t threads_per_socket;X86CPUTopoInfo topo_info; topo_info.dies_per_pkg = env->nr_dies;
@@ -6049,8 +6050,19 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *ecx |=CPUID_EXT_OSXSAVE ; } *edx = env->features[FEAT_1_EDX ]; -if (cs->nr_cores * cs->nr_threads >1 ) { - *ebx |= (cs->nr_cores * cs->nr_threads) <<16 ; +/* +* Only bits [23:16] represent the maximum number of addressable +* IDs for logical processors in this physical package. +* When thread_per_socket > 255, it will 1) overwrite bits[31:24] +* which is apic_id, 2) bits [23:16] get truncated. +*/ + threads_per_socket = cs->nr_cores * cs->nr_threads; +if (threads_per_socket >255 ) { + threads_per_socket =255 ; + } + +if (threads_per_socket >1 ) { + *ebx |= threads_per_socket <<16 ; *edx |=CPUID_HT ; }if (!cpu->enable_pmu) {
Signature · 1 line
2.25.1
Quoted 13 lines · On Wed, 9 Aug 2023 18:27:32 +0800 Qian Wen <[email protected]> wrote:
The legacy topology enumerated by CPUID.1.EBX[23:16] is defined in SDM Vol2:
Bits 23-16: Maximum number of addressable IDs for logical processors in this physical package.
When launching the VM with -smp 256, the value written to EBX[23:16] is 0 because of data overflow. If the guest only supports legacy topology, without V2 Extended Topology enumerated by CPUID.0x1f or Extended Topology enumerated by CPUID.0x0b to support over 255 CPUs, the return of the kernel invoking
cpu_smt_allowed()is false and AP's bring-up will fail. Then only CPU 0 is online, and others are offline.
Quoted 2 lines
To avoid this issue caused by overflow, limit the max value written to EBX[23:16] to 255.
what happens on real hw or in lack of thereof what SDM says about this value when there is more than 255 threads?.
Quoted 43 lines
Signed-off-by Qian Wen <[email protected]>---
Changes v1 -> v2:
- Revise the commit message and comment to more clearer.
- Rebased to v8.1.0-rc2.--- target/i386/cpu.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)target/i386/cpu.c +14
-2@@ -6008,6 +6008,7 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,uint32_t die_offset;uint32_t limit;uint32_t signature[3 ]; +uint32_t threads_per_socket;X86CPUTopoInfo topo_info; topo_info.dies_per_pkg = env->nr_dies;@@ -6049,8 +6050,19 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *ecx |=CPUID_EXT_OSXSAVE ; } *edx = env->features[FEAT_1_EDX ]; -if (cs->nr_cores * cs->nr_threads >1 ) { - *ebx |= (cs->nr_cores * cs->nr_threads) <<16 ; +/* +* Only bits [23:16] represent the maximum number of addressable +* IDs for logical processors in this physical package. +* When thread_per_socket > 255, it will 1) overwrite bits[31:24] +* which is apic_id, 2) bits [23:16] get truncated. +*/ + threads_per_socket = cs->nr_cores * cs->nr_threads; +if (threads_per_socket >255 ) { + threads_per_socket =255 ; + } + +if (threads_per_socket >1 ) { + *ebx |= threads_per_socket <<16 ; *edx |=CPUID_HT ; }if (!cpu->enable_pmu) {
Quoted 16 lines · Igor Mammedov
Quoted 13 lines · On Wed, 9 Aug 2023 18:27:32 +0800 Qian Wen <[email protected]> wrote:
The legacy topology enumerated by CPUID.1.EBX[23:16] is defined in SDM Vol2:
Bits 23-16: Maximum number of addressable IDs for logical processors in this physical package.
When launching the VM with -smp 256, the value written to EBX[23:16] is 0 because of data overflow. If the guest only supports legacy topology, without V2 Extended Topology enumerated by CPUID.0x1f or Extended Topology enumerated by CPUID.0x0b to support over 255 CPUs, the return of the kernel invoking
cpu_smt_allowed()is false and AP's bring-up will fail. Then only CPU 0 is online, and others are offline.
Quoted 5 lines
Quoted 2 lines
To avoid this issue caused by overflow, limit the max value written to EBX[23:16] to 255.
what happens on real hw or in lack of thereof what SDM says about this value when there is more than 255 threads?.
Current SDM doesn't specify what the value should be when APIC IDs per package exceeds 255. So we asked the internal HW architect, the response is that EBX[23:16] will report 255 instead of being truncated to a smaller value.
Thanks,
Qian
Quoted 43 lines
Quoted 43 lines
Signed-off-by Qian Wen <[email protected]>---
Changes v1 -> v2:
- Revise the commit message and comment to more clearer.
- Rebased to v8.1.0-rc2.--- target/i386/cpu.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)target/i386/cpu.c +14
-2@@ -6008,6 +6008,7 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,uint32_t die_offset;uint32_t limit;uint32_t signature[3 ]; +uint32_t threads_per_socket;X86CPUTopoInfo topo_info; topo_info.dies_per_pkg = env->nr_dies;@@ -6049,8 +6050,19 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *ecx |=CPUID_EXT_OSXSAVE ; } *edx = env->features[FEAT_1_EDX ]; -if (cs->nr_cores * cs->nr_threads >1 ) { - *ebx |= (cs->nr_cores * cs->nr_threads) <<16 ; +/* +* Only bits [23:16] represent the maximum number of addressable +* IDs for logical processors in this physical package. +* When thread_per_socket > 255, it will 1) overwrite bits[31:24] +* which is apic_id, 2) bits [23:16] get truncated. +*/ + threads_per_socket = cs->nr_cores * cs->nr_threads; +if (threads_per_socket >255 ) { + threads_per_socket =255 ; + } + +if (threads_per_socket >1 ) { + *ebx |= threads_per_socket <<16 ; *edx |=CPUID_HT ; }if (!cpu->enable_pmu) {
Quoted 17 lines · Qian
Quoted 16 lines · Igor Mammedov
Quoted 13 lines · On Wed, 9 Aug 2023 18:27:32 +0800 Qian Wen <[email protected]> wrote:
The legacy topology enumerated by CPUID.1.EBX[23:16] is defined in SDM Vol2:
Bits 23-16: Maximum number of addressable IDs for logical processors in this physical package.
When launching the VM with -smp 256, the value written to EBX[23:16] is 0 because of data overflow. If the guest only supports legacy topology, without V2 Extended Topology enumerated by CPUID.0x1f or Extended Topology enumerated by CPUID.0x0b to support over 255 CPUs, the return of the kernel invoking
cpu_smt_allowed()is false and AP's bring-up will fail. Then only CPU 0 is online, and others are offline.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
To avoid this issue caused by overflow, limit the max value written to EBX[23:16] to 255.
what happens on real hw or in lack of thereof what SDM says about this value when there is more than 255 threads?.
Current SDM doesn't specify what the value should be when APIC IDs per package exceeds 255. So we asked the internal HW architect, the response is that EBX[23:16] will report 255 instead of being truncated to a smaller value.
then mention it in commit log so one wouldn't wonder where the value came from.
Quoted 45 lines
Thanks,
QianQuoted 41 lines
Quoted 41 lines
Signed-off-by Qian Wen <[email protected]>---
Changes v1 -> v2:
- Revise the commit message and comment to more clearer.
- Rebased to v8.1.0-rc2.--- target/i386/cpu.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)target/i386/cpu.c +14
-2@@ -6008,6 +6008,7 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,uint32_t die_offset;uint32_t limit;uint32_t signature[3 ]; +uint32_t threads_per_socket;X86CPUTopoInfo topo_info; topo_info.dies_per_pkg = env->nr_dies;@@ -6049,8 +6050,19 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *ecx |=CPUID_EXT_OSXSAVE ; } *edx = env->features[FEAT_1_EDX ]; -if (cs->nr_cores * cs->nr_threads >1 ) { - *ebx |= (cs->nr_cores * cs->nr_threads) <<16 ; +/* +* Only bits [23:16] represent the maximum number of addressable +* IDs for logical processors in this physical package. +* When thread_per_socket > 255, it will 1) overwrite bits[31:24] +* which is apic_id, 2) bits [23:16] get truncated. +*/ + threads_per_socket = cs->nr_cores * cs->nr_threads; +if (threads_per_socket >255 ) { + threads_per_socket =255 ; + } + +if (threads_per_socket >1 ) { + *ebx |= threads_per_socket <<16 ;
^^^^^^^^^^^^^^^^^^^^^^^^^
more robust would be mask out non-relevant fields at rhs
also perhaps double check if we could do induce similar overflow tweaking other -smp properties (todo for another patch[es] if there are such places).
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
target/i386/cpu.c +0
-0*edx |=CPUID_HT ; }if (!cpu->enable_pmu) {
Quoted 20 lines · Igor Mammedov
Quoted 17 lines · Qian
Quoted 16 lines · Igor Mammedov
Quoted 13 lines · On Wed, 9 Aug 2023 18:27:32 +0800 Qian Wen <[email protected]> wrote:
The legacy topology enumerated by CPUID.1.EBX[23:16] is defined in SDM Vol2:
Bits 23-16: Maximum number of addressable IDs for logical processors in this physical package.
When launching the VM with -smp 256, the value written to EBX[23:16] is 0 because of data overflow. If the guest only supports legacy topology, without V2 Extended Topology enumerated by CPUID.0x1f or Extended Topology enumerated by CPUID.0x0b to support over 255 CPUs, the return of the kernel invoking
cpu_smt_allowed()is false and AP's bring-up will fail. Then only CPU 0 is online, and others are offline.
Quoted 7 lines
Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
To avoid this issue caused by overflow, limit the max value written to EBX[23:16] to 255.
what happens on real hw or in lack of thereof what SDM says about this value when there is more than 255 threads?.
Current SDM doesn't specify what the value should be when APIC IDs per package exceeds 255. So we asked the internal HW architect, the response is that EBX[23:16] will report 255 instead of being truncated to a smaller value.
then mention it in commit log so one wouldn't wonder where the value came from.
Ok, thanks for your suggestion!
Quoted 44 lines
Quoted 35 lines
Thanks,
QianQuoted 32 lines
Quoted 32 lines
Signed-off-by Qian Wen <[email protected]>---
Changes v1 -> v2:
- Revise the commit message and comment to more clearer.
- Rebased to v8.1.0-rc2.--- target/i386/cpu.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)target/i386/cpu.c +6
-2@@ -6008,6 +6008,7 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,uint32_t die_offset;uint32_t limit;uint32_t signature[3 ]; +uint32_t threads_per_socket;X86CPUTopoInfo topo_info; topo_info.dies_per_pkg = env->nr_dies;@@ -6049,8 +6050,19 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *ecx |=CPUID_EXT_OSXSAVE ; } *edx = env->features[FEAT_1_EDX ]; -if (cs->nr_cores * cs->nr_threads >1 ) { - *ebx |= (cs->nr_cores * cs->nr_threads) <<16 ; +/* +* Only bits [23:16] represent the maximum number of addressable +* IDs for logical processors in this physical package. +* When thread_per_socket > 255, it will 1) overwrite bits[31:24] +* which is apic_id, 2) bits [23:16] get truncated. Quoted 8 lines
Quoted 8 lines
target/i386/cpu.c void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, · line 6065
Quoted 8 lines
target/i386/cpu.c +8
-0+*/ + threads_per_socket = cs->nr_cores * cs->nr_threads; +if (threads_per_socket >255 ) { + threads_per_socket =255 ; + } + +if (threads_per_socket >1 ) { + *ebx |= threads_per_socket <<16 ;^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 1 line
more robust would be mask out non-relevant fields at rhs
I think a mask for this case is a bit redundant, since the limitation of 255 already filtered non-relevant fields. I prefer not to add the mask here and keep code style consistency with others place.
Quoted 2 lines
also perhaps double check if we could do induce similar overflow tweaking other -smp properties (todo for another patch[es] if there are such places).
I have a check, the CPUID.0x4:EAX[31:26] indicates the Maximum number of addressable IDs for processor cores in the physical package. If we launch over 64 cores VM, the 6-bits field will also overflow. I will add the following fix to patch2 in v2.
target/i386/cpu.c +0 -0
@@ -243,6 +243,7 @@ static void encode_cache_cpuid4(CPUCacheInfo *cache,
cache->partitions * cache->sets);
assert(num_apic_ids > 0);
+ num_cores = num_cores > 64 ? 64 : num_cores; *eax = CACHE_TYPE(cache->type) | CACHE_LEVEL(cache->level) | (cache->self_init ? CACHE_SELF_INIT_LEVEL : 0) |
Thanks,
Qian
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
target/i386/cpu.c +0
-0*edx |=CPUID_HT ; }if (!cpu->enable_pmu) {
Hi Qian,
On Sun, Aug 13, 2023 at 06:49:40PM +0800, Wen, Qian wrote:
[snip]
Quoted 6 lines
Quoted 3 lines
also perhaps double check if we could do induce similar overflow tweaking other -smp properties (todo for another patch[es] if there are such places).
I have a check, the CPUID.0x4:EAX[31:26] indicates the Maximum number of addressable IDs for processor cores in the physical package. If we launch over 64 cores VM, the 6-bits field will also overflow. I will add the following fix to patch2 in v2.
Good catch!
Just discussion, I find if we use APIC ID offset to encode 0x4, then it seems no need for an explicit check [1], right?
[1]: https://lists.gnu.org/archive/html/qemu-devel/2023-08/msg00027.html
Thanks,
Zhao
Quoted 20 lines
target/i386/cpu.c +1
-0@@ -243,6 +243,7 @@ static void encode_cache_cpuid4(CPUCacheInfo *cache, cache->partitions * cache->sets); assert(num_apic_ids > 0); + num_cores = num_cores > 64 ? 64 : num_cores; *eax = CACHE_TYPE(cache->type) |
CACHE_LEVEL(cache->level)| (cache->self_init?CACHE_SELF_INIT_LEVEL: 0) |Thanks,
QianQuoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
*edx |= CPUID_HT; } if (!cpu->enable_pmu) {
Quoted 8 lines · Zhao Liu
Hi Qian,
On Sun, Aug 13, 2023 at 06:49:40PM +0800, Wen, Qian wrote:
[snip]
Quoted 2 lines
Quoted 2 lines
also perhaps double check if we could do induce similar overflow tweaking other -smp properties (todo for another patch[es] if there are such places).
Quoted 8 lines
Quoted 2 lines
I have a check, the CPUID.0x4:EAX[31:26] indicates the Maximum number of addressable IDs for processor cores in the physical package. If we launch over 64 cores VM, the 6-bits field will also overflow. I will add the following fix to patch2 in v3.
Good catch!
Just discussion, I find if we use APIC ID offset to encode
0x4, then it seems no need for an explicit check [1], right?[1]: https://lists.gnu.org/archive/html/qemu-devel/2023-08/msg00027.html
Yes. The offset is always power of 2, so the value written to the 6-bit field likes 0b1111, 0b11111, 0b111111, 0b1111111... So, EAX[31:26] will be expected, i.e., 0x3f, when the value is overflow and truncated.
Quoted 23 lines
Thanks,
ZhaoQuoted 19 lines
target/i386/cpu.c +1
-0@@ -243,6 +243,7 @@ static void encode_cache_cpuid4(CPUCacheInfo *cache, cache->partitions * cache->sets); assert(num_apic_ids > 0); + num_cores = num_cores > 64 ? 64 : num_cores; *eax = CACHE_TYPE(cache->type) |
CACHE_LEVEL(cache->level)| (cache->self_init?CACHE_SELF_INIT_LEVEL: 0) |Thanks,
QianQuoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
*edx |= CPUID_HT; } if (!cpu->enable_pmu) {