Skip to contentSkip to conversation

target-i386: Simplify kvm_cpu_fill_host() and kvm_check_features_against_host()

PATCH Superseded Eduardo Habkost 2 participants 11 messages Nov 24, 2013 – Jan 20, 2014

lore
Share & tools
lore
#0 lore raw

cover target-i386: Simplify kvm_cpu_fill_host() and kvm_check_features_against_host()

This series simplifies kvm_cpu_fill_host() and kvm_check_features_against_host() to simply use FeatureWord & feature_word_info loops to fill/check feature words.

The initial motivation for this was to avoid hacks involving the "host" CPU class on the forthcoming conversion of CPU models to be X86CPU subclasses. Instead of requiring the kvm_arch_get_supported_cpuid() results to be stored in the class struct for "host" (thus requiring KVM initialization hacks).

Eduardo Habkost (8):
target-i386: kvm_cpu_fill_host(): Kill unused code
target-i386: kvm_cpu_fill_host(): No need to check level
target-i386: kvm_cpu_fill_host(): No need to check CPU vendor
target-i386: kvm_cpu_fill_host(): No need to check xlevel2
target-i386: kvm_cpu_fill_host(): Set all feature words at end of
function
target-i386: kvm_cpu_fill_host(): Fill feature words in a loop
target-i386: kvm_check_features_against_host(): Kill feature word
array
target-i386: kvm_check_features_against_host(): Don't use

    kvm_cpu_fill_host()
 target-i386/cpu.c | 89 +++++++++++++------------------------------------------
 1 file changed, 20 insertions(+), 69 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/8 target-i386: kvm_cpu_fill_host(): Kill unused code

Those host_cpuid() calls are useless. They are leftovers from when the old code using host_cpuid() was removed.

---
 target-i386/cpu.c | 2 --
 1 file changed, 2 deletions(-)
