[libvirt] [RFC 0/5] 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(-) -- 1.8.1.4

FEAT_7_0_EBX uses ECX as input, so we have to take that into account when reporting feature word values. Signed-off-by: Eduardo Habkost <ehabkost@redhat.com> --- target-i386/cpu.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/target-i386/cpu.c b/target-i386/cpu.c index df6d6f0..6bb228b 100644 --- a/target-i386/cpu.c +++ b/target-i386/cpu.c @@ -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, }, }; -- 1.8.1.4

This 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 Signed-off-by: Eduardo Habkost <ehabkost@redhat.com> --- 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(-) diff --git a/.gitignore b/.gitignore index 487813a..71408e3 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/Makefile.objs b/Makefile.objs index f99841c..944a0e8 100644 --- a/Makefile.objs +++ b/Makefile.objs @@ -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 diff --git a/qapi-schema.json b/qapi-schema.json index f629a24..5e6c221 100644 --- a/qapi-schema.json +++ b/qapi-schema.json @@ -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' } } diff --git a/target-i386/cpu.c b/target-i386/cpu.c index 6bb228b..75ffc9e 100644 --- a/target-i386/cpu.c +++ b/target-i386/cpu.c @@ -30,6 +30,8 @@ #include "qemu/config-file.h" #include "qapi/qmp/qerror.h" +#include "qapi-types.h" +#include "qapi-visit.h" #include "qapi/visitor.h" #include "sysemu/arch_init.h" @@ -194,23 +196,34 @@ static FeatureWordInfo feature_word_info[FEATURE_WORDS] = { }, }; +typedef struct X86RegisterInfo32 { + /* Name of register */ + const char *name; + /* QAPI enum value register */ + X86CPURegister32 qapi_enum; +} X86RegisterInfo32; + +#define REGISTER(reg) \ + [R_##reg] = { .name = #reg, .qapi_enum = X86_C_P_U_REGISTER32_##reg } +X86RegisterInfo32 x86_reg_info_32[CPU_NB_REGS32] = { + REGISTER(EAX), + REGISTER(ECX), + REGISTER(EDX), + REGISTER(EBX), + REGISTER(ESP), + REGISTER(EBP), + REGISTER(ESI), + REGISTER(EDI), +}; +#undef REGISTER + + const char *get_register_name_32(unsigned int reg) { - static const char *reg_names[CPU_NB_REGS32] = { - [R_EAX] = "EAX", - [R_ECX] = "ECX", - [R_EDX] = "EDX", - [R_EBX] = "EBX", - [R_ESP] = "ESP", - [R_EBP] = "EBP", - [R_ESI] = "ESI", - [R_EDI] = "EDI", - }; - if (reg > CPU_NB_REGS32) { return NULL; } - return reg_names[reg]; + return x86_reg_info_32[reg].name; } /* collects per-function cpuid data @@ -1359,6 +1372,36 @@ static void x86_cpuid_set_tsc_freq(Object *obj, Visitor *v, void *opaque, cpu->env.tsc_khz = value / 1000; } +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; + FeatureWord w; + Error *err = NULL; + X86CPUFeatureWordInfo word_infos[FEATURE_WORDS] = { }; + X86CPUFeatureWordInfoList list_entries[FEATURE_WORDS] = { }; + X86CPUFeatureWordInfoList *list = NULL; + + for (w = 0; w < FEATURE_WORDS; w++) { + FeatureWordInfo *wi = &feature_word_info[w]; + X86CPUFeatureWordInfo *qwi = &word_infos[w]; + qwi->cpuid_input_eax = wi->cpuid_eax; + 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]; + + /* List will be in reverse order, but order shouldn't matter */ + list_entries[w].next = list; + list_entries[w].value = &word_infos[w]; + list = &list_entries[w]; + } + + visit_type_X86CPUFeatureWordInfoList(v, &list, "feature-words", &err); + error_propagate(errp, err); +} + static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *name) { x86_def_t *def; @@ -2316,6 +2359,9 @@ static void x86_cpu_initfn(Object *obj) object_property_add(obj, "tsc-frequency", "int", x86_cpuid_get_tsc_freq, x86_cpuid_set_tsc_freq, NULL, NULL, NULL); + object_property_add(obj, "feature-words", "X86CPUFeatureWordInfo", + x86_cpu_get_feature_words, + NULL, NULL, NULL, NULL); env->cpuid_apic_id = x86_cpu_apic_id_from_index(cs->cpu_index); -- 1.8.1.4

Instead 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. Signed-off-by: Eduardo Habkost <ehabkost@redhat.com> --- target-i386/cpu.c | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/target-i386/cpu.c b/target-i386/cpu.c index 75ffc9e..3f4bcfd 100644 --- a/target-i386/cpu.c +++ b/target-i386/cpu.c @@ -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 -- 1.8.1.4

This field will contain the feature bits that were filtered out because of missing host support. Signed-off-by: Eduardo Habkost <ehabkost@redhat.com> --- target-i386/cpu-qom.h | 3 +++ target-i386/cpu.c | 9 ++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/target-i386/cpu-qom.h b/target-i386/cpu-qom.h index 08f9eb6..159378f 100644 --- a/target-i386/cpu-qom.h +++ b/target-i386/cpu-qom.h @@ -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) diff --git a/target-i386/cpu.c b/target-i386/cpu.c index 3f4bcfd..64e5570 100644 --- a/target-i386/cpu.c +++ b/target-i386/cpu.c @@ -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 -- 1.8.1.4

This 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 Signed-off-by: Eduardo Habkost <ehabkost@redhat.com> --- target-i386/cpu.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/target-i386/cpu.c b/target-i386/cpu.c index 64e5570..0dceaf2 100644 --- a/target-i386/cpu.c +++ b/target-i386/cpu.c @@ -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); -- 1.8.1.4
participants (1)
-
Eduardo Habkost