All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH 0/5] x86 CPU init cleanup, short version
@ 2012-12-03 17:27 Eduardo Habkost
  2012-12-03 17:27 ` [Qemu-devel] [PATCH 1/5] target-i386: cpu: separate feature string parsing from CPU model lookup Eduardo Habkost
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: Eduardo Habkost @ 2012-12-03 17:27 UTC (permalink / raw)
  To: qemu-devel; +Cc: Igor Mammedov, Don Slutz, Andreas Färber

Hi,

This is a much shorter version of x86 CPU cleanup series[1] I sent earlier,
including only what's really essential. I hope it will be easier to review and
include, while Igor and I work on the CPU properties/subclasses code using this
as base.

[1] The one sent as:
    Subject: [Qemu-devel] [PATCH 00/17] target-i386: CPU init cleanup for CPU
        classes/properties

Eduardo Habkost (1):
  target-i386: cpu: separate feature string parsing from CPU model
    lookup

Igor Mammedov (4):
  target-i386: use define for cpuid vendor string size
  target-i386: postpone cpuid_level update to realize time
  add visitor for parsing hz[KMG] input string
  target-i386: use visit_type_hz to parse tsc_freq property value

 qapi/qapi-visit-core.c      | 11 +++++++
 qapi/qapi-visit-core.h      |  2 ++
 qapi/string-input-visitor.c | 22 +++++++++++++
 target-i386/cpu.c           | 80 ++++++++++++++++++++++++++++++---------------
 target-i386/cpu.h           |  2 ++
 5 files changed, 90 insertions(+), 27 deletions(-)

-- 
1.7.11.7

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Qemu-devel] [PATCH 1/5] target-i386: cpu: separate feature string parsing from CPU model lookup
  2012-12-03 17:27 [Qemu-devel] [PATCH 0/5] x86 CPU init cleanup, short version Eduardo Habkost
@ 2012-12-03 17:27 ` Eduardo Habkost
  2012-12-03 23:27   ` Igor Mammedov
  2012-12-04 18:24   ` Blue Swirl
  2012-12-03 17:27 ` [Qemu-devel] [PATCH 2/5] target-i386: use define for cpuid vendor string size Eduardo Habkost
                   ` (3 subsequent siblings)
  4 siblings, 2 replies; 8+ messages in thread
From: Eduardo Habkost @ 2012-12-03 17:27 UTC (permalink / raw)
  To: qemu-devel; +Cc: Igor Mammedov, Don Slutz, Andreas Färber

Instead of using parsing the whole cpu_model string inside
cpu_x86_find_by_name(), first split it into the CPU model name and the
full feature string, then parse the feature string into pieces.

When using CPU model classes, those two pieces of information will be
used at different moments (CPU model name will be used to find CPU
class, feature string will be used after CPU object was created), so
making the split in two steps will make it easier to refactor the code
later.

This should also help on the CPU properties work, that will just need to
replace the cpu_x86_parse_featurestr() logic (and can keep the CPU model
lookup code as-is).

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 target-i386/cpu.c | 64 ++++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 44 insertions(+), 20 deletions(-)

diff --git a/target-i386/cpu.c b/target-i386/cpu.c
index c6c2ca0..89fd700 100644
--- a/target-i386/cpu.c
+++ b/target-i386/cpu.c
@@ -1208,13 +1208,31 @@ static void x86_cpuid_set_tsc_freq(Object *obj, Visitor *v, void *opaque,
     cpu->env.tsc_khz = value / 1000;
 }
 
