qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Igor Mammedov <imammedo@redhat.com>
To: qemu-devel@nongnu.org
Cc: ehabkost@redhat.com, afaerber@suse.de
Subject: [Qemu-devel] [PATCH 4/5] target-i386: set custom features/properties without intermediate x86_def_t
Date: Thu, 17 Jan 2013 16:16:33 +0100	[thread overview]
Message-ID: <1358435794-8406-5-git-send-email-imammedo@redhat.com> (raw)
In-Reply-To: <1358435794-8406-1-git-send-email-imammedo@redhat.com>

Move custom features parsing after built-in cpu_model defaults are set
and set custom features directly on CPU instance. That allows to make
clear distinction between built-in cpu model defaults that eventually
should go into clas_init() and extra property setting which is done
after defaults are set on CPU instance.

Impl. details:
 - features that are already properties, are converted to normalized
   (name, values) list. And after featurestr has been parsed,
   properties from the list are applied directly to CPU instance.
   * For now it provides uniform handling of properties with single
     object_property_parse() property setter.
   * And after current features/properties are converted into static
     properties, it will take a trivial patch to switch to global properties.
     Which will allow to:
     * get CPU instance initialized with all parameters passed on -cpu ...
       cmd. line from object_new() call.
     * call cpu_model/featurestr parsing only once before CPUs are created
     * open a road for removing CPUxxxState.cpu_model_str field, when other
       CPUs are similarly converted to subclasses and static properties.
 - re-factor error handling, to use Error instead of fprintf()s, since
   it is anyway passed in for property setter.

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
 target-i386/cpu.c |  144 ++++++++++++++++++++++++++++-------------------------
 1 files changed, 77 insertions(+), 67 deletions(-)

diff --git a/target-i386/cpu.c b/target-i386/cpu.c
index 5cd7917..50e10b1 100644
--- a/target-i386/cpu.c
+++ b/target-i386/cpu.c
@@ -1302,9 +1302,24 @@ static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *name)
     return 0;
 }
 
+typedef struct NameValuePair {
+    char *name;
+    char *value;
+    QTAILQ_ENTRY(NameValuePair) next;
+} NameValuePair;
+typedef QTAILQ_HEAD(NVList, NameValuePair) NVList;
+
+static void x86_cpu_add_nv_pair(NVList *list, const char *name,
+                                const char *value) {
+    NameValuePair *p = g_malloc0(sizeof(*p));
+    p->name = g_strdup(name);
+    p->value = g_strdup(value);
+    QTAILQ_INSERT_TAIL(list, p, next);
+}
+
 /* Parse "+feature,-feature,feature=foo" CPU feature string
  */
