x86 CPU subclasses, v4
RFC 1/6 target-i386: Move CPU object creation to cpu.c
In reply to Eduardo HabkostAs we will need to create the CPU object after splitting the CPU model string (because we're going to use different subclasses for each CPU model), move the CPU object creation to cpu_x86_register(), and at the same time rename cpu_x86_register() to cpu_x86_create().
This will also simplify the CPU creation code to a trivial cpu_x86_create()+cpu_x86_realize() sequence. This will be useful for code that have to set additional properties before cpu_x86_realize() is called (e.g. the PC CPU initialization code, that needs to set APIC IDs depending on the CPU cores/threads topology).
---
Changes v2:
- Move CPU creation code after cpu_model split (as we will eventually use just the "model name" part to find the right CPU class)
- Small update on comment about feature string before cpu_x86_create() function
--- target-i386/cpu.c | 18 +++++++++++++++--- target-i386/cpu.h | 2 +- target-i386/helper.c | 9 ++------- 3 files changed, 18 insertions(+), 11 deletions(-)
target-i386/cpu.c +15 -3
@@ -1519,8 +1519,13 @@ static void filter_features_for_kvm(X86CPU *cpu) }#endif -int cpu_x86_register (X86CPU *cpu,const char *cpu_model) +/* Create and initialize a X86CPU object, based on the full CPU model string +* (that may include "+feature,-feature,feature=xxx,feature" feature strings) +*/ +X86CPU *cpu_x86_create (const char *cpu_model) { +X86CPU *cpu =NULL ; +CPUX86State *env;x86_def_t def1, *def = &def1;QDict *props =NULL ;Error *error =NULL ;
@@ -1542,6 +1547,10 @@ int cpu_x86_register(X86CPU *cpu, const char *cpu_model) goto out; } + cpu =X86_CPU (object_new (TYPE_X86_CPU )); + env = &cpu->env; + env->cpu_model_str = cpu_model; + def->kvm_features |= kvm_default_features; add_flagname_to_bitmaps("hypervisor", &def->features, &def->ext_features, &def->ext2_features,
@@ -1562,9 +1571,12 @@ out:if (error) {fprintf (stderr,"%s\n" ,error_get_pretty (error));error_free (error); -return -1 ; +if (cpu) { +object_delete (OBJECT (cpu)); + } +return NULL ; } -return 0 ; +return cpu; }#if !defined(CONFIG_USER_ONLY )
target-i386/cpu.h +1 -1
@@ -981,7 +981,7 @@ int cpu_x86_signal_handler(int host_signum, void *pinfo,void cpu_x86_cpuid (CPUX86State *env,uint32_t index,uint32_t count,uint32_t *eax,uint32_t *ebx,uint32_t *ecx,uint32_t *edx); -int cpu_x86_register (X86CPU *cpu,const char *cpu_model); +X86CPU *cpu_x86_create (const char *cpu_model);void cpu_clear_apic_feature (CPUX86State *env);void host_cpuid (uint32_t function,uint32_t count,uint32_t *eax,uint32_t *ebx,uint32_t *ecx,uint32_t *edx);
target-i386/helper.c +2 -7
@@ -1240,15 +1240,10 @@ int cpu_x86_get_descr_debug(CPUX86State *env, unsigned int selector,X86CPU *cpu_x86_init (const char *cpu_model) {X86CPU *cpu; -CPUX86State *env;Error *error =NULL ; - cpu =X86_CPU (object_new (TYPE_X86_CPU )); - env = &cpu->env; - env->cpu_model_str = cpu_model; - -if (cpu_x86_register (cpu, cpu_model) <0 ) { -object_delete (OBJECT (cpu)); + cpu =cpu_x86_create (cpu_model); +if (!cpu) {return NULL ; }
Signature · 1 line
1.7.11.7
RFC 5/6 target-i386: Move kvm_features/hypervisor initialization to cpu_x86_find_by_name()
In reply to Eduardo HabkostMove the initialization to the cpu_x86_find_by_name(), inside the block for predefined CPU models.
That code is not necessary for -cpu "host", because:
- kvm_features doesn't need to be set because kvm_cpu_fill_host() already sets all bits supported by the host.
- the CPUID_EXT_HYPERVISOR flag doesn't need to be manually set because kvm_arch_get_supported_cpuid() already sets it.
--- target-i386/cpu.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
target-i386/cpu.c +9 -6
@@ -1281,6 +1281,15 @@ static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *name)host_cpuid (0 ,0 ,NULL , &ebx, &ecx, &edx);x86cpu_vendor_words2str (x86_cpu_def->vendor, ebx, edx, ecx); } + + x86_cpu_def->kvm_features |= kvm_default_features; +add_flagname_to_bitmaps ("hypervisor" , &x86_cpu_def->features, + &x86_cpu_def->ext_features, + &x86_cpu_def->ext2_features, + &x86_cpu_def->ext3_features, + &x86_cpu_def->kvm_features, + &x86_cpu_def->svm_features, + &x86_cpu_def->cpuid_7_0_ebx_features); }return 0 ;
@@ -1555,12 +1564,6 @@ X86CPU *cpu_x86_create(const char *cpu_model, Error **errp) env = &cpu->env; env->cpu_model_str = cpu_model; - def->kvm_features |= kvm_default_features; -add_flagname_to_bitmaps ("hypervisor" , &def->features, - &def->ext_features, &def->ext2_features, - &def->ext3_features, &def->kvm_features, - &def->svm_features, &def->cpuid_7_0_ebx_features); -cpudef_2_x86_cpu (cpu, def, &error);if (cpu_x86_parse_featurestr (cpu, features, &props) <0 ) {
Signature · 1 line
1.7.11.7
RFC 2/6 target-i386: Make cpu_x86_create() get Error argument
In reply to Eduardo HabkostInstead of forcing the caller to guess what went wrong while creating the CPU object, return error information in a Error argument.
Also, as cpu_x86_create() won't print error messages itself anymore, change cpu_x86_init() to print any error returned by cpu_x86_create() or cpu_x86_realize().
---
Changes v2:
- Fix include "qemu-error.h" to use the new "qemu/error-report.h"
- Remove bogus error_free() call just after error_propagate()
--- target-i386/cpu.c | 5 ++--- target-i386/cpu.h | 2 +- target-i386/helper.c | 21 ++++++++++++++------- 3 files changed, 17 insertions(+), 11 deletions(-)
target-i386/cpu.c +2 -3
@@ -1522,7 +1522,7 @@ static void filter_features_for_kvm(X86CPU *cpu)/* Create and initialize a X86CPU object, based on the full CPU model string * (that may include "+feature,-feature,feature=xxx,feature" feature strings) */ -X86CPU *cpu_x86_create (const char *cpu_model) +X86CPU *cpu_x86_create (const char *cpu_model,Error **errp) {X86CPU *cpu =NULL ;CPUX86State *env;
@@ -1569,8 +1569,7 @@ out:QDECREF (props);g_strfreev (model_pieces);if (error) { -fprintf (stderr,"%s\n" ,error_get_pretty (error)); -error_free (error); +error_propagate (errp, error);if (cpu) {object_delete (OBJECT (cpu)); }
target-i386/cpu.h +1 -1
@@ -981,7 +981,7 @@ int cpu_x86_signal_handler(int host_signum, void *pinfo,void cpu_x86_cpuid (CPUX86State *env,uint32_t index,uint32_t count,uint32_t *eax,uint32_t *ebx,uint32_t *ecx,uint32_t *edx); -X86CPU *cpu_x86_create (const char *cpu_model); +X86CPU *cpu_x86_create (const char *cpu_model,Error **errp);void cpu_clear_apic_feature (CPUX86State *env);void host_cpuid (uint32_t function,uint32_t count,uint32_t *eax,uint32_t *ebx,uint32_t *ecx,uint32_t *edx);
target-i386/helper.c +14 -7
@@ -23,6 +23,7 @@#include "sysemu/sysemu.h" #include "monitor/monitor.h" #endif +#include "qemu/error-report.h" //#define DEBUG_MMU
@@ -1239,21 +1240,27 @@ int cpu_x86_get_descr_debug(CPUX86State *env, unsigned int selector,X86CPU *cpu_x86_init (const char *cpu_model) { -X86CPU *cpu; +X86CPU *cpu =NULL ;Error *error =NULL ; - cpu =cpu_x86_create (cpu_model); -if (!cpu) { -return NULL ; + cpu =cpu_x86_create (cpu_model, &error); +if (error) { + goto error; }x86_cpu_realize (OBJECT (cpu), &error);if (error) { -error_free (error); -object_delete (OBJECT (cpu)); -return NULL ; + goto error; }return cpu; + +error: +if (cpu) { +object_delete (OBJECT (cpu)); + } +error_report ("%s" ,error_get_pretty (error)); +error_free (error); +return NULL ; }#if !defined(CONFIG_USER_ONLY )
Signature · 1 line
1.7.11.7
RFC 3/6 target-i386: Simplify cpu_x86_find_by_name() logic
In reply to Eduardo HabkostMove the check for "host" to beginning of function, so instead of a confusing if/else-if/else mess we now have just two obvious if/else blocks:
1) Special case for "host";
2) General case for CPU model lookup on x86_defs list.
This way, we will be able to easily move those two parts to separate class instance_init functions.
--- target-i386/cpu.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-)
target-i386/cpu.c +12 -8
@@ -1250,20 +1250,24 @@ static void cpudef_2_x86_cpu(X86CPU *cpu, x86_def_t *def, Error **errp)static int cpu_x86_find_by_name (x86_def_t *x86_cpu_def,const char *name) { -x86_def_t *def; -for (def = x86_defs; def; def = def->next) { -if (name && !strcmp (name, def->name)) { -break ; - } - }if (kvm_enabled () && name &&strcmp (name,"host" ) ==0 ) {#ifdef CONFIG_KVM kvm_cpu_fill_host (x86_cpu_def);#endif - }else if (!def) { -return -1 ; }else { +x86_def_t *def; + +for (def = x86_defs; def; def = def->next) { +if (name && !strcmp (name, def->name)) { +break ; + } + } + +if (!def) { +return -1 ; + } +memcpy (x86_cpu_def, def,sizeof (*def));/* sysenter isn't supported on compatibility mode on AMD, syscall * isn't supported in compatibility mode on Intel.
Signature · 1 line
1.7.11.7
RFC 4/6 target-i386: Set feature string parsing results directly on CPU object
In reply to Eduardo HabkostNote that this is temporary: just like we would eventually kill usage of x86_def_t inside cpu_x86_parse_featurestr(), we will eventually kill usage of X86CPU inside that function as well.
This will help us keep the x86_def_t usage restricted to the CPU object creation code.
--- target-i386/cpu.c | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-)
target-i386/cpu.c +19 -18
@@ -1303,7 +1303,7 @@ static void cpu_x86_set_props(X86CPU *cpu, QDict *features, Error **errp)/* Parse "+feature,-feature,feature=foo" CPU feature string */ -static int cpu_x86_parse_featurestr (x86_def_t *x86_cpu_def,char *features, +static int cpu_x86_parse_featurestr (X86CPU *cpu,char *features,QDict **props) {char *featurestr;/* Single 'key=value" string being parsed */
@@ -1374,7 +1374,7 @@ static int cpu_x86_parse_featurestr(x86_def_t *x86_cpu_def, char *features,fprintf (stderr,"bad numerical value %s\n" , val); goto error; } - x86_cpu_def->tsc_khz = tsc_freq /1000 ; + cpu->env.tsc_khz = tsc_freq /1000 ; }else if (!strcmp (featurestr,"hv_spinlocks" )) {char *err; numvalue =strtoul (val, &err,0 );
@@ -1400,20 +1400,20 @@ static int cpu_x86_parse_featurestr(x86_def_t *x86_cpu_def, char *features, goto error; } } - x86_cpu_def->features |= plus_features; - x86_cpu_def->ext_features |= plus_ext_features; - x86_cpu_def->ext2_features |= plus_ext2_features; - x86_cpu_def->ext3_features |= plus_ext3_features; - x86_cpu_def->kvm_features |= plus_kvm_features; - x86_cpu_def->svm_features |= plus_svm_features; - x86_cpu_def->cpuid_7_0_ebx_features |= plus_7_0_ebx_features; - x86_cpu_def->features &= ~minus_features; - x86_cpu_def->ext_features &= ~minus_ext_features; - x86_cpu_def->ext2_features &= ~minus_ext2_features; - x86_cpu_def->ext3_features &= ~minus_ext3_features; - x86_cpu_def->kvm_features &= ~minus_kvm_features; - x86_cpu_def->svm_features &= ~minus_svm_features; - x86_cpu_def->cpuid_7_0_ebx_features &= ~minus_7_0_ebx_features; + cpu->env.cpuid_features |= plus_features; + cpu->env.cpuid_ext_features |= plus_ext_features; + cpu->env.cpuid_ext2_features |= plus_ext2_features; + cpu->env.cpuid_ext3_features |= plus_ext3_features; + cpu->env.cpuid_kvm_features |= plus_kvm_features; + cpu->env.cpuid_svm_features |= plus_svm_features; + cpu->env.cpuid_7_0_ebx_features |= plus_7_0_ebx_features; + cpu->env.cpuid_features &= ~minus_features; + cpu->env.cpuid_ext_features &= ~minus_ext_features; + cpu->env.cpuid_ext2_features &= ~minus_ext2_features; + cpu->env.cpuid_ext3_features &= ~minus_ext3_features; + cpu->env.cpuid_kvm_features &= ~minus_kvm_features; + cpu->env.cpuid_svm_features &= ~minus_svm_features; + cpu->env.cpuid_7_0_ebx_features &= ~minus_7_0_ebx_features;g_strfreev (feat_array);return 0 ;
@@ -1561,12 +1561,13 @@ X86CPU *cpu_x86_create(const char *cpu_model, Error **errp) &def->ext3_features, &def->kvm_features, &def->svm_features, &def->cpuid_7_0_ebx_features); -if (cpu_x86_parse_featurestr (def, features, &props) <0 ) { +cpudef_2_x86_cpu (cpu, def, &error); + +if (cpu_x86_parse_featurestr (cpu, features, &props) <0 ) {error_setg (&error,"Invalid cpu_model string format: %s" , cpu_model); goto out; } -cpudef_2_x86_cpu (cpu, def, &error);cpu_x86_set_props (cpu, props, &error); out:
Signature · 1 line
1.7.11.7
cover x86 CPU subclasses, v4
This is a new RFC for the x86 CPU subclasses. This version handles the "host" subclass differently, and I believe this made the code much simpler.
However, a few tricks were necessary:
- To keep the "default_kvm_features" compat functions working (this shouldn't be necessary once we start using global properties for the compat code).
- For the vendor property setting (because it depends on a kvm_enabled() check, that can't be made on class_init). I don't know how we will be able to convert that logic using global properties/defaults in the future.
- To keep the original model names in the "-cpu ?" output, for compatibility.
This series depends on multiple series:
- pc-1.4 kvm_mmu_opt disable series I sent today;
- -cpu "enforce" fix series I sent today;
- A experimental rebased version of Igor's "wave 2" x86 CPU init cleanup.
Because of the tricky dependencies, I recommend you use the git tree for testing:
git://github.com/ehabkost/qemu-hacks.git x86-cpu-model-classes.RFC.v4
https://github.com/ehabkost/qemu-hacks/tree/x86-cpu-model-classes.RFC.v4
Eduardo Habkost (6):
target-i386: Move CPU object creation to cpu.c
target-i386: Make cpu_x86_create() get Error argument
target-i386: Simplify cpu_x86_find_by_name() logic
target-i386: Set feature string parsing results directly on CPU
object
target-i386: Move kvm_features/hypervisor initialization to
cpu_x86_find_by_name()
target-i386: CPU model subclasses
include/sysemu/kvm.h | 3 + target-i386/cpu-qom.h | 26 + target-i386/cpu.c | 1548 ++++++++++++++++++++++++++++++------------------- target-i386/cpu.h | 4 +- target-i386/helper.c | 26 +- target-i386/kvm.c | 2 + 6 files changed, 988 insertions(+), 621 deletions(-)
Signature · 1 line
1.7.11.7
RFC 6/6 target-i386: CPU model subclasses
This requires a small hack for -cpu host: the definition of -cpu host requires KVM to be initialized, so there's a small hook added to kvm_arch_init() so that the full CPU model data can be added to the "host" CPU class after KVM is initialized, and a kvm_required field was added to X86CPUClass to make sure we won't try to use the "host" CPU model if KVM is not initialized.
The x86_def_t structs were mechanically translated into class_init functions for each class. Note that the feature bit initialization uses "|=" so it will be easier to change that code to reuse definitions set in other functions or other classes.
The qemu_get_version() hacks inside x86_cpudef_setup() were moved to the class_init functions of qemu64, qemu32, and athlon CPU models.
The additional feature initialization steps inside cpu_x86_find_by_name() were moved as follows:
- The KVM-specific vendor string initialization is now inside x86_cpu_initfn(), as it requires a kvm_enabled() check.
- The setting of the "hypervisor" flag is now inside the common x86_cpu_class_init() function.
- The initialization of kvm_features is a bit complicated, because:
- kvm_default_features may be changed by the machine init functions,
that may be called after class_init is called;
- The "host" CPU model doesn't use kvm_default_features.
The solution is to make kvm_features a pointer. Most CPU models will point to &kvm_default_features, but the "host" CPU model will point to another static variable that will carry the host CPU features. The pointer won't be necessary anymore once we start using global properties for the KVM features machine-type compatibility code.
The "-cpu ?" output is not printing the full class name to not break the libvirt parsing.
--- include/sysemu/kvm.h | 3 + target-i386/cpu-qom.h | 26 + target-i386/cpu.c | 1505 ++++++++++++++++++++++++++++++------------------- target-i386/cpu.h | 2 - target-i386/kvm.c | 2 + 5 files changed, 942 insertions(+), 596 deletions(-)
include/sysemu/kvm.h +3 -0
@@ -131,6 +131,9 @@ void *kvm_arch_vmalloc(ram_addr_t size);void kvm_setup_guest_memory (void *start,size_t size);void kvm_flush_coalesced_mmio_buffer (void ); + +void x86_cpu_finish_host_class_init (KVMState *s); +#endif int kvm_insert_breakpoint(CPUArchState *current_env, target_ulong addr,
target-i386/cpu-qom.h +26 -0
@@ -49,6 +49,32 @@ typedef struct X86CPUClass {/*< public >*/ void (*parent_reset)(CPUState *cpu); + +/* Original model name for -cpu ? listing */ +const char *model_name; + +/* CPU model definition information: */ +uint32_t level; +char vendor[CPUID_VENDOR_SZ +1 ]; +int family; +int model; +int stepping; +int tsc_khz; +uint32_t features, ext_features, ext2_features, ext3_features; +/* KVM features are a pointer because currently we change the default +* set of features using compatibility functions on the machine init +* function, and those may be called after class_init. +*/ +uint32_t *kvm_features; +uint32_t svm_features; +uint32_t xlevel; +char model_id[48 ]; +/* Store the results of Centaur's CPUID instructions */ +uint32_t ext4_features; +uint32_t xlevel2; +/* The feature bits on CPUID[EAX=7,ECX=0].EBX */ +uint32_t cpuid_7_0_ebx_features; +bool kvm_required; } X86CPUClass;/**
target-i386/cpu.c +911 -594
1,727 lines not on this page · raw message
target-i386/cpu.h +0 -2
@@ -866,7 +866,6 @@ typedef struct CPUX86State {X86CPU *cpu_x86_init (const char *cpu_model);int cpu_x86_exec (CPUX86State *s);void x86_cpu_list (FILE *f,fprintf_function cpu_fprintf); -void x86_cpudef_setup (void );int cpu_x86_support_mca_broadcast (CPUX86State *env);int cpu_get_pic_interrupt (CPUX86State *s);
@@ -1048,7 +1047,6 @@ static inline CPUX86State *cpu_init(const char *cpu_model)#define cpu_gen_code cpu_x86_gen_code#define cpu_signal_handler cpu_x86_signal_handler#define cpu_list x86_cpu_list -#define cpudef_setup x86_cpudef_setup#define CPU_SAVE_VERSION 12
target-i386/kvm.c +2 -0
@@ -767,6 +767,8 @@ int kvm_arch_init(KVMState *s) } } } + +x86_cpu_finish_host_class_init (s);return 0 ; }
Signature · 1 line
1.7.11.7
Quoted 12 lines · Eduardo Habkost
This is a new RFC for the x86 CPU subclasses. This version handles the "host" subclass differently, and I believe this made the code much simpler.
However, a few tricks were necessary:
- To keep the "
default_kvm_features" compat functions working (this shouldn't be necessary once we start using global properties for the compat code).
- For the vendor property setting (because it depends on akvm_enabled()check, that can't be made onclass_init). I don't know how we will be able to convert that logic using global properties/defaults in the future.
- To keep the original model names in the "-cpu ?" output, for compatibility.
Quoted 8 lines
This series depends on multiple series:
- pc-1.4kvm_mmu_optdisable series I sent today;
- -cpu "enforce" fix series I sent today;
- A experimental rebased version of Igor's "wave 2" x86 CPU init cleanup.Because of the tricky dependencies, I recommend you use the git tree for testing:
git://github.com/ehabkost/qemu-hacks.gitx86-cpu-model-classes.RFC.v4
https://github.com/ehabkost/qemu-hacks/tree/x86-cpu-model-classes.RFC.v4
I submitted newer versions of the dependencies. There's a git branch of this RFC rebased on top of the newer dependencies, here:
git://github.com/ehabkost/qemu-hacks.git x86-cpu-model-classes.RFC.v5
https://github.com/ehabkost/qemu-hacks/tree/x86-cpu-model-classes.RFC.v5
Quoted 23 lines
Eduardo Habkost (6):
target-i386: Move CPU object creation tocpu.c
target-i386: Makecpu_x86_create()get Error argument
target-i386: Simplifycpu_x86_find_by_name()logic
target-i386: Set feature string parsing results directly on CPU
object
target-i386: Move kvm_features/hypervisor initialization tocpu_x86_find_by_name()target-i386: CPU model subclasses
include/sysemu/kvm.h | 3 + target-i386/cpu-qom.h | 26 + target-i386/cpu.c | 1548 ++++++++++++++++++++++++++++++------------------- target-i386/cpu.h | 4 +- target-i386/helper.c | 26 +- target-i386/kvm.c | 2 + 6 files changed, 988 insertions(+), 621 deletions(-)Signature · 1 line
1.7.11.7
Signature · 1 line
Eduardo
Re: [Qemu-devel] [RFC 6/6] target-i386: CPU model subclasses
In reply to Eduardo Habkosttarget-i386/cpu-qom.h typedef struct X86CPUClass { · line 54
Quoted 2 lines · Eduardo Habkost
target-i386/cpu-qom.h +2
-0+/* Original model name for -cpu ? listing */ +const char *model_name;
This is a leftover of a test I was making. It will be removed.
Signature · 1 line
Eduardo