Skip to contentSkip to conversation

i386: Misc cleanup and fix

PATCH Applied Xiaoyao Li 3 participants 7 messages Jun 30, 2025 – Jul 11, 2025

lore
Share & tools
lore

Xiaoyao Li

author
#0 lore raw

cover i386: Misc cleanup and fix

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(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/4 i386/cpu: Use CPUID_MODEL_ID_SZ instead of hardcoded 48

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.

---
 target/i386/cpu.c      | 8 ++++----
 target/i386/cpu.h      | 3 ++-
 target/i386/host-cpu.c | 1 -
 3 files changed, 6 insertions(+), 6 deletions(-)
target/i386/cpu.c +4 -4
index 0d35e95430fe..d80e57d0cc0d 100644
@@ -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';
target/i386/cpu.h +2 -1
index 51e10139dfdf..b83c521d9fbb 100644
@@ -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" */
target/i386/host-cpu.c +0 -1
index 7512567298bc..16c236478e2b 100644
@@ -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.
Signature · 1 line
2.43.0
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] i386/cpu: Use CPUID_MODEL_ID_SZ instead of hardcoded 48

Quoted 11 lines · 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.

---
  target/i386/cpu.c      | 8 ++++----
  target/i386/cpu.h      | 3 ++-
  target/i386/host-cpu.c | 1 -
  3 files changed, 6 insertions(+), 6 deletions(-)
author
#3 lore raw

PATCH 2/4 i386: Cleanup the usage of CPUID_VENDOR_INTEL_1

In reply to 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()

---
 target/i386/cpu.c                | 2 +-
 target/i386/tcg/decode-new.c.inc | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
target/i386/cpu.c +1 -1
index d80e57d0cc0d..83858358f5ec 100644
@@ -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;
         }
target/i386/tcg/decode-new.c.inc +2 -2
index 55216e0d2490..853b1c8bf95e 100644
@@ -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;
             }
         }
Signature · 1 line
2.43.0
author
#4 lore raw

PATCH 3/4 i386/kvm-cpu: Fix the indentation inside kvm_cpu_realizefn()

In reply to Xiaoyao Li

The indentation of one of the } inside kvm_cpu_realizefn() isn'f correct. fix it.

---
 target/i386/kvm/kvm-cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/i386/kvm/kvm-cpu.c +1 -1
index 16bde4de01e5..6df92dc6d703 100644
@@ -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 =
Signature · 1 line
2.43.0
author
#5 lore raw

PATCH 4/4 i386/cpu: Unify family, model and stepping calculation for x86 CPU

In reply to 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.

---
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(-)
target/i386/cpu.c +4 -8
index 83858358f5ec..51fcc8ba9867 100644
@@ -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 |
target/i386/cpu.h +30 -0
index b83c521d9fbb..b589a00c80d7 100644
@@ -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;
target/i386/host-cpu.c +3 -3
index 16c236478e2b..383c42d4ae3d 100644
@@ -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);
     }
 }
 
target/i386/kvm/kvm.c +1 -1
index 234878c613f6..650d96210192 100644
@@ -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;
Signature · 1 line
2.43.0