-static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)
+static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *name)
 {
-    unsigned int i;
     x86_def_t *def;
 
-    char *s = g_strdup(cpu_model);
-    char *featurestr, *name = strtok(s, ",");
+    for (def = x86_defs; def; def = def->next)
+        if (name && !strcmp(name, def->name))
+            break;
+    if (kvm_enabled() && name && strcmp(name, "host") == 0) {
+        kvm_cpu_fill_host(x86_cpu_def);
+    } else if (!def) {
+        goto error;
+    } else {
+        memcpy(x86_cpu_def, def, sizeof(*def));
+    }
+    return 0;
+error:
+    return -1;
+}
+
+/* Parse "+feature,-feature,feature=foo" CPU feature string
+ */
+static int cpu_x86_parse_featurestr(x86_def_t *x86_cpu_def, char *features)
+{
+    unsigned int i;
+    char *featurestr; /* Single 'key=value" string being parsed */
     /* Features to be added*/
     uint32_t plus_features = 0, plus_ext_features = 0;
     uint32_t plus_ext2_features = 0, plus_ext3_features = 0;
@@ -1227,22 +1245,11 @@ static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)
     uint32_t minus_7_0_ebx_features = 0;
     uint32_t numvalue;
 
-    for (def = x86_defs; def; def = def->next)
-        if (name && !strcmp(name, def->name))
-            break;
-    if (kvm_enabled() && name && strcmp(name, "host") == 0) {
-        kvm_cpu_fill_host(x86_cpu_def);
-    } else if (!def) {
-        goto error;
-    } else {
-        memcpy(x86_cpu_def, def, sizeof(*def));
-    }
-
     add_flagname_to_bitmaps("hypervisor", &plus_features,
             &plus_ext_features, &plus_ext2_features, &plus_ext3_features,
             &plus_kvm_features, &plus_svm_features,  &plus_7_0_ebx_features);
 
-    featurestr = strtok(NULL, ",");
+    featurestr = features ? strtok(features, ",") : NULL;
 
     while (featurestr) {
         char *val;
@@ -1376,11 +1383,9 @@ static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)
     if (x86_cpu_def->cpuid_7_0_ebx_features && x86_cpu_def->level < 7) {
         x86_cpu_def->level = 7;
     }
-    g_free(s);
     return 0;
 
 error:
-    g_free(s);
     return -1;
 }
 
@@ -1490,11 +1495,25 @@ int cpu_x86_register(X86CPU *cpu, const char *cpu_model)
     CPUX86State *env = &cpu->env;
     x86_def_t def1, *def = &def1;
     Error *error = NULL;
+    char *name, *features;
+    gchar **model_pieces;
 
     memset(def, 0, sizeof(*def));
 
-    if (cpu_x86_find_by_name(def, cpu_model) < 0)
-        return -1;
+    model_pieces = g_strsplit(cpu_model, ",", 2);
+    if (!model_pieces[0]) {
+        goto error;
+    }
+    name = model_pieces[0];
+    features = model_pieces[1];
+
+    if (cpu_x86_find_by_name(def, name) < 0) {
+        goto error;
+    }
+
+    if (cpu_x86_parse_featurestr(def, features) < 0) {
+        goto error;
+    }
     if (def->vendor1) {
         env->cpuid_vendor1 = def->vendor1;
         env->cpuid_vendor2 = def->vendor2;
@@ -1553,7 +1572,12 @@ int cpu_x86_register(X86CPU *cpu, const char *cpu_model)
         error_free(error);
         return -1;
     }
+
+    g_strfreev(model_pieces);
     return 0;
+error:
+    g_strfreev(model_pieces);
+    return -1;
 }
 
 #if !defined(CONFIG_USER_ONLY)