target-i386/cpu.c +0 -2
index 864c80e..5796c6d 100644
@@ -1181,12 +1181,10 @@ static void kvm_cpu_fill_host(x86_def_t *x86_cpu_def)
 
     /* Call Centaur's CPUID instruction. */
     if (!strcmp(x86_cpu_def->vendor, CPUID_VENDOR_VIA)) {
-        host_cpuid(0xC0000000, 0, &eax, &ebx, &ecx, &edx);
         eax = kvm_arch_get_supported_cpuid(s, 0xC0000000, 0, R_EAX);
         if (eax >= 0xC0000001) {
             /* Support VIA max extended level */
             x86_cpu_def->xlevel2 = eax;
-            host_cpuid(0xC0000001, 0, &eax, &ebx, &ecx, &edx);
             x86_cpu_def->features[FEAT_C000_0001_EDX] =
                     kvm_arch_get_supported_cpuid(s, 0xC0000001, 0, R_EDX);
         }
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 2/8 target-i386: kvm_cpu_fill_host(): No need to check level

In reply to Eduardo Habkost

There's no need to check level (CPUID[0].EAX) before calling kvm_arch_get_supported_cpuid(s, 0x7, 0, R_EBX), because:

* The kernel won't return any entry for CPUID 7 if CPUID[0].EAX is < 7 (See kvm_dev_ioctl_get_supported_cpuid() on the kernel code);
* kvm_arch_get_supported_cpuid() will return 0 if no entry is returned by the kernel for the requested leaf.

This will simplify the kvm_cpu_fill_host() code a little.

---
 target-i386/cpu.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
target-i386/cpu.c +2 -6
index 5796c6d..f659a3b 100644
@@ -1164,12 +1164,8 @@ static void kvm_cpu_fill_host(x86_def_t *x86_cpu_def)
     x86_cpu_def->features[FEAT_1_ECX] =
         kvm_arch_get_supported_cpuid(s, 0x1, 0, R_ECX);
 
-    if (x86_cpu_def->level >= 7) {
-        x86_cpu_def->features[FEAT_7_0_EBX] =
-                    kvm_arch_get_supported_cpuid(s, 0x7, 0, R_EBX);
-    } else {
-        x86_cpu_def->features[FEAT_7_0_EBX] = 0;
-    }
+    x86_cpu_def->features[FEAT_7_0_EBX] =
+                kvm_arch_get_supported_cpuid(s, 0x7, 0, R_EBX);
 
     x86_cpu_def->xlevel = kvm_arch_get_supported_cpuid(s, 0x80000000, 0, R_EAX);
     x86_cpu_def->features[FEAT_8000_0001_EDX] =
Signature · 1 line
1.8.3.1
#3 lore raw

PATCH 3/8 target-i386: kvm_cpu_fill_host(): No need to check CPU vendor

In reply to Eduardo Habkost

There's no need to check CPU vendor before calling kvm_arch_get_supported_cpuid(s, 0xC0000000, 0, R_EAX), because:

* The kernel won't return any entry for 0xC0000000 if host CPU vendor is not Centaur (See kvm_dev_ioctl_get_supported_cpuid() on the kernel code);
* kvm_arch_get_supported_cpuid() will return 0 if no entry is returned by the kernel for the requested leaf.

This will simplify the kvm_cpu_fill_host() code a little.

---
 target-i386/cpu.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
target-i386/cpu.c +6 -8
index f659a3b..41726f8 100644
@@ -1176,14 +1176,12 @@ static void kvm_cpu_fill_host(x86_def_t *x86_cpu_def)
     cpu_x86_fill_model_id(x86_cpu_def->model_id);
 
     /* Call Centaur's CPUID instruction. */
-    if (!strcmp(x86_cpu_def->vendor, CPUID_VENDOR_VIA)) {
-        eax = kvm_arch_get_supported_cpuid(s, 0xC0000000, 0, R_EAX);
-        if (eax >= 0xC0000001) {
-            /* Support VIA max extended level */
-            x86_cpu_def->xlevel2 = eax;
-            x86_cpu_def->features[FEAT_C000_0001_EDX] =
-                    kvm_arch_get_supported_cpuid(s, 0xC0000001, 0, R_EDX);
-        }
+    eax = kvm_arch_get_supported_cpuid(s, 0xC0000000, 0, R_EAX);
+    if (eax >= 0xC0000001) {
+        /* Support VIA max extended level */
+        x86_cpu_def->xlevel2 = eax;
+        x86_cpu_def->features[FEAT_C000_0001_EDX] =
+                kvm_arch_get_supported_cpuid(s, 0xC0000001, 0, R_EDX);
     }
 
     /* Other KVM-specific feature fields: */
Signature · 1 line
1.8.3.1
#4 lore raw

PATCH 4/8 target-i386: kvm_cpu_fill_host(): No need to check xlevel2

In reply to Eduardo Habkost

There's no need to check CPU xlevel2 before calling kvm_arch_get_supported_cpuid(s, 0xC0000001, 0, R_EDX), because:

* The kernel won't return any entry for 0xC0000000 if host CPU vendor is not Centaur (See kvm_dev_ioctl_get_supported_cpuid() on the kernel code)
* Similarly, the kernel won't return any entry for 0xC0000001 if CPUID[0xC0000000].EAX is < 0xC0000001
* kvm_arch_get_supported_cpuid() will return 0 if no entry is returned by the kernel for the requested leaf

For similar reasons, we can simply set x86_cpu_def->xlevel2 directly instead of making it conditional, because it will be set to 0 CPU vendor is not Centaur.

This will simplify the kvm_cpu_fill_host() code a little.

---
 target-i386/cpu.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
target-i386/cpu.c +5 -7
index 41726f8..9731493 100644
@@ -1176,13 +1176,11 @@ static void kvm_cpu_fill_host(x86_def_t *x86_cpu_def)
     cpu_x86_fill_model_id(x86_cpu_def->model_id);
 
     /* Call Centaur's CPUID instruction. */
-    eax = kvm_arch_get_supported_cpuid(s, 0xC0000000, 0, R_EAX);
-    if (eax >= 0xC0000001) {
-        /* Support VIA max extended level */
-        x86_cpu_def->xlevel2 = eax;
-        x86_cpu_def->features[FEAT_C000_0001_EDX] =
-                kvm_arch_get_supported_cpuid(s, 0xC0000001, 0, R_EDX);
-    }
+    x86_cpu_def->xlevel2 =
+        kvm_arch_get_supported_cpuid(s, 0xC0000000, 0, R_EAX);
+    /* Support VIA max extended level */
+    x86_cpu_def->features[FEAT_C000_0001_EDX] =
+        kvm_arch_get_supported_cpuid(s, 0xC0000001, 0, R_EDX);
 
     /* Other KVM-specific feature fields: */
     x86_cpu_def->features[FEAT_SVM] =
Signature · 1 line
1.8.3.1
#5 lore raw

Re: [Qemu-devel] [PATCH 4/8] target-i386: kvm_cpu_fill_host(): No need to check xlevel2

Am 24.11.2013 20:55, schrieb Eduardo Habkost:

Quoted 11 lines

There's no need to check CPU xlevel2 before calling kvm_arch_get_supported_cpuid(s, 0xC0000001, 0, R_EDX), because:

* The kernel won't return any entry for 0xC0000000 if host CPU vendor is not Centaur (See kvm_dev_ioctl_get_supported_cpuid() on the kernel code)
* Similarly, the kernel won't return any entry for 0xC0000001 if CPUID[0xC0000000].EAX is < 0xC0000001
* kvm_arch_get_supported_cpuid() will return 0 if no entry is returned by the kernel for the requested leaf

Quoted 3 lines

For similar reasons, we can simply set x86_cpu_def->xlevel2 directly instead of making it conditional, because it will be set to 0 CPU vendor is not Centaur.

"... if CPU vendor is not ..." I guess. :)

Quoted 21 lines

This will simplify the kvm_cpu_fill_host() code a little.

---
 target-i386/cpu.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
target-i386/cpu.c +0 -5
index 41726f8..9731493 100644
@@ -1176,13 +1176,11 @@ static void kvm_cpu_fill_host(x86_def_t *x86_cpu_def)
     cpu_x86_fill_model_id(x86_cpu_def->model_id);
 
     /* Call Centaur's CPUID instruction. */
-    eax = kvm_arch_get_supported_cpuid(s, 0xC0000000, 0, R_EAX);
-    if (eax >= 0xC0000001) {
-        /* Support VIA max extended level */
-        x86_cpu_def->xlevel2 = eax;
-        x86_cpu_def->features[FEAT_C000_0001_EDX] =

target-i386/cpu.c static void kvm_cpu_fill_host(x86_def_t *x86_cpu_def) · line 1186

Quoted 10 lines
target-i386/cpu.c +5 -2
-                kvm_arch_get_supported_cpuid(s, 0xC0000001, 0, R_EDX);
-    }
+    x86_cpu_def->xlevel2 =
+        kvm_arch_get_supported_cpuid(s, 0xC0000000, 0, R_EAX);
+    /* Support VIA max extended level */
+    x86_cpu_def->features[FEAT_C000_0001_EDX] =
+        kvm_arch_get_supported_cpuid(s, 0xC0000001, 0, R_EDX);
 
     /* Other KVM-specific feature fields: */
     x86_cpu_def->features[FEAT_SVM] =

Sounds okay otherwise.

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#6 lore raw

PATCH 5/8 target-i386: kvm_cpu_fill_host(): Set all feature words at end of function

In reply to Eduardo Habkost

Reorder the code so all the code that sets x86_cpu_def->features is at the end of the function.

---
 target-i386/cpu.c | 24 +++++++++---------------
 1 file changed, 9 insertions(+), 15 deletions(-)
target-i386/cpu.c +9 -15
index 9731493..57bec6c 100644
@@ -1159,30 +1159,24 @@ static void kvm_cpu_fill_host(x86_def_t *x86_cpu_def)
     x86_cpu_def->stepping = eax & 0x0F;
 
     x86_cpu_def->level = kvm_arch_get_supported_cpuid(s, 0x0, 0, R_EAX);
+    x86_cpu_def->xlevel = kvm_arch_get_supported_cpuid(s, 0x80000000, 0, R_EAX);
+    x86_cpu_def->xlevel2 =
+        kvm_arch_get_supported_cpuid(s, 0xC0000000, 0, R_EAX);
+
+    cpu_x86_fill_model_id(x86_cpu_def->model_id);
+
     x86_cpu_def->features[FEAT_1_EDX] =
         kvm_arch_get_supported_cpuid(s, 0x1, 0, R_EDX);
     x86_cpu_def->features[FEAT_1_ECX] =
         kvm_arch_get_supported_cpuid(s, 0x1, 0, R_ECX);
-
     x86_cpu_def->features[FEAT_7_0_EBX] =
-                kvm_arch_get_supported_cpuid(s, 0x7, 0, R_EBX);
-
-    x86_cpu_def->xlevel = kvm_arch_get_supported_cpuid(s, 0x80000000, 0, R_EAX);
+        kvm_arch_get_supported_cpuid(s, 0x7, 0, R_EBX);
     x86_cpu_def->features[FEAT_8000_0001_EDX] =
-                kvm_arch_get_supported_cpuid(s, 0x80000001, 0, R_EDX);
+        kvm_arch_get_supported_cpuid(s, 0x80000001, 0, R_EDX);
     x86_cpu_def->features[FEAT_8000_0001_ECX] =
-                kvm_arch_get_supported_cpuid(s, 0x80000001, 0, R_ECX);
-
-    cpu_x86_fill_model_id(x86_cpu_def->model_id);
-
-    /* Call Centaur's CPUID instruction. */
-    x86_cpu_def->xlevel2 =
-        kvm_arch_get_supported_cpuid(s, 0xC0000000, 0, R_EAX);
-    /* Support VIA max extended level */
+        kvm_arch_get_supported_cpuid(s, 0x80000001, 0, R_ECX);
     x86_cpu_def->features[FEAT_C000_0001_EDX] =
         kvm_arch_get_supported_cpuid(s, 0xC0000001, 0, R_EDX);
-
-    /* Other KVM-specific feature fields: */
     x86_cpu_def->features[FEAT_SVM] =
         kvm_arch_get_supported_cpuid(s, 0x8000000A, 0, R_EDX);
     x86_cpu_def->features[FEAT_KVM] =
Signature · 1 line
1.8.3.1
#7 lore raw

PATCH 6/8 target-i386: kvm_cpu_fill_host(): Fill feature words in a loop

In reply to Eduardo Habkost

Now that the kvm_cpu_fill_host() code is simplified, we can simply set the feature word array using a simple loop.

---
 target-i386/cpu.c | 23 +++++++----------------
 1 file changed, 7 insertions(+), 16 deletions(-)
target-i386/cpu.c +7 -16
index 57bec6c..9fc8500 100644
@@ -1165,22 +1165,13 @@ static void kvm_cpu_fill_host(x86_def_t *x86_cpu_def)
 
     cpu_x86_fill_model_id(x86_cpu_def->model_id);
 
-    x86_cpu_def->features[FEAT_1_EDX] =
-        kvm_arch_get_supported_cpuid(s, 0x1, 0, R_EDX);
-    x86_cpu_def->features[FEAT_1_ECX] =
-        kvm_arch_get_supported_cpuid(s, 0x1, 0, R_ECX);
-    x86_cpu_def->features[FEAT_7_0_EBX] =
-        kvm_arch_get_supported_cpuid(s, 0x7, 0, R_EBX);
-    x86_cpu_def->features[FEAT_8000_0001_EDX] =
-        kvm_arch_get_supported_cpuid(s, 0x80000001, 0, R_EDX);
-    x86_cpu_def->features[FEAT_8000_0001_ECX] =
-        kvm_arch_get_supported_cpuid(s, 0x80000001, 0, R_ECX);
-    x86_cpu_def->features[FEAT_C000_0001_EDX] =
-        kvm_arch_get_supported_cpuid(s, 0xC0000001, 0, R_EDX);
-    x86_cpu_def->features[FEAT_SVM] =
-        kvm_arch_get_supported_cpuid(s, 0x8000000A, 0, R_EDX);
-    x86_cpu_def->features[FEAT_KVM] =
-        kvm_arch_get_supported_cpuid(s, KVM_CPUID_FEATURES, 0, R_EAX);
+    FeatureWord w;
+    for (w = 0; w < FEATURE_WORDS; w++) {
+        FeatureWordInfo *wi = &feature_word_info[w];
+        x86_cpu_def->features[w] =
+            kvm_arch_get_supported_cpuid(s, wi->cpuid_eax, wi->cpuid_ecx,
+                                         wi->cpuid_reg);
+    }
 
 #endif /* CONFIG_KVM */
 }
