Patchwork [qom-cpu,for-1.4,07/14] target-openrisc: Detect attempt to instantiate non-CPU type in cpu_init()

login
register
mail settings
Submitter Andreas Färber
Date Jan. 23, 2013, 12:07 p.m.
Message ID <1358942867-8612-8-git-send-email-afaerber@suse.de>
Download mbox | patch
Permalink /patch/214942/
State New
Headers show

Comments

Andreas Färber - Jan. 23, 2013, 12:07 p.m.
Consolidate model checking into a new openrisc_cpu_class_by_name().

If the name matches an existing type, also check whether that type is
actually (a sub-type of) TYPE_OPENRISC_CPU.

This fixes, e.g., -cpu open_eth asserting.

Cc: qemu-stable@nongnu.org
Signed-off-by: Andreas Färber <afaerber@suse.de>
---
 target-openrisc/cpu.c |   24 ++++++++++++++++++++++--
 1 Datei geändert, 22 Zeilen hinzugefügt(+), 2 Zeilen entfernt(-)
Andreas Färber - Jan. 26, 2013, 4:11 p.m.
Am 23.01.2013 13:07, schrieb Andreas Färber:
> Consolidate model checking into a new openrisc_cpu_class_by_name().
> 
> If the name matches an existing type, also check whether that type is
> actually (a sub-type of) TYPE_OPENRISC_CPU.
> 
> This fixes, e.g., -cpu open_eth asserting.
> 
> Cc: qemu-stable@nongnu.org
> Signed-off-by: Andreas Färber <afaerber@suse.de>
> ---
>  target-openrisc/cpu.c |   24 ++++++++++++++++++++++--
>  1 Datei geändert, 22 Zeilen hinzugefügt(+), 2 Zeilen entfernt(-)
> 
> diff --git a/target-openrisc/cpu.c b/target-openrisc/cpu.c
> index 9bd4be4..6f41930 100644
> --- a/target-openrisc/cpu.c
> +++ b/target-openrisc/cpu.c
> @@ -88,6 +88,22 @@ static void openrisc_cpu_initfn(Object *obj)
>  }
>  
>  /* CPU models */
> +
> +static ObjectClass *openrisc_cpu_class_by_name(const char *cpu_model)
> +{
> +    ObjectClass *oc;
> +
> +    if (cpu_model == NULL) {
> +        return NULL;
> +    }
> +
> +    oc = object_class_by_name(cpu_model);
> +    if (oc != NULL && object_class_dynamic_cast(oc, TYPE_OPENRISC_CPU)) {

== NULL missing. Fixed by using ! operator.

Andreas

> +        return NULL;
> +    }
> +    return oc;
> +}
> +
>  static void or1200_initfn(Object *obj)
>  {
>      OpenRISCCPU *cpu = OPENRISC_CPU(obj);
> @@ -120,6 +136,8 @@ static void openrisc_cpu_class_init(ObjectClass *oc, void *data)
>  
>      occ->parent_reset = cc->reset;
>      cc->reset = openrisc_cpu_reset;
> +
> +    cc->class_by_name = openrisc_cpu_class_by_name;
>  }
>  
>  static void cpu_register(const OpenRISCCPUInfo *info)
> @@ -158,11 +176,13 @@ static void openrisc_cpu_register_types(void)
>  OpenRISCCPU *cpu_openrisc_init(const char *cpu_model)
>  {
>      OpenRISCCPU *cpu;
> +    ObjectClass *oc;
>  
> -    if (!object_class_by_name(cpu_model)) {
> +    oc = openrisc_cpu_class_by_name(cpu_model);
> +    if (oc == NULL) {
>          return NULL;
>      }
> -    cpu = OPENRISC_CPU(object_new(cpu_model));
> +    cpu = OPENRISC_CPU(object_new(object_class_get_name(oc)));
>      cpu->env.cpu_model_str = cpu_model;
>  
>      openrisc_cpu_realize(OBJECT(cpu), NULL);
>

Patch

diff --git a/target-openrisc/cpu.c b/target-openrisc/cpu.c
index 9bd4be4..6f41930 100644
--- a/target-openrisc/cpu.c
+++ b/target-openrisc/cpu.c
@@ -88,6 +88,22 @@  static void openrisc_cpu_initfn(Object *obj)
 }
 
 /* CPU models */
+
+static ObjectClass *openrisc_cpu_class_by_name(const char *cpu_model)
+{
+    ObjectClass *oc;
+
+    if (cpu_model == NULL) {
+        return NULL;
+    }
+
+    oc = object_class_by_name(cpu_model);
+    if (oc != NULL && object_class_dynamic_cast(oc, TYPE_OPENRISC_CPU)) {
+        return NULL;
+    }
+    return oc;
+}
+
 static void or1200_initfn(Object *obj)
 {
     OpenRISCCPU *cpu = OPENRISC_CPU(obj);
@@ -120,6 +136,8 @@  static void openrisc_cpu_class_init(ObjectClass *oc, void *data)
 
     occ->parent_reset = cc->reset;
     cc->reset = openrisc_cpu_reset;
+
+    cc->class_by_name = openrisc_cpu_class_by_name;
 }
 
 static void cpu_register(const OpenRISCCPUInfo *info)
@@ -158,11 +176,13 @@  static void openrisc_cpu_register_types(void)
 OpenRISCCPU *cpu_openrisc_init(const char *cpu_model)
 {
     OpenRISCCPU *cpu;
+    ObjectClass *oc;
 
-    if (!object_class_by_name(cpu_model)) {
+    oc = openrisc_cpu_class_by_name(cpu_model);
+    if (oc == NULL) {
         return NULL;
     }
-    cpu = OPENRISC_CPU(object_new(cpu_model));
+    cpu = OPENRISC_CPU(object_new(object_class_get_name(oc)));
     cpu->env.cpu_model_str = cpu_model;
 
     openrisc_cpu_realize(OBJECT(cpu), NULL);