-- 
1.7.11.7

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [Qemu-devel] [PATCH 2/5] target-i386: use define for cpuid vendor string size
  2012-12-03 17:27 [Qemu-devel] [PATCH 0/5] x86 CPU init cleanup, short version Eduardo Habkost
  2012-12-03 17:27 ` [Qemu-devel] [PATCH 1/5] target-i386: cpu: separate feature string parsing from CPU model lookup Eduardo Habkost
@ 2012-12-03 17:27 ` Eduardo Habkost
  2012-12-03 17:27 ` [Qemu-devel] [PATCH 3/5] target-i386: postpone cpuid_level update to realize time Eduardo Habkost
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Eduardo Habkost @ 2012-12-03 17:27 UTC (permalink / raw)
  To: qemu-devel; +Cc: Igor Mammedov, Don Slutz, Andreas Färber

From: Igor Mammedov <imammedo@redhat.com>

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
 target-i386/cpu.c | 6 +++---
 target-i386/cpu.h | 2 ++
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/target-i386/cpu.c b/target-i386/cpu.c
index 89fd700..569acac 100644
--- a/target-i386/cpu.c
+++ b/target-i386/cpu.c
@@ -1106,13 +1106,13 @@ static char *x86_cpuid_get_vendor(Object *obj, Error **errp)
     char *value;
     int i;
 
-    value = (char *)g_malloc(12 + 1);
+    value = (char *)g_malloc(CPUID_VENDOR_SZ + 1);
     for (i = 0; i < 4; i++) {
         value[i    ] = env->cpuid_vendor1 >> (8 * i);
         value[i + 4] = env->cpuid_vendor2 >> (8 * i);
         value[i + 8] = env->cpuid_vendor3 >> (8 * i);
     }
-    value[12] = '\0';
+    value[CPUID_VENDOR_SZ] = '\0';
     return value;
 }
 
@@ -1123,7 +1123,7 @@ static void x86_cpuid_set_vendor(Object *obj, const char *value,
     CPUX86State *env = &cpu->env;
     int i;
 
-    if (strlen(value) != 12) {
+    if (strlen(value) != CPUID_VENDOR_SZ) {
         error_set(errp, QERR_PROPERTY_VALUE_BAD, "",
                   "vendor", value);
         return;
diff --git a/target-i386/cpu.h b/target-i386/cpu.h
index 90ef1ff..386c4f6 100644
--- a/target-i386/cpu.h
+++ b/target-i386/cpu.h
@@ -510,6 +510,8 @@
 #define CPUID_7_0_EBX_ADX      (1 << 19)
 #define CPUID_7_0_EBX_SMAP     (1 << 20)
 
+#define CPUID_VENDOR_SZ      12
+
 #define CPUID_VENDOR_INTEL_1 0x756e6547 /* "Genu" */
 #define CPUID_VENDOR_INTEL_2 0x49656e69 /* "ineI" */
 #define CPUID_VENDOR_INTEL_3 0x6c65746e /* "ntel" */
-- 
1.7.11.7

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [Qemu-devel] [PATCH 3/5] target-i386: postpone cpuid_level update to realize time
  2012-12-03 17:27 [Qemu-devel] [PATCH 0/5] x86 CPU init cleanup, short version Eduardo Habkost
  2012-12-03 17:27 ` [Qemu-devel] [PATCH 1/5] target-i386: cpu: separate feature string parsing from CPU model lookup Eduardo Habkost
  2012-12-03 17:27 ` [Qemu-devel] [PATCH 2/5] target-i386: use define for cpuid vendor string size Eduardo Habkost
@ 2012-12-03 17:27 ` Eduardo Habkost
  2012-12-03 17:28 ` [Qemu-devel] [PATCH 4/5] add visitor for parsing hz[KMG] input string Eduardo Habkost
  2012-12-03 17:28 ` [Qemu-devel] [PATCH 5/5] target-i386: use visit_type_hz to parse tsc_freq property value Eduardo Habkost
  4 siblings, 0 replies; 8+ messages in thread
From: Eduardo Habkost @ 2012-12-03 17:27 UTC (permalink / raw)
  To: qemu-devel; +Cc: Igor Mammedov, Don Slutz, Andreas Färber

From: Igor Mammedov <imammedo@redhat.com>

delay capping cpuid_level to 7 to realize time so property setters
for cpuid_7_0_ebx_features and "level" could be used in any order/time
between x86_cpu_initfn() and x86_cpu_realize().

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
 target-i386/cpu.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/target-i386/cpu.c b/target-i386/cpu.c
index 569acac..ee03652 100644
--- a/target-i386/cpu.c
+++ b/target-i386/cpu.c
@@ -1380,9 +1380,6 @@ static int cpu_x86_parse_featurestr(x86_def_t *x86_cpu_def, char *features)
         if (kvm_check_features_against_host(x86_cpu_def) && enforce_cpuid)
             goto error;
     }
