short x86 CPU init cleanup (v2)
cover short x86 CPU init cleanup (v2)
Changes v1 -> v2:
- Coding style changes (patches 1-2)
- Use "return -1" directly instead of "goto error" on cpu_x86_find_by_name() (patch 2)
This series is based on the qom-cpu branch from afaerber's tree. The full branch can be found in a git tree, at:
git://github.com/ehabkost/qemu-hacks.git cpu-init-cleanup-short-v2
https://github.com/ehabkost/qemu-hacks/tree/cpu-init-cleanup-short-v2
References to previous versions:
v1: http://article.gmane.org/gmane.comp.emulators.qemu/182713
previous (long) cleanup series:
http://article.gmane.org/gmane.comp.emulators.qemu/180137
Eduardo Habkost (2):
target-i386/cpu.c: coding style fixes
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 | 103 ++++++++++++++++++++++++++++---------------- target-i386/cpu.h | 2 + 5 files changed, 102 insertions(+), 38 deletions(-)
Signature · 1 line
1.7.11.7
PATCH 1/6 target-i386/cpu.c: coding style fixes
- Use spaces instead of tabs on cpu_x86_cpuid().
- Use braces on 'if' statement cpu_x86_find_by_name().
--- target-i386/cpu.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-)
target-i386/cpu.c +15 -13
@@ -1227,9 +1227,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)) +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) {
@@ -1835,17 +1837,17 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, }break ;case 0x8000000A : -if (env->cpuid_ext3_features &CPUID_EXT3_SVM ) { - *eax =0x00000001 ;/* SVM Revision */ - *ebx =0x00000010 ;/* nr of ASIDs */ - *ecx =0 ; - *edx = env->cpuid_svm_features;/* optional features */ - }else { - *eax =0 ; - *ebx =0 ; - *ecx =0 ; - *edx =0 ; - } +if (env->cpuid_ext3_features &CPUID_EXT3_SVM ) { + *eax =0x00000001 ;/* SVM Revision */ + *ebx =0x00000010 ;/* nr of ASIDs */ + *ecx =0 ; + *edx = env->cpuid_svm_features;/* optional features */ + }else { + *eax =0 ; + *ebx =0 ; + *ecx =0 ; + *edx =0 ; + }break ;case 0xC0000000 : *eax = env->cpuid_xlevel2;
Signature · 1 line
1.7.11.7
PATCH 2/6 target-i386: cpu: separate feature string parsing from CPU model lookup
In reply to Eduardo HabkostInstead 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).
---
Changes v1 -> v2:
- Coding style changes
- Replace "goto error" with "return -1"
--- target-i386/cpu.c | 67 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 22 deletions(-)
target-i386/cpu.c +45 -22
@@ -1208,25 +1208,10 @@ 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,"," ); -/* Features to be added*/ -uint32_t plus_features =0 , plus_ext_features =0 ; -uint32_t plus_ext2_features =0 , plus_ext3_features =0 ; -uint32_t plus_kvm_features = kvm_default_features, plus_svm_features =0 ; -uint32_t plus_7_0_ebx_features =0 ; -/* Features to be removed */ -uint32_t minus_features =0 , minus_ext_features =0 ; -uint32_t minus_ext2_features =0 , minus_ext3_features =0 ; -uint32_t minus_kvm_features =0 , minus_svm_features =0 ; -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 ;
@@ -1235,16 +1220,37 @@ static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)if (kvm_enabled () && name &&strcmp (name,"host" ) ==0 ) {kvm_cpu_fill_host (x86_cpu_def); }else if (!def) { - goto error; +return -1 ; }else {memcpy (x86_cpu_def, def,sizeof (*def)); } +return 0 ; +} + +/* 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 ; +uint32_t plus_kvm_features = kvm_default_features, plus_svm_features =0 ; +uint32_t plus_7_0_ebx_features =0 ; +/* Features to be removed */ +uint32_t minus_features =0 , minus_ext_features =0 ; +uint32_t minus_ext2_features =0 , minus_ext3_features =0 ; +uint32_t minus_kvm_features =0 , minus_svm_features =0 ; +uint32_t minus_7_0_ebx_features =0 ; +uint32_t numvalue; +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;
@@ -1378,11 +1384,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 ; }
@@ -1492,11 +1496,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;
@@ -1555,7 +1573,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 )
Signature · 1 line
1.7.11.7
Re: [Qemu-devel] [PATCH 2/6] target-i386: cpu: separate feature string parsing from CPU model lookup
Quoted 135 lines · On Tue, 4 Dec 2012 16:58:19 -0200 Eduardo Habkost <[email protected]> wrote:
Instead of using parsing the whole
cpu_modelstring insidecpu_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 <[email protected]>---
Changes v1 -> v2:
- Coding style changes
- Replace "goto error" with "return -1"--- target-i386/cpu.c | 67 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 22 deletions(-)target-i386/cpu.c +40
-22@@ -1208,25 +1208,10 @@ 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,"," ); -/* Features to be added*/ -uint32_t plus_features =0 , plus_ext_features =0 ; -uint32_t plus_ext2_features =0 , plus_ext3_features =0 ; -uint32_t plus_kvm_features = kvm_default_features, plus_svm_features =0 ; -uint32_t plus_7_0_ebx_features =0 ; -/* Features to be removed */ -uint32_t minus_features =0 , minus_ext_features =0 ; -uint32_t minus_ext2_features =0 , minus_ext3_features =0 ; -uint32_t minus_kvm_features =0 , minus_svm_features =0 ; -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 ;@@ -1235,16 +1220,37 @@ static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)if (kvm_enabled () && name &&strcmp (name,"host" ) ==0 ) {kvm_cpu_fill_host (x86_cpu_def); }else if (!def) { - goto error; +return -1 ; }else {memcpy (x86_cpu_def, def,sizeof (*def)); } +return 0 ; +} + +/* 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 ; +uint32_t plus_kvm_features = kvm_default_features, plus_svm_features =0 ; +uint32_t plus_7_0_ebx_features =0 ; +/* Features to be removed */ +uint32_t minus_features =0 , minus_ext_features =0 ; +uint32_t minus_ext2_features =0 , minus_ext3_features =0 ; +uint32_t minus_kvm_features =0 , minus_svm_features =0 ; +uint32_t minus_7_0_ebx_features =0 ; +uint32_t numvalue; +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;@@ -1378,11 +1384,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 ; }@@ -1492,11 +1496,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;
target-i386/cpu.c int cpu_x86_register(X86CPU *cpu, const char *cpu_model) · line 1573
Quoted 1 line
target-i386/cpu.c +0
-0@@ -1555,7 +1573,12 @@ int cpu_x86_register(X86CPU *cpu, const char *cpu_model)error_free (error);
model_pieces is leaked,
s/return -1/goto error/
Quoted 15 lines
target-i386/cpu.c +5
-0return -1 ; } + +g_strfreev (model_pieces);return 0 ; +error: +g_strfreev (model_pieces); +return -1 ; }#if !defined(CONFIG_USER_ONLY )Signature · 1 line
1.7.11.7
Signature · 2 lines
Regards, Igor
PATCH 3/6 target-i386: use define for cpuid vendor string size
In reply to Eduardo HabkostFrom: Igor Mammedov <[email protected]>
--- target-i386/cpu.c | 6 +++--- target-i386/cpu.h | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-)
target-i386/cpu.c +3 -3
@@ -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 ;
target-i386/cpu.h +2 -0
@@ -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" */
Signature · 1 line
1.7.11.7
PATCH 4/6 target-i386: postpone cpuid_level update to realize time
In reply to Eduardo HabkostFrom: Igor Mammedov <[email protected]>
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().
--- target-i386/cpu.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
target-i386/cpu.c +5 -3
@@ -1381,9 +1381,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:
@@ -2074,6 +2071,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);
Signature · 1 line
1.7.11.7
PATCH 5/6 add visitor for parsing hz[KMG] input string
In reply to Eduardo HabkostFrom: Igor Mammedov <[email protected]>
--- qapi/qapi-visit-core.c | 11 +++++++++++ qapi/qapi-visit-core.h | 2 ++ qapi/string-input-visitor.c | 22 ++++++++++++++++++++++ 3 files changed, 35 insertions(+)
qapi/qapi-visit-core.c +11 -0
@@ -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); + } + } +}
qapi/qapi-visit-core.h +2 -0
@@ -62,6 +62,7 @@ struct Visitorvoid (*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
qapi/string-input-visitor.c +22 -0
@@ -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;
Signature · 1 line
1.7.11.7
PATCH 6/6 target-i386: use visit_type_hz to parse tsc_freq property value
In reply to Eduardo HabkostFrom: Igor Mammedov <[email protected]>
--- target-i386/cpu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target-i386/cpu.c +1 -1
@@ -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 ; }
Signature · 1 line
1.7.11.7