Patchwork [RFC,9/9] target-i386: Unify CPU object creation on x86_cpu_create_from_name()

login
register
mail settings
Submitter Eduardo Habkost
Date Dec. 28, 2012, 8:34 p.m.
Message ID <1356726846-10637-10-git-send-email-ehabkost@redhat.com>
Download mbox | patch
Permalink /patch/208569/
State New
Headers show

Comments

Eduardo Habkost - Dec. 28, 2012, 8:34 p.m.
To keep the kvm_enabled() check for "host" working, a 'kvm_required'
field was added to X86CPUClass.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 target-i386/cpu-qom.h |  1 +
 target-i386/cpu.c     | 32 ++++++++++++++++++++------------
 2 files changed, 21 insertions(+), 12 deletions(-)

Patch

diff --git a/target-i386/cpu-qom.h b/target-i386/cpu-qom.h
index 332916a..fc58f83 100644
--- a/target-i386/cpu-qom.h
+++ b/target-i386/cpu-qom.h
@@ -49,6 +49,7 @@  typedef struct X86CPUClass {
     /*< public >*/
 
     void (*parent_reset)(CPUState *cpu);
+    bool kvm_required;
 } X86CPUClass;
 
 /**
diff --git a/target-i386/cpu.c b/target-i386/cpu.c
index fa7fab0..7beacdf 100644
--- a/target-i386/cpu.c
+++ b/target-i386/cpu.c
@@ -1605,23 +1605,24 @@  static X86CPU *x86_cpu_create_from_name(const char *name, Error **errp)
 {
     Error *error = NULL;
     X86CPU *cpu = NULL;
+    ObjectClass *oc;
+    X86CPUClass *cc;
     char *class_name = g_strdup_printf(CPU_CLASS_NAME("%s"), name);
 
+    oc = object_class_by_name(class_name);
+    if (!oc) {
+        error_setg(&error, "Unable to find CPU definition: %s", name);
+        goto out;
+    }
 
-    if (kvm_enabled() && name && strcmp(name, "host") == 0) {
-#ifdef CONFIG_KVM
-        cpu = X86_CPU(object_new(TYPE_X86_HOST_CPU));
-#endif
-    } else {
-
-        if (!object_class_by_name(class_name)) {
-            error_setg(&error, "Unable to find CPU definition: %s", name);
-            goto out;
-        }
-
-        cpu = X86_CPU(object_new(class_name));
+    cc = X86_CPU_CLASS(oc);
+    if (cc->kvm_required && !kvm_enabled()) {
+        error_setg(&error, "CPU model '%s' requires KVM", name);
+        goto out;
     }
 
+    cpu = X86_CPU(object_new(class_name));
+
 out:
     g_free(class_name);
     if (error) {
@@ -2499,6 +2500,12 @@  static void x86_host_cpu_initfn(Object *obj)
     }
 }
 
+static void x86_host_cpu_class_init(ObjectClass *oc, void *data)
+{
+    X86CPUClass *cc = X86_CPU_CLASS(oc);
+    cc->kvm_required = true;
+}
+
 static const TypeInfo x86_host_cpu_type_info = {
     .name = TYPE_X86_HOST_CPU,
     .parent = TYPE_X86_CPU,
@@ -2506,6 +2513,7 @@  static const TypeInfo x86_host_cpu_type_info = {
     .instance_init = x86_host_cpu_initfn,
     .abstract = false,
     .class_size = sizeof(X86CPUClass),
+    .class_init = x86_host_cpu_class_init,
 };
 
 #endif /* CONFIG_KVM */