Patchwork [RFC,3/4] target-i386: Add "feature-words" property

login
register
mail settings
Submitter Eduardo Habkost
Date April 1, 2013, 7:46 p.m.
Message ID <1364845609-10543-4-git-send-email-ehabkost@redhat.com>
Download mbox | patch
Permalink /patch/232795/
State New
Headers show

Comments

Eduardo Habkost - April 1, 2013, 7:46 p.m.
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: 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

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
* This is the solution not using qapi. The next patch will introduce a
  solution using qapi, so both approaches can get feedback.
* Current solution is not based on the "feature word array" patch,
  but it may be easily rebased and simplified if we decide to include
  the CPUX86State feature_word array later.
---
 target-i386/cpu.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

Patch

diff --git a/target-i386/cpu.c b/target-i386/cpu.c
index 2020147..ebf6358 100644
--- a/target-i386/cpu.c
+++ b/target-i386/cpu.c
@@ -1277,6 +1277,44 @@  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;
+    uint32_t feat_values[FEATURE_WORDS] = { };
+
+    /* We need to set the entries one-by-one, by now, while we don't have
+     * a single feature_words array on X86CPU
+     */
+    feat_values[FEAT_1_EDX] = env->cpuid_features;
+    feat_values[FEAT_1_ECX] = env->cpuid_ext_features;
+    feat_values[FEAT_8000_0001_EDX] = env->cpuid_ext2_features;
+    feat_values[FEAT_8000_0001_ECX] = env->cpuid_ext3_features;
+    feat_values[FEAT_C000_0001_EDX] = env->cpuid_ext4_features;
+    feat_values[FEAT_KVM] = env->cpuid_kvm_features;
+    feat_values[FEAT_SVM] = env->cpuid_svm_features;
+    feat_values[FEAT_7_0_EBX] = env->cpuid_7_0_ebx_features;
+
+    visit_start_list(v, "feature-words", &err);
+    for (w = 0; w < FEATURE_WORDS; w++) {
+        FeatureWordInfo *wi = &feature_word_info[w];
+        visit_start_struct(v, NULL, NULL, NULL, 0, &err);
+        visit_type_uint32(v, &wi->cpuid_eax, "cpuid-input-eax", &err);
+        if (wi->cpuid_needs_ecx) {
+            visit_type_uint32(v, &wi->cpuid_ecx, "cpuid-input-ecx", &err);
+        }
+        visit_type_enum(v, &wi->cpuid_reg, reg_names_32, "x86-register-32",
+                        "cpuid-register", &err);
+        visit_type_uint32(v, &feat_values[w], "features", &err);
+        visit_end_struct(v, &err);
+    }
+    visit_end_list(v, &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;
@@ -2232,6 +2270,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", "x86-cpu-feature-words",
+                        x86_cpu_get_feature_words,
+                        NULL, NULL, NULL, NULL);
 
     env->cpuid_apic_id = x86_cpu_apic_id_from_index(cs->cpu_index);