-    if (x86_cpu_def->cpuid_7_0_ebx_features && x86_cpu_def->level < 7) {
-        x86_cpu_def->level = 7;
-    }
     return 0;
 
 error:
@@ -2073,6 +2070,11 @@ static void x86_cpu_apic_init(X86CPU *cpu, Error **errp)
 void x86_cpu_realize(Object *obj, Error **errp)
 {
     X86CPU *cpu = X86_CPU(obj);
+    CPUX86State *env = &cpu->env;
+
+    if (env->cpuid_7_0_ebx_features && env->cpuid_level < 7) {
+        env->cpuid_level = 7;
+    }
 
 #ifndef CONFIG_USER_ONLY
     qemu_register_reset(x86_cpu_machine_reset_cb, cpu);
-- 
1.7.11.7

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [Qemu-devel] [PATCH 4/5] add visitor for parsing hz[KMG] input string
  2012-12-03 17:27 [Qemu-devel] [PATCH 0/5] x86 CPU init cleanup, short version Eduardo Habkost
                   ` (2 preceding siblings ...)
  2012-12-03 17:27 ` [Qemu-devel] [PATCH 3/5] target-i386: postpone cpuid_level update to realize time Eduardo Habkost
@ 2012-12-03 17:28 ` Eduardo Habkost
  2012-12-03 17:28 ` [Qemu-devel] [PATCH 5/5] target-i386: use visit_type_hz to parse tsc_freq property value Eduardo Habkost
  4 siblings, 0 replies; 8+ messages in thread
From: Eduardo Habkost @ 2012-12-03 17:28 UTC (permalink / raw)
  To: qemu-devel; +Cc: Igor Mammedov, Don Slutz, Andreas Färber

From: Igor Mammedov <imammedo@redhat.com>

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
Acked-by: Andreas Färber <afaerber@suse.de>
---
 qapi/qapi-visit-core.c      | 11 +++++++++++
 qapi/qapi-visit-core.h      |  2 ++
 qapi/string-input-visitor.c | 22 ++++++++++++++++++++++
 3 files changed, 35 insertions(+)

diff --git a/qapi/qapi-visit-core.c b/qapi/qapi-visit-core.c
index 7a82b63..5c8705e 100644
--- a/qapi/qapi-visit-core.c
+++ b/qapi/qapi-visit-core.c
@@ -311,3 +311,14 @@ void input_type_enum(Visitor *v, int *obj, const char *strings[],
     g_free(enum_str);
     *obj = value;
 }
+
+void visit_type_freq(Visitor *v, int64_t *obj, const char *name, Error **errp)
+{
+    if (!error_is_set(errp)) {
+        if (v->type_freq) {
+            v->type_freq(v, obj, name, errp);
+        } else {
+            v->type_int(v, obj, name, errp);
+        }
+    }
+}
diff --git a/qapi/qapi-visit-core.h b/qapi/qapi-visit-core.h
index 60aceda..e5e7dd7 100644
--- a/qapi/qapi-visit-core.h
+++ b/qapi/qapi-visit-core.h
@@ -62,6 +62,7 @@ struct Visitor
     void (*type_int64)(Visitor *v, int64_t *obj, const char *name, Error **errp);
     /* visit_type_size() falls back to (*type_uint64)() if type_size is unset */
     void (*type_size)(Visitor *v, uint64_t *obj, const char *name, Error **errp);
+    void (*type_freq)(Visitor *v, int64_t *obj, const char *name, Error **errp);
 };
 
 void visit_start_handle(Visitor *v, void **obj, const char *kind,
@@ -91,5 +92,6 @@ void visit_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp);
 void visit_type_bool(Visitor *v, bool *obj, const char *name, Error **errp);
 void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp);
 void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp);
+void visit_type_freq(Visitor *v, int64_t *obj, const char *name, Error **errp);
 
 #endif
diff --git a/qapi/string-input-visitor.c b/qapi/string-input-visitor.c
index 497eb9a..74fe395 100644
--- a/qapi/string-input-visitor.c
+++ b/qapi/string-input-visitor.c
@@ -110,6 +110,27 @@ static void parse_start_optional(Visitor *v, bool *present,
     *present = true;
 }
 
