i386: Track X86CPUTopoInfo in CPUX86State and track features in env->features[]
v2 cover i386: Track X86CPUTopoInfo in CPUX86State and track features in env->features[]
This v2 series is follow up work of [1],[2].
The initial purpose is to track CPUID_HT in env->features[] for x86. It's supposed to be implemented in x86_cpu_realizefn() and before qemu_init_vcpu(). However, CPUID_HT bit needs to be evaluated based on topology information, either 1) nr_cores and nr_threads of CPUState or
2) MachineState::smp.
1) are initialized in qemu_init_vcpu() which is too late. So [1] was sent to initialize nr_threads and nr_cores of CPUState earlier. It touches all the ARCHes and people disliked it. Followup attempt to initialize them in cpu_common_initfn() was also rejected. [3]
2) needs to grab MachineState in cpu context and was disagreed[3]
As suggested by Igor[3], aduit nr_threads and nr_cores and initialize them in pre_plug() callback for the ARCHes where they are actually needed. I found nr_cores is only used by x86 and nr_threads is used by X86/MIPS/PPC[4]. I didn't play with MIPS and PPC before and I'm incapable to write patch for them. So I send an RFC work [2], which drops nr_cores from CPUState and it instead maintains a substitute for x86 only. The RFC work left how to handle nr_threads as open however I didn't get any feedback yet.
Eventually, I decided to fix x86 only. The sulution is to maintain a X86CPUTopoInfo in CPUX86State, so that x86 doesn't need to use nr_threads and nr_cores anymore and the dependency on qemu_init_vcpu() goes away.
x86 is the only user of CPUState::nr_core currently. When it switches to use CPUX86state::X86CPUTopoInfo, CPUState::nr_core loses its only user and can be dropped in patch 8.
In the future, if MIPS and PPC starts to maintain nr_threads in its own CPU state, common CPUState::nr_threads can be dropped as well.
Besides the patches to fulfill the main goal to track CPUID_HT in env->features[] for x86. This series also contains the cleanup patches during the work.
[1] https://lore.kernel.org/qemu-devel/[email protected]/
[2] https://lore.kernel.org/qemu-devel/[email protected]/
[3] https://lore.kernel.org/qemu-devel/[email protected]/
[4] https://lore.kernel.org/qemu-devel/[email protected]/
Xiaoyao Li (10):
i386/cpu: Extract a common fucntion to setup value of
MSR_CORE_THREAD_COUNT
i386/cpu: Drop the variable smp_cores and smp_threads in
x86_cpu_pre_plug()
i386/cpu: Drop cores_per_pkg in cpu_x86_cpuid()
i386/topology: Update the comment of x86_apicid_from_topo_ids()
i386/topology: Introduce helpers for various topology info of
different level
i386/cpu: Track a X86CPUTopoInfo directly in CPUX86State
i386/cpu: Hoist check of CPUID_EXT3_TOPOEXT against threads_per_core
cpu: Remove nr_cores from struct CPUState
i386/cpu: Set up CPUID_HT in x86_cpu_expand_features() instead of
cpu_x86_cpuid()
i386/cpu: Set and track CPUID_EXT3_CMP_LEG in
env->features[FEAT_8000_0001_ECX]hw/core/cpu-common.c | 1 - hw/i386/x86-common.c | 22 +++--- include/hw/core/cpu.h | 2 - include/hw/i386/topology.h | 30 ++++++- system/cpus.c | 1 - target/i386/cpu-sysemu.c | 11 +++ target/i386/cpu.c | 113 ++++++++++++--------------- target/i386/cpu.h | 8 +- target/i386/hvf/x86_emu.c | 3 +- target/i386/kvm/kvm.c | 5 +- target/i386/tcg/sysemu/misc_helper.c | 3 +- 11 files changed, 103 insertions(+), 96 deletions(-)
Signature · 1 line
2.34.1
PATCH v2 1/10 i386/cpu: Extract a common fucntion to setup value of MSR_CORE_THREAD_COUNT
There are duplicated code to setup the value of MSR_CORE_THREAD_COUNT. Extract a common function for it.
---
Changes in v2:
- move the implementation of cpu_x86_get_msr_core_thread_count() to target/i386/cpu-sysemu.c;
--- target/i386/cpu-sysemu.c | 11 +++++++++++ target/i386/cpu.h | 2 ++ target/i386/hvf/x86_emu.c | 3 +-- target/i386/kvm/kvm.c | 5 +---- target/i386/tcg/sysemu/misc_helper.c | 3 +-- 5 files changed, 16 insertions(+), 8 deletions(-)
target/i386/cpu-sysemu.c +11 -0
@@ -309,3 +309,14 @@ void x86_cpu_get_crash_info_qom(Object *obj, Visitor *v, errp);qapi_free_GuestPanicInformation (panic_info); } + +uint64_t cpu_x86_get_msr_core_thread_count (X86CPU *cpu) +{ +CPUState *cs =CPU (cpu); +uint64_t val; + + val = cs->nr_threads * cs->nr_cores;/* thread count, bits 15..0 */ + val |= ((uint32_t )cs->nr_cores <<16 );/* core count, bits 31..16 */ + +return val; +}
target/i386/cpu.h +2 -0
@@ -2390,6 +2390,8 @@ static inline void cpu_x86_load_seg_cache_sipi(X86CPU *cpu, cs->halted =0 ; } +uint64_t cpu_x86_get_msr_core_thread_count (X86CPU *cpu); +int cpu_x86_get_descr_debug (CPUX86State *env,unsigned int selector,target_ulong *base,unsigned int *limit,unsigned int *flags);
target/i386/hvf/x86_emu.c +1 -2
@@ -765,8 +765,7 @@ void simulate_rdmsr(CPUX86State *env) val = env->mtrr_deftype;break ;case MSR_CORE_THREAD_COUNT : - val = cs->nr_threads * cs->nr_cores;/* thread count, bits 15..0 */ - val |= ((uint32_t )cs->nr_cores <<16 );/* core count, bits 31..16 */ + val =cpu_x86_get_msr_core_thread_count (cpu);break ;default :/* fprintf(stderr, "%s: unknown msr 0x%x\n", __func__, msr); */
target/i386/kvm/kvm.c +1 -4
@@ -2602,10 +2602,7 @@ static bool kvm_rdmsr_core_thread_count(X86CPU *cpu,uint32_t msr, uint64_t *val) { - CPUState *cs = CPU(cpu); - - *val = cs->nr_threads * cs->nr_cores;/* thread count, bits 15..0 */ - *val |= ((uint32_t )cs->nr_cores <<16 );/* core count, bits 31..16 */ + *val =cpu_x86_get_msr_core_thread_count (cpu);return true; }
target/i386/tcg/sysemu/misc_helper.c +1 -2
@@ -468,8 +468,7 @@ void helper_rdmsr(CPUX86State *env) val = x86_cpu->ucode_rev;break ;case MSR_CORE_THREAD_COUNT : { -CPUState *cs =CPU (x86_cpu); - val = (cs->nr_threads * cs->nr_cores) | (cs->nr_cores <<16 ); + val =cpu_x86_get_msr_core_thread_count (x86_cpu);break ; } case MSR_APIC_START ... MSR_APIC_END: {
Signature · 1 line
2.34.1
Re: [PATCH v2 01/10] i386/cpu: Extract a common fucntion to setup value of MSR_CORE_THREAD_COUNT
Quoted 14 lines · Xiaoyao Li
Date: Thu, 19 Dec 2024 06:01:16 -0500
From: Xiaoyao Li <[email protected]>
Subject: [PATCH v2 01/10] i386/cpu: Extract a common fucntion to setup
value ofMSR_CORE_THREAD_COUNT
X-Mailer: git-send-email 2.34.1There are duplicated code to setup the value of
MSR_CORE_THREAD_COUNT. Extract a common function for it.Signed-off-by Xiaoyao Li <[email protected]>---
Changes in v2:
- move the implementation ofcpu_x86_get_msr_core_thread_count()totarget/i386/cpu-sysemu.c;
Quoted 8 lines
--- target/i386/cpu-sysemu.c | 11 +++++++++++ target/i386/cpu.h | 2 ++ target/i386/hvf/x86_emu.c | 3 +-- target/i386/kvm/kvm.c | 5 +---- target/i386/tcg/sysemu/misc_helper.c | 3 +-- 5 files changed, 16 insertions(+), 8 deletions(-)
Re: [PATCH v2 01/10] i386/cpu: Extract a common fucntion to setup value of MSR_CORE_THREAD_COUNT
In reply to Xiaoyao LiQuoted 17 lines · Xiaoyao Li
There are duplicated code to setup the value of
MSR_CORE_THREAD_COUNT. Extract a common function for it.Signed-off-by Xiaoyao Li <[email protected]>---
Changes in v2:
- move the implementation ofcpu_x86_get_msr_core_thread_count()totarget/i386/cpu-sysemu.c;--- target/i386/cpu-sysemu.c | 11 +++++++++++ target/i386/cpu.h | 2 ++ target/i386/hvf/x86_emu.c | 3 +-- target/i386/kvm/kvm.c | 5 +---- target/i386/tcg/sysemu/misc_helper.c | 3 +-- 5 files changed, 16 insertions(+), 8 deletions(-)
target/i386/cpu-sysemu.c void x86_cpu_get_crash_info_qom(Object *obj, Visitor *v, · line 321
Quoted 9 lines
target/i386/cpu-sysemu.c +9
-0+uint64_t cpu_x86_get_msr_core_thread_count (X86CPU *cpu) +{ +CPUState *cs =CPU (cpu); +uint64_t val; + + val = cs->nr_threads * cs->nr_cores;/* thread count, bits 15..0 */ + val |= ((uint32_t )cs->nr_cores <<16 );/* core count, bits 31..16 */ + +return val;
Alternatively:
return deposit64(cs->nr_threads * cs->nr_cores, 16, 16,
cs->nr_cores);Quoted 1 line
+}
Re: [PATCH v2 01/10] i386/cpu: Extract a common fucntion to setup value of MSR_CORE_THREAD_COUNT
Quoted 30 lines · Philippe Mathieu-Daudé
Quoted 17 lines · Xiaoyao Li
There are duplicated code to setup the value of
MSR_CORE_THREAD_COUNT. Extract a common function for it.Signed-off-by Xiaoyao Li <[email protected]>---
Changes in v2:
- move the implementation ofcpu_x86_get_msr_core_thread_count()totarget/i386/cpu-sysemu.c;
---
target/i386/cpu-sysemu.c | 11 +++++++++++ target/i386/cpu.h | 2 ++ target/i386/hvf/x86_emu.c | 3 +-- target/i386/kvm/kvm.c | 5 +----target/i386/tcg/sysemu/misc_helper.c| 3 +--5 files changed, 16 insertions(+), 8 deletions(-)target/i386/cpu-sysemu.c void x86_cpu_get_crash_info_qom(Object *obj, Visitor *v, · line 321
Quoted 9 lines
target/i386/cpu-sysemu.c +9
-0+uint64_t cpu_x86_get_msr_core_thread_count (X86CPU *cpu) +{ + CPUState *cs = CPU(cpu); + uint64_t val; + + val = cs->nr_threads * cs->nr_cores; /* thread count, bits 15..0 */ + val |= ((uint32_t)cs->nr_cores << 16); /* core count, bits 31..16 */ + + return val;Alternatively:
Quoted 5 lines
return
deposit64(cs->nr_threads * cs->nr_cores, 16, 16, cs->nr_cores);Quoted 1 line
+}
Typo "function" in patch subject.
Re: [PATCH v2 01/10] i386/cpu: Extract a common fucntion to setup value of MSR_CORE_THREAD_COUNT
Quoted 33 lines · Philippe Mathieu-Daudé
Quoted 32 lines · Philippe Mathieu-Daudé
Quoted 23 lines · Xiaoyao Li
There are duplicated code to setup the value of
MSR_CORE_THREAD_COUNT. Extract a common function for it.Signed-off-by Xiaoyao Li <[email protected]>---
Changes in v2:
- move the implementation ofcpu_x86_get_msr_core_thread_count()totarget/i386/cpu-sysemu.c;
---
target/i386/cpu-sysemu.c | 11 +++++++++++ target/i386/cpu.h | 2 ++ target/i386/hvf/x86_emu.c | 3 +-- target/i386/kvm/kvm.c | 5 +----target/i386/tcg/sysemu/misc_helper.c| 3 +--5 files changed, 16 insertions(+), 8 deletions(-)target/i386/cpu-sysemu.c +6
-0+uint64_t cpu_x86_get_msr_core_thread_count (X86CPU *cpu) +{ + CPUState *cs = CPU(cpu); + uint64_t val; + + val = cs->nr_threads * cs->nr_cores; /* thread count, bitsQuoted 5 lines
15..0 */
+ val |= ((uint32_t)cs->nr_cores << 16); /* core count, bits 31..16 */
+
+ return val;Alternatively:
Quoted 2 lines
Quoted 2 lines
return
deposit64(cs->nr_threads * cs->nr_cores, 16, 16, cs->nr_cores);
I would rather keep it as-is as suguested by Philippe[1].
(deposit64() is really a new thing for i386)
[1] https://lore.kernel.org/qemu-devel/[email protected]/
Quoted 4 lines
Quoted 2 lines
Quoted 1 line
+}
Typo "function" in patch subject.
thanks for catching it!
Re: [PATCH v2 01/10] i386/cpu: Extract a common fucntion to setup value of MSR_CORE_THREAD_COUNT
Quoted 34 lines · Xiaoyao Li
Quoted 33 lines · Philippe Mathieu-Daudé
Quoted 32 lines · Philippe Mathieu-Daudé
Quoted 23 lines · Xiaoyao Li
There are duplicated code to setup the value of
MSR_CORE_THREAD_COUNT. Extract a common function for it.Signed-off-by Xiaoyao Li <[email protected]>---
Changes in v2:
- move the implementation ofcpu_x86_get_msr_core_thread_count()totarget/i386/cpu-sysemu.c;
---
target/i386/cpu-sysemu.c | 11 +++++++++++ target/i386/cpu.h | 2 ++ target/i386/hvf/x86_emu.c | 3 +-- target/i386/kvm/kvm.c | 5 +----target/i386/tcg/sysemu/misc_helper.c| 3 +--5 files changed, 16 insertions(+), 8 deletions(-)target/i386/cpu-sysemu.c +6
-0+uint64_t cpu_x86_get_msr_core_thread_count (X86CPU *cpu) +{ + CPUState *cs = CPU(cpu); + uint64_t val; + + val = cs->nr_threads * cs->nr_cores; /* thread count, bitsQuoted 5 lines
15..0 */
+ val |= ((uint32_t)cs->nr_cores << 16); /* core count, bits 31..16 */
+
+ return val;Alternatively:
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
return
deposit64(cs->nr_threads * cs->nr_cores, 16, 16, cs->nr_cores);I would rather keep it as-is as suguested by Philippe[1].
Sorry for being wrong with the name. (awkward)
s/Philippe/Igor
Quoted 12 lines
(
deposit64()is really a new thing for i386)[1] https://lore.kernel.org/qemu- devel/[email protected]/
Quoted 4 lines
Quoted 2 lines
Quoted 1 line
+}
Typo "function" in patch subject.
thanks for catching it!
Re: [PATCH v2 01/10] i386/cpu: Extract a common fucntion to setup value of MSR_CORE_THREAD_COUNT
Quoted 35 lines · Xiaoyao Li
Quoted 34 lines · Xiaoyao Li
Quoted 33 lines · Philippe Mathieu-Daudé
Quoted 32 lines · Philippe Mathieu-Daudé
Quoted 23 lines · Xiaoyao Li
There are duplicated code to setup the value of
MSR_CORE_THREAD_COUNT. Extract a common function for it.Signed-off-by Xiaoyao Li <[email protected]>---
Changes in v2:
- move the implementation ofcpu_x86_get_msr_core_thread_count()totarget/i386/cpu-sysemu.c;
---
target/i386/cpu-sysemu.c | 11 +++++++++++ target/i386/cpu.h | 2 ++ target/i386/hvf/x86_emu.c | 3 +-- target/i386/kvm/kvm.c | 5 +----target/i386/tcg/sysemu/misc_helper.c| 3 +--5 files changed, 16 insertions(+), 8 deletions(-)target/i386/cpu-sysemu.c +6
-0+uint64_t cpu_x86_get_msr_core_thread_count (X86CPU *cpu) +{ + CPUState *cs = CPU(cpu); + uint64_t val; + + val = cs->nr_threads * cs->nr_cores; /* thread count, bitsQuoted 5 lines
15..0 */
+ val |= ((uint32_t)cs->nr_cores << 16); /* core count, bits 31..16 */
+
+ return val;Alternatively:
Quoted 12 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
return
deposit64(cs->nr_threads * cs->nr_cores, 16, 16, cs->nr_cores);I would rather keep it as-is as suguested by Philippe[1].
Sorry for being wrong with the name. (awkward)
s/Philippe/Igor
Quoted 3 lines
(
deposit64()is really a new thing for i386)[1] https://lore.kernel.org/qemu- devel/[email protected]/
Then use deposit64() in another patch on top?
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Quoted 1 line
+}
Typo "function" in patch subject.
thanks for catching it!
PATCH v2 2/10 i386/cpu: Drop the variable smp_cores and smp_threads in x86_cpu_pre_plug()
In reply to Xiaoyao LiNo need to define smp_cores and smp_threads, just using ms->smp.cores and ms->smp.threads is straightforward. It's also consistent with other checks of socket/die/module.
--- hw/i386/x86-common.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
hw/i386/x86-common.c +4 -6
@@ -248,8 +248,6 @@ void x86_cpu_pre_plug(HotplugHandler *hotplug_dev,CPUX86State *env = &cpu->env;MachineState *ms =MACHINE (hotplug_dev);X86MachineState *x86ms =X86_MACHINE (hotplug_dev); -unsigned int smp_cores = ms->smp.cores; -unsigned int smp_threads = ms->smp.threads;X86CPUTopoInfo topo_info;if (!object_dynamic_cast (OBJECT (cpu), ms->cpu_type)) {
@@ -329,17 +327,17 @@ void x86_cpu_pre_plug(HotplugHandler *hotplug_dev,if (cpu->core_id <0 ) {error_setg (errp,"CPU core-id is not set" );return ; - }else if (cpu->core_id > (smp_cores -1 )) { + }else if (cpu->core_id > (ms->smp.cores -1 )) {error_setg (errp,"Invalid CPU core-id: %u must be in range 0:%u" , - cpu->core_id, smp_cores -1 ); + cpu->core_id, ms->smp.cores -1 );return ; }if (cpu->thread_id <0 ) {error_setg (errp,"CPU thread-id is not set" );return ; - }else if (cpu->thread_id > (smp_threads -1 )) { + }else if (cpu->thread_id > (ms->smp.threads -1 )) {error_setg (errp,"Invalid CPU thread-id: %u must be in range 0:%u" , - cpu->thread_id, smp_threads -1 ); + cpu->thread_id, ms->smp.threads -1 );return ; }
Signature · 1 line
2.34.1
Re: [PATCH v2 02/10] i386/cpu: Drop the variable smp_cores and smp_threads in x86_cpu_pre_plug()
Quoted 6 lines · Xiaoyao Li
Date: Thu, 19 Dec 2024 06:01:17 -0500
From: Xiaoyao Li <[email protected]>
Subject: [PATCH v2 02/10] i386/cpu: Drop the variablesmp_coresand
smp_threadsinx86_cpu_pre_plug()
X-Mailer: git-send-email 2.34.1
Quoted 9 lines
No need to define
smp_coresandsmp_threads, just usingms->smp.coresandms->smp.threadsis straightforward. It's also consistent with other checks of socket/die/module.Signed-off-by Xiaoyao Li <[email protected]>--- hw/i386/x86-common.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
PATCH v2 3/10 i386/cpu: Drop cores_per_pkg in cpu_x86_cpuid()
In reply to Xiaoyao LiLocal variable cores_per_pkg is only used to calculate threads_per_pkg. No need for it. Drop it and open-code it instead.
--- target/i386/cpu.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
target/i386/cpu.c +2 -4
@@ -6498,7 +6498,6 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,uint32_t limit;uint32_t signature[3 ];X86CPUTopoInfo topo_info; -uint32_t cores_per_pkg;uint32_t threads_per_pkg; topo_info.dies_per_pkg = env->nr_dies;
@@ -6506,9 +6505,8 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, topo_info.cores_per_module = cs->nr_cores / env->nr_dies / env->nr_modules; topo_info.threads_per_core = cs->nr_threads; - cores_per_pkg = topo_info.cores_per_module * topo_info.modules_per_die * - topo_info.dies_per_pkg; - threads_per_pkg = cores_per_pkg * topo_info.threads_per_core; + threads_per_pkg = topo_info.threads_per_core * topo_info.cores_per_module * + topo_info.modules_per_die * topo_info.dies_per_pkg;/* Calculate & apply limits for different index ranges */ if (index >=0xC0000000 ) {
Signature · 1 line
2.34.1
Re: [PATCH v2 03/10] i386/cpu: Drop cores_per_pkg in cpu_x86_cpuid()
Quoted 5 lines · Xiaoyao Li
Date: Thu, 19 Dec 2024 06:01:18 -0500
From: Xiaoyao Li <[email protected]>
Subject: [PATCH v2 03/10] i386/cpu: Dropcores_per_pkgincpu_x86_cpuid()
X-Mailer: git-send-email 2.34.1
Quoted 8 lines
Local variable
cores_per_pkgis only used to calculatethreads_per_pkg. No need for it. Drop it and open-code it instead.Signed-off-by Xiaoyao Li <[email protected]>--- target/i386/cpu.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
PATCH v2 4/10 i386/topology: Update the comment of x86_apicid_from_topo_ids()
In reply to Xiaoyao LiUpdate the comment of x86_apicid_from_topo_ids() to match the current implementation,
--- include/hw/i386/topology.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
include/hw/i386/topology.h +3 -2
@@ -121,9 +121,10 @@ static inline unsigned apicid_pkg_offset(X86CPUTopoInfo *topo_info) }/* -* Make APIC ID for the CPU based on Pkg_ID, Core_ID, SMT_ID +* Make APIC ID for the CPU based on topology and IDs of each topology level. * -* The caller must make sure core_id < nr_cores and smt_id < nr_threads. +* The caller must make sure the ID of each level doesn't exceed the width of +* the level. */ static inline apic_id_t x86_apicid_from_topo_ids (X86CPUTopoInfo *topo_info,const X86CPUTopoIDs *topo_ids)
Signature · 1 line
2.34.1
PATCH v2 5/10 i386/topology: Introduce helpers for various topology info of different level
In reply to Xiaoyao LiIntroduce various helpers for getting the topology info of different semantics. Using the helper is more self-explanatory.
Besides, the semantic of the helper will stay unchanged even when new topology is added in the future. At that time, updating the implementation of the helper without affecting the callers.
--- include/hw/i386/topology.h | 25 +++++++++++++++++++++++++ target/i386/cpu.c | 11 ++++------- 2 files changed, 29 insertions(+), 7 deletions(-)
include/hw/i386/topology.h +25 -0
@@ -203,4 +203,29 @@ static inline bool x86_has_extended_topo(unsigned long *topo_bitmap) test_bit(CPU_TOPOLOGY_LEVEL_DIE, topo_bitmap); } +static inline unsigned x86_module_per_pkg (X86CPUTopoInfo *topo_info) +{ +return topo_info->modules_per_die * topo_info->dies_per_pkg; +} + +static inline unsigned x86_cores_per_pkg (X86CPUTopoInfo *topo_info) +{ +return topo_info->cores_per_module *x86_module_per_pkg (topo_info); +} + +static inline unsigned x86_threads_per_pkg (X86CPUTopoInfo *topo_info) +{ +return topo_info->threads_per_core *x86_cores_per_pkg (topo_info); +} + +static inline unsigned x86_threads_per_module (X86CPUTopoInfo *topo_info) +{ +return topo_info->threads_per_core * topo_info->cores_per_module; +} + +static inline unsigned x86_threads_per_die (X86CPUTopoInfo *topo_info) +{ +return x86_threads_per_module (topo_info) * topo_info->modules_per_die; +} +#endif /* HW_I386_TOPOLOGY_H */
target/i386/cpu.c +4 -7
@@ -311,13 +311,11 @@ static uint32_t num_threads_by_topo_level(X86CPUTopoInfo *topo_info,case CPU_TOPOLOGY_LEVEL_CORE :return topo_info->threads_per_core;case CPU_TOPOLOGY_LEVEL_MODULE : -return topo_info->threads_per_core * topo_info->cores_per_module; +return x86_threads_per_module (topo_info);case CPU_TOPOLOGY_LEVEL_DIE : -return topo_info->threads_per_core * topo_info->cores_per_module * - topo_info->modules_per_die; +return x86_threads_per_die (topo_info);case CPU_TOPOLOGY_LEVEL_SOCKET : -return topo_info->threads_per_core * topo_info->cores_per_module * - topo_info->modules_per_die * topo_info->dies_per_pkg; +return x86_threads_per_pkg (topo_info);default :g_assert_not_reached (); }
@@ -6505,8 +6503,7 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, topo_info.cores_per_module = cs->nr_cores / env->nr_dies / env->nr_modules; topo_info.threads_per_core = cs->nr_threads; - threads_per_pkg = topo_info.threads_per_core * topo_info.cores_per_module * - topo_info.modules_per_die * topo_info.dies_per_pkg; + threads_per_pkg =x86_threads_per_pkg (&topo_info);/* Calculate & apply limits for different index ranges */ if (index >=0xC0000000 ) {
Signature · 1 line
2.34.1
Re: [PATCH v2 05/10] i386/topology: Introduce helpers for various topology info of different level
include/hw/i386/topology.h static inline bool x86_has_extended_topo(unsigned long *topo_bitmap) · line 214
Quoted 9 lines
include/hw/i386/topology.h +9
-0+static inline unsigned x86_module_per_pkg (X86CPUTopoInfo *topo_info) +{ +return topo_info->modules_per_die * topo_info->dies_per_pkg; +} + +static inline unsigned x86_cores_per_pkg (X86CPUTopoInfo *topo_info) +{ +return topo_info->cores_per_module *x86_module_per_pkg (topo_info); +}
The above helpers can be ignored this time until someone wants them...
include/hw/i386/topology.h static inline bool x86_has_extended_topo(unsigned long *topo_bitmap) · line 219
Quoted 4 lines
include/hw/i386/topology.h +4
-0+static inline unsigned x86_threads_per_pkg (X86CPUTopoInfo *topo_info) +{ +return topo_info->threads_per_core *x86_cores_per_pkg (topo_info); +}
...then this can be x86_threads_per_die(topo_info) * topo_info->dies_per_package
Quoted 11 lines
include/hw/i386/topology.h +10
-0+static inline unsigned x86_threads_per_module (X86CPUTopoInfo *topo_info) +{ +return topo_info->threads_per_core * topo_info->cores_per_module; +} + +static inline unsigned x86_threads_per_die (X86CPUTopoInfo *topo_info) +{ +return x86_threads_per_module (topo_info) * topo_info->modules_per_die; +} +
Re: [PATCH v2 05/10] i386/topology: Introduce helpers for various topology info of different level
Quoted 11 lines · Zhao Liu
include/hw/i386/topology.h static inline bool x86_has_extended_topo(unsigned long *topo_bitmap) · line 214
Quoted 9 lines
include/hw/i386/topology.h +9
-0+static inline unsigned x86_module_per_pkg (X86CPUTopoInfo *topo_info) +{ +return topo_info->modules_per_die * topo_info->dies_per_pkg; +} + +static inline unsigned x86_cores_per_pkg (X86CPUTopoInfo *topo_info) +{ +return topo_info->cores_per_module *x86_module_per_pkg (topo_info); +}The above helpers can be ignored this time until someone wants them...
x86_cores_per_pkg() will be used in next patch (06).
So the only one without a real user is x86_module_per_pkg(). We can drop it, and implement x86_cores_per_pkg() as
return topo_info->cores_per_module * topo_info->modules_per_die * topo_info->dies_per_pkg;
However, I don't see no real user as a big concern and I think current implementation looks more consistent and complete.
Quoted 18 lines
include/hw/i386/topology.h static inline bool x86_has_extended_topo(unsigned long *topo_bitmap) · line 219
Quoted 4 lines
include/hw/i386/topology.h +4
-0+static inline unsigned x86_threads_per_pkg (X86CPUTopoInfo *topo_info) +{ +return topo_info->threads_per_core *x86_cores_per_pkg (topo_info); +}...then this can be
x86_threads_per_die(topo_info)*topo_info->dies_per_packageQuoted 11 lines
include/hw/i386/topology.h +10
-0+static inline unsigned x86_threads_per_module (X86CPUTopoInfo *topo_info) +{ +return topo_info->threads_per_core * topo_info->cores_per_module; +} + +static inline unsigned x86_threads_per_die (X86CPUTopoInfo *topo_info) +{ +return x86_threads_per_module (topo_info) * topo_info->modules_per_die; +} +
PATCH v2 6/10 i386/cpu: Track a X86CPUTopoInfo directly in CPUX86State
In reply to Xiaoyao LiThe name of nr_modules/nr_dies are ambiguous and they mislead people.
The purpose of them is to record and form the topology information. So just maintain a X86CPUTopoInfo member in CPUX86State instead. Then nr_modules and nr_dies can be dropped.
As the benefit, x86 can switch to use information in CPUX86State::topo_info and get rid of the nr_cores and nr_threads in CPUState. This helps remove the dependency on qemu_init_vcpu() so that x86 can get and use topology info earlier in x86_cpu_realizefn().
--- hw/i386/x86-common.c | 12 ++++------ target/i386/cpu-sysemu.c | 6 ++--- target/i386/cpu.c | 51 ++++++++++++++++------------------------ target/i386/cpu.h | 6 +---- 4 files changed, 29 insertions(+), 46 deletions(-)
hw/i386/x86-common.c +5 -7
@@ -248,7 +248,7 @@ void x86_cpu_pre_plug(HotplugHandler *hotplug_dev,CPUX86State *env = &cpu->env;MachineState *ms =MACHINE (hotplug_dev);X86MachineState *x86ms =X86_MACHINE (hotplug_dev); -X86CPUTopoInfo topo_info; +X86CPUTopoInfo *topo_info = &env->topo_info;if (!object_dynamic_cast (OBJECT (cpu), ms->cpu_type)) { error_setg(errp, "Invalid CPU type, expected cpu type: '%s'",
@@ -267,15 +267,13 @@ void x86_cpu_pre_plug(HotplugHandler *hotplug_dev, } } -init_topo_info (&topo_info, x86ms); +init_topo_info (topo_info, x86ms);if (ms->smp.modules >1 ) { - env->nr_modules = ms->smp.modules;set_bit (CPU_TOPOLOGY_LEVEL_MODULE , env->avail_cpu_topo); }if (ms->smp.dies >1 ) { - env->nr_dies = ms->smp.dies;set_bit (CPU_TOPOLOGY_LEVEL_DIE , env->avail_cpu_topo); }
@@ -346,12 +344,12 @@ void x86_cpu_pre_plug(HotplugHandler *hotplug_dev, topo_ids.module_id = cpu->module_id; topo_ids.core_id = cpu->core_id; topo_ids.smt_id = cpu->thread_id; - cpu->apic_id =x86_apicid_from_topo_ids (&topo_info, &topo_ids); + cpu->apic_id =x86_apicid_from_topo_ids (topo_info, &topo_ids); } cpu_slot =x86_find_cpu_slot (MACHINE (x86ms), cpu->apic_id, &idx);if (!cpu_slot) { -x86_topo_ids_from_apicid (cpu->apic_id, &topo_info, &topo_ids); +x86_topo_ids_from_apicid (cpu->apic_id, topo_info, &topo_ids);error_setg (errp, "Invalid CPU [socket: %u, die: %u, module: %u, core: %u, thread: %u]"
@@ -374,7 +372,7 @@ void x86_cpu_pre_plug(HotplugHandler *hotplug_dev,/* TODO: move socket_id/core_id/thread_id checks into x86_cpu_realizefn() * once -smp refactoring is complete and there will be CPU private * CPUState::nr_cores and CPUState::nr_threads fields instead of globals */ -x86_topo_ids_from_apicid (cpu->apic_id, &topo_info, &topo_ids); +x86_topo_ids_from_apicid (cpu->apic_id, topo_info, &topo_ids);if (cpu->socket_id !=-1 && cpu->socket_id != topo_ids.pkg_id) { error_setg(errp, "property socket-id: %u doesn't match set apic-id:" " 0x%x (socket-id: %u)", cpu->socket_id, cpu->apic_id,
target/i386/cpu-sysemu.c +3 -3
@@ -312,11 +312,11 @@ void x86_cpu_get_crash_info_qom(Object *obj, Visitor *v,uint64_t cpu_x86_get_msr_core_thread_count (X86CPU *cpu) { -CPUState *cs =CPU (cpu); +CPUX86State *env = &cpu->env;uint64_t val; - val = cs->nr_threads * cs->nr_cores;/* thread count, bits 15..0 */ - val |= ((uint32_t )cs->nr_cores <<16 );/* core count, bits 31..16 */ + val =x86_threads_per_pkg (&env->topo_info);/* thread count, bits 15..0 */ + val |=x86_cores_per_pkg (&env->topo_info) <<16 ;/* core count, bits 31..16 */ return val; }
target/i386/cpu.c +20 -31
152 lines not on this page · raw message
target/i386/cpu.h +1 -5
@@ -2045,11 +2045,7 @@ typedef struct CPUArchState {TPRAccess tpr_access_type; -/* Number of dies within this CPU package. */ -unsigned nr_dies; - -/* Number of modules within one die. */ -unsigned nr_modules; +X86CPUTopoInfo topo_info;/* Bitmap of available CPU topology levels for this CPU. */ DECLARE_BITMAP (avail_cpu_topo,CPU_TOPOLOGY_LEVEL__MAX );
Signature · 1 line
2.34.1
Re: [PATCH v2 06/10] i386/cpu: Track a X86CPUTopoInfo directly in CPUX86State
target/i386/cpu.c static void x86_cpu_init_default_topo(X86CPU *cpu) · line 8170
Quoted 7 lines
target/i386/cpu.c +0
-3@@ -8165,9 +8157,6 @@ static void x86_cpu_init_default_topo(X86CPU *cpu) {CPUX86State *env = &cpu->env; - env->nr_modules =1 ; - env->nr_dies =1 ; -
Instead, initialize topo_info?
Quoted 2 lines
target/i386/cpu.c +0
-0/* thread, core and socket levels are set by default. */ set_bit (CPU_TOPOLOGY_LEVEL_THREAD , env->avail_cpu_topo);
PATCH v2 7/10 i386/cpu: Hoist check of CPUID_EXT3_TOPOEXT against threads_per_core
In reply to Xiaoyao LiNow it changes to use env->topo_info.threads_per_core and doesn't depend on qemu_init_vcpu() anymore. Drop the comment of the order dependency on qemu_init_vcpu() and hoist code to put it together with other feature checking.
--- target/i386/cpu.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-)
target/i386/cpu.c +15 -15
@@ -7880,6 +7880,21 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)*/ cpu->mwait.ecx |=CPUID_MWAIT_EMX |CPUID_MWAIT_IBE ; +/* +* Most Intel and certain AMD CPUs support hyperthreading. Even though QEMU +* fixes this issue by adjusting CPUID_0000_0001_EBX and CPUID_8000_0008_ECX +* based on inputs (sockets,cores,threads), it is still better to give +* users a warning. +*/ +if (IS_AMD_CPU (env) && + !(env->features[FEAT_8000_0001_ECX ] &CPUID_EXT3_TOPOEXT ) && + env->nr_threads >1 ) { +warn_report_once ("This family of AMD CPU doesn't support " +"hyperthreading(%d). Please configure -smp " +"options properly or try enabling topoext " +"feature." , env->nr_threads); + } +/* For 64bit systems think about the number of physical bits to present. * ideally this should be the same as the host; anything other than matching * the host can cause incorrect guest behaviour.
@@ -7984,21 +7999,6 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)x86_cpu_gdb_init (cs);qemu_init_vcpu (cs); -/* -* Most Intel and certain AMD CPUs support hyperthreading. Even though QEMU -* fixes this issue by adjusting CPUID_0000_0001_EBX and CPUID_8000_0008_ECX -* based on inputs (sockets,cores,threads), it is still better to give -* users a warning. -*/ -if (IS_AMD_CPU (env) && - !(env->features[FEAT_8000_0001_ECX ] &CPUID_EXT3_TOPOEXT ) && - env->topo_info.threads_per_core >1 ) { -warn_report_once ("This family of AMD CPU doesn't support " -"hyperthreading(%d). Please configure -smp " -"options properly or try enabling topoext " -"feature." , env->topo_info.threads_per_core); - } -#ifndef CONFIG_USER_ONLY x86_cpu_apic_realize (cpu, &local_err);if (local_err !=NULL ) {
Signature · 1 line
2.34.1
Re: [PATCH v2 07/10] i386/cpu: Hoist check of CPUID_EXT3_TOPOEXT against threads_per_core
Quoted 10 lines · Xiaoyao Li
Date: Thu, 19 Dec 2024 06:01:22 -0500
From: Xiaoyao Li <[email protected]>
Subject: [PATCH v2 07/10] i386/cpu: Hoist check ofCPUID_EXT3_TOPOEXT
againstthreads_per_core
X-Mailer: git-send-email 2.34.1Now it changes to use
env->topo_info.threads_per_coreand doesn't depend onqemu_init_vcpu()anymore. Drop the comment of the order dependency onqemu_init_vcpu()and hoist code to put it together with other feature checking.
The comment was dropped in patch 6. You could move such description to there.
Quoted 12 lines
Signed-off-by Xiaoyao Li <[email protected]>--- target/i386/cpu.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-)target/i386/cpu.c +0
-0@@ -7880,6 +7880,21 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)*/ cpu->mwait.ecx |=CPUID_MWAIT_EMX |CPUID_MWAIT_IBE ;
target/i386/cpu.c static void x86_cpu_realizefn(DeviceState *dev, Error **errp) · line 7891
Quoted 10 lines
target/i386/cpu.c +9
-0+/* +* Most Intel and certain AMD CPUs support hyperthreading. Even though QEMU +* fixes this issue by adjusting CPUID_0000_0001_EBX and CPUID_8000_0008_ECX +* based on inputs (sockets,cores,threads), it is still better to give +* users a warning. +*/ +if (IS_AMD_CPU (env) && + !(env->features[FEAT_8000_0001_ECX ] &CPUID_EXT3_TOPOEXT ) && + env->nr_threads >1 ) {
Typo, env->topo_info.threads_per_core.
Quoted 9 lines
target/i386/cpu.c +6
-0+warn_report_once ("This family of AMD CPU doesn't support " +"hyperthreading(%d). Please configure -smp " +"options properly or try enabling topoext " +"feature." , env->nr_threads); + } +/* For 64bit systems think about the number of physical bits to present. * ideally this should be the same as the host; anything other than matching * the host can cause incorrect guest behaviour.
Re: [PATCH v2 07/10] i386/cpu: Hoist check of CPUID_EXT3_TOPOEXT against threads_per_core
Quoted 7 lines · Zhao Liu
Quoted 6 lines · Xiaoyao Li
Date: Thu, 19 Dec 2024 06:01:22 -0500
From: Xiaoyao Li <[email protected]>
Subject: [PATCH v2 07/10] i386/cpu: Hoist check ofCPUID_EXT3_TOPOEXT
againstthreads_per_core
X-Mailer: git-send-email 2.34.1
Quoted 7 lines
Quoted 4 lines
Now it changes to use
env->topo_info.threads_per_coreand doesn't depend onqemu_init_vcpu()anymore. Drop the comment of the order dependency onqemu_init_vcpu()and hoist code to put it together with other feature checking.The comment was dropped in patch 6. You could move such description to there.
Obviously, I didn't take care of the patch split and reorder.
I tent to move the dropping of the comment from patch 6 to this one.
Quoted 13 lines
Quoted 13 lines
Signed-off-by Xiaoyao Li <[email protected]>--- target/i386/cpu.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-)target/i386/cpu.c +0
-0@@ -7880,6 +7880,21 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)*/ cpu->mwait.ecx |=CPUID_MWAIT_EMX |CPUID_MWAIT_IBE ;
Quoted 11 lines
target/i386/cpu.c static void x86_cpu_realizefn(DeviceState *dev, Error **errp) · line 7891
Quoted 9 lines
target/i386/cpu.c +9
-0+/* +* Most Intel and certain AMD CPUs support hyperthreading. Even though QEMU +* fixes this issue by adjusting CPUID_0000_0001_EBX and CPUID_8000_0008_ECX +* based on inputs (sockets,cores,threads), it is still better to give +* users a warning. +*/ +if (IS_AMD_CPU (env) && + !(env->features[FEAT_8000_0001_ECX ] &CPUID_EXT3_TOPOEXT ) && + env->nr_threads >1 ) {Typo,
env->topo_info.threads_per_core.
Thanks for catching it.
Quoted 9 lines
Quoted 9 lines
target/i386/cpu.c +6
-0+warn_report_once ("This family of AMD CPU doesn't support " +"hyperthreading(%d). Please configure -smp " +"options properly or try enabling topoext " +"feature." , env->nr_threads); + } +/* For 64bit systems think about the number of physical bits to present. * ideally this should be the same as the host; anything other than matching * the host can cause incorrect guest behaviour.
PATCH v2 8/10 cpu: Remove nr_cores from struct CPUState
In reply to Xiaoyao LiThere is no user of it now, remove it.
--- hw/core/cpu-common.c | 1 - include/hw/core/cpu.h | 2 -- system/cpus.c | 1 - 3 files changed, 4 deletions(-)
hw/core/cpu-common.c +0 -1
@@ -243,7 +243,6 @@ static void cpu_common_initfn(Object *obj) cpu->cluster_index =UNASSIGNED_CLUSTER_INDEX ;/* user-mode doesn't have configurable SMP topology */ /* the default value is changed by qemu_init_vcpu() for system-mode */ - cpu->nr_cores =1 ; cpu->nr_threads =1 ; cpu->cflags_next_tb =-1 ;
include/hw/core/cpu.h +0 -2
@@ -407,7 +407,6 @@ struct qemu_work_item;* Under TCG this value is propagated to @tcg_cflags. * See TranslationBlock::TCG CF_CLUSTER_MASK. * @tcg_cflags: Pre-computed cflags for this cpu. -* @nr_cores: Number of cores within this CPU package. * @nr_threads: Number of threads within this CPU core. * @thread: Host thread details, only live once @created is #true * @sem: WIN32 only semaphore used only for qtest
@@ -466,7 +465,6 @@ struct CPUState {CPUClass *cc;/*< public >*/ -int nr_cores;int nr_threads;struct QemuThread *thread;
system/cpus.c +0 -1
@@ -681,7 +681,6 @@ void qemu_init_vcpu(CPUState *cpu) {MachineState *ms =MACHINE (qdev_get_machine ()); - cpu->nr_cores =machine_topo_get_cores_per_socket (ms); cpu->nr_threads = ms->smp.threads; cpu->stopped = true; cpu->random_seed =qemu_guest_random_seed_thread_part1 ();
Signature · 1 line
2.34.1
PATCH v2 9/10 i386/cpu: Set up CPUID_HT in x86_cpu_expand_features() instead of cpu_x86_cpuid()
In reply to Xiaoyao LiCurrently CPUID_HT is evaluated in cpu_x86_cpuid() each time. It's not a correct usage of how feature bit is maintained and evaluated. The expected practice is that features are tracked in env->features[] and cpu_x86_cpuid() should be the consumer of env->features[].
Track CPUID_HT in env->features[FEAT_1_EDX] instead and evaluate it in cpu's realizefn().
---
There is one issue[1] of CPUID_HT being user settable that when "-cpu xxx,-ht" with "-smp 2", HT flag is still exposed to guest. However, the issue is not irrelevant to this patch. If anyone has interest to reslove it please go ahead.
[1] https://lore.kernel.org/qemu-devel/[email protected]/
--- target/i386/cpu.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
target/i386/cpu.c +4 -1
@@ -6537,7 +6537,6 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *edx = env->features[FEAT_1_EDX ];if (threads_per_pkg >1 ) { *ebx |= threads_per_pkg <<16 ; - *edx |=CPUID_HT ; }if (!cpu->enable_pmu) { *ecx &= ~CPUID_EXT_PDCM ;
@@ -7528,6 +7527,10 @@ void x86_cpu_expand_features(X86CPU *cpu, Error **errp) } } +if (x86_threads_per_pkg (&env->topo_info) >1 ) { + env->features[FEAT_1_EDX ] |=CPUID_HT ; + } +for (i =0 ; i <ARRAY_SIZE (feature_dependencies); i++) {FeatureDep *d = &feature_dependencies[i];if (!(env->features[d->from.index] & d->from.mask)) {
Signature · 1 line
2.34.1
PATCH v2 10/10 i386/cpu: Set and track CPUID_EXT3_CMP_LEG in env->features[FEAT_8000_0001_ECX]
In reply to Xiaoyao LiThe correct usage is tracking and maintaining features in env->features[] instead of manually set it in cpu_x86_cpuid().
--- target/i386/cpu.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-)
target/i386/cpu.c +9 -11
@@ -6952,17 +6952,6 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *ecx = env->features[FEAT_8000_0001_ECX ]; *edx = env->features[FEAT_8000_0001_EDX ]; -/* The Linux kernel checks for the CMPLegacy bit and -* discards multiple thread information if it is set. -* So don't set it here for Intel to make Linux guests happy. -*/ -if (threads_per_pkg >1 ) { -if (env->cpuid_vendor1 !=CPUID_VENDOR_INTEL_1 || - env->cpuid_vendor2 !=CPUID_VENDOR_INTEL_2 || - env->cpuid_vendor3 !=CPUID_VENDOR_INTEL_3 ) { - *ecx |=1 <<1 ;/* CmpLegacy bit */ - } - }if (tcg_enabled () && env->cpuid_vendor1 ==CPUID_VENDOR_INTEL_1 && !(env->hflags &HF_LMA_MASK )) { *edx &= ~CPUID_EXT2_SYSCALL ;
@@ -7529,6 +7518,15 @@ void x86_cpu_expand_features(X86CPU *cpu, Error **errp)if (x86_threads_per_pkg (&env->topo_info) >1 ) { env->features[FEAT_1_EDX ] |=CPUID_HT ; + +/* +* The Linux kernel checks for the CMPLegacy bit and +* discards multiple thread information if it is set. +* So don't set it here for Intel to make Linux guests happy. +*/ +if (!IS_INTEL_CPU (env)) { + env->features[FEAT_8000_0001_ECX ] |=CPUID_EXT3_CMP_LEG ; + } }for (i =0 ; i <ARRAY_SIZE (feature_dependencies); i++) {
Signature · 1 line
2.34.1