Skip to contentSkip to conversation

i386: Add "max" CPU model to TCG and KVM

PATCH Applied Eduardo Habkost 4 participants 13 messages Jan 19, 2017 – Feb 22, 2017

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover i386: Add "max" CPU model to TCG and KVM

This is v2 of the previous series that enabled the "host" CPU model on TCG. Now a new "max" CPU is being added, while keeping "host" KVM-specific.

In addition to simply adding "max" as a copy of the existing "host" CPU model, additional patches change it to not use any host CPUID information when in TCG mode.

---
Cc: "Richard W.M. Jones" <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Cornelia Huck <[email protected]>
Cc: "Daniel P. Berrange" <[email protected]>
Cc: Peter Maydell <[email protected]>
Cc: Igor Mammedov <[email protected]>
Cc: Jiri Denemark <[email protected]>
Cc: Richard Henderson <[email protected]>
Cc: Christian Borntraeger <[email protected]>
Cc: "Jason J. Herne" <[email protected]>
Cc: Paolo Bonzini <[email protected]>

Eduardo Habkost (6):
i386: Unset cannot_destroy_with_object_finalize_yet on "host" model
i386: Add ordering field to CPUClass
i386: Rename X86CPU::host_features to X86CPU::max_features
i386: Create "max" CPU model
i386: Make "max" model not use any host CPUID info on TCG
i386: Don't set CPUClass::cpu_def on "max" model

 target/i386/cpu-qom.h |   6 ++-
 target/i386/cpu.h     |   2 +-
 target/i386/cpu.c     | 110 +++++++++++++++++++++++++++++---------------------
 3 files changed, 71 insertions(+), 47 deletions(-)
Signature · 1 line
2.11.0.259.g40922b1
#1 lore raw

PATCH 1/6 i386: Unset cannot_destroy_with_object_finalize_yet on "host" model

The class is now safe because the assert(kvm_enabled()) line was removed by commit e435601058e656e6d24e3e87b187e5518f7bf16a.

---
 target/i386/cpu.c | 2 --
 1 file changed, 2 deletions(-)
target/i386/cpu.c +0 -2
index cff23e129d..7947c8737c 100644
@@ -1571,8 +1571,6 @@ static void host_x86_cpu_class_init(ObjectClass *oc, void *data)
      */
 
     dc->props = host_x86_cpu_properties;
-    /* Reason: host_x86_cpu_initfn() dies when !kvm_enabled() */
-    dc->cannot_destroy_with_object_finalize_yet = true;
 }
 
 static void host_x86_cpu_initfn(Object *obj)
Signature · 1 line
2.11.0.259.g40922b1
author Applied
#2 lore raw

Re: [Qemu-devel] [PATCH 1/6] i386: Unset cannot_destroy_with_object_finalize_yet on "host" model

Quoted 4 lines · Eduardo Habkost

The class is now safe because the assert(kvm_enabled()) line was removed by commit e435601058e656e6d24e3e87b187e5518f7bf16a.

Applied to x86-next.

Signature · 1 line
Eduardo
#3 lore raw

PATCH 2/6 i386: Add ordering field to CPUClass

In reply to Eduardo Habkost

Instead of using kvm_enabled to order the "-cpu help" list, use a new "ordering" field for that.

---
 target/i386/cpu-qom.h | 2 ++
 target/i386/cpu.c     | 8 ++++----
 2 files changed, 6 insertions(+), 4 deletions(-)
target/i386/cpu-qom.h +2 -0
index 8cd607e9a2..14abd0a8c1 100644
@@ -48,6 +48,7 @@ typedef struct X86CPUDefinition X86CPUDefinition;
  * X86CPUClass:
  * @cpu_def: CPU model definition
  * @kvm_required: Whether CPU model requires KVM to be enabled.
+ * @ordering: Ordering on the "-cpu help" CPU model list.
  * @migration_safe: See CpuDefinitionInfo::migration_safe
  * @parent_realize: The parent class' realize handler.
  * @parent_reset: The parent class' reset handler.
