Patchwork [10/15] target-i386: convert 'hv_spinlocks' to static property

login
register
mail settings
Submitter Igor Mammedov
Date June 5, 2013, 1:18 p.m.
Message ID <1370438326-27054-11-git-send-email-imammedo@redhat.com>
Download mbox | patch
Permalink /patch/249056/
State New
Headers show

Comments

Igor Mammedov - June 5, 2013, 1:18 p.m.
Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
 target-i386/cpu.c |   50 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 48 insertions(+), 2 deletions(-)
Eduardo Habkost - June 25, 2013, 8:30 p.m.
On Wed, Jun 05, 2013 at 03:18:41PM +0200, Igor Mammedov wrote:
> Signed-off-by: Igor Mammedov <imammedo@redhat.com>
> ---
[...]
> @@ -1632,6 +1677,7 @@ static void cpu_x86_parse_featurestr(X86CPU *cpu, char *features, Error **errp)
>              } else if (!strcmp(featurestr, "hv-spinlocks")) {
>                  char *err;
>                  const int min = 0xFFF;
> +                char num[32];
>                  numvalue = strtoul(val, &err, 0);
>                  if (!*val || *err) {
>                      error_setg(errp, "bad numerical value %s", val);
> @@ -1643,7 +1689,8 @@ static void cpu_x86_parse_featurestr(X86CPU *cpu, char *features, Error **errp)
>                              min);
>                      numvalue = min;
>                  }
> -                env->hyperv_spinlock_attempts = numvalue;
> +                snprintf(num, sizeof(num), "%" PRId32, numvalue);
> +                object_property_parse(OBJECT(cpu), num, featurestr, errp);

Why not use object_property_set_int()?
Eduardo Habkost - June 25, 2013, 8:34 p.m.
On Tue, Jun 25, 2013 at 05:30:50PM -0300, Eduardo Habkost wrote:
> On Wed, Jun 05, 2013 at 03:18:41PM +0200, Igor Mammedov wrote:
> > Signed-off-by: Igor Mammedov <imammedo@redhat.com>
> > ---
> [...]
> > @@ -1632,6 +1677,7 @@ static void cpu_x86_parse_featurestr(X86CPU *cpu, char *features, Error **errp)
> >              } else if (!strcmp(featurestr, "hv-spinlocks")) {
> >                  char *err;
> >                  const int min = 0xFFF;
> > +                char num[32];
> >                  numvalue = strtoul(val, &err, 0);
> >                  if (!*val || *err) {
> >                      error_setg(errp, "bad numerical value %s", val);
> > @@ -1643,7 +1689,8 @@ static void cpu_x86_parse_featurestr(X86CPU *cpu, char *features, Error **errp)
> >                              min);
> >                      numvalue = min;
> >                  }
> > -                env->hyperv_spinlock_attempts = numvalue;
> > +                snprintf(num, sizeof(num), "%" PRId32, numvalue);
> > +                object_property_parse(OBJECT(cpu), num, featurestr, errp);
> 
> Why not use object_property_set_int()?

Oh, I believe I have asked that before and you have already explained
it: you are using strings to allow the existing object_property_parse()
calls to be easily converted to qdev_prop_register_global() calls later.
Correct?
Igor Mammedov - June 26, 2013, 8:30 a.m.
On Tue, 25 Jun 2013 17:34:44 -0300
Eduardo Habkost <ehabkost@redhat.com> wrote:

> On Tue, Jun 25, 2013 at 05:30:50PM -0300, Eduardo Habkost wrote:
> > On Wed, Jun 05, 2013 at 03:18:41PM +0200, Igor Mammedov wrote:
> > > Signed-off-by: Igor Mammedov <imammedo@redhat.com>
> > > ---
> > [...]
> > > @@ -1632,6 +1677,7 @@ static void cpu_x86_parse_featurestr(X86CPU *cpu, char *features, Error **errp)
> > >              } else if (!strcmp(featurestr, "hv-spinlocks")) {
> > >                  char *err;
> > >                  const int min = 0xFFF;
> > > +                char num[32];
> > >                  numvalue = strtoul(val, &err, 0);
> > >                  if (!*val || *err) {
> > >                      error_setg(errp, "bad numerical value %s", val);
> > > @@ -1643,7 +1689,8 @@ static void cpu_x86_parse_featurestr(X86CPU *cpu, char *features, Error **errp)
> > >                              min);
> > >                      numvalue = min;
> > >                  }
> > > -                env->hyperv_spinlock_attempts = numvalue;
> > > +                snprintf(num, sizeof(num), "%" PRId32, numvalue);
> > > +                object_property_parse(OBJECT(cpu), num, featurestr, errp);
> > 
> > Why not use object_property_set_int()?
> 
> Oh, I believe I have asked that before and you have already explained
> it: you are using strings to allow the existing object_property_parse()
> calls to be easily converted to qdev_prop_register_global() calls later.
> Correct?
> 

