target-i386: Register QOM properties for feature flags
PATCH v4 1/1 target-i386: Register QOM properties for feature flags
This uses the feature name arrays to register QOM properties for feature flags. This simply adds properties that can be configured using -global, but doesn't change x86_cpu_parse_featurestr() to use them yet.
---
Changes v1 -> v2:
* Use "cpuid-" prefix instead of "feat-"
* Register release function for property
* Convert '_' to '-' on feature name before registering property
* Add dev->realized check to property setter
Changes v2 -> v3:
* Register alias properties for feature name aliases
* Patch is based on x86 tree, at:
https://github.com/ehabkost/qemu.git x86
Changes v3 -> v4:
* Remove "cpuid-" prefix altogether
--- target-i386/cpu.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+)
target-i386/cpu.c +115 -0
@@ -2848,12 +2848,120 @@ out: } } +typedef struct FeatureProperty { +FeatureWord word; +uint32_t mask; +}FeatureProperty ; + +static void x86_cpu_get_feature_prop (Object *obj, +struct Visitor *v, +void *opaque, +const char *name, +Error **errp) +{ +X86CPU *cpu =X86_CPU (obj); +CPUX86State *env = &cpu->env; +FeatureProperty *fp = opaque; +bool value = (env->features[fp->word] & fp->mask) == fp->mask; +visit_type_bool (v, &value, name, errp); +} + +static void x86_cpu_set_feature_prop (Object *obj, +struct Visitor *v, +void *opaque, +const char *name, +Error **errp) +{ +X86CPU *cpu =X86_CPU (obj); +DeviceState *dev =DEVICE (obj); +CPUX86State *env = &cpu->env; +FeatureProperty *fp = opaque; +bool value; + +if (dev->realized) { +qdev_prop_set_after_realize (dev, name, errp); +return ; + } + +visit_type_bool (v, &value, name, errp); +if (value) { + env->features[fp->word] |= fp->mask; + }else { + env->features[fp->word] &= ~fp->mask; + } +} + +static void x86_cpu_release_feature_prop (Object *obj,const char *name, +void *opaque) +{ +FeatureProperty *prop = opaque; +g_free (prop); +} + +/* Register a boolean feature-bits property. +* If mask has multiple bits, all must be set for the property to return true. +* The same property name can be registered multiple times to make it affect +* multiple bits in the same FeatureWord. +*/ +static void x86_cpu_register_feature_prop (X86CPU *cpu, +const char *prop_name, +FeatureWord w, +uint32_t mask) +{ +FeatureProperty *fp; +ObjectProperty *op; + op =object_property_find (OBJECT (cpu), prop_name,NULL ); +if (op) { + fp = op->opaque; +assert (fp->word == w); + fp->mask |= mask; + }else { + fp =g_new0 (FeatureProperty,1 ); + fp->word = w; + fp->mask = mask; +object_property_add (OBJECT (cpu), prop_name,"bool" , + x86_cpu_get_feature_prop, + x86_cpu_set_feature_prop, + x86_cpu_release_feature_prop, fp, &error_abort); + } +} + +static void x86_cpu_register_feature_bit_props (X86CPU *cpu, +FeatureWord w, +int bit) +{ +Object *obj =OBJECT (cpu); +int i; +char **names; +FeatureWordInfo *fi = &feature_word_info[w]; + +if (!fi->feat_names) { +return ; + } +if (!fi->feat_names[bit]) { +return ; + } + + names =g_strsplit (fi->feat_names[bit],"|" ,0 ); + +feat2prop (names[0 ]); +x86_cpu_register_feature_prop (cpu, names[0 ], w, (1UL << bit)); + +for (i =1 ; names[i]; i++) { +feat2prop (names[i]); +object_property_add_alias (obj, names[i], obj, names[0 ], &error_abort); + } + +g_strfreev (names); +} +static void x86_cpu_initfn (Object *obj) {CPUState *cs =CPU (obj);X86CPU *cpu =X86_CPU (obj);X86CPUClass *xcc =X86_CPU_GET_CLASS (obj);CPUX86State *env = &cpu->env; +FeatureWord w;static int inited; cs->env_ptr = env;
@@ -2894,6 +3002,13 @@ static void x86_cpu_initfn(Object *obj) cpu->apic_id =-1 ;#endif +for (w =0 ; w <FEATURE_WORDS ; w++) { +int bit; +for (bit =0 ; bit <32 ; bit++) { +x86_cpu_register_feature_bit_props (cpu, w, bit); + } + } +x86_cpu_load_def (cpu, xcc->cpu_def, &error_abort);/* init various static tables used in TCG mode */
Signature · 1 line
2.1.0
Quoted 36 lines · On Fri, 10 Apr 2015 17:02:29 -0300 Eduardo Habkost <[email protected]> wrote:
This uses the feature name arrays to register QOM properties for feature flags. This simply adds properties that can be configured using -global, but doesn't change
x86_cpu_parse_featurestr()to use them yet.Signed-off-by Eduardo Habkost <[email protected]>---
Changes v1 -> v2:
* Use "cpuid-" prefix instead of "feat-"
* Register release function for property
* Convert '_' to '-' on feature name before registering property
* Adddev->realizedcheck to property setterChanges v2 -> v3:
* Register alias properties for feature name aliases
* Patch is based on x86 tree, at:
https://github.com/ehabkost/qemu.git x86Changes v3 -> v4:
* Remove "cpuid-" prefix altogether--- target-i386/cpu.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+)target-i386/cpu.c +5
-0@@ -2848,12 +2848,120 @@ out: } } +typedef struct FeatureProperty { +FeatureWord word; +uint32_t mask; +}FeatureProperty ; +
target-i386/cpu.c out: · line 2865
Quoted 10 lines
target-i386/cpu.c +10
-0+static void x86_cpu_get_feature_prop (Object *obj, +struct Visitor *v, +void *opaque, +const char *name, +Error **errp) +{ +X86CPU *cpu =X86_CPU (obj); +CPUX86State *env = &cpu->env; +FeatureProperty *fp = opaque; +bool value = (env->features[fp->word] & fp->mask) == fp->mask;
maybe s/env->features[fp->word]/*fp->feature_word_ptr/ it would consolidate possible out of bound error place to 1 instead of several as it's now.
Also it makes it easier to read for me :).
Quoted 11 lines
target-i386/cpu.c +11
-0+visit_type_bool (v, &value, name, errp); +} + +static void x86_cpu_set_feature_prop (Object *obj, +struct Visitor *v, +void *opaque, +const char *name, +Error **errp) +{ +X86CPU *cpu =X86_CPU (obj); +DeviceState *dev =DEVICE (obj);
target-i386/cpu.c out: · line 2886
Quoted 10 lines
target-i386/cpu.c +10
-0+CPUX86State *env = &cpu->env; +FeatureProperty *fp = opaque; +bool value; + +if (dev->realized) { +qdev_prop_set_after_realize (dev, name, errp); +return ; + } + +visit_type_bool (v, &value, name, errp);
should check error here and return in case of it's set,
+ local_err + error_propagate()
Quoted 13 lines
target-i386/cpu.c +13
-0+if (value) { + env->features[fp->word] |= fp->mask; + }else { + env->features[fp->word] &= ~fp->mask; + } +} + +static void x86_cpu_release_feature_prop (Object *obj,const char *name, +void *opaque) +{ +FeatureProperty *prop = opaque; +g_free (prop); +}
target-i386/cpu.c out: · line 2909
Quoted 10 lines
target-i386/cpu.c +10
-0+ +/* Register a boolean feature-bits property. +* If mask has multiple bits, all must be set for the property to return true. +* The same property name can be registered multiple times to make it affect +* multiple bits in the same FeatureWord. +*/ +static void x86_cpu_register_feature_prop (X86CPU *cpu, +const char *prop_name, +FeatureWord w, +uint32_t mask)
isn't used as mask by caller, s/mask/bit/ ???
target-i386/cpu.c out: · line 2912
Quoted 3 lines
target-i386/cpu.c +3
-0+{ +FeatureProperty *fp; +ObjectProperty *op;
white line here?
Quoted 7 lines
target-i386/cpu.c +7
-0+ op =object_property_find (OBJECT (cpu), prop_name,NULL ); +if (op) { + fp = op->opaque; +assert (fp->word == w); + fp->mask |= mask; + }else { + fp =g_new0 (FeatureProperty,1 );
target-i386/cpu.c out: · line 2929
Quoted 10 lines
target-i386/cpu.c +10
-0+ fp->word = w; + fp->mask = mask; +object_property_add (OBJECT (cpu), prop_name,"bool" , + x86_cpu_get_feature_prop, + x86_cpu_set_feature_prop, + x86_cpu_release_feature_prop, fp, &error_abort); + } +} + +static void x86_cpu_register_feature_bit_props(X86CPU *cpu,
this adds 1 property and possibly aliases, _props() is confusing here. I'd rename it to x86_cpu_add_feature_bit_prop() and inline above x86_cpu_register_feature_prop() since it's not going to be reused
Quoted 39 lines
target-i386/cpu.c +29
-0+ FeatureWord w, + int bit) +{ +Object *obj =OBJECT (cpu); +int i; +char **names; +FeatureWordInfo *fi = &feature_word_info[w]; + +if (!fi->feat_names) { +return ; + } +if (!fi->feat_names[bit]) { +return ; + } + + names =g_strsplit (fi->feat_names[bit],"|" ,0 ); + +feat2prop (names[0 ]); +x86_cpu_register_feature_prop (cpu, names[0 ], w, (1UL << bit)); + +for (i =1 ; names[i]; i++) { +feat2prop (names[i]); +object_property_add_alias (obj, names[i], obj, names[0 ], &error_abort); + } + +g_strfreev (names); +} +static void x86_cpu_initfn (Object *obj) {CPUState *cs =CPU (obj);X86CPU *cpu =X86_CPU (obj);X86CPUClass *xcc =X86_CPU_GET_CLASS (obj);CPUX86State *env = &cpu->env; +FeatureWord w;static int inited; cs->env_ptr = env;
target-i386/cpu.c static void x86_cpu_initfn(Object *obj) · line 3006
Quoted 5 lines
target-i386/cpu.c +2
-0@@ -2894,6 +3002,13 @@ static void x86_cpu_initfn(Object *obj) cpu->apic_id =-1 ;#endif +for (w =0 ; w <FEATURE_WORDS ; w++) { +int bit;
white line here?
Quoted 8 lines
target-i386/cpu.c +5
-0+for (bit =0 ; bit <32 ; bit++) { +x86_cpu_register_feature_bit_props (cpu, w, bit); + } + } +x86_cpu_load_def (cpu, xcc->cpu_def, &error_abort);/* init various static tables used in TCG mode */
Quoted 45 lines · Igor Mammedov
Quoted 42 lines · On Fri, 10 Apr 2015 17:02:29 -0300 Eduardo Habkost <[email protected]> wrote:
This uses the feature name arrays to register QOM properties for feature flags. This simply adds properties that can be configured using -global, but doesn't change
x86_cpu_parse_featurestr()to use them yet.Signed-off-by Eduardo Habkost <[email protected]>---
Changes v1 -> v2:
* Use "cpuid-" prefix instead of "feat-"
* Register release function for property
* Convert '_' to '-' on feature name before registering property
* Adddev->realizedcheck to property setterChanges v2 -> v3:
* Register alias properties for feature name aliases
* Patch is based on x86 tree, at:
https://github.com/ehabkost/qemu.git x86Changes v3 -> v4:
* Remove "cpuid-" prefix altogether--- target-i386/cpu.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+)target-i386/cpu.c +11
-0@@ -2848,12 +2848,120 @@ out: } } +typedef struct FeatureProperty { +FeatureWord word; +uint32_t mask; +}FeatureProperty ; + +static void x86_cpu_get_feature_prop (Object *obj, +struct Visitor *v, +void *opaque, +const char *name, +Error **errp) +{
Quoted 8 lines
target-i386/cpu.c out: · line 2865
Quoted 4 lines
target-i386/cpu.c +4
-0+X86CPU *cpu =X86_CPU (obj); +CPUX86State *env = &cpu->env; +FeatureProperty *fp = opaque; +bool value = (env->features[fp->word] & fp->mask) == fp->mask;maybe s/env->features[fp->word]/*fp->feature_word_ptr/ it would consolidate possible out of bound error place to 1 instead of several as it's now.
Also it makes it easier to read for me :).
That makes sense, and would make it easier to convert this code to generic QOM code later.
Quoted 16 lines
Quoted 16 lines
target-i386/cpu.c +15
-0+visit_type_bool (v, &value, name, errp); +} + +static void x86_cpu_set_feature_prop (Object *obj, +struct Visitor *v, +void *opaque, +const char *name, +Error **errp) +{ +X86CPU *cpu =X86_CPU (obj); +DeviceState *dev =DEVICE (obj); +CPUX86State *env = &cpu->env; +FeatureProperty *fp = opaque; +bool value; +
Quoted 8 lines
target-i386/cpu.c out: · line 2886
Quoted 6 lines
target-i386/cpu.c +6
-0+if (dev->realized) { +qdev_prop_set_after_realize (dev, name, errp); +return ; + } + +visit_type_bool (v, &value, name, errp);should check error here and return in case of it's set,
+local_err+error_propagate()
Will do it. Thanks for catching it!
Quoted 17 lines
Quoted 17 lines
target-i386/cpu.c +16
-0+if (value) { + env->features[fp->word] |= fp->mask; + }else { + env->features[fp->word] &= ~fp->mask; + } +} + +static void x86_cpu_release_feature_prop (Object *obj,const char *name, +void *opaque) +{ +FeatureProperty *prop = opaque; +g_free (prop); +} + +/* Register a boolean feature-bits property. +* If mask has multiple bits, all must be set for the property to return true.
Quoted 8 lines
target-i386/cpu.c out: · line 2909
Quoted 7 lines
target-i386/cpu.c +7
-0+* The same property name can be registered multiple times to make it affect +* multiple bits in the same FeatureWord. +*/ +static void x86_cpu_register_feature_prop (X86CPU *cpu, +const char *prop_name, +FeatureWord w, +uint32_t mask)isn't used as mask by caller, s/mask/bit/ ???
There will be an use case for mask containing multiple bits, later. My plan is to remove the duplicate "kvmclock" alias from kvm_feature_name, and call this manually:
x86_cpu_register_feature_prop(cpu, "kvmclock", FEAT_KVM,
(1 << KVM_FEATURE_CLOCKSOURCE) | (1 << KVM_FEATURE_CLOCKSOURCE2));
I didn't do that yet because I need the existing x86_cpu_parse_featurestr() code to keep working until it is converted to use object_property_set().
Quoted 5 lines
target-i386/cpu.c out: · line 2912
Quoted 4 lines
target-i386/cpu.c +3
-0+{ +FeatureProperty *fp; +ObjectProperty *op;white line here?
Will do it.
Quoted 6 lines
target-i386/cpu.c out: · line 2917
Quoted 6 lines
target-i386/cpu.c +5
-0+ op =object_property_find (OBJECT (cpu), prop_name,NULL ); +if (op) { + fp = op->opaque; +assert (fp->word == w); + fp->mask |= mask;
^^^ This is the block of code that will be removed once I add the manual "kvmclock" registration call I mentioned above.
Quoted 5 lines
Quoted 5 lines
target-i386/cpu.c +5
-0+ }else { + fp =g_new0 (FeatureProperty,1 ); + fp->word = w; + fp->mask = mask; + object_property_add(OBJECT(cpu), prop_name, "bool",
Quoted 8 lines
target-i386/cpu.c out: · line 2929
Quoted 7 lines
target-i386/cpu.c +7
-0+ x86_cpu_get_feature_prop, + x86_cpu_set_feature_prop, + x86_cpu_release_feature_prop, fp, &error_abort); + } +} + +static void x86_cpu_register_feature_bit_props(X86CPU *cpu,this adds 1 property and possibly aliases,
_props()is confusing here.
Alias properties are still properties like any other, aren't they? The function is still responsible for registering multiple properties. Is the "_props()" suffix really that confusing?
Quoted 2 lines
I'd rename it to
x86_cpu_add_feature_bit_prop()and inline abovex86_cpu_register_feature_prop()since it's not going to be reused
I prefer to keep the single-property function separated, as it may become a generic bitmap property registration function inside generic QOM code later. With your feature_word_ptr suggestion, it would be even more generic and non-x86-specific.
(To be honest, I would prefer to keep the single-property function registration code clearly separated even if it was never going to be reused anywhere. 20-line functions are already too long for my taste.)
Quoted 38 lines
Quoted 38 lines
target-i386/cpu.c +29
-0+ FeatureWord w, + int bit) +{ +Object *obj =OBJECT (cpu); +int i; +char **names; +FeatureWordInfo *fi = &feature_word_info[w]; + +if (!fi->feat_names) { +return ; + } +if (!fi->feat_names[bit]) { +return ; + } + + names =g_strsplit (fi->feat_names[bit],"|" ,0 ); + +feat2prop (names[0 ]); +x86_cpu_register_feature_prop (cpu, names[0 ], w, (1UL << bit)); + +for (i =1 ; names[i]; i++) { +feat2prop (names[i]); +object_property_add_alias (obj, names[i], obj, names[0 ], &error_abort); + } + +g_strfreev (names); +} +static void x86_cpu_initfn (Object *obj) {CPUState *cs =CPU (obj);X86CPU *cpu =X86_CPU (obj);X86CPUClass *xcc =X86_CPU_GET_CLASS (obj);CPUX86State *env = &cpu->env; +FeatureWord w;static int inited;
Quoted 8 lines
target-i386/cpu.c static void x86_cpu_initfn(Object *obj) · line 3006
Quoted 7 lines
target-i386/cpu.c +2
-0cs->env_ptr = env;@@ -2894,6 +3002,13 @@ static void x86_cpu_initfn(Object *obj) cpu->apic_id =-1 ;#endif +for (w =0 ; w <FEATURE_WORDS ; w++) { +int bit;white line here?
Will change it. Thanks!
Quoted 10 lines
Quoted 9 lines
target-i386/cpu.c +5
-0+for (bit =0 ; bit <32 ; bit++) { +x86_cpu_register_feature_bit_props (cpu, w, bit); + } + } +x86_cpu_load_def (cpu, xcc->cpu_def, &error_abort);/* init various static tables used in TCG mode */
Signature · 1 line
Eduardo
On Tue, 14 Apr 2015 11:08:39 -0300
Eduardo Habkost <[email protected]> wrote:
[...]
Quoted 11 lines
Quoted 10 lines
target-i386/cpu.c out: · line 2909
Quoted 9 lines
target-i386/cpu.c +9
-0+/* Register a boolean feature-bits property. +* If mask has multiple bits, all must be set for the property to return true. +* The same property name can be registered multiple times to make it affect +* multiple bits in the same FeatureWord. +*/ +static void x86_cpu_register_feature_prop (X86CPU *cpu, +const char *prop_name, +FeatureWord w, +uint32_t mask)isn't used as mask by caller, s/mask/bit/ ???
Quoted 7 lines
There will be an use case for mask containing multiple bits, later. My plan is to remove the duplicate "kvmclock" alias from
kvm_feature_name, and call this manually:x86_cpu_register_feature_prop(cpu, "kvmclock",
FEAT_KVM,
(1 <<KVM_FEATURE_CLOCKSOURCE) | (1 <<KVM_FEATURE_CLOCKSOURCE2));
make it mask when it starts to be used as such
Quoted 5 lines
I didn't do that yet because I need the existing
x86_cpu_parse_featurestr()code to keep working until it is converted to useobject_property_set().
[...]
Quoted 23 lines
Quoted 5 lines
target-i386/cpu.c out: · line 2917
Quoted 5 lines
target-i386/cpu.c +5
-0+ op =object_property_find (OBJECT (cpu), prop_name,NULL ); +if (op) { + fp = op->opaque; +assert (fp->word == w); + fp->mask |= mask;^^^ This is the block of code that will be removed once I add the manual "kvmclock" registration call I mentioned above.
Quoted 5 lines
Quoted 5 lines
target-i386/cpu.c +5
-0+ }else { + fp =g_new0 (FeatureProperty,1 ); + fp->word = w; + fp->mask = mask; + object_property_add(OBJECT(cpu), prop_name, "bool",Quoted 8 lines
target-i386/cpu.c out: · line 2929
Quoted 7 lines
target-i386/cpu.c +7
-0+ x86_cpu_get_feature_prop, + x86_cpu_set_feature_prop, + x86_cpu_release_feature_prop, fp, &error_abort); + } +} + +static void x86_cpu_register_feature_bit_props(X86CPU *cpu,this adds 1 property and possibly aliases,
_props()is confusing here.
Quoted 3 lines
Alias properties are still properties like any other, aren't they? The function is still responsible for registering multiple properties. Is the "
_props()" suffix really that confusing?
technically aliases are properties but from user pov it's the same property just with another name.
Quoted 5 lines
Quoted 4 lines
I'd rename it to
x86_cpu_add_feature_bit_prop()and inline abovex86_cpu_register_feature_prop()since it's not going to be reused
Quoted 8 lines
I prefer to keep the single-property function separated, as it may become a generic bitmap property registration function inside generic QOM code later. With your
feature_word_ptrsuggestion, it would be even more generic and non-x86-specific.(To be honest, I would prefer to keep the single-property function registration code clearly separated even if it was never going to be reused anywhere. 20-line functions are already too long for my taste.)
ok
[...]