+static void parse_type_freq(Visitor *v, int64_t *obj, const char *name,
+                            Error **errp)
+{
+    StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
+    char *endp = (char *) siv->string;
+    long long val = 0;
+
+    errno = 0;
+    if (siv->string) {
+        val = strtosz_suffix_unit(siv->string, &endp,
+                             STRTOSZ_DEFSUFFIX_B, 1000);
+    }
+    if (!siv->string || val == -1 || *endp) {
+        error_set(errp, QERR_INVALID_PARAMETER_VALUE, name,
+              "a value representable as a non-negative int64");
+        return;
+    }
+
+    *obj = val;
+}
+
 Visitor *string_input_get_visitor(StringInputVisitor *v)
 {
     return &v->visitor;
@@ -132,6 +153,7 @@ StringInputVisitor *string_input_visitor_new(const char *str)
     v->visitor.type_str = parse_type_str;
     v->visitor.type_number = parse_type_number;
     v->visitor.start_optional = parse_start_optional;
+    v->visitor.type_freq = parse_type_freq;
 
     v->string = str;
     return v;
-- 
1.7.11.7

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [Qemu-devel] [PATCH 5/5] target-i386: use visit_type_hz to parse tsc_freq property value
  2012-12-03 17:27 [Qemu-devel] [PATCH 0/5] x86 CPU init cleanup, short version Eduardo Habkost
                   ` (3 preceding siblings ...)
  2012-12-03 17:28 ` [Qemu-devel] [PATCH 4/5] add visitor for parsing hz[KMG] input string Eduardo Habkost
@ 2012-12-03 17:28 ` Eduardo Habkost
  4 siblings, 0 replies; 8+ messages in thread
From: Eduardo Habkost @ 2012-12-03 17:28 UTC (permalink / raw)
  To: qemu-devel; +Cc: Igor Mammedov, Don Slutz, Andreas Färber

From: Igor Mammedov <imammedo@redhat.com>

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
Reviewed-by: Andreas Färber <afaerber@suse.de>
---
 target-i386/cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/target-i386/cpu.c b/target-i386/cpu.c
index ee03652..def801a 100644
--- a/target-i386/cpu.c
+++ b/target-i386/cpu.c
@@ -1195,7 +1195,7 @@ static void x86_cpuid_set_tsc_freq(Object *obj, Visitor *v, void *opaque,
     const int64_t max = INT64_MAX;
     int64_t value;
 
-    visit_type_int(v, &value, name, errp);
+    visit_type_freq(v, &value, name, errp);
     if (error_is_set(errp)) {
         return;
     }
-- 
1.7.11.7

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [Qemu-devel] [PATCH 1/5] target-i386: cpu: separate feature string parsing from CPU model lookup
  2012-12-03 17:27 ` [Qemu-devel] [PATCH 1/5] target-i386: cpu: separate feature string parsing from CPU model lookup Eduardo Habkost
@ 2012-12-03 23:27   ` Igor Mammedov
  2012-12-04 18:24   ` Blue Swirl
  1 sibling, 0 replies; 8+ messages in thread
From: Igor Mammedov @ 2012-12-03 23:27 UTC (permalink / raw)
  To: Eduardo Habkost; +Cc: Don Slutz, qemu-devel, Andreas Färber

On Mon,  3 Dec 2012 15:27:57 -0200
Eduardo Habkost <ehabkost@redhat.com> wrote:

> Instead of using parsing the whole cpu_model string inside
> cpu_x86_find_by_name(), first split it into the CPU model name and the
> full feature string, then parse the feature string into pieces.
> 
> When using CPU model classes, those two pieces of information will be
> used at different moments (CPU model name will be used to find CPU
> class, feature string will be used after CPU object was created), so
> making the split in two steps will make it easier to refactor the code
> later.
> 
> This should also help on the CPU properties work, that will just need to
> replace the cpu_x86_parse_featurestr() logic (and can keep the CPU model
> lookup code as-is).
> 
> Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
> ---
>  target-i386/cpu.c | 64 ++++++++++++++++++++++++++++++++++++++-----------------
>  1 file changed, 44 insertions(+), 20 deletions(-)
> 
> diff --git a/target-i386/cpu.c b/target-i386/cpu.c
> index c6c2ca0..89fd700 100644
> --- a/target-i386/cpu.c
> +++ b/target-i386/cpu.c
> @@ -1208,13 +1208,31 @@ static void x86_cpuid_set_tsc_freq(Object *obj, Visitor *v, void *opaque,
>      cpu->env.tsc_khz = value / 1000;
>  }
>  
> -static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)
> +static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *name)
>  {
> -    unsigned int i;
>      x86_def_t *def;
>  
> -    char *s = g_strdup(cpu_model);
> -    char *featurestr, *name = strtok(s, ",");
> +    for (def = x86_defs; def; def = def->next)
> +        if (name && !strcmp(name, def->name))
> +            break;
> +    if (kvm_enabled() && name && strcmp(name, "host") == 0) {
> +        kvm_cpu_fill_host(x86_cpu_def);
> +    } else if (!def) {
> +        goto error;
Could you use "return -1;" here, there is no point to use goto in case of
single error exit point.

> +    } else {
> +        memcpy(x86_cpu_def, def, sizeof(*def));
> +    }
> +    return 0;
> +error:
> +    return -1;
and would be nice to remove above 2 lines, it will make patch shorter.

> +}
> +
> +/* Parse "+feature,-feature,feature=foo" CPU feature string
> + */
> +static int cpu_x86_parse_featurestr(x86_def_t *x86_cpu_def, char *features)
> +{
> +    unsigned int i;
> +    char *featurestr; /* Single 'key=value" string being parsed */
>      /* Features to be added*/
>      uint32_t plus_features = 0, plus_ext_features = 0;
>      uint32_t plus_ext2_features = 0, plus_ext3_features = 0;
> @@ -1227,22 +1245,11 @@ static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)
>      uint32_t minus_7_0_ebx_features = 0;
>      uint32_t numvalue;
>  
> -    for (def = x86_defs; def; def = def->next)
> -        if (name && !strcmp(name, def->name))
> -            break;
> -    if (kvm_enabled() && name && strcmp(name, "host") == 0) {
> -        kvm_cpu_fill_host(x86_cpu_def);
> -    } else if (!def) {
> -        goto error;
> -    } else {
> -        memcpy(x86_cpu_def, def, sizeof(*def));
> -    }
> -
>      add_flagname_to_bitmaps("hypervisor", &plus_features,
>              &plus_ext_features, &plus_ext2_features, &plus_ext3_features,
>              &plus_kvm_features, &plus_svm_features,  &plus_7_0_ebx_features);
>  
> -    featurestr = strtok(NULL, ",");
> +    featurestr = features ? strtok(features, ",") : NULL;
>  
>      while (featurestr) {
>          char *val;
> @@ -1376,11 +1383,9 @@ static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)
>      if (x86_cpu_def->cpuid_7_0_ebx_features && x86_cpu_def->level < 7) {
>          x86_cpu_def->level = 7;
>      }
> -    g_free(s);
>      return 0;
>  
>  error:
> -    g_free(s);
>      return -1;
>  }
>  
> @@ -1490,11 +1495,25 @@ int cpu_x86_register(X86CPU *cpu, const char *cpu_model)
>      CPUX86State *env = &cpu->env;
>      x86_def_t def1, *def = &def1;
>      Error *error = NULL;
> +    char *name, *features;
> +    gchar **model_pieces;
>  
>      memset(def, 0, sizeof(*def));
>  
> -    if (cpu_x86_find_by_name(def, cpu_model) < 0)
> -        return -1;
> +    model_pieces = g_strsplit(cpu_model, ",", 2);
> +    if (!model_pieces[0]) {
> +        goto error;
> +    }
> +    name = model_pieces[0];
> +    features = model_pieces[1];
> +
> +    if (cpu_x86_find_by_name(def, name) < 0) {
> +        goto error;
> +    }
> +
> +    if (cpu_x86_parse_featurestr(def, features) < 0) {
> +        goto error;
> +    }
>      if (def->vendor1) {
>          env->cpuid_vendor1 = def->vendor1;
>          env->cpuid_vendor2 = def->vendor2;
> @@ -1553,7 +1572,12 @@ int cpu_x86_register(X86CPU *cpu, const char *cpu_model)
>          error_free(error);
>          return -1;
>      }
> +
> +    g_strfreev(model_pieces);
>      return 0;
> +error:
> +    g_strfreev(model_pieces);
> +    return -1;
>  }
>  
>  #if !defined(CONFIG_USER_ONLY)
> -- 
> 1.7.11.7
> 
> 