Signature · 1 line
1.8.3.1
#8 lore raw

PATCH 7/8 target-i386: kvm_check_features_against_host(): Kill feature word array

In reply to Eduardo Habkost

We don't need the ft[] array on kvm_check_features_against_host() anymore, as we can simply use the feature_word_info[] array, that has everything we need.

---
 target-i386/cpu.c | 41 ++++++++---------------------------------
 1 file changed, 8 insertions(+), 33 deletions(-)
target-i386/cpu.c +8 -33
index 9fc8500..6c540c6 100644
@@ -1204,44 +1204,19 @@ static int kvm_check_features_against_host(X86CPU *cpu)
 {
     CPUX86State *env = &cpu->env;
     x86_def_t host_def;
-    uint32_t mask;
-    int rv, i;
-    struct model_features_t ft[] = {
-        {&env->features[FEAT_1_EDX],
-            &host_def.features[FEAT_1_EDX],
-            FEAT_1_EDX },
-        {&env->features[FEAT_1_ECX],
-            &host_def.features[FEAT_1_ECX],
-            FEAT_1_ECX },
-        {&env->features[FEAT_8000_0001_EDX],
-            &host_def.features[FEAT_8000_0001_EDX],
-            FEAT_8000_0001_EDX },
-        {&env->features[FEAT_8000_0001_ECX],
-            &host_def.features[FEAT_8000_0001_ECX],
-            FEAT_8000_0001_ECX },
-        {&env->features[FEAT_C000_0001_EDX],
-            &host_def.features[FEAT_C000_0001_EDX],
-            FEAT_C000_0001_EDX },
-        {&env->features[FEAT_7_0_EBX],
-            &host_def.features[FEAT_7_0_EBX],
-            FEAT_7_0_EBX },
-        {&env->features[FEAT_SVM],
-            &host_def.features[FEAT_SVM],
-            FEAT_SVM },
-        {&env->features[FEAT_KVM],
-            &host_def.features[FEAT_KVM],
-            FEAT_KVM },
-    };
+    int rv = 0;
+    FeatureWord w;
 
     assert(kvm_enabled());
-
     kvm_cpu_fill_host(&host_def);
-    for (rv = 0, i = 0; i < ARRAY_SIZE(ft); ++i) {
-        FeatureWord w = ft[i].feat_word;
+
+    for (w = 0; w < FEATURE_WORDS; w++) {
         FeatureWordInfo *wi = &feature_word_info[w];
+        uint32_t guest_feat = env->features[w];
+        uint32_t host_feat = host_def.features[w];
+        uint32_t mask;
         for (mask = 1; mask; mask <<= 1) {
-            if (*ft[i].guest_feat & mask &&
-                !(*ft[i].host_feat & mask)) {
+            if (guest_feat & mask && !(host_feat & mask)) {
                 unavailable_host_feature(wi, mask);
                 rv = 1;
             }
Signature · 1 line
1.8.3.1
#9 lore raw

PATCH 8/8 target-i386: kvm_check_features_against_host(): Don't use kvm_cpu_fill_host()

In reply to Eduardo Habkost

As now kvm_cpu_fill_host() will simply fill the x86_def_t.features array with the results of kvm_arch_get_supported_cpuid() calls, we can simply call kvm_arch_get_supported_cpuid() directly.

Note how kvm_check_features_against_host() and filter_features_for_kvm() are getting very similar. Eventually they should become a single function.

---
 target-i386/cpu.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
target-i386/cpu.c +6 -5
index 6c540c6..048d87f 100644
@@ -1200,20 +1200,20 @@ static int unavailable_host_feature(FeatureWordInfo *f, uint32_t mask)
  *
  * This function may be called only if KVM is enabled.
  */
-static int kvm_check_features_against_host(X86CPU *cpu)
+static int kvm_check_features_against_host(KVMState *s, X86CPU *cpu)
 {
     CPUX86State *env = &cpu->env;
-    x86_def_t host_def;
     int rv = 0;
     FeatureWord w;
 
     assert(kvm_enabled());
-    kvm_cpu_fill_host(&host_def);
 
     for (w = 0; w < FEATURE_WORDS; w++) {
         FeatureWordInfo *wi = &feature_word_info[w];
         uint32_t guest_feat = env->features[w];
-        uint32_t host_feat = host_def.features[w];
+        uint32_t host_feat = kvm_arch_get_supported_cpuid(s, wi->cpuid_eax,
+                                                             wi->cpuid_ecx,
+                                                             wi->cpuid_reg);
         uint32_t mask;
         for (mask = 1; mask; mask <<= 1) {
             if (guest_feat & mask && !(host_feat & mask)) {
@@ -2516,7 +2516,8 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
         env->features[FEAT_8000_0001_ECX] &= TCG_EXT3_FEATURES;
         env->features[FEAT_SVM] &= TCG_SVM_FEATURES;
     } else {
-        if (check_cpuid && kvm_check_features_against_host(cpu)
+        KVMState *s = kvm_state;
+        if (check_cpuid && kvm_check_features_against_host(s, cpu)
             && enforce_cpuid) {
             error_setg(&local_err,
                        "Host's CPU doesn't support requested features");
Signature · 1 line
1.8.3.1
#10 lore raw
In reply to Eduardo Habkost

Ping? Any comments?

Quoted 29 lines · Eduardo Habkost

This series simplifies kvm_cpu_fill_host() and kvm_check_features_against_host() to simply use FeatureWord & feature_word_info loops to fill/check feature words.

The initial motivation for this was to avoid hacks involving the "host" CPU class on the forthcoming conversion of CPU models to be X86CPU subclasses. Instead of requiring the kvm_arch_get_supported_cpuid() results to be stored in the class struct for "host" (thus requiring KVM initialization hacks).

Eduardo Habkost (8):
target-i386: kvm_cpu_fill_host(): Kill unused code
target-i386: kvm_cpu_fill_host(): No need to check level
target-i386: kvm_cpu_fill_host(): No need to check CPU vendor
target-i386: kvm_cpu_fill_host(): No need to check xlevel2
target-i386: kvm_cpu_fill_host(): Set all feature words at end of
function
target-i386: kvm_cpu_fill_host(): Fill feature words in a loop
target-i386: kvm_check_features_against_host(): Kill feature word
array
target-i386: kvm_check_features_against_host(): Don't use

    kvm_cpu_fill_host()
 target-i386/cpu.c | 89 +++++++++++++------------------------------------------
 1 file changed, 20 insertions(+), 69 deletions(-)
Signature · 1 line
1.8.3.1
Signature · 1 line
Eduardo