X86CPU "feature-words" & "filtered-features" properties (v2)
cover X86CPU "feature-words" & "filtered-features" properties (v2)
Changes v2:
* Added "filtered-features" properties
* Removed the non-qapi version and kept only the qapi version of the code
* Moved the new types to qapi-schema.json instead of creating cpu-qapi-schema.json
* Rebased on top of "feature words array" series
Git tree:
https://github.com/ehabkost/qemu-hacks/tree/work/cpu-raw-feature-props-v2
git://github.com/ehabkost/qemu-hacks.git work/cpu-raw-feature-props-v2
Description:
As the work to get X86CPU subclasses static properties defined is taking very long to get reviewed and we risk not getting it finished in 1.5, I am sending an alternative mechanism to allow libvirt to probe for CPU model information.
It's interesting to note that this alternative solution is actually _easier_ to use for libvirt, because libvirt logic is already based on CPUID bit values, not "feature names", so it should be very easy to convert the current libvirt code that (incorrectly) query the host CPU directly for available features to instead start QEMU with "-cpu host" and use the "feature-words" array information to find out which CPU features are supported by the host.
The "feature-words" property will have two main use cases:
* Checking host capabilities, by checking the features of the "host" CPU model;
* Checking which features are enabled on each CPU model.
The "filtered-features" property will have two main use cases:
* Allowing libvirt to emulate the "check" & "enforce" flags while collecting more detailted data about the missing features;
* Allowing libvirt to probe for CPU model information even if host features are missing (it can simply combine the "feature-words" and "filtered-features" values to get the full CPU model definition)
Example output:
$ ./QMP/qmp --path=/tmp/m qom-get --path=/machine/unattached/device[1] --property=feature-words item[0].cpuid-register: EDX item[0].cpuid-input-eax: 1 item[0].features: 126614521 item[1].cpuid-register: ECX item[1].cpuid-input-eax: 1 item[1].features: 2155880449 item[2].cpuid-register: EBX item[2].cpuid-input-eax: 7 item[2].features: 0 item[2].cpuid-input-ecx: 0 item[3].cpuid-register: EDX item[3].cpuid-input-eax: 2147483649 item[3].features: 563346425 item[4].cpuid-register: ECX item[4].cpuid-input-eax: 2147483649 item[4].features: 101 item[5].cpuid-register: EDX item[5].cpuid-input-eax: 3221225473 item[5].features: 0 item[6].cpuid-register: EAX item[6].cpuid-input-eax: 1073741825 item[6].features: 0 item[7].cpuid-register: EDX item[7].cpuid-input-eax: 2147483658 item[7].features: 0
Eduardo Habkost (5):
target-i386: Add ECX information to FeatureWordInfo
target-i386: Add "feature-words" property
target-i386: Use FeatureWord loop on filter_features_for_kvm()
target-i386: Introduce X86CPU.filtered_features field
target-i386: Add "filtered-features" property to X86CPU
.gitignore | 2 + Makefile.objs | 7 +++- qapi-schema.json | 32 +++++++++++++++ target-i386/cpu-qom.h | 3 ++ target-i386/cpu.c | 110 +++++++++++++++++++++++++++++++++++--------------- 5 files changed, 121 insertions(+), 33 deletions(-)
Signature · 1 line
1.8.1.4
RFC 1/5 target-i386: Add ECX information to FeatureWordInfo
FEAT_7_0_EBX uses ECX as input, so we have to take that into account when reporting feature word values.
--- target-i386/cpu.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
target-i386/cpu.c +7 -3
@@ -151,8 +151,10 @@ static const char *cpuid_7_0_ebx_feature_name[] = {typedef struct FeatureWordInfo {const char **feat_names; -uint32_t cpuid_eax;/* Input EAX for CPUID */ -int cpuid_reg;/* R_* register constant */ +uint32_t cpuid_eax;/* Input EAX for CPUID */ +bool cpuid_needs_ecx;/* CPUID instruction uses ECX as input */ +uint32_t cpuid_ecx;/* Input ECX value for CPUID */ +int cpuid_reg;/* output register (R_* constant) */ }FeatureWordInfo ;static FeatureWordInfo feature_word_info[FEATURE_WORDS ] = {
@@ -186,7 +188,9 @@ static FeatureWordInfo feature_word_info[FEATURE_WORDS] = { }, [FEAT_7_0_EBX ] = { .feat_names = cpuid_7_0_ebx_feature_name, - .cpuid_eax =7 , .cpuid_reg =R_EBX , + .cpuid_eax =7 , + .cpuid_needs_ecx = true, .cpuid_ecx =0 , + .cpuid_reg =R_EBX , }, };
Signature · 1 line
1.8.1.4
RFC 2/5 target-i386: Add "feature-words" property
In reply to Eduardo HabkostThis property will be useful for libvirt, as libvirt already has logic based on low-level feature bits (not feature names), so it will be really easy to convert the current libvirt logic to something using the "feature-words" property.
The property will have two main use cases:
- Checking host capabilities, by checking the features of the "host" CPU model
- Checking which features are enabled on each CPU model
Example output:
$ ./QMP/qmp --path=/tmp/m qom-get --path=/machine/unattached/device[1] --property=feature-words item[0].cpuid-register: EDX item[0].cpuid-input-eax: 2147483658 item[0].features: 0 item[1].cpuid-register: EAX item[1].cpuid-input-eax: 1073741825 item[1].features: 0 item[2].cpuid-register: EDX item[2].cpuid-input-eax: 3221225473 item[2].features: 0 item[3].cpuid-register: ECX item[3].cpuid-input-eax: 2147483649 item[3].features: 101 item[4].cpuid-register: EDX item[4].cpuid-input-eax: 2147483649 item[4].features: 563346425 item[5].cpuid-register: EBX item[5].cpuid-input-eax: 7 item[5].features: 0 item[5].cpuid-input-ecx: 0 item[6].cpuid-register: ECX item[6].cpuid-input-eax: 1 item[6].features: 2155880449 item[7].cpuid-register: EDX item[7].cpuid-input-eax: 1 item[7].features: 126614521
---
Changes v1 -> v2:
* Merge the non-qapi and qapi patches, to keep series simpler
* Use the feature word array series as base, so we don't have to set the feature word values one-by-one in the code
* Change type name of property from "x86-cpu-feature-words" to "X86CPUFeatureWordInfo"
* Remove cpu-qapi-schema.json and simply add the type definitions to qapi-schema.json, to keep the changes simpler
* This required compiling qapi-types.o and qapi-visit.o into *-user as well
--- .gitignore | 2 ++ Makefile.objs | 7 +++++- qapi-schema.json | 32 +++++++++++++++++++++++++ target-i386/cpu.c | 70 +++++++++++++++++++++++++++++++++++++++++++++---------- 4 files changed, 98 insertions(+), 13 deletions(-)
.gitignore +2 -0
@@ -21,6 +21,8 @@ linux-headers/asm qapi-generated qapi-types.[ch] qapi-visit.[ch] +cpu-qapi-types.[ch] +cpu-qapi-visit.[ch] qmp-commands.h qmp-marshal.c qemu-doc.html
Makefile.objs +6 -1
@@ -87,10 +87,15 @@ common-obj-$(CONFIG_SMARTCARD_NSS) += $(libcacard-y) ###################################################################### # qapi -common-obj-y += qmp-marshal.o qapi-visit.o qapi-types.o +common-obj-y += qmp-marshal.o common-obj-y += qmp.o hmp.o endif +###################################################################### +# some qapi visitors are used by both system and user emulation: + +common-obj-y += qapi-visit.o qapi-types.o + ####################################################################### # Target-independent parts used in system and user emulation common-obj-y += qemu-log.o
qapi-schema.json +32 -0
@@ -3453,3 +3453,35 @@ # Since: 1.5 ## { 'command': 'query-tpm', 'returns': ['TPMInfo'] } + +## +# @X86CPURegister32 +# +# A X86 32-bit register +# +# Since: 1.5 +## +{ 'enum': 'X86CPURegister32', + 'data': [ 'EAX', 'EBX', 'ECX', 'EDX', 'ESP', 'EBP', 'ESI', 'EDI' ] } + +## +# @X86CPUFeatureWordInfo +# +# Information about a X86 CPU feature word +# +# @cpuid-input-eax: Input EAX value for CPUID instruction for that feature word +# +# @cpuid-input-ecx: #optional Input ECX value for CPUID instruction for that +# feature word +# +# @cpuid-register: Output register containing the feature bits +# +# @features: value of output register, containing the feature bits +# +# Since: 1.5 +## +{ 'type': 'X86CPUFeatureWordInfo', + 'data': { 'cpuid-input-eax': 'int', + '*cpuid-input-ecx': 'int', + 'cpuid-register': 'X86CPURegister32', + 'features': 'int' } }
target-i386/cpu.c +58 -12
99 lines not on this page · raw message
Signature · 1 line
1.8.1.4
RFC 3/5 target-i386: Use FeatureWord loop on filter_features_for_kvm()
In reply to Eduardo HabkostInstead of open-coding the filtering code for each feature word, change the existing code to use the feature_word_info array, that have exactly the same CPUID eax/ecx/register values for each feature word.
--- target-i386/cpu.c | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-)
target-i386/cpu.c +7 -17
@@ -1637,24 +1637,14 @@ static void filter_features_for_kvm(X86CPU *cpu) {CPUX86State *env = &cpu->env;KVMState *s = kvm_state; +FeatureWord w; - env->features[FEAT_1_EDX ] &= -kvm_arch_get_supported_cpuid (s,1 ,0 ,R_EDX ); - env->features[FEAT_1_ECX ] &= -kvm_arch_get_supported_cpuid (s,1 ,0 ,R_ECX ); - env->features[FEAT_8000_0001_EDX ] &= -kvm_arch_get_supported_cpuid (s,0x80000001 ,0 ,R_EDX ); - env->features[FEAT_8000_0001_ECX ] &= -kvm_arch_get_supported_cpuid (s,0x80000001 ,0 ,R_ECX ); - env->features[FEAT_SVM ] &= -kvm_arch_get_supported_cpuid (s,0x8000000A ,0 ,R_EDX ); - env->features[FEAT_7_0_EBX ] &= -kvm_arch_get_supported_cpuid (s,7 ,0 ,R_EBX ); - env->features[FEAT_KVM ] &= -kvm_arch_get_supported_cpuid (s,KVM_CPUID_FEATURES ,0 ,R_EAX ); - env->features[FEAT_C000_0001_EDX ] &= -kvm_arch_get_supported_cpuid (s,0xC0000001 ,0 ,R_EDX ); - +for (w =0 ; w <FEATURE_WORDS ; w++) { +FeatureWordInfo *wi = &feature_word_info[w]; + env->features[w] &=kvm_arch_get_supported_cpuid (s, wi->cpuid_eax, + wi->cpuid_ecx, + wi->cpuid_reg); + } }#endif
Signature · 1 line
1.8.1.4
RFC 4/5 target-i386: Introduce X86CPU.filtered_features field
In reply to Eduardo HabkostThis field will contain the feature bits that were filtered out because of missing host support.
--- target-i386/cpu-qom.h | 3 +++ target-i386/cpu.c | 9 ++++++--- 2 files changed, 9 insertions(+), 3 deletions(-)
target-i386/cpu-qom.h +3 -0
@@ -65,6 +65,9 @@ typedef struct X86CPU {/*< public >*/ CPUX86State env; + +/* Features that were filtered out because of missing host capabilities */ +uint32_t filtered_features[FEATURE_WORDS ]; }X86CPU ;static inline X86CPU *x86_env_get_cpu (CPUX86State *env)
target-i386/cpu.c +6 -3
@@ -1641,9 +1641,12 @@ static void filter_features_for_kvm(X86CPU *cpu)for (w =0 ; w <FEATURE_WORDS ; w++) {FeatureWordInfo *wi = &feature_word_info[w]; - env->features[w] &=kvm_arch_get_supported_cpuid (s, wi->cpuid_eax, - wi->cpuid_ecx, - wi->cpuid_reg); +uint32_t host_feat =kvm_arch_get_supported_cpuid (s, wi->cpuid_eax, + wi->cpuid_ecx, + wi->cpuid_reg); +uint32_t requested_features = env->features[w]; + env->features[w] &= host_feat; + cpu->filtered_features[w] = requested_features & ~env->features[w]; } }#endif
Signature · 1 line
1.8.1.4
RFC 5/5 target-i386: Add "filtered-features" property to X86CPU
In reply to Eduardo HabkostThis property will contain all the features that were removed from the CPU because they are not supported by the host.
This way, libvirt or other management tools can emulate the check/enforce behavior by checking if filtered-properties is all zeroes, before starting the guest.
Example output where some features were missing:
$ ./install/bin/qemu-system-x86_64 -enable-kvm -cpu Haswell,check -S -qmp unix:/tmp/m,server,nowait warning: host doesn't support requested feature: CPUID.01H:ECX.fma [bit 12] warning: host doesn't support requested feature: CPUID.01H:ECX.movbe [bit 22] warning: host doesn't support requested feature: CPUID.01H:ECX.tsc-deadline [bit 24] warning: host doesn't support requested feature: CPUID.01H:ECX.xsave [bit 26] warning: host doesn't support requested feature: CPUID.01H:ECX.avx [bit 28] warning: host doesn't support requested feature: CPUID.07H:EBX.fsgsbase [bit 0] warning: host doesn't support requested feature: CPUID.07H:EBX.bmi1 [bit 3] warning: host doesn't support requested feature: CPUID.07H:EBX.hle [bit 4] warning: host doesn't support requested feature: CPUID.07H:EBX.avx2 [bit 5] warning: host doesn't support requested feature: CPUID.07H:EBX.smep [bit 7] warning: host doesn't support requested feature: CPUID.07H:EBX.bmi2 [bit 8] warning: host doesn't support requested feature: CPUID.07H:EBX.erms [bit 9] warning: host doesn't support requested feature: CPUID.07H:EBX.invpcid [bit 10] warning: host doesn't support requested feature: CPUID.07H:EBX.rtm [bit 11] [...] $ ./QMP/qmp --path=/tmp/m qom-get --path=/machine/unattached/device[1] --property=filtered-features item[0].cpuid-register: EDX item[0].cpuid-input-eax: 2147483658 item[0].features: 0 item[1].cpuid-register: EAX item[1].cpuid-input-eax: 1073741825 item[1].features: 0 item[2].cpuid-register: EDX item[2].cpuid-input-eax: 3221225473 item[2].features: 0 item[3].cpuid-register: ECX item[3].cpuid-input-eax: 2147483649 item[3].features: 0 item[4].cpuid-register: EDX item[4].cpuid-input-eax: 2147483649 item[4].features: 0 item[5].cpuid-register: EBX item[5].cpuid-input-eax: 7 item[5].features: 4025 item[5].cpuid-input-ecx: 0 item[6].cpuid-register: ECX item[6].cpuid-input-eax: 1 item[6].features: 356519936 item[7].cpuid-register: EDX item[7].cpuid-input-eax: 1 item[7].features: 0
Example output when no feature is missing:
$ ./install/bin/qemu-system-x86_64 -enable-kvm -cpu Nehalem,enforce -S -qmp unix:/tmp/m,server,nowait [...] $ ./QMP/qmp --path=/tmp/m qom-get --path=/machine/unattached/device[1] --property=filtered-features item[0].cpuid-register: EDX item[0].cpuid-input-eax: 2147483658 item[0].features: 0 item[1].cpuid-register: EAX item[1].cpuid-input-eax: 1073741825 item[1].features: 0 item[2].cpuid-register: EDX item[2].cpuid-input-eax: 3221225473 item[2].features: 0 item[3].cpuid-register: ECX item[3].cpuid-input-eax: 2147483649 item[3].features: 0 item[4].cpuid-register: EDX item[4].cpuid-input-eax: 2147483649 item[4].features: 0 item[5].cpuid-register: EBX item[5].cpuid-input-eax: 7 item[5].features: 0 item[5].cpuid-input-ecx: 0 item[6].cpuid-register: ECX item[6].cpuid-input-eax: 1 item[6].features: 0 item[7].cpuid-register: EDX item[7].cpuid-input-eax: 1 item[7].features: 0
--- target-i386/cpu.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
target-i386/cpu.c +7 -4
@@ -1372,11 +1372,11 @@ static void x86_cpuid_set_tsc_freq(Object *obj, Visitor *v, void *opaque, cpu->env.tsc_khz = value /1000 ; } +/* Generic getter for "feature-words" and "filtered-features" properties */ static void x86_cpu_get_feature_words (Object *obj,Visitor *v,void *opaque,const char *name,Error **errp) { -X86CPU *cpu =X86_CPU (obj); -CPUX86State *env = &cpu->env; +uint32_t *array = (uint32_t *)opaque;FeatureWord w;Error *err =NULL ;X86CPUFeatureWordInfo word_infos[FEATURE_WORDS ] = { };
@@ -1390,7 +1390,7 @@ static void x86_cpu_get_feature_words(Object *obj, Visitor *v, void *opaque, qwi->has_cpuid_input_ecx = wi->cpuid_needs_ecx; qwi->cpuid_input_ecx = wi->cpuid_ecx; qwi->cpuid_register = x86_reg_info_32[wi->cpuid_reg].qapi_enum; - qwi->features = env->features[w]; + qwi->features = array[w];/* List will be in reverse order, but order shouldn't matter */ list_entries[w].next = list;
@@ -2354,7 +2354,10 @@ static void x86_cpu_initfn(Object *obj) x86_cpuid_set_tsc_freq, NULL, NULL, NULL);object_property_add (obj,"feature-words" ,"X86CPUFeatureWordInfo" , x86_cpu_get_feature_words, -NULL ,NULL ,NULL ,NULL ); +NULL ,NULL , (void *)env->features,NULL ); +object_property_add (obj,"filtered-features" ,"X86CPUFeatureWordInfo" , + x86_cpu_get_feature_words, +NULL ,NULL , (void *)cpu->filtered_features,NULL ); env->cpuid_apic_id =x86_cpu_apic_id_from_index (cs->cpu_index);
Signature · 1 line
1.8.1.4