Provided requested changes are applied,
 Reviewed-By:  Igor Mammedov <imammedo@redhat.com>

-- 
Regards,
  Igor

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Qemu-devel] [PATCH 1/5] target-i386: cpu: separate feature string parsing from CPU model lookup
  2012-12-03 17:27 ` [Qemu-devel] [PATCH 1/5] target-i386: cpu: separate feature string parsing from CPU model lookup Eduardo Habkost
  2012-12-03 23:27   ` Igor Mammedov
@ 2012-12-04 18:24   ` Blue Swirl
  1 sibling, 0 replies; 8+ messages in thread
From: Blue Swirl @ 2012-12-04 18:24 UTC (permalink / raw)
  To: Eduardo Habkost; +Cc: Igor Mammedov, Don Slutz, qemu-devel, Andreas Färber

On Mon, Dec 3, 2012 at 5:27 PM, Eduardo Habkost <ehabkost@redhat.com> wrote:
> Instead of using parsing the whole cpu_model string inside
> cpu_x86_find_by_name(), first split it into the CPU model name and the
> full feature string, then parse the feature string into pieces.
>
> When using CPU model classes, those two pieces of information will be
> used at different moments (CPU model name will be used to find CPU
> class, feature string will be used after CPU object was created), so
> making the split in two steps will make it easier to refactor the code
> later.
>
> This should also help on the CPU properties work, that will just need to
> replace the cpu_x86_parse_featurestr() logic (and can keep the CPU model
> lookup code as-is).
>
> Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
> ---
>  target-i386/cpu.c | 64 ++++++++++++++++++++++++++++++++++++++-----------------
>  1 file changed, 44 insertions(+), 20 deletions(-)
>
> diff --git a/target-i386/cpu.c b/target-i386/cpu.c
> index c6c2ca0..89fd700 100644
> --- a/target-i386/cpu.c
> +++ b/target-i386/cpu.c
> @@ -1208,13 +1208,31 @@ static void x86_cpuid_set_tsc_freq(Object *obj, Visitor *v, void *opaque,
>      cpu->env.tsc_khz = value / 1000;
>  }
>
> -static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)
> +static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *name)
>  {
> -    unsigned int i;
>      x86_def_t *def;
>
> -    char *s = g_strdup(cpu_model);
> -    char *featurestr, *name = strtok(s, ",");
> +    for (def = x86_defs; def; def = def->next)

Please add braces and check your patches with checkpatch.pl.

> +        if (name && !strcmp(name, def->name))

Ditto.

> +            break;
> +    if (kvm_enabled() && name && strcmp(name, "host") == 0) {
> +        kvm_cpu_fill_host(x86_cpu_def);
> +    } else if (!def) {
> +        goto error;
> +    } else {
> +        memcpy(x86_cpu_def, def, sizeof(*def));
> +    }
> +    return 0;
> +error:
> +    return -1;
> +}
> +
> +/* Parse "+feature,-feature,feature=foo" CPU feature string
> + */
> +static int cpu_x86_parse_featurestr(x86_def_t *x86_cpu_def, char *features)
> +{
> +    unsigned int i;
> +    char *featurestr; /* Single 'key=value" string being parsed */
>      /* Features to be added*/
>      uint32_t plus_features = 0, plus_ext_features = 0;
>      uint32_t plus_ext2_features = 0, plus_ext3_features = 0;
> @@ -1227,22 +1245,11 @@ static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)
>      uint32_t minus_7_0_ebx_features = 0;
>      uint32_t numvalue;
>
> -    for (def = x86_defs; def; def = def->next)
> -        if (name && !strcmp(name, def->name))
> -            break;
> -    if (kvm_enabled() && name && strcmp(name, "host") == 0) {
> -        kvm_cpu_fill_host(x86_cpu_def);
> -    } else if (!def) {
> -        goto error;
> -    } else {
> -        memcpy(x86_cpu_def, def, sizeof(*def));
> -    }
> -
>      add_flagname_to_bitmaps("hypervisor", &plus_features,
>              &plus_ext_features, &plus_ext2_features, &plus_ext3_features,
>              &plus_kvm_features, &plus_svm_features,  &plus_7_0_ebx_features);
>
> -    featurestr = strtok(NULL, ",");
> +    featurestr = features ? strtok(features, ",") : NULL;
>
>      while (featurestr) {
>          char *val;
> @@ -1376,11 +1383,9 @@ static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)
>      if (x86_cpu_def->cpuid_7_0_ebx_features && x86_cpu_def->level < 7) {
>          x86_cpu_def->level = 7;
>      }
> -    g_free(s);
>      return 0;
>
>  error:
> -    g_free(s);
>      return -1;
>  }
>
> @@ -1490,11 +1495,25 @@ int cpu_x86_register(X86CPU *cpu, const char *cpu_model)
>      CPUX86State *env = &cpu->env;
>      x86_def_t def1, *def = &def1;
>      Error *error = NULL;
> +    char *name, *features;
> +    gchar **model_pieces;
>
>      memset(def, 0, sizeof(*def));
>
> -    if (cpu_x86_find_by_name(def, cpu_model) < 0)
> -        return -1;
> +    model_pieces = g_strsplit(cpu_model, ",", 2);
> +    if (!model_pieces[0]) {
> +        goto error;
> +    }
> +    name = model_pieces[0];
> +    features = model_pieces[1];
> +
> +    if (cpu_x86_find_by_name(def, name) < 0) {
> +        goto error;
> +    }
> +
> +    if (cpu_x86_parse_featurestr(def, features) < 0) {
> +        goto error;
> +    }
>      if (def->vendor1) {
>          env->cpuid_vendor1 = def->vendor1;
>          env->cpuid_vendor2 = def->vendor2;
> @@ -1553,7 +1572,12 @@ int cpu_x86_register(X86CPU *cpu, const char *cpu_model)
>          error_free(error);
>          return -1;
>      }
> +
> +    g_strfreev(model_pieces);
>      return 0;
> +error:
> +    g_strfreev(model_pieces);
> +    return -1;
>  }
>
>  #if !defined(CONFIG_USER_ONLY)
> --
> 1.7.11.7
>
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2012-12-04 18:25 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-12-03 17:27 [Qemu-devel] [PATCH 0/5] x86 CPU init cleanup, short version Eduardo Habkost
2012-12-03 17:27 ` [Qemu-devel] [PATCH 1/5] target-i386: cpu: separate feature string parsing from CPU model lookup Eduardo Habkost
2012-12-03 23:27   ` Igor Mammedov
2012-12-04 18:24   ` Blue Swirl
2012-12-03 17:27 ` [Qemu-devel] [PATCH 2/5] target-i386: use define for cpuid vendor string size Eduardo Habkost
2012-12-03 17:27 ` [Qemu-devel] [PATCH 3/5] target-i386: postpone cpuid_level update to realize time Eduardo Habkost
2012-12-03 17:28 ` [Qemu-devel] [PATCH 4/5] add visitor for parsing hz[KMG] input string Eduardo Habkost
2012-12-03 17:28 ` [Qemu-devel] [PATCH 5/5] target-i386: use visit_type_hz to parse tsc_freq property value Eduardo Habkost

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.