qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Glauber Costa <glommer@redhat.com>
To: qemu-devel@nongnu.org
Cc: aliguori@us.ibm.com
Subject: [Qemu-devel] [PATCH 3/3] mask out forbidden cpuid features
Date: Wed, 28 Jan 2009 14:02:05 -0500	[thread overview]
Message-ID: <1233169325-5487-4-git-send-email-glommer@redhat.com> (raw)
In-Reply-To: <1233169325-5487-3-git-send-email-glommer@redhat.com>

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)

Signed-off-by: Glauber Costa <glommer@redhat.com>
---
 target-i386/kvm.c |   44 +++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 39 insertions(+), 5 deletions(-)

diff --git a/target-i386/kvm.c b/target-i386/kvm.c
index 87ddff5..08584dd 100644
--- a/target-i386/kvm.c
+++ b/target-i386/kvm.c
@@ -68,8 +68,18 @@ int kvm_arch_init_vcpu(CPUState *env)
         struct kvm_cpuid2 cpuid;
         struct kvm_cpuid_entry2 entries[100];
     } __attribute__((packed)) cpuid_data;
-    uint32_t limit, i, cpuid_i = 0;
+
+    struct {
+        struct kvm_cpuid2 cpuid;
+        struct kvm_cpuid_entry2 entries[100];
+    } __attribute__((packed)) cpuid_base_data;
+
+    uint32_t limit, i, r, cpuid_i = 0;
     uint32_t eax, ebx, ecx, edx;
+    struct kvm_cpuid_entry2 *func1 = NULL;
+    struct kvm_cpuid_entry2 *ext_func1 = NULL;
+    uint32_t ecx_mask = ~0U;
+    uint32_t edx_mask = ~0U;
 #ifdef KVM_CPUID_SIGNATURE
     struct kvm_cpuid_entry2 *pv_ent;
 
@@ -89,32 +99,56 @@ int kvm_arch_init_vcpu(CPUState *env)
 #endif
 
 
+    cpuid_base_data.cpuid.nent = 100;
+    r = kvm_ioctl(env->kvm_state, KVM_GET_SUPPORTED_CPUID, &cpuid_base_data);
+    if (r == -1)
+        cpuid_base_data.cpuid.nent = 0;
+
+    for (i = 0; i < cpuid_base_data.cpuid.nent; i++) {
+        if (cpuid_base_data.entries[i].function == 1)
+            func1 = &cpuid_base_data.entries[i];
+        if (cpuid_base_data.entries[i].function == 0x80000001)
+            ext_func1 = &cpuid_base_data.entries[i];
+    }
+
     cpu_x86_cpuid(env, 0, &eax, &ebx, &ecx, &edx);
     limit = eax;
 
     for (i = 0; i <= limit; i++) {
         struct kvm_cpuid_entry2 *c = &cpuid_data.entries[cpuid_i++];
 
+        if (func1 && (i == 1)) {
+            ecx_mask = func1->ecx;
+            edx_mask = func1->edx;
+        } else
+            ecx_mask = edx_mask = ~0U;
+
         cpu_x86_cpuid(env, i, &eax, &ebx, &ecx, &edx);
         c->function = i;
         c->eax = eax;
         c->ebx = ebx;
-        c->ecx = ecx;
-        c->edx = edx;
+        c->ecx = ecx & ecx_mask;
+        c->edx = edx & edx_mask;
     }
 
     cpu_x86_cpuid(env, 0x80000000, &eax, &ebx, &ecx, &edx);
     limit = eax;
 
     for (i = 0x80000000; i <= limit; i++) {
+
         struct kvm_cpuid_entry2 *c = &cpuid_data.entries[cpuid_i++];
+        if (ext_func1 && (i == 0x80000001)) {
+            ecx_mask = ext_func1->ecx;
+            edx_mask = ext_func1->edx;
+        } else
+            ecx_mask = edx_mask = ~0U;
 
         cpu_x86_cpuid(env, i, &eax, &ebx, &ecx, &edx);
         c->function = i;
         c->eax = eax;
         c->ebx = ebx;
-        c->ecx = ecx;
-        c->edx = edx;
+        c->ecx = ecx & ecx_mask;
+        c->edx = edx & edx_mask;
     }
 
     cpuid_data.cpuid.nent = cpuid_i;
-- 
1.5.6.5

  reply	other threads:[~2009-01-28 19:02 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-28 19:02 [Qemu-devel] [PATCH 0/3] Enhance kvm cpuid Glauber Costa
2009-01-28 19:02 ` [Qemu-devel] [PATCH 1/3] expose paravirt feature list to cpuid Glauber Costa
2009-01-28 19:02   ` [Qemu-devel] [PATCH 2/3] convert cpuid registration to KVM_SET_CPUID2 Glauber Costa
2009-01-28 19:02     ` Glauber Costa [this message]
2009-01-28 21:36   ` [Qemu-devel] Re: [PATCH 1/3] expose paravirt feature list to cpuid Anthony Liguori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1233169325-5487-4-git-send-email-glommer@redhat.com \
    --to=glommer@redhat.com \
    --cc=aliguori@us.ibm.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).