Skip to contentSkip to conversation

target/i386: merge host_cpu_instance_init() and host_cpu_max_instance_init()

PATCH In review Paolo Bonzini 3 participants 5 messages Jul 11, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/i386: merge host_cpu_instance_init() and host_cpu_max_instance_init()

Simplify the accelerators' cpu_instance_init callbacks by doing all host-cpu setup in a single function.

Based-on: <[email protected]>

---
 target/i386/host-cpu.c    | 28 ++++++++++++++--------------
 target/i386/hvf/hvf-cpu.c |  2 --
 target/i386/kvm/kvm-cpu.c |  2 --
 3 files changed, 14 insertions(+), 18 deletions(-)
target/i386/host-cpu.c +14 -14
index 7512567298b..3399edc1ad0 100644
@@ -132,27 +132,27 @@ void host_cpu_instance_init(X86CPU *cpu)
 {
     X86CPUClass *xcc = X86_CPU_GET_CLASS(cpu);
 
-    if (xcc->model) {
-        char vendor[CPUID_VENDOR_SZ + 1];
-
-        host_cpu_vendor_fms(vendor, NULL, NULL, NULL);
-        object_property_set_str(OBJECT(cpu), "vendor", vendor, &error_abort);
-    }
-}
-
-void host_cpu_max_instance_init(X86CPU *cpu)
-{
     char vendor[CPUID_VENDOR_SZ + 1] = { 0 };
     char model_id[CPUID_MODEL_ID_SZ + 1] = { 0 };
     int family, model, stepping;
 
+    /*
+     * setting vendor applies to both max/host and builtin_x86_defs CPU.
+     * FIXME: this probably should warn or should be skipped if vendors do
+     * not match, because family numbers are incompatible between Intel and AMD.
+     */
+    host_cpu_vendor_fms(vendor, &family, &model, &stepping);
+    object_property_set_str(OBJECT(cpu), "vendor", vendor, &error_abort);
+
+    if (!xcc->max_features) {
+        return;
+    }
+
+    host_cpu_fill_model_id(model_id);
+
     /* Use max host physical address bits if -cpu max option is applied */
     object_property_set_bool(OBJECT(cpu), "host-phys-bits", true, &error_abort);
 
-    host_cpu_vendor_fms(vendor, &family, &model, &stepping);
-    host_cpu_fill_model_id(model_id);
-
-    object_property_set_str(OBJECT(cpu), "vendor", vendor, &error_abort);
     object_property_set_int(OBJECT(cpu), "family", family, &error_abort);
     object_property_set_int(OBJECT(cpu), "model", model, &error_abort);
     object_property_set_int(OBJECT(cpu), "stepping", stepping,
target/i386/hvf/hvf-cpu.c +0 -2
index 2b991f2fc8e..94ee096ecf7 100644
@@ -21,8 +21,6 @@ static void hvf_cpu_max_instance_init(X86CPU *cpu)
 {
     CPUX86State *env = &cpu->env;
 
-    host_cpu_max_instance_init(cpu);
-
     env->cpuid_min_level =
         hvf_get_supported_cpuid(0x0, 0, R_EAX);
     env->cpuid_min_xlevel =
target/i386/kvm/kvm-cpu.c +0 -2
index 6fed353548e..a99b8764644 100644
@@ -109,8 +109,6 @@ static void kvm_cpu_max_instance_init(X86CPU *cpu)
     CPUX86State *env = &cpu->env;
     KVMState *s = kvm_state;
 
-    host_cpu_max_instance_init(cpu);
-
     object_property_set_bool(OBJECT(cpu), "pmu", true, &error_abort);
 
     if (lmce_supported()) {
Signature · 1 line
2.50.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Paolo Bonzini

Simplify the accelerators' cpu_instance_init callbacks by doing all host-cpu setup in a single function.

Based-on: <[email protected]>

Miss to remove the declaration.

With below added,

-------------8<-------------------

target/i386/host-cpu.h +0 -1
index b97ec01c9bec..5b2ad491a8ca 100644
@@ -12,7 +12,6 @@
 
 uint32_t host_cpu_phys_bits(void);
 void host_cpu_instance_init(X86CPU *cpu);
-void host_cpu_max_instance_init(X86CPU *cpu);
 bool host_cpu_realizefn(CPUState *cs, Error **errp);
 
 void host_cpu_vendor_fms(char *vendor, int *family, int *model, int 

*stepping);

Quoted 78 lines
---
  target/i386/host-cpu.c    | 28 ++++++++++++++--------------
  target/i386/hvf/hvf-cpu.c |  2 --
  target/i386/kvm/kvm-cpu.c |  2 --
  3 files changed, 14 insertions(+), 18 deletions(-)
target/i386/host-cpu.c +14 -14
index 7512567298b..3399edc1ad0 100644
@@ -132,27 +132,27 @@ void host_cpu_instance_init(X86CPU *cpu)
  {
      X86CPUClass *xcc = X86_CPU_GET_CLASS(cpu);
 
-    if (xcc->model) {
-        char vendor[CPUID_VENDOR_SZ + 1];
-
-        host_cpu_vendor_fms(vendor, NULL, NULL, NULL);
-        object_property_set_str(OBJECT(cpu), "vendor", vendor, &error_abort);
-    }
-}
-
-void host_cpu_max_instance_init(X86CPU *cpu)
-{
      char vendor[CPUID_VENDOR_SZ + 1] = { 0 };
      char model_id[CPUID_MODEL_ID_SZ + 1] = { 0 };
      int family, model, stepping;
 
+    /*
+     * setting vendor applies to both max/host and builtin_x86_defs CPU.
+     * FIXME: this probably should warn or should be skipped if vendors do
+     * not match, because family numbers are incompatible between Intel and AMD.
+     */
+    host_cpu_vendor_fms(vendor, &family, &model, &stepping);
+    object_property_set_str(OBJECT(cpu), "vendor", vendor, &error_abort);
+
+    if (!xcc->max_features) {
+        return;
+    }
+
+    host_cpu_fill_model_id(model_id);
+
      /* Use max host physical address bits if -cpu max option is applied */
      object_property_set_bool(OBJECT(cpu), "host-phys-bits", true, &error_abort);
 
-    host_cpu_vendor_fms(vendor, &family, &model, &stepping);
-    host_cpu_fill_model_id(model_id);
-
-    object_property_set_str(OBJECT(cpu), "vendor", vendor, &error_abort);
      object_property_set_int(OBJECT(cpu), "family", family, &error_abort);
      object_property_set_int(OBJECT(cpu), "model", model, &error_abort);
      object_property_set_int(OBJECT(cpu), "stepping", stepping,
target/i386/hvf/hvf-cpu.c +0 -2
index 2b991f2fc8e..94ee096ecf7 100644
@@ -21,8 +21,6 @@ static void hvf_cpu_max_instance_init(X86CPU *cpu)
  {
      CPUX86State *env = &cpu->env;
 
-    host_cpu_max_instance_init(cpu);
-
      env->cpuid_min_level =
          hvf_get_supported_cpuid(0x0, 0, R_EAX);
      env->cpuid_min_xlevel =
target/i386/kvm/kvm-cpu.c +0 -2
index 6fed353548e..a99b8764644 100644
@@ -109,8 +109,6 @@ static void kvm_cpu_max_instance_init(X86CPU *cpu)
      CPUX86State *env = &cpu->env;
      KVMState *s = kvm_state;
 
-    host_cpu_max_instance_init(cpu);
-
      object_property_set_bool(OBJECT(cpu), "pmu", true, &error_abort);
 
      if (lmce_supported()) {
#2 lore raw
In reply to Paolo Bonzini
Quoted 2 lines · Paolo Bonzini

Simplify the accelerators' cpu_instance_init callbacks by doing all host-cpu setup in a single function.

btw, it changes the behavior for "-cpu base" with accelerator.

I think it should be OK considering "-cpu base" seems only for experiment case.

#3 lore raw
Quoted 8 lines · Xiaoyao Li
Quoted 2 lines · Paolo Bonzini

Simplify the accelerators' cpu_instance_init callbacks by doing all host-cpu setup in a single function.

btw, it changes the behavior for "-cpu base" with accelerator.

I think it should be OK considering "-cpu base" seems only for experiment case.

Yeah, I wonder if it should even be user creatable.

Paolo

#4 lore raw
Quoted 11 lines · Paolo Bonzini
Quoted 8 lines · Xiaoyao Li
Quoted 2 lines · Paolo Bonzini

Simplify the accelerators' cpu_instance_init callbacks by doing all host-cpu setup in a single function.

btw, it changes the behavior for "-cpu base" with accelerator.

I think it should be OK considering "-cpu base" seems only for experiment case.

Yeah, I wonder if it should even be user creatable.

Per commit 5adbed3088d ("i386: Define static "base" CPU model"):

Despite having no CPUID data set at all, "-cpu base" is even a functional CPU:

* It can boot a Slackware Linux 1.01 image with a Linux 0.99.12 kernel[1].
* It is even possible to boot[2] a modern Fedora x86_64 guest by manually enabling the following CPU features:
-cpu base,+lm,+msr,+pae,+fpu,+cx8,+cmov,+sse,+sse2,+fxsr

Anyhow I don't mind if it becomes hidden.

Regards,

Phil.