From: Eduardo Habkost <ehabkost@redhat.com>
To: qemu-devel@nongnu.org
Cc: kvm@vger.kernel.org, "Gleb Natapov" <gleb@redhat.com>,
"Marcelo Tosatti" <mtosatti@redhat.com>,
"Avi Kivity" <avi@redhat.com>,
"Igor Mammedov" <imammedo@redhat.com>,
"Andreas Färber" <afaerber@suse.de>
Subject: [Qemu-devel] [PATCH 13/15] i386: kvm: filter CPUID feature words earlier, on cpu.c
Date: Thu, 4 Oct 2012 17:49:05 -0300 [thread overview]
Message-ID: <1349383747-19383-14-git-send-email-ehabkost@redhat.com> (raw)
In-Reply-To: <1349383747-19383-1-git-send-email-ehabkost@redhat.com>
cpu.c contains the code that will check if all requested CPU features
are available, so the filtering of KVM features must be there, so we can
implement "check" and "enforce" properly.
The only point where kvm_arch_init_vcpu() is called on i386 is:
- cpu_x86_init()
- x86_cpu_realize() (after cpu_x86_register() is called)
- qemu_init_vcpu()
- qemu_kvm_start_vcpu()
- qemu_kvm_thread_fn() (on a new thread)
- kvm_init_vcpu()
- kvm_arch_init_vcpu()
With this patch, the filtering will be done earlier, at:
- cpu_x86_init()
- cpu_x86_register() (before x86_cpu_realize() is called)
Also, the KVM CPUID filtering will now be done at the same place where
the TCG CPUID feature filtering is done. Later, the code can be changed
to use the same filtering code for the "check" and "enforce" modes, as
now the cpu.c code knows exactly which CPU features are going to be
exposed to the guest (and much earlier).
One thing I was worrying about when doing this is that
kvm_arch_get_supported_cpuid() depends on kvm_irqchip_in_kernel(), and
maybe the 'kvm_kernel_irqchip' global variable wasn't initialized yet at
CPU creation time. But kvm_kernel_irqchip is initialized during
kvm_init(), that is called very early (much earlier than the machine
init function), and kvm_init() is already a requirement to run the
GET_SUPPORTED_CPUID ioctl() (as kvm_init() initializes the kvm_state
global variable).
Side note: it would be nice to keep KVM-specific code inside kvm.c. The
problem is that properly implementing -cpu check/enforce code (that's
inside cpu.c) depends directly on the feature bit filtering done using
kvm_arch_get_supported_cpuid(). Currently -cpu check/enforce is broken
because it simply uses the host CPU feature bits instead of
GET_SUPPORTED_CPUID, and we need to fix that.
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
kvm.h | 1 +
target-i386/cpu.c | 30 ++++++++++++++++++++++++++++++
target-i386/kvm.c | 18 ------------------
3 files changed, 31 insertions(+), 18 deletions(-)
diff --git a/kvm.h b/kvm.h
index dea2998..150f95d 100644
--- a/kvm.h
+++ b/kvm.h
@@ -20,6 +20,7 @@
#ifdef CONFIG_KVM
#include <linux/kvm.h>
+#include <linux/kvm_para.h>
#endif
extern int kvm_allowed;
diff --git a/target-i386/cpu.c b/target-i386/cpu.c
index bb1e44e..a5c1628 100644
--- a/target-i386/cpu.c
+++ b/target-i386/cpu.c
@@ -1360,6 +1360,32 @@ CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
return cpu_list;
}
+#ifdef CONFIG_KVM
+static void filter_features_for_kvm(X86CPU *cpu)
+{
+ CPUX86State *env = &cpu->env;
+ KVMState *s = kvm_state;
+
+ env->cpuid_features &= kvm_arch_get_supported_cpuid(s, 1, 0, R_EDX);
+
+ env->cpuid_ext_features &= kvm_arch_get_supported_cpuid(s, 1, 0, R_ECX);
+
+ env->cpuid_ext2_features &= kvm_arch_get_supported_cpuid(s, 0x80000001,
+ 0, R_EDX);
+ env->cpuid_ext3_features &= kvm_arch_get_supported_cpuid(s, 0x80000001,
+ 0, R_ECX);
+ env->cpuid_svm_features &= kvm_arch_get_supported_cpuid(s, 0x8000000A,
+ 0, R_EDX);
+
+ env->cpuid_kvm_features &=
+ kvm_arch_get_supported_cpuid(s, KVM_CPUID_FEATURES, 0, R_EAX);
+
+ env->cpuid_ext4_features &= kvm_arch_get_supported_cpuid(s, 0xC0000001,
+ 0, R_EDX);
+
+}
+#endif
+
int cpu_x86_register(X86CPU *cpu, const char *cpu_model)
{
CPUX86State *env = &cpu->env;
@@ -1417,6 +1443,10 @@ int cpu_x86_register(X86CPU *cpu, const char *cpu_model)
);
env->cpuid_ext3_features &= TCG_EXT3_FEATURES;
env->cpuid_svm_features &= TCG_SVM_FEATURES;
+ } else {
+#ifdef CONFIG_KVM
+ filter_features_for_kvm(cpu);
+#endif
}
object_property_set_str(OBJECT(cpu), def->model_id, "model-id", &error);
if (error_is_set(&error)) {
diff --git a/target-i386/kvm.c b/target-i386/kvm.c
index 335b3e7..0257083 100644
--- a/target-i386/kvm.c
+++ b/target-i386/kvm.c
@@ -410,30 +410,12 @@ int kvm_arch_init_vcpu(CPUX86State *env)
struct kvm_cpuid2 cpuid;
struct kvm_cpuid_entry2 entries[100];
} QEMU_PACKED cpuid_data;
- KVMState *s = env->kvm_state;
uint32_t limit, i, j, cpuid_i;
uint32_t unused;
struct kvm_cpuid_entry2 *c;
uint32_t signature[3];
int r;
- env->cpuid_features &= kvm_arch_get_supported_cpuid(s, 1, 0, R_EDX);
-
- env->cpuid_ext_features &= kvm_arch_get_supported_cpuid(s, 1, 0, R_ECX);
-
- env->cpuid_ext2_features &= kvm_arch_get_supported_cpuid(s, 0x80000001,
- 0, R_EDX);
- env->cpuid_ext3_features &= kvm_arch_get_supported_cpuid(s, 0x80000001,
- 0, R_ECX);
- env->cpuid_svm_features &= kvm_arch_get_supported_cpuid(s, 0x8000000A,
- 0, R_EDX);
-
- env->cpuid_kvm_features &=
- kvm_arch_get_supported_cpuid(s, KVM_CPUID_FEATURES, 0, R_EAX);
-
- env->cpuid_ext4_features &= kvm_arch_get_supported_cpuid(s, 0xC0000001,
- 0, R_EDX);
-
cpuid_i = 0;
/* Paravirtualization CPUIDs */
--
1.7.11.4
next prev parent reply other threads:[~2012-10-04 20:49 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-10-04 20:48 [Qemu-devel] [PATCH 00/15] QEMU KVM_GET_SUPPORTED_CPUID cleanups and fixes Eduardo Habkost
2012-10-04 20:48 ` [Qemu-devel] [PATCH 01/15] i386: kvm: kvm_arch_get_supported_cpuid: move R_EDX hack outside of for loop Eduardo Habkost
2012-10-04 20:48 ` [Qemu-devel] [PATCH 02/15] i386: kvm: kvm_arch_get_supported_cpuid: clean up has_kvm_features check Eduardo Habkost
2012-10-04 20:48 ` [Qemu-devel] [PATCH 03/15] i386: kvm: kvm_arch_get_supported_cpuid: use 'entry' variable Eduardo Habkost
2012-10-04 20:48 ` [Qemu-devel] [PATCH 04/15] i386: kvm: extract register switch to cpuid_entry_get_reg() function Eduardo Habkost
2012-10-04 20:48 ` [Qemu-devel] [PATCH 05/15] i386: kvm: extract CPUID entry lookup to cpuid_find_entry() function Eduardo Habkost
2012-10-04 20:48 ` [Qemu-devel] [PATCH 06/15] i386: kvm: extract try_get_cpuid() loop to get_supported_cpuid() function Eduardo Habkost
2012-10-04 20:48 ` [Qemu-devel] [PATCH 07/15] i386: kvm: kvm_arch_get_supported_cpuid: replace if+switch with single 'if' Eduardo Habkost
2012-10-04 20:49 ` [Qemu-devel] [PATCH 08/15] i386: kvm: set CPUID_EXT_HYPERVISOR on kvm_arch_get_supported_cpuid() Eduardo Habkost
2012-10-04 20:49 ` [Qemu-devel] [PATCH 09/15] i386: kvm: set CPUID_EXT_TSC_DEADLINE_TIMER " Eduardo Habkost
2012-10-04 20:49 ` [Qemu-devel] [PATCH 10/15] i386: kvm: x2apic is not supported without in-kernel irqchip Eduardo Habkost
2012-10-04 20:49 ` [Qemu-devel] [PATCH 11/15] i386: kvm: mask cpuid_kvm_features earlier Eduardo Habkost
2012-10-04 20:49 ` [Qemu-devel] [PATCH 12/15] i386: kvm: mask cpuid_ext4_features bits earlier Eduardo Habkost
2012-10-04 20:49 ` Eduardo Habkost [this message]
2012-10-04 20:49 ` [Qemu-devel] [PATCH 14/15] i386: kvm: reformat filter_features_for_kvm() code Eduardo Habkost
2012-10-04 20:49 ` [Qemu-devel] [PATCH 15/15] i386: kvm: filter CPUID leaf 7 based on GET_SUPPORTED_CPUID, too Eduardo Habkost
2012-10-24 17:54 ` [Qemu-devel] [PATCH 00/15] QEMU KVM_GET_SUPPORTED_CPUID cleanups and fixes Eduardo Habkost
2012-10-26 19:37 ` Marcelo Tosatti
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=1349383747-19383-14-git-send-email-ehabkost@redhat.com \
--to=ehabkost@redhat.com \
--cc=afaerber@suse.de \
--cc=avi@redhat.com \
--cc=gleb@redhat.com \
--cc=imammedo@redhat.com \
--cc=kvm@vger.kernel.org \
--cc=mtosatti@redhat.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).