* [PATCH 0/4] i386: Misc cleanup and fix
@ 2025-06-30 8:06 Xiaoyao Li
2025-06-30 8:06 ` [PATCH 1/4] i386/cpu: Use CPUID_MODEL_ID_SZ instead of hardcoded 48 Xiaoyao Li
` (4 more replies)
0 siblings, 5 replies; 7+ messages in thread
From: Xiaoyao Li @ 2025-06-30 8:06 UTC (permalink / raw)
To: Paolo Bonzini
Cc: Zhao Liu, Marcelo Tosatti, Richard Henderson, qemu-devel,
xiaoyao.li
This is a misc sereis.
The first 3 patches are cleanup and they are simple.
The last one is cleanup as well as a fix.
Xiaoyao Li (4):
i386/cpu: Use CPUID_MODEL_ID_SZ instead of hardcoded 48
i386: Cleanup the usage of CPUID_VENDOR_INTEL_1
i386/kvm-cpu: Fix the indentation inside kvm_cpu_realizefn()
i386/cpu: Unify family, model and stepping calculation for x86 CPU
target/i386/cpu.c | 22 +++++++++------------
target/i386/cpu.h | 33 +++++++++++++++++++++++++++++++-
target/i386/host-cpu.c | 7 +++----
target/i386/kvm/kvm-cpu.c | 2 +-
target/i386/kvm/kvm.c | 2 +-
target/i386/tcg/decode-new.c.inc | 4 ++--
6 files changed, 48 insertions(+), 22 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 1/4] i386/cpu: Use CPUID_MODEL_ID_SZ instead of hardcoded 48
2025-06-30 8:06 [PATCH 0/4] i386: Misc cleanup and fix Xiaoyao Li
@ 2025-06-30 8:06 ` Xiaoyao Li
2025-06-30 10:40 ` Philippe Mathieu-Daudé
2025-06-30 8:06 ` [PATCH 2/4] i386: Cleanup the usage of CPUID_VENDOR_INTEL_1 Xiaoyao Li
` (3 subsequent siblings)
4 siblings, 1 reply; 7+ messages in thread
From: Xiaoyao Li @ 2025-06-30 8:06 UTC (permalink / raw)
To: Paolo Bonzini
Cc: Zhao Liu, Marcelo Tosatti, Richard Henderson, qemu-devel,
xiaoyao.li
There is already the MACRO CPUID_MODEL_ID_SZ defined in QEMU. Use it to
replace all the hardcoded 48.
Opportunistically fix the indentation of CPUID_VENDOR_SZ.
Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
---
target/i386/cpu.c | 8 ++++----
target/i386/cpu.h | 3 ++-
target/i386/host-cpu.c | 1 -
3 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/target/i386/cpu.c b/target/i386/cpu.c
index 0d35e95430fe..d80e57d0cc0d 100644
--- a/target/i386/cpu.c
+++ b/target/i386/cpu.c
@@ -6468,11 +6468,11 @@ static char *x86_cpuid_get_model_id(Object *obj, Error **errp)
char *value;
int i;
- value = g_malloc(48 + 1);
- for (i = 0; i < 48; i++) {
+ value = g_malloc(CPUID_MODEL_ID_SZ + 1);
+ for (i = 0; i < CPUID_MODEL_ID_SZ; i++) {
value[i] = env->cpuid_model[i >> 2] >> (8 * (i & 3));
}
- value[48] = '\0';
+ value[CPUID_MODEL_ID_SZ] = '\0';
return value;
}
@@ -6487,7 +6487,7 @@ static void x86_cpuid_set_model_id(Object *obj, const char *model_id,
model_id = "";
}
len = strlen(model_id);
- memset(env->cpuid_model, 0, 48);
+ memset(env->cpuid_model, 0, CPUID_MODEL_ID_SZ);
for (i = 0; i < 48; i++) {
if (i >= len) {
c = '\0';
diff --git a/target/i386/cpu.h b/target/i386/cpu.h
index 51e10139dfdf..b83c521d9fbb 100644
--- a/target/i386/cpu.h
+++ b/target/i386/cpu.h
@@ -1159,7 +1159,8 @@ uint64_t x86_cpu_get_supported_feature_word(X86CPU *cpu, FeatureWord w);
/* PMM enabled */
#define CPUID_C000_0001_EDX_PMM_EN (1U << 13)
-#define CPUID_VENDOR_SZ 12
+#define CPUID_VENDOR_SZ 12
+#define CPUID_MODEL_ID_SZ 48
#define CPUID_VENDOR_INTEL_1 0x756e6547 /* "Genu" */
#define CPUID_VENDOR_INTEL_2 0x49656e69 /* "ineI" */
diff --git a/target/i386/host-cpu.c b/target/i386/host-cpu.c
index 7512567298bc..16c236478e2b 100644
--- a/target/i386/host-cpu.c
+++ b/target/i386/host-cpu.c
@@ -80,7 +80,6 @@ bool host_cpu_realizefn(CPUState *cs, Error **errp)
return true;
}
-#define CPUID_MODEL_ID_SZ 48
/**
* cpu_x86_fill_model_id:
* Get CPUID model ID string from host CPU.
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 2/4] i386: Cleanup the usage of CPUID_VENDOR_INTEL_1
2025-06-30 8:06 [PATCH 0/4] i386: Misc cleanup and fix Xiaoyao Li
2025-06-30 8:06 ` [PATCH 1/4] i386/cpu: Use CPUID_MODEL_ID_SZ instead of hardcoded 48 Xiaoyao Li
@ 2025-06-30 8:06 ` Xiaoyao Li
2025-06-30 8:06 ` [PATCH 3/4] i386/kvm-cpu: Fix the indentation inside kvm_cpu_realizefn() Xiaoyao Li
` (2 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Xiaoyao Li @ 2025-06-30 8:06 UTC (permalink / raw)
To: Paolo Bonzini
Cc: Zhao Liu, Marcelo Tosatti, Richard Henderson, qemu-devel,
xiaoyao.li
There are code using "env->cpuid_vendor1 == CPUID_VENDOR_INTEL_1" to
check if it is Intel vcpu. Cleanup them to just use IS_INTEL_CPU()
Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
---
target/i386/cpu.c | 2 +-
target/i386/tcg/decode-new.c.inc | 4 ++--
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/target/i386/cpu.c b/target/i386/cpu.c
index d80e57d0cc0d..83858358f5ec 100644
--- a/target/i386/cpu.c
+++ b/target/i386/cpu.c
@@ -7760,7 +7760,7 @@ 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];
- if (tcg_enabled() && env->cpuid_vendor1 == CPUID_VENDOR_INTEL_1 &&
+ if (tcg_enabled() && IS_INTEL_CPU(env) &&
!(env->hflags & HF_LMA_MASK)) {
*edx &= ~CPUID_EXT2_SYSCALL;
}
diff --git a/target/i386/tcg/decode-new.c.inc b/target/i386/tcg/decode-new.c.inc
index 55216e0d2490..853b1c8bf95e 100644
--- a/target/i386/tcg/decode-new.c.inc
+++ b/target/i386/tcg/decode-new.c.inc
@@ -2722,14 +2722,14 @@ static void disas_insn(DisasContext *s, CPUState *cpu)
if (decode.e.check & X86_CHECK_i64) {
goto illegal_op;
}
- if ((decode.e.check & X86_CHECK_i64_amd) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1) {
+ if ((decode.e.check & X86_CHECK_i64_amd) && !IS_INTEL_CPU(env)) {
goto illegal_op;
}
} else {
if (decode.e.check & X86_CHECK_o64) {
goto illegal_op;
}
- if ((decode.e.check & X86_CHECK_o64_intel) && env->cpuid_vendor1 == CPUID_VENDOR_INTEL_1) {
+ if ((decode.e.check & X86_CHECK_o64_intel) && IS_INTEL_CPU(env)) {
goto illegal_op;
}
}
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 3/4] i386/kvm-cpu: Fix the indentation inside kvm_cpu_realizefn()
2025-06-30 8:06 [PATCH 0/4] i386: Misc cleanup and fix Xiaoyao Li
2025-06-30 8:06 ` [PATCH 1/4] i386/cpu: Use CPUID_MODEL_ID_SZ instead of hardcoded 48 Xiaoyao Li
2025-06-30 8:06 ` [PATCH 2/4] i386: Cleanup the usage of CPUID_VENDOR_INTEL_1 Xiaoyao Li
@ 2025-06-30 8:06 ` Xiaoyao Li
2025-06-30 8:06 ` [PATCH 4/4] i386/cpu: Unify family, model and stepping calculation for x86 CPU Xiaoyao Li
2025-07-11 8:43 ` [PATCH 0/4] i386: Misc cleanup and fix Paolo Bonzini
4 siblings, 0 replies; 7+ messages in thread
From: Xiaoyao Li @ 2025-06-30 8:06 UTC (permalink / raw)
To: Paolo Bonzini
Cc: Zhao Liu, Marcelo Tosatti, Richard Henderson, qemu-devel,
xiaoyao.li
The indentation of one of the } inside kvm_cpu_realizefn() isn'f
correct. fix it.
Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
---
target/i386/kvm/kvm-cpu.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/target/i386/kvm/kvm-cpu.c b/target/i386/kvm/kvm-cpu.c
index 16bde4de01e5..6df92dc6d703 100644
--- a/target/i386/kvm/kvm-cpu.c
+++ b/target/i386/kvm/kvm-cpu.c
@@ -72,7 +72,7 @@ static bool kvm_cpu_realizefn(CPUState *cs, Error **errp)
if (env->features[FEAT_1_ECX] & CPUID_EXT_MONITOR) {
host_cpuid(5, 0, &cpu->mwait.eax, &cpu->mwait.ebx,
&cpu->mwait.ecx, &cpu->mwait.edx);
- }
+ }
}
if (cpu->ucode_rev == 0) {
cpu->ucode_rev =
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 4/4] i386/cpu: Unify family, model and stepping calculation for x86 CPU
2025-06-30 8:06 [PATCH 0/4] i386: Misc cleanup and fix Xiaoyao Li
` (2 preceding siblings ...)
2025-06-30 8:06 ` [PATCH 3/4] i386/kvm-cpu: Fix the indentation inside kvm_cpu_realizefn() Xiaoyao Li
@ 2025-06-30 8:06 ` Xiaoyao Li
2025-07-11 8:43 ` [PATCH 0/4] i386: Misc cleanup and fix Paolo Bonzini
4 siblings, 0 replies; 7+ messages in thread
From: Xiaoyao Li @ 2025-06-30 8:06 UTC (permalink / raw)
To: Paolo Bonzini
Cc: Zhao Liu, Marcelo Tosatti, Richard Henderson, qemu-devel,
xiaoyao.li
There are multiple places where CPUID family/model/stepping info
are retrieved from env->cpuid_version.
Besides, the calculation of family and model inside host_cpu_vendor_fms()
doesn't comply to what Intel and AMD define. For family, both Intel
and AMD define that Extended Family ID needs to be counted only when
(base) Family is 0xF. For model, Intel counts Extended Model when
(base) Family is 0x6 or 0xF, while AMD counts EXtended MOdel when
(base) Family is 0xF.
Introduce generic helper functions to get family, model and stepping
from the EAX value of CPUID leaf 1, with the correct calculation
formula.
Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
---
Note, for the calculation of model, it uses the same algorithm as Linux
kernel that counts Extended model when (base) Family is >= 6. To me, this
has the assumption that AMD always has a (base) Family of 0xF and Intel
doens't have processor with (base) Family between (0x6, 0xF).
I'm not sure about the rule on Zhaoxin and Hygon so that not sure if the
contidition of base Family >= 6 works for them or not.
For Zhaoxin, there is "YongFeng" defined in QEMU, which has Family 7 and
model 11. The model 11 doesn't require the Extended model field. So
I'm not sure the rule on Zhaoxin.
For Hygon, there is "Dhyana" defined in QEMU, which has Family 24 and
model 0. The model 0 doens't requrie the Extended model field as well.
---
target/i386/cpu.c | 12 ++++--------
target/i386/cpu.h | 30 ++++++++++++++++++++++++++++++
target/i386/host-cpu.c | 6 +++---
target/i386/kvm/kvm.c | 2 +-
4 files changed, 38 insertions(+), 12 deletions(-)
diff --git a/target/i386/cpu.c b/target/i386/cpu.c
index 83858358f5ec..51fcc8ba9867 100644
--- a/target/i386/cpu.c
+++ b/target/i386/cpu.c
@@ -6324,10 +6324,7 @@ static void x86_cpuid_version_get_family(Object *obj, Visitor *v,
CPUX86State *env = &cpu->env;
uint64_t value;
- value = (env->cpuid_version >> 8) & 0xf;
- if (value == 0xf) {
- value += (env->cpuid_version >> 20) & 0xff;
- }
+ value = x86_cpu_family(env->cpuid_version);
visit_type_uint64(v, name, &value, errp);
}
@@ -6365,8 +6362,7 @@ static void x86_cpuid_version_get_model(Object *obj, Visitor *v,
CPUX86State *env = &cpu->env;
uint64_t value;
- value = (env->cpuid_version >> 4) & 0xf;
- value |= ((env->cpuid_version >> 16) & 0xf) << 4;
+ value = x86_cpu_model(env->cpuid_version);
visit_type_uint64(v, name, &value, errp);
}
@@ -6400,7 +6396,7 @@ static void x86_cpuid_version_get_stepping(Object *obj, Visitor *v,
CPUX86State *env = &cpu->env;
uint64_t value;
- value = env->cpuid_version & 0xf;
+ value = x86_cpu_stepping(env->cpuid_version);
visit_type_uint64(v, name, &value, errp);
}
@@ -8154,7 +8150,7 @@ static void mce_init(X86CPU *cpu)
CPUX86State *cenv = &cpu->env;
unsigned int bank;
- if (((cenv->cpuid_version >> 8) & 0xf) >= 6
+ if (x86_cpu_family(cenv->cpuid_version) >= 6
&& (cenv->features[FEAT_1_EDX] & (CPUID_MCE | CPUID_MCA)) ==
(CPUID_MCE | CPUID_MCA)) {
cenv->mcg_cap = MCE_CAP_DEF | MCE_BANKS_DEF |
diff --git a/target/i386/cpu.h b/target/i386/cpu.h
index b83c521d9fbb..b589a00c80d7 100644
--- a/target/i386/cpu.h
+++ b/target/i386/cpu.h
@@ -2671,6 +2671,36 @@ static inline int32_t x86_get_a20_mask(CPUX86State *env)
}
}
+static inline uint32_t x86_cpu_family(uint32_t eax)
+{
+ uint32_t family = (eax >> 8) & 0xf;
+
+ if (family == 0xf) {
+ family += (eax >> 20) & 0xff;
+ }
+
+ return family;
+}
+
+static inline uint32_t x86_cpu_model(uint32_t eax)
+{
+ uint32_t family, model;
+
+ family = x86_cpu_family(eax);
+ model = (eax >> 4) & 0xf;
+
+ if (family >= 0x6) {
+ model += ((eax >> 16) & 0xf) << 4;
+ }
+
+ return model;
+}
+
+static inline uint32_t x86_cpu_stepping(uint32_t eax)
+{
+ return eax & 0xf;
+}
+
static inline bool cpu_has_vmx(CPUX86State *env)
{
return env->features[FEAT_1_ECX] & CPUID_EXT_VMX;
diff --git a/target/i386/host-cpu.c b/target/i386/host-cpu.c
index 16c236478e2b..383c42d4ae3d 100644
--- a/target/i386/host-cpu.c
+++ b/target/i386/host-cpu.c
@@ -117,13 +117,13 @@ void host_cpu_vendor_fms(char *vendor, int *family, int *model, int *stepping)
host_cpuid(0x1, 0, &eax, &ebx, &ecx, &edx);
if (family) {
- *family = ((eax >> 8) & 0x0F) + ((eax >> 20) & 0xFF);
+ *family = x86_cpu_family(eax);
}
if (model) {
- *model = ((eax >> 4) & 0x0F) | ((eax & 0xF0000) >> 12);
+ *model = x86_cpu_model(eax);
}
if (stepping) {
- *stepping = eax & 0x0F;
+ *stepping = x86_cpu_stepping(eax);
}
}
diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
index 234878c613f6..650d96210192 100644
--- a/target/i386/kvm/kvm.c
+++ b/target/i386/kvm/kvm.c
@@ -2259,7 +2259,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
cpuid_i = kvm_x86_build_cpuid(env, cpuid_data.entries, cpuid_i);
cpuid_data.cpuid.nent = cpuid_i;
- if (((env->cpuid_version >> 8)&0xF) >= 6
+ if (x86_cpu_family(env->cpuid_version) >= 6
&& (env->features[FEAT_1_EDX] & (CPUID_MCE | CPUID_MCA)) ==
(CPUID_MCE | CPUID_MCA)) {
uint64_t mcg_cap, unsupported_caps;
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 1/4] i386/cpu: Use CPUID_MODEL_ID_SZ instead of hardcoded 48
2025-06-30 8:06 ` [PATCH 1/4] i386/cpu: Use CPUID_MODEL_ID_SZ instead of hardcoded 48 Xiaoyao Li
@ 2025-06-30 10:40 ` Philippe Mathieu-Daudé
0 siblings, 0 replies; 7+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-06-30 10:40 UTC (permalink / raw)
To: Xiaoyao Li, Paolo Bonzini
Cc: Zhao Liu, Marcelo Tosatti, Richard Henderson, qemu-devel
On 30/6/25 10:06, Xiaoyao Li wrote:
> There is already the MACRO CPUID_MODEL_ID_SZ defined in QEMU. Use it to
> replace all the hardcoded 48.
>
> Opportunistically fix the indentation of CPUID_VENDOR_SZ.
>
> Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
> ---
> target/i386/cpu.c | 8 ++++----
> target/i386/cpu.h | 3 ++-
> target/i386/host-cpu.c | 1 -
> 3 files changed, 6 insertions(+), 6 deletions(-)
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 0/4] i386: Misc cleanup and fix
2025-06-30 8:06 [PATCH 0/4] i386: Misc cleanup and fix Xiaoyao Li
` (3 preceding siblings ...)
2025-06-30 8:06 ` [PATCH 4/4] i386/cpu: Unify family, model and stepping calculation for x86 CPU Xiaoyao Li
@ 2025-07-11 8:43 ` Paolo Bonzini
4 siblings, 0 replies; 7+ messages in thread
From: Paolo Bonzini @ 2025-07-11 8:43 UTC (permalink / raw)
To: Xiaoyao Li; +Cc: Zhao Liu, Marcelo Tosatti, Richard Henderson, qemu-devel
Queued, thanks.
Paolo
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2025-07-11 8:54 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-30 8:06 [PATCH 0/4] i386: Misc cleanup and fix Xiaoyao Li
2025-06-30 8:06 ` [PATCH 1/4] i386/cpu: Use CPUID_MODEL_ID_SZ instead of hardcoded 48 Xiaoyao Li
2025-06-30 10:40 ` Philippe Mathieu-Daudé
2025-06-30 8:06 ` [PATCH 2/4] i386: Cleanup the usage of CPUID_VENDOR_INTEL_1 Xiaoyao Li
2025-06-30 8:06 ` [PATCH 3/4] i386/kvm-cpu: Fix the indentation inside kvm_cpu_realizefn() Xiaoyao Li
2025-06-30 8:06 ` [PATCH 4/4] i386/cpu: Unify family, model and stepping calculation for x86 CPU Xiaoyao Li
2025-07-11 8:43 ` [PATCH 0/4] i386: Misc cleanup and fix Paolo Bonzini
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).