@@ -63,6 +64,7 @@ typedef struct X86CPUClass {
     X86CPUDefinition *cpu_def;
 
     bool kvm_required;
+    int ordering;
     bool migration_safe;
 
     /* Optional description of CPU model.
target/i386/cpu.c +4 -4
index 7947c8737c..0c6d8d605e 100644
@@ -1550,6 +1550,7 @@ static void host_x86_cpu_class_init(ObjectClass *oc, void *data)
     uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
 
     xcc->kvm_required = true;
+    xcc->ordering = 9;
 
     host_cpuid(0x0, 0, &eax, &ebx, &ecx, &edx);
     x86_cpu_vendor_words2str(host_cpudef.vendor, ebx, edx, ecx);
@@ -2127,7 +2128,7 @@ static void listflags(FILE *f, fprintf_function print, const char **featureset)
     }
 }
 
-/* Sort alphabetically by type name, listing kvm_required models last. */
+/* Sort alphabetically by type name, respecting X86CPUClass::ordering. */
 static gint x86_cpu_list_compare(gconstpointer a, gconstpointer b)
 {
     ObjectClass *class_a = (ObjectClass *)a;
@@ -2136,9 +2137,8 @@ static gint x86_cpu_list_compare(gconstpointer a, gconstpointer b)
     X86CPUClass *cc_b = X86_CPU_CLASS(class_b);
     const char *name_a, *name_b;
 
-    if (cc_a->kvm_required != cc_b->kvm_required) {
-        /* kvm_required items go last */
-        return cc_a->kvm_required ? 1 : -1;
+    if (cc_a->ordering != cc_b->ordering) {
+        return cc_a->ordering - cc_b->ordering;
     } else {
         name_a = object_class_get_name(class_a);
         name_b = object_class_get_name(class_b);
Signature · 1 line
2.11.0.259.g40922b1
author Applied
#4 lore raw

Re: [Qemu-devel] [PATCH 2/6] i386: Add ordering field to CPUClass

Quoted 4 lines · Eduardo Habkost

Instead of using kvm_enabled to order the "-cpu help" list, use a new "ordering" field for that.

Applied to x86-next.

Signature · 1 line
Eduardo
#5 lore raw

PATCH 3/6 i386: Rename X86CPU::host_features to X86CPU::max_features

In reply to Eduardo Habkost

Rename the field and add a small comment to make its purpose clearer.

---
 target/i386/cpu.h | 2 +-
 target/i386/cpu.c | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)
target/i386/cpu.h +1 -1
index 10c5a3538d..889363e120 100644
@@ -1211,7 +1211,7 @@ struct X86CPU {
     bool enforce_cpuid;
     bool expose_kvm;
     bool migratable;
-    bool host_features;
+    bool max_features; /* Enable all supported features automatically */
     uint32_t apic_id;
 
     /* if true the CPUID code directly forward host cache leaves to the guest */
target/i386/cpu.c +3 -3
index 0c6d8d605e..006d0a8b70 100644
@@ -1583,7 +1583,7 @@ static void host_x86_cpu_initfn(Object *obj)
     /* We can't fill the features array here because we don't know yet if
      * "migratable" is true or false.
      */
-    cpu->host_features = true;
+    cpu->max_features = true;
 
     /* If KVM is disabled, x86_cpu_realizefn() will report an error later */
     if (kvm_enabled()) {
@@ -3102,12 +3102,12 @@ static void x86_cpu_load_features(X86CPU *cpu, Error **errp)
     GList *l;
     Error *local_err = NULL;
 
-    /*TODO: cpu->host_features incorrectly overwrites features
+    /*TODO: cpu->max_features incorrectly overwrites features
      * set using "feat=on|off". Once we fix this, we can convert
      * plus_features & minus_features to global properties
      * inside x86_cpu_parse_featurestr() too.
      */
-    if (cpu->host_features) {
+    if (cpu->max_features) {
         for (w = 0; w < FEATURE_WORDS; w++) {
             env->features[w] =
                 x86_cpu_get_supported_feature_word(w, cpu->migratable);
Signature · 1 line
2.11.0.259.g40922b1
author Applied
#6 lore raw

Re: [Qemu-devel] [PATCH 3/6] i386: Rename X86CPU::host_features to X86CPU::max_features

Quoted 4 lines · Eduardo Habkost

Rename the field and add a small comment to make its purpose clearer.

Applied to x86-next.

Signature · 1 line
Eduardo
#7 lore raw

PATCH 4/6 i386: Create "max" CPU model

In reply to Eduardo Habkost

Rename the existing "host" CPU model to "max, and set it to kvm_enabled=false. The new "max" CPU model will be able to enable all features supported by TCG out of the box, because its logic is based on x86_cpu_get_supported_feature_word(), which already works with TCG.

A new KVM-specific "host" class was added, that simply inherits everything from "max" except the 'ordering' and 'description' fields.

---
 target/i386/cpu.c | 42 +++++++++++++++++++++++++++++-------------
 1 file changed, 29 insertions(+), 13 deletions(-)
target/i386/cpu.c +29 -13
index 006d0a8b70..6f3a62840f 100644
@@ -1503,8 +1503,6 @@ void x86_cpu_change_kvm_default(const char *prop, const char *value)
 static uint32_t x86_cpu_get_supported_feature_word(FeatureWord w,
                                                    bool migratable_only);
 
-#ifdef CONFIG_KVM
-
 static bool lmce_supported(void)
 {
     uint64_t mce_cap;
@@ -1533,23 +1531,22 @@ static int cpu_x86_fill_model_id(char *str)
 
 static X86CPUDefinition host_cpudef;
 
-static Property host_x86_cpu_properties[] = {
+static Property max_x86_cpu_properties[] = {
     DEFINE_PROP_BOOL("migratable", X86CPU, migratable, true),
     DEFINE_PROP_BOOL("host-cache-info", X86CPU, cache_info_passthrough, false),
     DEFINE_PROP_END_OF_LIST()
 };
 
-/* class_init for the "host" CPU model
+/* class_init for the "max" CPU model
  *
  * This function may be called before KVM is initialized.
  */
-static void host_x86_cpu_class_init(ObjectClass *oc, void *data)
+static void max_x86_cpu_class_init(ObjectClass *oc, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(oc);
     X86CPUClass *xcc = X86_CPU_CLASS(oc);
     uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
 
-    xcc->kvm_required = true;
     xcc->ordering = 9;
 
     host_cpuid(0x0, 0, &eax, &ebx, &ecx, &edx);
@@ -1564,17 +1561,16 @@ static void host_x86_cpu_class_init(ObjectClass *oc, void *data)
 
     xcc->cpu_def = &host_cpudef;
     xcc->model_description =
-        "KVM processor with all supported host features "
-        "(only available in KVM mode)";
+        "Enables all features supported by the accelerator in the current host";
 
     /* level, xlevel, xlevel2, and the feature words are initialized on
      * instance_init, because they require KVM to be initialized.
      */
 
-    dc->props = host_x86_cpu_properties;
+    dc->props = max_x86_cpu_properties;
 }
 
-static void host_x86_cpu_initfn(Object *obj)
+static void max_x86_cpu_initfn(Object *obj)
 {
     X86CPU *cpu = X86_CPU(obj);
     CPUX86State *env = &cpu->env;
@@ -1585,7 +1581,6 @@ static void host_x86_cpu_initfn(Object *obj)
      */
     cpu->max_features = true;
 
-    /* If KVM is disabled, x86_cpu_realizefn() will report an error later */
     if (kvm_enabled()) {
         env->cpuid_min_level =
             kvm_arch_get_supported_cpuid(s, 0x0, 0, R_EAX);
@@ -1602,10 +1597,30 @@ static void host_x86_cpu_initfn(Object *obj)
     object_property_set_bool(OBJECT(cpu), true, "pmu", &error_abort);
 }
 
+static const TypeInfo max_x86_cpu_type_info = {
+    .name = X86_CPU_TYPE_NAME("max"),
+    .parent = TYPE_X86_CPU,
+    .instance_init = max_x86_cpu_initfn,
+    .class_init = max_x86_cpu_class_init,
+};
+
+#ifdef CONFIG_KVM
+
+static void host_x86_cpu_class_init(ObjectClass *oc, void *data)
+{
+    X86CPUClass *xcc = X86_CPU_CLASS(oc);
+
+    xcc->kvm_required = true;
+    xcc->ordering = 8;
+
+    xcc->model_description =
+        "KVM processor with all supported host features "
+        "(only available in KVM mode)";
+}
+
 static const TypeInfo host_x86_cpu_type_info = {
     .name = X86_CPU_TYPE_NAME("host"),
-    .parent = TYPE_X86_CPU,
-    .instance_init = host_x86_cpu_initfn,
+    .parent = X86_CPU_TYPE_NAME("max"),
     .class_init = host_x86_cpu_class_init,
 };
 
@@ -3729,6 +3744,7 @@ static void x86_cpu_register_types(void)
     for (i = 0; i < ARRAY_SIZE(builtin_x86_defs); i++) {
         x86_register_cpudef_type(&builtin_x86_defs[i]);
     }
+    type_register_static(&max_x86_cpu_type_info);
 #ifdef CONFIG_KVM
     type_register_static(&host_x86_cpu_type_info);
 #endif
Signature · 1 line
2.11.0.259.g40922b1
#8 lore raw

PATCH 5/6 i386: Make "max" model not use any host CPUID info on TCG

In reply to Eduardo Habkost

Instead of reporting host CPUID data on "max", use the qemu64 CPU model as reference to initialize CPUID vendor/family/model/stepping/model-id.

---
 target/i386/cpu.c | 9 +++++++++
 1 file changed, 9 insertions(+)
target/i386/cpu.c +9 -0
index 6f3a62840f..ea4e1ae519 100644
@@ -1592,6 +1592,15 @@ static void max_x86_cpu_initfn(Object *obj)
         if (lmce_supported()) {
             object_property_set_bool(OBJECT(cpu), true, "lmce", &error_abort);
         }
+    } else {
+        object_property_set_str(OBJECT(cpu), CPUID_VENDOR_AMD,
+                                "vendor", &error_abort);
+        object_property_set_int(OBJECT(cpu), 6, "family", &error_abort);
+        object_property_set_int(OBJECT(cpu), 6, "model", &error_abort);
+        object_property_set_int(OBJECT(cpu), 3, "stepping", &error_abort);
+        object_property_set_str(OBJECT(cpu),
+                                "QEMU TCG CPU version " QEMU_HW_VERSION,
+                                "model-id", &error_abort);
     }
 
     object_property_set_bool(OBJECT(cpu), true, "pmu", &error_abort);
Signature · 1 line
2.11.0.259.g40922b1
#9 lore raw

PATCH 6/6 i386: Don't set CPUClass::cpu_def on "max" model

In reply to Eduardo Habkost

Host CPUID info is used by the "max" CPU model only in KVM mode. Move the initialization of CPUID data for "max" from class_init to instance_init, and don't set CPUClass::cpu_def for "max".

---
 target/i386/cpu-qom.h |  4 +++-
 target/i386/cpu.c     | 45 +++++++++++++++++++++------------------------
 2 files changed, 24 insertions(+), 25 deletions(-)
target/i386/cpu-qom.h +3 -1
index 14abd0a8c1..f6c704c3a9 100644
@@ -60,7 +60,9 @@ typedef struct X86CPUClass {
     CPUClass parent_class;
     /*< public >*/
 
-    /* Should be eventually replaced by subclass-specific property defaults. */
+    /* CPU definition, automatically loaded by instance_init if not NULL.
+     * Should be eventually replaced by subclass-specific property defaults.
+     */
     X86CPUDefinition *cpu_def;
 
     bool kvm_required;
target/i386/cpu.c +21 -24

88 lines not on this page · raw message

Signature · 1 line
2.11.0.259.g40922b1
#10 lore raw
In reply to Eduardo Habkost
Quoted 29 lines · Eduardo Habkost

This is v2 of the previous series that enabled the "host" CPU model on TCG. Now a new "max" CPU is being added, while keeping "host" KVM-specific.

In addition to simply adding "max" as a copy of the existing "host" CPU model, additional patches change it to not use any host CPUID information when in TCG mode.

---
Cc: "Richard W.M. Jones" <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Cornelia Huck <[email protected]>
Cc: "Daniel P. Berrange" <[email protected]>
Cc: Peter Maydell <[email protected]>
Cc: Igor Mammedov <[email protected]>
Cc: Jiri Denemark <[email protected]>
Cc: Richard Henderson <[email protected]>
Cc: Christian Borntraeger <[email protected]>
Cc: "Jason J. Herne" <[email protected]>
Cc: Paolo Bonzini <[email protected]>

Eduardo Habkost (6):
i386: Unset cannot_destroy_with_object_finalize_yet on "host" model
i386: Add ordering field to CPUClass
i386: Rename X86CPU::host_features to X86CPU::max_features
i386: Create "max" CPU model
i386: Make "max" model not use any host CPUID info on TCG
i386: Don't set CPUClass::cpu_def on "max" model

Quoted 4 lines
 target/i386/cpu-qom.h |   6 ++-
 target/i386/cpu.h     |   2 +-
 target/i386/cpu.c     | 110 +++++++++++++++++++++++++++++---------------------
 3 files changed, 71 insertions(+), 47 deletions(-)

Should we be documenting this new -cpu max somewhere?

(If this series is OK I can send patches to add support to ARM, and also to add "-machine gic-version=max" to allow picking the best available interrupt controller type, by analogy.)

thanks
-- PMM

#11 lore raw
In reply to Eduardo Habkost

Am 19.01.2017 um 22:04 schrieb Eduardo Habkost:

Quoted 8 lines

This is v2 of the previous series that enabled the "host" CPU model on TCG. Now a new "max" CPU is being added, while keeping "host" KVM-specific.

In addition to simply adding "max" as a copy of the existing "host" CPU model, additional patches change it to not use any host CPUID information when in TCG mode.

Concept looks good to me. Should also be fairly simple to implement for s390x. Of course, the same restriction as for the "host" model applies to "max" then. When running under KVM, and the CPU model KVM interface is missing, the "max" model won't be able to be expanded.

Signature · 1 line
David
#12 lore raw
In reply to Eduardo Habkost
Quoted 3 lines · On Thu, 19 Jan 2017 19:04:43 -0200 Eduardo Habkost <[email protected]> wrote:

This is v2 of the previous series that enabled the "host" CPU model on TCG. Now a new "max" CPU is being added, while keeping "host" KVM-specific.

This looks like a good idea, and I think we'll do the same on s390.