diff mbox

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

Message ID 1361754189-29809-5-git-send-email-imammedo@redhat.com
State New
Headers show

Commit Message

Igor Mammedov Feb. 25, 2013, 1:03 a.m. UTC
Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
 target-i386/cpu.c |   50 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 48 insertions(+), 2 deletions(-)

Comments

Eduardo Habkost Feb. 26, 2013, 2:38 p.m. UTC | #1
On Mon, Feb 25, 2013 at 02:03:03AM +0100, Igor Mammedov wrote:
> Signed-off-by: Igor Mammedov <imammedo@redhat.com>
> ---
>  target-i386/cpu.c |   50 ++++++++++++++++++++++++++++++++++++++++++++++++--
>  1 files changed, 48 insertions(+), 2 deletions(-)
> 
> diff --git a/target-i386/cpu.c b/target-i386/cpu.c
> index 35fc303..443c15e 100644
> --- a/target-i386/cpu.c
> +++ b/target-i386/cpu.c
> @@ -1295,6 +1295,50 @@ static PropertyInfo qdev_prop_tsc_freq = {
>  #define DEFINE_PROP_TSC_FREQ(_n)                                               \
>      DEFINE_PROP(_n, X86CPU, env.tsc_khz, qdev_prop_tsc_freq, int32_t)
>  
> +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"),
> @@ -1304,6 +1348,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),

I'm still bothered by how complicated it is to define a simple property
that has a custom setter/getter.

Registering a property with a getter/setter on instance_init require
only one line of code, why doing the same with a static property
requires _twelve_ lines?

Are we using the static property API in a wrong or unexpected way, or
this is really how things are supposed to work?


>      DEFINE_PROP_END_OF_LIST(),
>  };
>  
> @@ -1421,6 +1466,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);
> @@ -1432,7 +1478,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;
> @@ -1597,7 +1644,6 @@ static void cpu_x86_register(X86CPU *cpu, const char *name, Error **errp)
>          def->kvm_features |= kvm_default_features;
>      }
>      def->ext_features |= 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);
> -- 
> 1.7.1
>
Eduardo Habkost Feb. 26, 2013, 2:42 p.m. UTC | #2
On Mon, Feb 25, 2013 at 02:03:03AM +0100, Igor Mammedov wrote:
[...]
> +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;
> +}
[...]
> @@ -1421,6 +1466,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);
> @@ -1432,7 +1478,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);

Isn't the above validation code already inside x86_set_hv_spinlocks()?
Why do we need it here in cpu_x86_parse_featurestr() too?

> +                object_property_parse(OBJECT(cpu), num, featurestr, errp);
>              } else {
>                  error_setg(errp, "unrecognized feature %s", featurestr);
>                  goto out;
> @@ -1597,7 +1644,6 @@ static void cpu_x86_register(X86CPU *cpu, const char *name, Error **errp)
>          def->kvm_features |= kvm_default_features;
>      }
>      def->ext_features |= 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);
> -- 
> 1.7.1
>
Eduardo Habkost Feb. 26, 2013, 3:11 p.m. UTC | #3
TL;DR: I want to understand why a
  DEFINE_PROP_CUSTOM(name, type, getter_func, setter_func)
macro doesn't exist yet.


On Tue, Feb 26, 2013 at 11:38:59AM -0300, Eduardo Habkost wrote:
[...]
> > +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"),
> > @@ -1304,6 +1348,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),
> 
> I'm still bothered by how complicated it is to define a simple property
> that has a custom setter/getter.
> 
> Registering a property with a getter/setter on instance_init require
> only one line of code, why doing the same with a static property
> requires _twelve_ lines?
> 
> Are we using the static property API in a wrong or unexpected way, or
> this is really how things are supposed to work?

I was discussing this with Igor, and my main question is:

Maybe the static property API makes this really complicated on purpose
because we're supposed to validate property values on realizefn()
instead of the property setter?

