Skip to contentSkip to conversation

Improve cpuid x86 code.

PATCH In review Glauber Costa 6 participants 13 messages Jan 29, 2009 – Feb 4, 2009

lore
Share & tools
lore
#1 lore raw

PATCH 1/4 convert cpuid registration to KVM_SET_CPUID2

In reply to Glauber Costa

Use KVM_SET_CPUID2 instead of KVM_SET_CPUID. This will allow us to do registration of cpuid leaves without multiple calls to the kernel in the future.

---
 target-i386/kvm.c |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)
target-i386/kvm.c +5 -5
index 2412ae4..729568c 100644
@@ -36,8 +36,8 @@
 int kvm_arch_init_vcpu(CPUState *env)
 {
     struct {
-        struct kvm_cpuid cpuid;
-        struct kvm_cpuid_entry entries[100];
+        struct kvm_cpuid2 cpuid;
+        struct kvm_cpuid_entry2 entries[100];
     } __attribute__((packed)) cpuid_data;
     uint32_t limit, i, cpuid_i;
     uint32_t eax, ebx, ecx, edx;
@@ -48,7 +48,7 @@ int kvm_arch_init_vcpu(CPUState *env)
     limit = eax;
 
     for (i = 0; i <= limit; i++) {
-        struct kvm_cpuid_entry *c = &cpuid_data.entries[cpuid_i++];
+        struct kvm_cpuid_entry2 *c = &cpuid_data.entries[cpuid_i++];
 
         cpu_x86_cpuid(env, i, &eax, &ebx, &ecx, &edx);
         c->function = i;
@@ -62,7 +62,7 @@ int kvm_arch_init_vcpu(CPUState *env)
     limit = eax;
 
     for (i = 0x80000000; i <= limit; i++) {
-        struct kvm_cpuid_entry *c = &cpuid_data.entries[cpuid_i++];
+        struct kvm_cpuid_entry2 *c = &cpuid_data.entries[cpuid_i++];
 
         cpu_x86_cpuid(env, i, &eax, &ebx, &ecx, &edx);
         c->function = i;
@@ -74,7 +74,7 @@ int kvm_arch_init_vcpu(CPUState *env)
 
     cpuid_data.cpuid.nent = cpuid_i;
 
-    return kvm_vcpu_ioctl(env, KVM_SET_CPUID, &cpuid_data);
+    return kvm_vcpu_ioctl(env, KVM_SET_CPUID2, &cpuid_data);
 }
 
 static int kvm_has_msr_star(CPUState *env)
Signature · 1 line
1.5.6.5
#3 lore raw

PATCH 3/4 mask out forbidden cpuid features with kvm ioctl

In reply to Glauber Costa

KVM has a (so far unused) ioctl to inform userspace about supported cpuid bits in the current host.

The lack of this kind of checking can lead to bugs in which
a cpuid bit is exposed but not supported by the host kernel
(an example is fedora bug https://bugzilla.redhat.com/show_bug.cgi?id=481274)

The current host_cpuid code is replaced by this general check.

---
 target-i386/helper.c |   89 ++++++++++++++++++++++---------------------------
 1 files changed, 40 insertions(+), 49 deletions(-)
target-i386/helper.c +40 -49
index a28ab93..1410002 100644
@@ -1370,37 +1370,33 @@ static void breakpoint_handler(CPUState *env)
 }
 #endif /* !CONFIG_USER_ONLY */
 
-static void host_cpuid(uint32_t function, uint32_t *eax, uint32_t *ebx,
+static void host_cpuid(CPUState *env, uint32_t function, uint32_t *eax, uint32_t *ebx,
                        uint32_t *ecx, uint32_t *edx)
 {
 #if defined(CONFIG_KVM)
-    uint32_t vec[4];
+    static struct {
+        struct kvm_cpuid2 cpuid;
+        struct kvm_cpuid_entry2 entries[100];
+    } __attribute__((packed)) cpuid_base;
+    int i;
 
-#ifdef __x86_64__
-    asm volatile("cpuid"
-		 : "=a"(vec[0]), "=b"(vec[1]),
-		   "=c"(vec[2]), "=d"(vec[3])
-		 : "0"(function) : "cc");
-#else
-    asm volatile("pusha \n\t"
-		 "cpuid \n\t"
-		 "mov %%eax, 0(%1) \n\t"
-		 "mov %%ebx, 4(%1) \n\t"
-		 "mov %%ecx, 8(%1) \n\t"
-		 "mov %%edx, 12(%1) \n\t"
-		 "popa"
-		 : : "a"(function), "S"(vec)
-		 : "memory", "cc");
-#endif
+    if (cpuid_base.cpuid.nent == 0) {
+        cpuid_base.cpuid.nent = 100;
+        kvm_ioctl(env->kvm_state, KVM_GET_SUPPORTED_CPUID, &cpuid_base);
+    }
 
-    if (eax)
-	*eax = vec[0];
-    if (ebx)
-	*ebx = vec[1];
-    if (ecx)
-	*ecx = vec[2];
-    if (edx)
-	*edx = vec[3];
+    for (i = 0; i < cpuid_base.cpuid.nent; i++) {
+        if (cpuid_base.entries[i].function == function) {
+            if (eax)
+                *eax = cpuid_base.entries[i].eax;
+            if (ebx)
+                *ebx = cpuid_base.entries[i].ebx;
+            if (ecx)
+                *ecx = cpuid_base.entries[i].ecx;
+            if (edx)
+                *edx = cpuid_base.entries[i].edx;
+        }
+    }
 #endif
 }
 
@@ -1429,7 +1425,7 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index,
          * actuall cpu, and say goodbye to migration between different vendors
          * is you use compatibility mode. */
         if (kvm_enabled())
-            host_cpuid(0, NULL, ebx, ecx, edx);
+            host_cpuid(env, 0, NULL, ebx, ecx, edx);
         break;
     case 1:
         *eax = env->cpuid_version;
@@ -1437,9 +1433,18 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index,
         *ecx = env->cpuid_ext_features;
         *edx = env->cpuid_features;
 
-        /* "Hypervisor present" bit required for Microsoft SVVP */
-        if (kvm_enabled())
+        if (kvm_enabled()) {
+            uint32_t h_eax;
+            uint32_t h_ecx = ~0U;
+            uint32_t h_edx = ~0U;
+
+            host_cpuid(env, 1, &h_eax, NULL, &h_ecx, &h_edx);
+
+            *ecx &= h_ecx;
+            *edx &= h_edx;
+            /* "Hypervisor present" bit required for Microsoft SVVP */
             *ecx |= (1 << 31);
+        }
         break;
     case 2:
         /* cache info: needed for Pentium Pro compatibility */
@@ -1519,28 +1524,14 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index,
         *edx = env->cpuid_ext2_features;
 
         if (kvm_enabled()) {
-            uint32_t h_eax, h_edx;
-
-            host_cpuid(0x80000001, &h_eax, NULL, NULL, &h_edx);
-
-            /* disable CPU features that the host does not support */
-
-            /* long mode */
-            if ((h_edx & 0x20000000) == 0 /* || !lm_capable_kernel */)
-                *edx &= ~0x20000000;
-            /* syscall */
-            if ((h_edx & 0x00000800) == 0)
-                *edx &= ~0x00000800;
-            /* nx */
-            if ((h_edx & 0x00100000) == 0)
-                *edx &= ~0x00100000;
+            uint32_t h_eax;
+            uint32_t h_ecx = ~0U;
+            uint32_t h_edx = ~0U;
 
-            /* disable CPU features that KVM cannot support */
+            host_cpuid(env, 0x80000001, &h_eax, NULL, &h_ecx, &h_edx);
 
-            /* svm */
-            *ecx &= ~4UL;
-            /* 3dnow */
-            *edx &= ~0xc0000000;
+            *ecx &= h_ecx;
+            *edx &= h_edx;
         }
         break;
     case 0x80000002:
Signature · 1 line
1.5.6.5
#4 lore raw

PATCH 4/4 kvm pv features PART I

---
 kvm.h                |    5 +++++
 target-i386/helper.c |   19 ++++++++++++++++++-
 target-i386/kvm.c    |   30 ++++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+), 1 deletions(-)
kvm.h +5 -0
index efce145..49a2653 100644
@@ -17,6 +17,8 @@
 #include "config.h"
 
 #ifdef CONFIG_KVM
+#include <linux/kvm.h>
+#include <linux/kvm_para.h>
 extern int kvm_allowed;
 
 #define kvm_enabled() (kvm_allowed)
@@ -76,4 +78,7 @@ int kvm_arch_init(KVMState *s, int smp_cpus);
 
 int kvm_arch_init_vcpu(CPUState *env);
 
+/* x86 specific */
+uint32_t kvm_get_para_features(CPUState *env);
+
 #endif
target-i386/helper.c +18 -1
index 1410002..6bcd94d 100644
@@ -1405,7 +1405,10 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index,
                    uint32_t *ecx, uint32_t *edx)
 {
     /* test if maximum index reached */
-    if (index & 0x80000000) {
+    if (index & 0x40000000) {
+        /* Those are KVM-specific, dodge them without messing with
+         * exposed cpuid_level */
+    } else if (index & 0x80000000) {
         if (index > env->cpuid_xlevel)
             index = env->cpuid_level;
     } else {
@@ -1511,6 +1514,20 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index,
         *ecx = 0;
         *edx = 0;
         break;
+    case 0x40000000:
+        if (kvm_enabled()) {
+            /* KVM CPUID signature */
+            *eax = 0;
+            *ebx = 0x4b4d564b; /* KVMKVMK */
+            *ecx = 0x564b4d56; /* VMKV */
+            *edx = 0x0000004d; /* M */
+        }
+        break;
+    case 0x40000001:
+        if (kvm_enabled()) {
+            *eax = kvm_get_para_features(env);
+        }
+        break;
     case 0x80000000:
         *eax = env->cpuid_xlevel;
         *ebx = env->cpuid_vendor1;
target-i386/kvm.c +30 -0
index 64b24db..c9f0272 100644
@@ -56,6 +56,36 @@ static void kvm_fill_cpuid(CPUState *env, KVMCpuid *cpuid_data,
     }
 }
 
+static struct kvm_para_features {
+    int cap;
+    int feature;
+} para_features[] = {
+#ifdef KVM_CAP_CLOCKSOURCE
+       { KVM_CAP_CLOCKSOURCE, KVM_FEATURE_CLOCKSOURCE },
+#endif
+#ifdef KVM_CAP_NOP_IO_DELAY
+       { KVM_CAP_NOP_IO_DELAY, KVM_FEATURE_NOP_IO_DELAY },
+#endif
+#ifdef KVM_CAP_PV_MMU
+       { KVM_CAP_PV_MMU, KVM_FEATURE_MMU_OP },
+#endif
+#ifdef KVM_CAP_CR3_CACHE
+       { KVM_CAP_CR3_CACHE, KVM_FEATURE_CR3_CACHE },
+#endif
+};
+
+uint32_t kvm_get_para_features(CPUState *env)
+ {
+    uint32_t i, features = 0;
+
+    for (i = 0; i < ARRAY_SIZE(para_features); i++) {
+        if (kvm_ioctl(env->kvm_state, KVM_CHECK_EXTENSION, para_features[i].cap))
+            features |= (1 << para_features[i].feature);
+    }
+
+    return features;
+}
+
 int kvm_arch_init_vcpu(CPUState *env)
 {
     KVMCpuid cpuid_data;
Signature · 1 line
1.5.6.5
#0 lore raw

cover Improve cpuid x86 code.

Hey,

This is a resend of the cpuid patches, following Anthony's last suggestion. It proved to lead to a much better code IMHO.

I'm also using the existing host_cpuid function to mask out features, but now relying on kvm's provided ioctl. This seem to provide the expected behavior.

#2 lore raw

PATCH 2/4 Factor out common code in filling cpuid code

In reply to Glauber Costa

Use kvm_fill_cpuid to query qemu for cpuid data. There are two exactly equal instances of this code, and the future introduction of kvm paravirt features will make it three.

---
 target-i386/kvm.c |   46 +++++++++++++++++++++++-----------------------
 1 files changed, 23 insertions(+), 23 deletions(-)
target-i386/kvm.c +23 -23
index 729568c..64b24db 100644
@@ -33,22 +33,19 @@
     do { } while (0)
 #endif
 
-int kvm_arch_init_vcpu(CPUState *env)
-{
-    struct {
+typedef struct KVMCpuid {
         struct kvm_cpuid2 cpuid;
         struct kvm_cpuid_entry2 entries[100];
-    } __attribute__((packed)) cpuid_data;
-    uint32_t limit, i, cpuid_i;
-    uint32_t eax, ebx, ecx, edx;
+} KVMCpuid;
 
-    cpuid_i = 0;
-
-    cpu_x86_cpuid(env, 0, &eax, &ebx, &ecx, &edx);
-    limit = eax;
+static void kvm_fill_cpuid(CPUState *env, KVMCpuid *cpuid_data,
+                               uint32_t start, uint32_t limit)
+{
+    int i;
+    uint32_t eax, ebx, ecx, edx;
 
-    for (i = 0; i <= limit; i++) {
-        struct kvm_cpuid_entry2 *c = &cpuid_data.entries[cpuid_i++];
+    for (i = start; i <= limit; i++) {
+        struct kvm_cpuid_entry2 *c = &cpuid_data->entries[cpuid_data->cpuid.nent++];
 
         cpu_x86_cpuid(env, i, &eax, &ebx, &ecx, &edx);
         c->function = i;
@@ -57,22 +54,25 @@ int kvm_arch_init_vcpu(CPUState *env)
         c->ecx = ecx;
         c->edx = edx;
     }
+}
 
-    cpu_x86_cpuid(env, 0x80000000, &eax, &ebx, &ecx, &edx);
+int kvm_arch_init_vcpu(CPUState *env)
+{
+    KVMCpuid cpuid_data;
+    uint32_t limit;
+    uint32_t eax, ebx, ecx, edx;
+
+    cpuid_data.cpuid.nent = 0;
+
+    cpu_x86_cpuid(env, 0, &eax, &ebx, &ecx, &edx);
     limit = eax;
 
-    for (i = 0x80000000; i <= limit; i++) {
-        struct kvm_cpuid_entry2 *c = &cpuid_data.entries[cpuid_i++];
+    kvm_fill_cpuid(env, &cpuid_data, 0, limit);
 
-        cpu_x86_cpuid(env, i, &eax, &ebx, &ecx, &edx);
-        c->function = i;
-        c->eax = eax;
-        c->ebx = ebx;
-        c->ecx = ecx;
-        c->edx = edx;
-    }
+    cpu_x86_cpuid(env, 0x80000000, &eax, &ebx, &ecx, &edx);
+    limit = eax;
 
-    cpuid_data.cpuid.nent = cpuid_i;
+    kvm_fill_cpuid(env, &cpuid_data, 0x80000000, limit);
 
     return kvm_vcpu_ioctl(env, KVM_SET_CPUID2, &cpuid_data);
 }
Signature · 1 line
1.5.6.5
#5 lore raw

Re: [Qemu-devel] [PATCH 4/4] kvm pv features PART I

In reply to Glauber Costa
Quoted 1 line · Glauber Costa

Shame on you, what a great changelog entry!

This is where temporary becomes definitive by accident ;-)

Anthony, let me know if you are okay with this patch, and if so, I'll provide a more
meaningful changelog (that would be easy).

Quoted 111 lines
---
 kvm.h                |    5 +++++
 target-i386/helper.c |   19 ++++++++++++++++++-
 target-i386/kvm.c    |   30 ++++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+), 1 deletions(-)
kvm.h +5 -0
index efce145..49a2653 100644
@@ -17,6 +17,8 @@
 #include "config.h"
 
 #ifdef CONFIG_KVM
+#include <linux/kvm.h>
+#include <linux/kvm_para.h>
 extern int kvm_allowed;
 
 #define kvm_enabled() (kvm_allowed)
@@ -76,4 +78,7 @@ int kvm_arch_init(KVMState *s, int smp_cpus);
 
 int kvm_arch_init_vcpu(CPUState *env);
 
+/* x86 specific */
+uint32_t kvm_get_para_features(CPUState *env);
+
 #endif
target-i386/helper.c +18 -1
index 1410002..6bcd94d 100644
@@ -1405,7 +1405,10 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index,
                   uint32_t *ecx, uint32_t *edx)
 {
    /* test if maximum index reached */
-    if (index & 0x80000000) {
+    if (index & 0x40000000) {
+        /* Those are KVM-specific, dodge them without messing with
+         * exposed cpuid_level */
+    } else if (index & 0x80000000) {
        if (index > env->cpuid_xlevel)
            index = env->cpuid_level;
    } else {
@@ -1511,6 +1514,20 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index,
        *ecx = 0;
        *edx = 0;
        break;
+    case 0x40000000:
+        if (kvm_enabled()) {
+            /* KVM CPUID signature */
+            *eax = 0;
+            *ebx = 0x4b4d564b; /* KVMKVMK */
+            *ecx = 0x564b4d56; /* VMKV */
+            *edx = 0x0000004d; /* M */
+        }
+        break;
+    case 0x40000001:
+        if (kvm_enabled()) {
+            *eax = kvm_get_para_features(env);
+        }
+        break;
    case 0x80000000:
        *eax = env->cpuid_xlevel;
        *ebx = env->cpuid_vendor1;
target-i386/kvm.c +30 -1
index 64b24db..c9f0272 100644
@@ -56,6 +56,36 @@ static void kvm_fill_cpuid(CPUState *env, KVMCpuid *cpuid_data,
    }
 }
 
+static struct kvm_para_features {
+    int cap;
+    int feature;
+} para_features[] = {
+#ifdef KVM_CAP_CLOCKSOURCE
+       { KVM_CAP_CLOCKSOURCE, KVM_FEATURE_CLOCKSOURCE },
+#endif
+#ifdef KVM_CAP_NOP_IO_DELAY
+       { KVM_CAP_NOP_IO_DELAY, KVM_FEATURE_NOP_IO_DELAY },
+#endif
+#ifdef KVM_CAP_PV_MMU
+       { KVM_CAP_PV_MMU, KVM_FEATURE_MMU_OP },
+#endif
+#ifdef KVM_CAP_CR3_CACHE
+       { KVM_CAP_CR3_CACHE, KVM_FEATURE_CR3_CACHE },
+#endif
+};
+
+uint32_t kvm_get_para_features(CPUState *env)
+ {
+    uint32_t i, features = 0;
+
+    for (i = 0; i < ARRAY_SIZE(para_features); i++) {
+        if (kvm_ioctl(env->kvm_state, KVM_CHECK_EXTENSION, para_features[i].cap))
+            features |= (1 << para_features[i].feature);
+    }
+
+    return features;
+}
+
 int kvm_arch_init_vcpu(CPUState *env)
 {
    KVMCpuid cpuid_data;
--

1.5.6.5

Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#6 lore raw

[Qemu-devel] Re: [PATCH 3/4] mask out forbidden cpuid features with kvm ioctl

In reply to Glauber Costa
Quoted 11 lines · Glauber Costa

KVM has a (so far unused) ioctl to inform userspace about supported cpuid bits in the current host.

The lack of this kind of checking can lead to bugs in which
a cpuid bit is exposed but not supported by the host kernel
(an example is fedora bug https://bugzilla.redhat.com/show_bug.cgi?id=481274)

The current host_cpuid code is replaced by this general check.

So what do we do about live migration? Does KVM mask a set of bits unconditionally? Before, we were masking unconditionally which should insulate migration reasonable well.

Regards,

Anthony Liguori

Quoted 134 lines
---
 target-i386/helper.c |   89 ++++++++++++++++++++++---------------------------
 1 files changed, 40 insertions(+), 49 deletions(-)
target-i386/helper.c +40 -49
index a28ab93..1410002 100644
@@ -1370,37 +1370,33 @@ static void breakpoint_handler(CPUState *env)
 }
 #endif /* !CONFIG_USER_ONLY */
 
-static void host_cpuid(uint32_t function, uint32_t *eax, uint32_t *ebx,
+static void host_cpuid(CPUState *env, uint32_t function, uint32_t *eax, uint32_t *ebx,
                        uint32_t *ecx, uint32_t *edx)
 {
 #if defined(CONFIG_KVM)
-    uint32_t vec[4];
+    static struct {
+        struct kvm_cpuid2 cpuid;
+        struct kvm_cpuid_entry2 entries[100];
+    } __attribute__((packed)) cpuid_base;
+    int i;
 
-#ifdef __x86_64__
-    asm volatile("cpuid"
-		 : "=a"(vec[0]), "=b"(vec[1]),
-		   "=c"(vec[2]), "=d"(vec[3])
-		 : "0"(function) : "cc");
-#else
-    asm volatile("pusha \n\t"
-		 "cpuid \n\t"
-		 "mov %%eax, 0(%1) \n\t"
-		 "mov %%ebx, 4(%1) \n\t"
-		 "mov %%ecx, 8(%1) \n\t"
-		 "mov %%edx, 12(%1) \n\t"
-		 "popa"
-		 : : "a"(function), "S"(vec)
-		 : "memory", "cc");
-#endif
+    if (cpuid_base.cpuid.nent == 0) {
+        cpuid_base.cpuid.nent = 100;
+        kvm_ioctl(env->kvm_state, KVM_GET_SUPPORTED_CPUID, &cpuid_base);
+    }
 
-    if (eax)
-	*eax = vec[0];
-    if (ebx)
-	*ebx = vec[1];
-    if (ecx)
-	*ecx = vec[2];
-    if (edx)
-	*edx = vec[3];
+    for (i = 0; i < cpuid_base.cpuid.nent; i++) {
+        if (cpuid_base.entries[i].function == function) {
+            if (eax)
+                *eax = cpuid_base.entries[i].eax;
+            if (ebx)
+                *ebx = cpuid_base.entries[i].ebx;
+            if (ecx)
+                *ecx = cpuid_base.entries[i].ecx;
+            if (edx)
+                *edx = cpuid_base.entries[i].edx;
+        }
+    }
 #endif
 }
 
@@ -1429,7 +1425,7 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index,
          * actuall cpu, and say goodbye to migration between different vendors
          * is you use compatibility mode. */
         if (kvm_enabled())
-            host_cpuid(0, NULL, ebx, ecx, edx);
+            host_cpuid(env, 0, NULL, ebx, ecx, edx);
         break;
     case 1:
         *eax = env->cpuid_version;
@@ -1437,9 +1433,18 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index,
         *ecx = env->cpuid_ext_features;
         *edx = env->cpuid_features;
 
-        /* "Hypervisor present" bit required for Microsoft SVVP */
-        if (kvm_enabled())
+        if (kvm_enabled()) {
+            uint32_t h_eax;
+            uint32_t h_ecx = ~0U;
+            uint32_t h_edx = ~0U;
+
+            host_cpuid(env, 1, &h_eax, NULL, &h_ecx, &h_edx);
+
+            *ecx &= h_ecx;
+            *edx &= h_edx;
+            /* "Hypervisor present" bit required for Microsoft SVVP */
             *ecx |= (1 << 31);
+        }
         break;
     case 2:
         /* cache info: needed for Pentium Pro compatibility */
@@ -1519,28 +1524,14 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index,
         *edx = env->cpuid_ext2_features;
 
         if (kvm_enabled()) {
-            uint32_t h_eax, h_edx;
-
-            host_cpuid(0x80000001, &h_eax, NULL, NULL, &h_edx);
-
-            /* disable CPU features that the host does not support */
-
-            /* long mode */
-            if ((h_edx & 0x20000000) == 0 /* || !lm_capable_kernel */)
-                *edx &= ~0x20000000;
-            /* syscall */
-            if ((h_edx & 0x00000800) == 0)
-                *edx &= ~0x00000800;
-            /* nx */
-            if ((h_edx & 0x00100000) == 0)
-                *edx &= ~0x00100000;
+            uint32_t h_eax;
+            uint32_t h_ecx = ~0U;
+            uint32_t h_edx = ~0U;
 
-            /* disable CPU features that KVM cannot support */
+            host_cpuid(env, 0x80000001, &h_eax, NULL, &h_ecx, &h_edx);
 
-            /* svm */
-            *ecx &= ~4UL;
-            /* 3dnow */
-            *edx &= ~0xc0000000;
+            *ecx &= h_ecx;
+            *edx &= h_edx;
         }
         break;
     case 0x80000002:
#12 lore raw
In reply to Glauber Costa
Quoted 8 lines · Glauber Costa

Hey,

This is a resend of the cpuid patches, following Anthony's last suggestion. It proved to lead to a much better code IMHO.

I'm also using the existing host_cpuid function to mask out features, but now relying on kvm's provided ioctl. This seem to provide the expected behavior.

I had submitted patches previously to convert to get_cpuid2. If Anthony has them in his queue, we're going to have clashes. My patches add full support for cupid functions 4, 0xb and 0xd that's missing currently.

I also have a patch ready for function 2; guess I'll send them after this stuff is in.

#7 lore raw

Re: [Qemu-devel] Re: [PATCH 3/4] mask out forbidden cpuid features with kvm ioctl

In reply to Anthony Liguori
Quoted 10 lines · Anthony Liguori
Quoted 9 lines · Glauber Costa

KVM has a (so far unused) ioctl to inform userspace about supported cpuid bits in the current host.

The lack of this kind of checking can lead to bugs in which a cpuid bit is exposed but not supported by the host kernel (an example is fedora bug
https://bugzilla.redhat.com/show_bug.cgi?id=481274)

Quoted 9 lines
Quoted 4 lines

The current host_cpuid code is replaced by this general check.

So what do we do about live migration? Does KVM mask a set of bits unconditionally? Before, we were masking unconditionally which should insulate migration reasonable well.

Just to be clear:

Are you raising the flag against the lack of unconditional mask only, or about it + querying kvm.ko for supported bits?

I think we need to query it anyway, because calling cpuid will only give you information about your cpu, and querying the kernel will give you this, plus information about what the kernel supports (think of the 32 bit kernel on 64 bit hardware case), plus whatever else kvm wants to mask for whatever reason.

About migration, I was under the impression that the solution we were going towards was based on defining a kvm machine type that would contain a common set of features among the hosts involved. If this is the case, I believe we don't need to unconditionally mask out anything, since the features we expose in the machine type won't even have the problematic features.

If this is not the case, I'm fine with redoing this patch with querying kvm.ko + unconditional mask of potentially problematic features.

Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#8 lore raw

Re: [Qemu-devel] Re: [PATCH 3/4] mask out forbidden cpuid features with kvm ioctl

In reply to Anthony Liguori
Quoted 8 lines · Anthony Liguori
Quoted 7 lines · Glauber Costa

KVM has a (so far unused) ioctl to inform userspace about supported cpuid bits in the current host.

The lack of this kind of checking can lead to bugs in which
a cpuid bit is exposed but not supported by the host kernel
(an example is fedora bug https://bugzilla.redhat.com/show_bug.cgi?id=481274)

Quoted 6 lines
Quoted 4 lines

The current host_cpuid code is replaced by this general check.

So what do we do about live migration?

We need a way to completely specify cpuid from the command line. The management utility would figure out what is supported (driven by both cpu capabilities and installed kvm capabilities) and fgure out a greatest common denominator, or something.

Quoted 1 line

Does KVM mask a set of bits unconditionally?

The kernel doesn't touch cpuid (except nx due to an historical mistake).

Quoted 1 line

Before, we were masking unconditionally which should insulate migration reasonable well.

That suits migration, but not other uses of qemu.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#10 lore raw

Re: [Qemu-devel] Re: [PATCH 3/4] mask out forbidden cpuid features with kvm ioctl

In reply to Anthony Liguori
Quoted 8 lines · Anthony Liguori
Quoted 7 lines · Glauber Costa

KVM has a (so far unused) ioctl to inform userspace about supported cpuid bits in the current host.

The lack of this kind of checking can lead to bugs in which
a cpuid bit is exposed but not supported by the host kernel
(an example is fedora bug https://bugzilla.redhat.com/show_bug.cgi?id=481274)

Quoted 6 lines
Quoted 4 lines

The current host_cpuid code is replaced by this general check.

So what do we do about live migration?

We need a way to completely specify cpuid from the command line. The management utility would figure out what is supported (driven by both cpu capabilities and installed kvm capabilities) and fgure out a greatest common denominator, or something.

Quoted 1 line

Does KVM mask a set of bits unconditionally?

The kernel doesn't touch cpuid (except nx due to an historical mistake).

Quoted 1 line

Before, we were masking unconditionally which should insulate migration reasonable well.

That suits migration, but not other uses of qemu.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#9 lore raw

Re: [Qemu-devel] Re: [PATCH 3/4] mask out forbidden cpuid features with kvm ioctl

In reply to Avi Kivity
Quoted 9 lines · Avi Kivity
Quoted 8 lines · Anthony Liguori
Quoted 7 lines · Glauber Costa

KVM has a (so far unused) ioctl to inform userspace about supported cpuid bits in the current host.

The lack of this kind of checking can lead to bugs in which
a cpuid bit is exposed but not supported by the host kernel
(an example is fedora bug https://bugzilla.redhat.com/show_bug.cgi?id=481274)

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

The current host_cpuid code is replaced by this general check.

So what do we do about live migration?

We need a way to completely specify cpuid from the command line. The management utility would figure out what is supported (driven by both cpu capabilities and installed kvm capabilities) and fgure out a greatest common denominator, or something.

What is the default case? Pass everything through or pick a reasonable small, constant subset? That is, is -cpuid a union or an intersection.

Regards,

Anthony Liguori

Quoted 8 lines
Quoted 1 line

Does KVM mask a set of bits unconditionally?

The kernel doesn't touch cpuid (except nx due to an historical mistake).

Quoted 1 line

Before, we were masking unconditionally which should insulate migration reasonable well.

That suits migration, but not other uses of qemu.

#11 lore raw

Re: [Qemu-devel] Re: [PATCH 3/4] mask out forbidden cpuid features with kvm ioctl

In reply to Avi Kivity
Quoted 15 lines · Avi Kivity
Quoted 14 lines · Anthony Liguori
Quoted 12 lines · Glauber Costa

KVM has a (so far unused) ioctl to inform userspace about supported cpuid bits in the current host.

The lack of this kind of checking can lead to bugs in which a cpuid bit is exposed but not supported by the host kernel (an example is fedora bug
https://bugzilla.redhat.com/show_bug.cgi?id=481274)

The current host_cpuid code is replaced by this general check.

Quoted 6 lines
Quoted 1 line

So what do we do about live migration?

We need a way to completely specify cpuid from the command line. The management utility would figure out what is supported (driven by both cpu capabilities and installed kvm capabilities) and fgure out a greatest common denominator, or something.

I had proposed this a few days ago, pasted below:

http://thread.gmane.org/gmane.comp.emulators.qemu/37134/focus=37306

---

What I'm thinking of is moving all the CPU definitions out from helper.c into $(PREFIX)/usr/share/qemu/cpu-defs/... with one file holding one definition, like

$ cat core2duo.def
model= 15
family = 16
stepping = 11
level = 10
.
.
.

Adding a new def then just becomes a matter of submitting a text file for inclusion and doesn't need any recompilation.

Also, there are many details not captured in a CPU def, like the cache sizes and types. They're hard-coded for all the CPUs in the cpuid helper function. So we could have those values too incorporated in the CPU def file an fall back to the defaults when not available.

When we do get some kind of a machine FDT, we can specify the cpu type by either pointing to one of these files or by constructing a new CPU type by specifying individual values.

---

I have a patch that adds some of this functionality to the command line. It needs cleaning up, but shows the general idea that I'm progressing in. It takes as input a file from which to read the cpuid definitions. The cpuid defs could be straight from a host or from a management application that has computed the "greatest common denominator":

qemu/target-i386/cpu.h +6 -0
index 944e386..98dc4d0 100644
@@ -643,6 +643,12 @@ typedef struct CPUX86State {
     int mp_state;
 } CPUX86State;
 
+struct KMScpuid {
+    uint32_t function, eax, ebx, ecx, edx;
+};
+extern struct KMScpuid *kms_cpuids;
+extern uint32_t kms_leaves;
+
 CPUX86State *cpu_x86_init(const char *cpu_model);
 int cpu_x86_exec(CPUX86State *s);
 void cpu_x86_close(CPUX86State *s);
qemu/target-i386/helper.c +125 -19

220 lines not on this page · raw message