-static int cpu_x86_parse_featurestr(x86_def_t *x86_cpu_def, char *features)
+static void cpu_x86_parse_featurestr(X86CPU *cpu, char *features, Error **errp)
 {
     char *featurestr; /* Single 'key=value" string being parsed */
     /* Features to be added */
@@ -1312,6 +1327,9 @@ static int cpu_x86_parse_featurestr(x86_def_t *x86_cpu_def, char *features)
     /* Features to be removed */
     FeatureWordArray minus_features = { 0 };
     uint32_t numvalue;
+    CPUX86State *env = &cpu->env;
+    NVList props = QTAILQ_HEAD_INITIALIZER(props);
+    NameValuePair *p, *tmp;
 
     featurestr = features ? strtok(features, ",") : NULL;
 
@@ -1324,77 +1342,57 @@ static int cpu_x86_parse_featurestr(x86_def_t *x86_cpu_def, char *features)
         } else if ((val = strchr(featurestr, '='))) {
             *val = 0; val++;
             if (!strcmp(featurestr, "family")) {
-                char *err;
-                numvalue = strtoul(val, &err, 0);
-                if (!*val || *err || numvalue > 0xff + 0xf) {
-                    fprintf(stderr, "bad numerical value %s\n", val);
-                    goto error;
-                }
-                x86_cpu_def->family = numvalue;
+                x86_cpu_add_nv_pair(&props, featurestr, val);
             } else if (!strcmp(featurestr, "model")) {
-                char *err;
-                numvalue = strtoul(val, &err, 0);
-                if (!*val || *err || numvalue > 0xff) {
-                    fprintf(stderr, "bad numerical value %s\n", val);
-                    goto error;
-                }
-                x86_cpu_def->model = numvalue;
+                x86_cpu_add_nv_pair(&props, featurestr, val);
             } else if (!strcmp(featurestr, "stepping")) {
-                char *err;
-                numvalue = strtoul(val, &err, 0);
-                if (!*val || *err || numvalue > 0xf) {
-                    fprintf(stderr, "bad numerical value %s\n", val);
-                    goto error;
-                }
-                x86_cpu_def->stepping = numvalue ;
+                x86_cpu_add_nv_pair(&props, featurestr, val);
             } else if (!strcmp(featurestr, "level")) {
-                char *err;
-                numvalue = strtoul(val, &err, 0);
-                if (!*val || *err) {
-                    fprintf(stderr, "bad numerical value %s\n", val);
-                    goto error;
-                }
-                x86_cpu_def->level = numvalue;
+                x86_cpu_add_nv_pair(&props, featurestr, val);
             } else if (!strcmp(featurestr, "xlevel")) {
                 char *err;
+                char num[32];
+
                 numvalue = strtoul(val, &err, 0);
                 if (!*val || *err) {
-                    fprintf(stderr, "bad numerical value %s\n", val);
-                    goto error;
+                    error_setg(errp, "bad numerical value %s\n", val);
+                    goto out;
                 }
                 if (numvalue < 0x80000000) {
                     fprintf(stderr, "xlevel value shall always be >= 0x80000000"
                             ", fixup will be deprecated in future versions\n");
                     numvalue += 0x80000000;
                 }
-                x86_cpu_def->xlevel = numvalue;
+                snprintf(num, sizeof(num), "%" PRIu32, numvalue);
+                x86_cpu_add_nv_pair(&props, featurestr, num);
             } else if (!strcmp(featurestr, "vendor")) {
-                pstrcpy(x86_cpu_def->vendor, sizeof(x86_cpu_def->vendor), val);
+                x86_cpu_add_nv_pair(&props, featurestr, val);
             } else if (!strcmp(featurestr, "model_id")) {
-                pstrcpy(x86_cpu_def->model_id, sizeof(x86_cpu_def->model_id),
-                        val);
+                x86_cpu_add_nv_pair(&props, "model-id", val);
             } else if (!strcmp(featurestr, "tsc_freq")) {
                 int64_t tsc_freq;
                 char *err;
+                char num[32];
 
                 tsc_freq = strtosz_suffix_unit(val, &err,
                                                STRTOSZ_DEFSUFFIX_B, 1000);
                 if (tsc_freq < 0 || *err) {
-                    fprintf(stderr, "bad numerical value %s\n", val);
-                    goto error;
+                    error_setg(errp, "bad numerical value %s\n", val);
+                    goto out;
                 }
-                x86_cpu_def->tsc_khz = tsc_freq / 1000;
+                snprintf(num, sizeof(num), "%" PRId64, tsc_freq);
+                x86_cpu_add_nv_pair(&props, "tsc-frequency", num);
             } else if (!strcmp(featurestr, "hv_spinlocks")) {
                 char *err;
                 numvalue = strtoul(val, &err, 0);
                 if (!*val || *err) {
-                    fprintf(stderr, "bad numerical value %s\n", val);
-                    goto error;
+                    error_setg(errp, "bad numerical value %s\n", val);
+                    goto out;
                 }
                 hyperv_set_spinlock_retries(numvalue);
             } else {
-                fprintf(stderr, "unrecognized feature %s\n", featurestr);
-                goto error;
+                error_setg(errp, "unrecognized feature %s\n", featurestr);
+                goto out;
             }
         } else if (!strcmp(featurestr, "check")) {
             check_cpuid = 1;
@@ -1405,31 +1403,46 @@ static int cpu_x86_parse_featurestr(x86_def_t *x86_cpu_def, char *features)
         } else if (!strcmp(featurestr, "hv_vapic")) {
             hyperv_enable_vapic_recommended(true);
         } else {
-            fprintf(stderr, "feature string `%s' not in format (+feature|-feature|feature=xyz)\n", featurestr);
-            goto error;
+            error_setg(errp, "feature string `%s' not in format (+feature|"
+                       "-feature|feature=xyz)\n", featurestr);
+            goto out;
         }
         featurestr = strtok(NULL, ",");
     }
