target-i386: Implement query-cpu-model-expansion
v2 cover target-i386: Implement query-cpu-model-expansion
This series implements query-cpu-model-expansion on target-i386.
Changes v1 -> v2:
-----------------
This version is highly simplified compared to v1. It contains only an implementation that will return a limited set of properties. I have a follow-up series that will expend type=full expansion to return every single QOM property, but this version will return the same data for type=static and type=full expansion for simplicity (except that type=static expansion will use the "base" CPU model as base).
This means this version also won't include "pmu" and "host-cache-info" in full expansion, and won't require special code for those properties.
The unit test code was also removed in this version, to keep the series simple and easier to review. Most of the patches on the previous series were changes just to make the test case work. I will send the test-case-related changes as a follow-up series.
---
Cc: Cornelia Huck <[email protected]>
Cc: Christian Borntraeger <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: [email protected]
Cc: Jiri Denemark <[email protected]>
Cc: "Jason J. Herne" <[email protected]>
Cc: Markus Armbruster <[email protected]>
Cc: Richard Henderson <[email protected]>
Cc: Igor Mammedov <[email protected]>
Cc: Eric Blake <[email protected]>
Eduardo Habkost (4):
target-i386: Reorganize and document CPUID initialization steps
qapi-schema: Comment about full expansion of non-migration-safe models
target-i386: Define static "base" CPU model
target-i386: Implement query-cpu-model-expansion QMP command
qapi-schema.json | 9 ++ target/i386/cpu-qom.h | 2 + monitor.c | 4 +- target/i386/cpu.c | 317 ++++++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 298 insertions(+), 34 deletions(-)
Signature · 1 line
2.11.0.259.g40922b1
PATCH v2 1/4 target-i386: Reorganize and document CPUID initialization steps
CPU runnability checks and CPU model expansion have slightly different requirements. Document the steps involved in loading a CPU model and realizing a CPU, so their requirements and purpose are clearly defined.
This patch doesn't change any implementation. It just add comments, rename the x86_cpu_load_features() function for clarity (so it won't be confused with x86_cpu_load_def()), and move x86_cpu_filter_features() closer to it.
---
Changes v1 -> v2:
* Small reword of comments
--- target/i386/cpu.c | 102 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 71 insertions(+), 31 deletions(-)
target/i386/cpu.c +71 -31
@@ -2060,7 +2060,7 @@ static void x86_cpu_parse_featurestr(const char *typename, char *features, } } -static void x86_cpu_load_features (X86CPU *cpu,Error **errp); +static void x86_cpu_expand_features (X86CPU *cpu,Error **errp);static int x86_cpu_filter_features (X86CPU *cpu);/* Check for missing features that may prevent the CPU class from
@@ -2076,9 +2076,9 @@ static void x86_cpu_class_check_missing_features(X86CPUClass *xcc, xc =X86_CPU (object_new (object_class_get_name (OBJECT_CLASS (xcc)))); -x86_cpu_load_features (xc, &err); +x86_cpu_expand_features (xc, &err);if (err) { -/* Errors at x86_cpu_load_features should never happen, +/* Errors at x86_cpu_expand_features should never happen, * but in case it does, just report the model as not * runnable at all using the "type" property. */
@@ -2239,31 +2239,6 @@ static uint32_t x86_cpu_get_supported_feature_word(FeatureWord w,return r; } -/* -* Filters CPU feature words based on host availability of each feature. -* -* Returns: 0 if all flags are supported by the host, non-zero otherwise. -*/ -static int x86_cpu_filter_features (X86CPU *cpu) -{ -CPUX86State *env = &cpu->env; -FeatureWord w; -int rv =0 ; - -for (w =0 ; w <FEATURE_WORDS ; w++) { -uint32_t host_feat = -x86_cpu_get_supported_feature_word (w, false); -uint32_t requested_features = env->features[w]; - env->features[w] &= host_feat; - cpu->filtered_features[w] = requested_features & ~env->features[w]; -if (cpu->filtered_features[w]) { - rv =1 ; - } - } - -return rv; -} -static void x86_cpu_report_filtered_features (X86CPU *cpu) {FeatureWord w;
@@ -3089,8 +3064,47 @@ static void x86_cpu_enable_xsave_components(X86CPU *cpu) env->features[FEAT_XSAVE_COMP_HI ] = mask >>32 ; } -/* Load CPUID data based on configured features */ -static void x86_cpu_load_features (X86CPU *cpu,Error **errp) +/***** Steps involved on loading and filtering CPUID data +* +* When initializing and realizing a CPU object, the steps +* involved in setting up CPUID data are: +* +* 1) Loading CPU model definition (X86CPUDefinition). This is +* implemented by x86_cpu_load_def() and should be completely +* transparent, as it is done automatically by instance_init. +* No code should need to look at X86CPUDefinition structs +* outside instance_init. +* +* 2) CPU expansion. This is done by realize before CPUID +* filtering, and will make sure host/accelerator data is +* loaded for CPU models that depend on host capabilities +* (e.g. "host"). Done by x86_cpu_expand_features(). +* +* 3) CPUID filtering. This initializes extra data related to +* CPUID, and checks if the host supports all capabilities +* required by the CPU. Runnability of a CPU model is +* determined at this step. Done by x86_cpu_filter_features(). +* +* Some operations don't require all steps to be performed. +* More precisely: +* +* - CPU instance creation (instance_init) will run only CPU +* model loading. CPU expansion can't run at instance_init-time +* because host/accelerator data may be not available yet. +* - CPU realization will perform both CPU model expansion and CPUID +* filtering, and return an error in case one of them fails. +* - query-cpu-definitions needs to run all 3 steps. It needs +* to run CPUID filtering, as the 'unavailable-features' +* field is set based on the filtering results. +* - The query-cpu-model-expansion QMP command only needs to run +* CPU model loading and CPU expansion. It should not filter +* any CPUID data based on host capabilities. +*/ + +/* Expand CPU configuration data, based on configured features +* and host/accelerator capabilities when appropriate. +*/ +static void x86_cpu_expand_features (X86CPU *cpu,Error **errp) {CPUX86State *env = &cpu->env;FeatureWord w;
@@ -3167,6 +3181,32 @@ out: } } +/* +* Finishes initialization of CPUID data, filters CPU feature +* words based on host availability of each feature. +* +* Returns: 0 if all flags are supported by the host, non-zero otherwise. +*/ +static int x86_cpu_filter_features (X86CPU *cpu) +{ +CPUX86State *env = &cpu->env; +FeatureWord w; +int rv =0 ; + +for (w =0 ; w <FEATURE_WORDS ; w++) { +uint32_t host_feat = +x86_cpu_get_supported_feature_word (w, false); +uint32_t requested_features = env->features[w]; + env->features[w] &= host_feat; + cpu->filtered_features[w] = requested_features & ~env->features[w]; +if (cpu->filtered_features[w]) { + rv =1 ; + } + } + +return rv; +} +#define IS_INTEL_CPU (env) ((env)->cpuid_vendor1 == CPUID_VENDOR_INTEL_1 && \ (env)->cpuid_vendor2 == CPUID_VENDOR_INTEL_2 && \ (env)->cpuid_vendor3 == CPUID_VENDOR_INTEL_3)
@@ -3187,7 +3227,7 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)return ; } -x86_cpu_load_features (cpu, &local_err); +x86_cpu_expand_features (cpu, &local_err);if (local_err) { goto out; }
Signature · 1 line
2.11.0.259.g40922b1
Eduardo Habkost
AppliedRe: [Qemu-devel] [PATCH v2 1/4] target-i386: Reorganize and document CPUID initialization steps
Quoted 11 lines · Eduardo Habkost
CPU runnability checks and CPU model expansion have slightly different requirements. Document the steps involved in loading a CPU model and realizing a CPU, so their requirements and purpose are clearly defined.
This patch doesn't change any implementation. It just add comments, rename the
x86_cpu_load_features()function for clarity (so it won't be confused withx86_cpu_load_def()), and movex86_cpu_filter_features()closer to it.Signed-off-by Eduardo Habkost <[email protected]>
Applied to x86-next.
Signature · 1 line
Eduardo
PATCH v2 2/4 qapi-schema: Comment about full expansion of non-migration-safe models
In reply to Eduardo HabkostAdd a note warning that static expansion may not be 100% accurate when the CPU model is not migration-safe. This will be the case on x86 when expansing the "host" CPU model, because there are "host" features that can't have a migration-safe representation (e.g. "host-cache-info").
--- qapi-schema.json | 9 +++++++++ 1 file changed, 9 insertions(+)
qapi-schema.json +9 -0
@@ -3294,6 +3294,15 @@ # migration-safe, but allows tooling to get an insight and work with # model details. # +# Note: When a non-migration-safe CPU model is expanded in static mode, some +# features enabled by the CPU model may be omitted, because they can't be +# implemented by a static CPU model definition (e.g. cache info passthrough and +# PMU passthrough in x86). If you need an accurate representation of the +# features enabled by a non-migration-safe CPU model, use @full. If you need a +# static representation that will keep ABI compatibility even when changing QEMU +# version or machine-type, use @static (but keep in mind that some features may +# be omitted). +# # Since: 2.8.0 ## { 'enum': 'CpuModelExpansionType',
Signature · 1 line
2.11.0.259.g40922b1
Eduardo Habkost
AppliedRe: [Qemu-devel] [PATCH v2 2/4] qapi-schema: Comment about full expansion of non-migration-safe models
Quoted 7 lines · Eduardo Habkost
Add a note warning that static expansion may not be 100% accurate when the CPU model is not migration-safe. This will be the case on x86 when expansing the "host" CPU model, because there are "host" features that can't have a migration-safe representation (e.g. "host-cache-info").
Signed-off-by Eduardo Habkost <[email protected]>
Applied to x86-next.
Quoted 28 lines
--- qapi-schema.json | 9 +++++++++ 1 file changed, 9 insertions(+)qapi-schema.json +9
-0@@ -3294,6 +3294,15 @@ # migration-safe, but allows tooling to get an insight and work with # model details. # +# Note: When a non-migration-safe CPU model is expanded in static mode, some +# features enabled by the CPU model may be omitted, because they can't be +# implemented by a static CPU model definition (e.g. cache info passthrough and +# PMU passthrough in x86). If you need an accurate representation of the +# features enabled by a non-migration-safe CPU model, use @full. If you need a +# static representation that will keep ABI compatibility even when changing QEMU +# version or machine-type, use @static (but keep in mind that some features may +# be omitted). +# # Since: 2.8.0 ## { 'enum': 'CpuModelExpansionType',Signature · 1 line
2.11.0.259.g40922b1
Signature · 1 line
Eduardo
PATCH v2 3/4 target-i386: Define static "base" CPU model
In reply to Eduardo HabkostThe query-cpu-model-expand QMP command needs at least one static model, to allow the "static" expansion mode to be implemented. Instead of defining static versions of every CPU model, define a "base" CPU model that has absolutely no feature flag enabled.
Despite having no CPUID data set at all, "-cpu base" is even a functional CPU:
* It can boot a Slackware Linux 1.01 image with a Linux 0.99.12 kernel[1].
* It is even possible to boot[2] a modern Fedora x86_64 guest by manually enabling the following CPU features:
-cpu base,+lm,+msr,+pae,+fpu,+cx8,+cmov,+sse,+sse2,+fxsr
[1] http://www.qemu-advent-calendar.org/2014/#day-1
[2] This is what can be seen in the guest:
[root@localhost ~]# cat /proc/cpuinfo
processor : 0
vendor_id : unknown
cpu family : 0
model : 0
model name : 00/00
stepping : 0
physical id : 0
siblings : 1
core id : 0
cpu cores : 1
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 1
wp : yes
flags : fpu msr pae cx8 cmov fxsr sse sse2 lm nopl
bugs :
bogomips : 5832.70
clflush size : 64
cache_alignment : 64
address sizes : 36 bits physical, 48 bits virtual
power management:
[root@localhost ~]# x86info -v -a
x86info v1.30. Dave Jones 2001-2011
Feedback to <[email protected]>.
No TSC, MHz calculation cannot be performed.
Unknown vendor (0)
MP Table:
Family: 0 Model: 0 Stepping: 0
CPU Model (x86info's best guess):
eax in: 0x00000000, eax = 00000001 ebx = 00000000 ecx = 00000000 edx = 00000000 eax in: 0x00000001, eax = 00000000 ebx = 00000800 ecx = 00000000 edx = 07008161
eax in: 0x80000000, eax = 80000001 ebx = 00000000 ecx = 00000000 edx = 00000000 eax in: 0x80000001, eax = 00000000 ebx = 00000000 ecx = 00000000 edx = 20000000
Feature flags:
fpu Onboard FPU
msr Model-Specific Registers
pae Physical Address Extensions
cx8 CMPXCHG8 instruction
cmov CMOV instruction
fxsr FXSAVE and FXRSTOR instructions
sse SSE support
sse2 SSE2 support
Long NOPs supported: yes
Address sizes : 0 bits physical, 0 bits virtual 0MHz processor (estimate).
running at an estimated 0MHz
[root@localhost ~]#
--- target/i386/cpu-qom.h | 2 ++ target/i386/cpu.c | 24 +++++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-)
target/i386/cpu-qom.h +2 -0
@@ -49,6 +49,7 @@ typedef struct X86CPUDefinition X86CPUDefinition;* @cpu_def: CPU model definition * @ordering: Ordering on the "-cpu help" CPU model list. * @migration_safe: See CpuDefinitionInfo::migration_safe +* @static_model: See CpuDefinitionInfo::static * @parent_realize: The parent class' realize handler. * @parent_reset: The parent class' reset handler. *
@@ -64,6 +65,7 @@ typedef struct X86CPUClass {int ordering;bool migration_safe; +bool static_model;/* Optional description of CPU model. * If unavailable, cpu_def->model_id is used */
target/i386/cpu.c +23 -1
@@ -2202,6 +2202,7 @@ static void x86_cpu_definition_entry(gpointer data, gpointer user_data) info->q_typename =g_strdup (object_class_get_name (oc)); info->migration_safe = cc->migration_safe; info->has_migration_safe = true; + info->q_static = cc->static_model; entry =g_malloc0 (sizeof (*entry)); entry->value = info;
@@ -3596,7 +3597,9 @@ static void x86_cpu_initfn(Object *obj)object_property_add_alias (obj,"sse4_1" , obj,"sse4.1" , &error_abort);object_property_add_alias (obj,"sse4_2" , obj,"sse4.2" , &error_abort); -x86_cpu_load_def (cpu, xcc->cpu_def, &error_abort); +if (xcc->cpu_def) { +x86_cpu_load_def (cpu, xcc->cpu_def, &error_abort); + } }static int64_t x86_cpu_get_arch_id (CPUState *cs)
@@ -3749,6 +3752,24 @@ static const TypeInfo x86_cpu_type_info = { .class_init = x86_cpu_common_class_init, }; + +/* "base" CPU model, used by query-cpu-model-expansion */ +static void x86_cpu_base_class_init (ObjectClass *oc,void *data) +{ +X86CPUClass *xcc =X86_CPU_CLASS (oc); + + xcc->static_model = true; + xcc->migration_safe = true; + xcc->model_description ="base CPU model type with no feature enabled" ; + xcc->ordering =8 ; +} + +static const TypeInfo x86_base_cpu_type_info = { + .name =X86_CPU_TYPE_NAME ("base" ), + .parent =TYPE_X86_CPU , + .class_init = x86_cpu_base_class_init, +}; +static void x86_cpu_register_types (void ) {int i;
@@ -3757,6 +3778,7 @@ static void x86_cpu_register_types(void)for (i =0 ; i <ARRAY_SIZE (builtin_x86_defs); i++) {x86_register_cpudef_type (&builtin_x86_defs[i]); } +type_register_static (&x86_base_cpu_type_info);#ifdef CONFIG_KVM type_register_static (&host_x86_cpu_type_info);#endif
Signature · 1 line
2.11.0.259.g40922b1
PATCH v2 4/4 target-i386: Implement query-cpu-model-expansion QMP command
In reply to Eduardo HabkostImplement query-cpu-model-expansion for target-i386.
This should meet all the requirements while being simple. In the case of static expansion, it will use the new "base" CPU model, and in the case of full expansion, it will keep the original CPU model name+props, and append extra properties.
A future follow-up should improve the implementation of type=full, so that it returns more detailed data, including every writable QOM property in the CPU object.
---
Changes v1 -> v2:
* Fix typos
* Reported-by: Markus Armbruster <[email protected]>
* Removed static/migration-safe field from return value (they will be added by a separate patch)
* Use a dictionary to keep track of the properties that should be included in the expansion, instead of hardcoding them
* Removed test case to keep series simple (test case will be submitted as a separate series)
--- monitor.c | 4 +- target/i386/cpu.c | 191 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 193 insertions(+), 2 deletions(-)
monitor.c +3 -1
@@ -983,8 +983,10 @@ static void qmp_unregister_commands_hack(void)#ifndef TARGET_ARM qmp_unregister_command ("query-gic-capabilities" );#endif -#if !defined(TARGET_S390X ) +#if !defined(TARGET_S390X ) && !defined(TARGET_I386 )qmp_unregister_command ("query-cpu-model-expansion" ); +#endif +#if !defined(TARGET_S390X )qmp_unregister_command ("query-cpu-model-baseline" );qmp_unregister_command ("query-cpu-model-comparison" );#endif
target/i386/cpu.c +190 -1
219 lines not on this page · raw message
Signature · 1 line
2.11.0.259.g40922b1