Yep, this will be used later to consolidate code.

Patch

diff --git a/target-i386/cpu.c b/target-i386/cpu.c
index 40f2a7c..c2125f4 100644
--- a/target-i386/cpu.c
+++ b/target-i386/cpu.c
@@ -1507,6 +1507,50 @@  static void x86_cpu_get_feature_words(Object *obj, Visitor *v, void *opaque,
     error_propagate(errp, err);
 }
 
+static void x86_get_hv_spinlocks(Object *obj, Visitor *v, void *opaque,
+                                 const char *name, Error **errp)
+{
+    X86CPU *cpu = X86_CPU(obj);
+    int64_t value = cpu->env.hyperv_spinlock_attempts;
+
+    visit_type_int(v, &value, name, errp);
+}
+
+static void x86_set_hv_spinlocks(Object *obj, Visitor *v, void *opaque,
+                                 const char *name, Error **errp)
+{
+    const int64_t min = 0xFFF;
+    const int64_t max = UINT_MAX;
+    X86CPU *cpu = X86_CPU(obj);
+    int64_t value;
+
+    visit_type_int(v, &value, name, errp);
+    if (error_is_set(errp)) {
+        return;
+    }
+
+    if (value < min || value > max) {
+        error_setg(errp, "Property %s.%s doesn't take value %" PRId64
+                  " (minimum: %" PRId64 ", maximum: %" PRId64 ")",
+                  object_get_typename(obj), name ? name : "null",
+                  value, min, max);
+        return;
+    }
+    cpu->env.hyperv_spinlock_attempts = value;
+}
+
+static PropertyInfo qdev_prop_spinlocks = {
+    .name  = "int",
+    .get   = x86_get_hv_spinlocks,
+    .set   = x86_set_hv_spinlocks,
+};
+#define DEFINE_PROP_HV_SPINLOCKS(_n, _defval) {                                \
+    .name  = _n,                                                               \
+    .info  = &qdev_prop_spinlocks,                                             \
+    .qtype = QTYPE_QINT,                                                       \
+    .defval = _defval                                                          \
+}
+
 static Property cpu_x86_properties[] = {
     DEFINE_PROP_FAMILY("family"),
     DEFINE_PROP_MODEL("model"),
@@ -1516,6 +1560,7 @@  static Property cpu_x86_properties[] = {
     DEFINE_PROP_VENDOR("vendor"),
     DEFINE_PROP_MODEL_ID("model-id"),
     DEFINE_PROP_TSC_FREQ("tsc-frequency"),
+    DEFINE_PROP_HV_SPINLOCKS("hv-spinlocks", HYPERV_SPINLOCK_NEVER_RETRY),
     DEFINE_PROP_END_OF_LIST(),
 };
 
@@ -1632,6 +1677,7 @@  static void cpu_x86_parse_featurestr(X86CPU *cpu, char *features, Error **errp)
             } else if (!strcmp(featurestr, "hv-spinlocks")) {
                 char *err;
                 const int min = 0xFFF;
+                char num[32];
                 numvalue = strtoul(val, &err, 0);
                 if (!*val || *err) {
                     error_setg(errp, "bad numerical value %s", val);
@@ -1643,7 +1689,8 @@  static void cpu_x86_parse_featurestr(X86CPU *cpu, char *features, Error **errp)
                             min);
                     numvalue = min;
                 }
-                env->hyperv_spinlock_attempts = numvalue;
+                snprintf(num, sizeof(num), "%" PRId32, numvalue);
+                object_property_parse(OBJECT(cpu), num, featurestr, errp);
             } else {
                 error_setg(errp, "unrecognized feature %s", featurestr);
                 goto out;
@@ -1802,7 +1849,6 @@  static void cpu_x86_register(X86CPU *cpu, const char *name, Error **errp)
         def->features[FEAT_KVM] |= kvm_default_features;
     }
     def->features[FEAT_1_ECX] |= CPUID_EXT_HYPERVISOR;
-    env->hyperv_spinlock_attempts = HYPERV_SPINLOCK_NEVER_RETRY;
 
     object_property_set_str(OBJECT(cpu), def->vendor, "vendor", errp);
     object_property_set_int(OBJECT(cpu), def->level, "level", errp);