-    x86_cpu_def->features |= plus_features[FEAT_1_EDX];
-    x86_cpu_def->ext_features |= plus_features[FEAT_1_ECX];
-    x86_cpu_def->ext2_features |= plus_features[FEAT_8000_0001_EDX];
-    x86_cpu_def->ext3_features |= plus_features[FEAT_8000_0001_ECX];
-    x86_cpu_def->ext4_features |= plus_features[FEAT_C000_0001_EDX];
-    x86_cpu_def->kvm_features |= plus_features[FEAT_KVM];
-    x86_cpu_def->svm_features |= plus_features[FEAT_SVM];
-    x86_cpu_def->cpuid_7_0_ebx_features |= plus_features[FEAT_7_0_EBX];
-    x86_cpu_def->features &= ~minus_features[FEAT_1_EDX];
-    x86_cpu_def->ext_features &= ~minus_features[FEAT_1_ECX];
-    x86_cpu_def->ext2_features &= ~minus_features[FEAT_8000_0001_EDX];
-    x86_cpu_def->ext3_features &= ~minus_features[FEAT_8000_0001_ECX];
-    x86_cpu_def->ext4_features &= ~minus_features[FEAT_C000_0001_EDX];
-    x86_cpu_def->kvm_features &= ~minus_features[FEAT_KVM];
-    x86_cpu_def->svm_features &= ~minus_features[FEAT_SVM];
-    x86_cpu_def->cpuid_7_0_ebx_features &= ~minus_features[FEAT_7_0_EBX];
-    return 0;
+    env->cpuid_features |= plus_features[FEAT_1_EDX];
+    env->cpuid_ext_features |= plus_features[FEAT_1_ECX];
+    env->cpuid_ext2_features |= plus_features[FEAT_8000_0001_EDX];
+    env->cpuid_ext3_features |= plus_features[FEAT_8000_0001_ECX];
+    env->cpuid_ext4_features |= plus_features[FEAT_C000_0001_EDX];
+    env->cpuid_kvm_features |= plus_features[FEAT_KVM];
+    env->cpuid_svm_features |= plus_features[FEAT_SVM];
+    env->cpuid_7_0_ebx_features |= plus_features[FEAT_7_0_EBX];
+    env->cpuid_features &= ~minus_features[FEAT_1_EDX];
+    env->cpuid_ext_features &= ~minus_features[FEAT_1_ECX];
+    env->cpuid_ext2_features &= ~minus_features[FEAT_8000_0001_EDX];
+    env->cpuid_ext3_features &= ~minus_features[FEAT_8000_0001_ECX];
+    env->cpuid_ext4_features &= ~minus_features[FEAT_C000_0001_EDX];
+    env->cpuid_kvm_features &= ~minus_features[FEAT_KVM];
+    env->cpuid_svm_features &= ~minus_features[FEAT_SVM];
+    env->cpuid_7_0_ebx_features &= ~minus_features[FEAT_7_0_EBX];
+
+    /* Set features on X86CPU object based on a provided key,value list */
+    QTAILQ_FOREACH_SAFE(p, &props, next, tmp) {
+        /* TODO: switch to using global properties after subclasses and
+         * static properties are done */
+        object_property_parse(OBJECT(cpu), p->value, p->name, errp);
+        if (error_is_set(errp)) {
+            goto out;
+        }
+    }
 
-error:
-    return -1;
+out:
+    QTAILQ_FOREACH_SAFE(p, &props, next, tmp) {
+        QTAILQ_REMOVE(&props, p, next);
+        g_free(p->value);
+        g_free(p->name);
+        g_free(p);
+    }
 }
 
 /* generate a composite string into buf of all cpuid names in featureset
@@ -1559,10 +1572,6 @@ int cpu_x86_register(X86CPU *cpu, const char *cpu_model)
     def->kvm_features |= kvm_default_features;
     def->ext_features |= CPUID_EXT_HYPERVISOR;
 
-    if (cpu_x86_parse_featurestr(def, features) < 0) {
-        error_setg(&error, "Invalid cpu_model string format: %s", cpu_model);
-        goto out;
-    }
     assert(def->vendor[0]);
     object_property_set_str(OBJECT(cpu), def->vendor, "vendor", &error);
     object_property_set_int(OBJECT(cpu), def->level, "level", &error);
@@ -1584,6 +1593,7 @@ int cpu_x86_register(X86CPU *cpu, const char *cpu_model)
 
     object_property_set_str(OBJECT(cpu), def->model_id, "model-id", &error);
 
+    cpu_x86_parse_featurestr(cpu, features, &error);
 out:
     g_strfreev(model_pieces);
     if (error) {
-- 
1.7.1

  parent reply	other threads:[~2013-01-17 15:19 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-17 15:16 [Qemu-devel] [PATCH qom-cpu 0/5] x86 CPU cleanup, part 4 Igor Mammedov
2013-01-17 15:16 ` [Qemu-devel] [PATCH 1/5] target-i386: print deprecated warning if xlevel < 0x80000000 Igor Mammedov
2013-01-21  8:39   ` Andreas Färber
2013-01-21 12:14     ` Igor Mammedov
2013-01-17 15:16 ` [Qemu-devel] [PATCH 2/5] target-i386: replace uint32_t vendor fields by vendor string in x86_def_t Igor Mammedov
2013-01-17 15:29   ` Eduardo Habkost
2013-01-18  7:12   ` li guang
2013-01-18 13:40     ` Igor Mammedov
2013-01-21  3:16       ` li guang
2013-01-21  8:18   ` Andreas Färber
2013-01-17 15:16 ` [Qemu-devel] [PATCH 3/5] target-i386: remove vendor_override field from CPUX86State Igor Mammedov
2013-01-17 15:30   ` Eduardo Habkost
2013-01-17 15:16 ` Igor Mammedov [this message]
2013-01-17 17:44   ` [Qemu-devel] [PATCH 4/5] target-i386: set custom features/properties without intermediate x86_def_t Eduardo Habkost
2013-01-18 14:49     ` Igor Mammedov
2013-01-17 15:16 ` [Qemu-devel] [PATCH 5/5] target-i386: remove setting tsc-frequency from x86_def_t Igor Mammedov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1358435794-8406-5-git-send-email-imammedo@redhat.com \
    --to=imammedo@redhat.com \
    --cc=afaerber@suse.de \
    --cc=ehabkost@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).