> 
> 
> >      DEFINE_PROP_END_OF_LIST(),
> >  };
> >  
> > @@ -1421,6 +1466,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);
> > @@ -1432,7 +1478,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;
> > @@ -1597,7 +1644,6 @@ static void cpu_x86_register(X86CPU *cpu, const char *name, Error **errp)
> >          def->kvm_features |= kvm_default_features;
> >      }
> >      def->ext_features |= 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);
> > -- 
> > 1.7.1
> > 
> 
> -- 
> Eduardo
>
Igor Mammedov Feb. 26, 2013, 3:50 p.m. UTC | #4
On Tue, 26 Feb 2013 11:42:08 -0300
Eduardo Habkost <ehabkost@redhat.com> wrote:

> On Mon, Feb 25, 2013 at 02:03:03AM +0100, Igor Mammedov wrote:
> [...]
> > +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;
> > +}
> [...]
> > @@ -1421,6 +1466,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);
> > @@ -1432,7 +1478,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);
> 
> Isn't the above validation code already inside x86_set_hv_spinlocks()?
> Why do we need it here in cpu_x86_parse_featurestr() too?
Purpose of above is not a validation, it's silent fix-up made visible in
3/10, like we did for xlevel.
And will be accompanied by entry in Changelog, once patch is in.

> 
> > +                object_property_parse(OBJECT(cpu), num, featurestr,
> > errp); } else {
> >                  error_setg(errp, "unrecognized feature %s", featurestr);
> >                  goto out;
> > @@ -1597,7 +1644,6 @@ static void cpu_x86_register(X86CPU *cpu, const
> > char *name, Error **errp) def->kvm_features |= kvm_default_features;
> >      }
> >      def->ext_features |= 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);
> > -- 
> > 1.7.1
> > 
>
Eduardo Habkost Feb. 26, 2013, 4:11 p.m. UTC | #5
On Tue, Feb 26, 2013 at 04:50:22PM +0100, Igor Mammedov wrote:
> On Tue, 26 Feb 2013 11:42:08 -0300
> Eduardo Habkost <ehabkost@redhat.com> wrote:
> 
> > On Mon, Feb 25, 2013 at 02:03:03AM +0100, Igor Mammedov wrote:
> > [...]
> > > +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;
> > > +}
> > [...]
> > > @@ -1421,6 +1466,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);
> > > @@ -1432,7 +1478,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);
> > 
> > Isn't the above validation code already inside x86_set_hv_spinlocks()?
> > Why do we need it here in cpu_x86_parse_featurestr() too?
> Purpose of above is not a validation, it's silent fix-up made visible in
> 3/10, like we did for xlevel.

OK.

> And will be accompanied by entry in Changelog, once patch is in.
> 
> > 
> > > +                object_property_parse(OBJECT(cpu), num, featurestr,
> > > errp); } else {

I was going to ask: "in that case, isn't it easiser to use
object_property_set_int() instead of snprintf() +
object_property_parse()?"

But then I remembered that we're going to eventually replace all
object_property_parse() calls here with qdev_prop_set_global() calls.
:-)


> > >                  error_setg(errp, "unrecognized feature %s", featurestr);
> > >                  goto out;
> > > @@ -1597,7 +1644,6 @@ static void cpu_x86_register(X86CPU *cpu, const
> > > char *name, Error **errp) def->kvm_features |= kvm_default_features;
> > >      }
> > >      def->ext_features |= 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);
> > > -- 
> > > 1.7.1
> > > 
> > 
>
diff mbox

Patch

diff --git a/target-i386/cpu.c b/target-i386/cpu.c
index 35fc303..443c15e 100644
--- a/target-i386/cpu.c
+++ b/target-i386/cpu.c
@@ -1295,6 +1295,50 @@  static PropertyInfo qdev_prop_tsc_freq = {
 #define DEFINE_PROP_TSC_FREQ(_n)                                               \
     DEFINE_PROP(_n, X86CPU, env.tsc_khz, qdev_prop_tsc_freq, int32_t)
 
+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"),
@@ -1304,6 +1348,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(),
 };
 
@@ -1421,6 +1466,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);
@@ -1432,7 +1478,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;
@@ -1597,7 +1644,6 @@  static void cpu_x86_register(X86CPU *cpu, const char *name, Error **errp)
         def->kvm_features |= kvm_default_features;
     }
     def->ext_features |= 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);