Skip to contentSkip to conversation

x86: properly handle KVM emulation of hyperv

PATCH In review Jason Wang 3 participants 5 messages Jul 19, 2013

Subsystems (4)
lore
Share & tools
lore

Jason Wang

author
#0 lore raw

PATCH 1/1 x86: properly handle KVM emulation of hyperv

Recent kvm has some basic support of hyperv, this will cause the guest to identify itself running on top of hyperv instead of kvm which will disable kvm pv functionality. Solve this by check kvm para platform first and also check KVM_CPUID_SIGNATURE_NEXT which were set when kvm emulate hyperv.

Cc Gleb Natapov <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Vadim Rozenfeld <[email protected]>
Cc K. Y. Srinivasan <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
---
 arch/x86/include/asm/kvm_para.h      |   25 +++++++++++++++++--------
 arch/x86/include/uapi/asm/kvm_para.h |    1 +
 arch/x86/kernel/cpu/hypervisor.c     |    2 +-
 3 files changed, 19 insertions(+), 9 deletions(-)
arch/x86/include/asm/kvm_para.h +17 -8
index 695399f..aa8f3a7 100644
@@ -85,22 +85,31 @@ static inline long kvm_hypercall4(unsigned int nr, unsigned long p1,
 	return ret;
 }
 
-static inline bool kvm_para_available(void)
+static inline bool kvm_para_available_function(unsigned int function)
 {
 	unsigned int eax, ebx, ecx, edx;
 	char signature[13];
 
+	cpuid(function, &eax, &ebx, &ecx, &edx);
+	memcpy(signature + 0, &ebx, 4);
+	memcpy(signature + 4, &ecx, 4);
+	memcpy(signature + 8, &edx, 4);
+	signature[12] = 0;
+
+	if (strcmp(signature, "KVMKVMKVM") == 0)
+		return true;
+
+	return false;
+}
+
+static inline bool kvm_para_available(void)
+{
 	if (boot_cpu_data.cpuid_level < 0)
 		return false;	/* So we don't blow up on old processors */
 
 	if (cpu_has_hypervisor) {
-		cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
-		memcpy(signature + 0, &ebx, 4);
-		memcpy(signature + 4, &ecx, 4);
-		memcpy(signature + 8, &edx, 4);
-		signature[12] = 0;
-
-		if (strcmp(signature, "KVMKVMKVM") == 0)
+		if (kvm_para_available_function(KVM_CPUID_SIGNATURE) ||
+		    kvm_para_available_function(KVM_CPUID_SIGNATURE_NEXT))
 			return true;
 	}
 
arch/x86/include/uapi/asm/kvm_para.h +1 -0
index 06fdbd9..4e3ffd8 100644
@@ -8,6 +8,7 @@
  * should be used to determine that a VM is running under KVM.
  */
 #define KVM_CPUID_SIGNATURE	0x40000000
+#define KVM_CPUID_SIGNATURE_NEXT	0x40000100
 
 /* This CPUID returns a feature bitmap in eax.  Before enabling a particular
  * paravirtualization, the appropriate feature bit should be checked.
arch/x86/kernel/cpu/hypervisor.c +1 -1
index 8727921..3e149b6 100644
@@ -36,10 +36,10 @@ static const __initconst struct hypervisor_x86 * const hypervisors[] =
 	&x86_hyper_xen_hvm,
 #endif
 	&x86_hyper_vmware,
-	&x86_hyper_ms_hyperv,
 #ifdef CONFIG_KVM_GUEST
 	&x86_hyper_kvm,
 #endif
+	&x86_hyper_ms_hyperv,
 };
 
 const struct hypervisor_x86 *x86_hyper;
Signature · 1 line
1.7.1
maintainer
#1 lore raw

Il 19/07/2013 12:25, Jason Wang ha scritto:

Quoted 52 lines

Recent kvm has some basic support of hyperv, this will cause the guest to identify itself running on top of hyperv instead of kvm which will disable kvm pv functionality. Solve this by check kvm para platform first and also check KVM_CPUID_SIGNATURE_NEXT which were set when kvm emulate hyperv.

Cc Gleb Natapov <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Vadim Rozenfeld <[email protected]>
Cc K. Y. Srinivasan <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
---
 arch/x86/include/asm/kvm_para.h      |   25 +++++++++++++++++--------
 arch/x86/include/uapi/asm/kvm_para.h |    1 +
 arch/x86/kernel/cpu/hypervisor.c     |    2 +-
 3 files changed, 19 insertions(+), 9 deletions(-)
arch/x86/include/asm/kvm_para.h +15 -2
index 695399f..aa8f3a7 100644
@@ -85,22 +85,31 @@ static inline long kvm_hypercall4(unsigned int nr, unsigned long p1,
 	return ret;
 }
 
-static inline bool kvm_para_available(void)
+static inline bool kvm_para_available_function(unsigned int function)
 {
 	unsigned int eax, ebx, ecx, edx;
 	char signature[13];
 
+	cpuid(function, &eax, &ebx, &ecx, &edx);
+	memcpy(signature + 0, &ebx, 4);
+	memcpy(signature + 4, &ecx, 4);
+	memcpy(signature + 8, &edx, 4);
+	signature[12] = 0;
+
+	if (strcmp(signature, "KVMKVMKVM") == 0)
+		return true;
+
+	return false;
+}
+
+static inline bool kvm_para_available(void)
+{
 	if (boot_cpu_data.cpuid_level < 0)
 		return false;	/* So we don't blow up on old processors */
 
 	if (cpu_has_hypervisor) {
-		cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);

arch/x86/include/asm/kvm_para.h static inline long kvm_hypercall4(unsigned int nr, unsigned long p1, · line 114

Quoted 10 lines
arch/x86/include/asm/kvm_para.h +2 -6
-		memcpy(signature + 0, &ebx, 4);
-		memcpy(signature + 4, &ecx, 4);
-		memcpy(signature + 8, &edx, 4);
-		signature[12] = 0;
-
-		if (strcmp(signature, "KVMKVMKVM") == 0)
+		if (kvm_para_available_function(KVM_CPUID_SIGNATURE) ||
+		    kvm_para_available_function(KVM_CPUID_SIGNATURE_NEXT))
 			return true;
 	}

Nice catch. Just one small thing, you should loop until 0x40010000, as done in arch/x86/include/asm/xen/hypervisor.h, in case one day a third hypervisor implements three extensions (Hyper-V, KVM and its own set).

Paolo

#2 lore raw
Quoted 6 lines · Paolo Bonzini
Quoted 6 lines
arch/x86/include/asm/kvm_para.h +3 -0
+
+static inline bool kvm_para_available(void)
+{
 	if (boot_cpu_data.cpuid_level < 0)
 		return false;	/* So we don't blow up on old processors */
Quoted 17 lines

arch/x86/include/asm/kvm_para.h static inline long kvm_hypercall4(unsigned int nr, unsigned long p1, · line 114

Quoted 12 lines
arch/x86/include/asm/kvm_para.h +2 -7
 	if (cpu_has_hypervisor) {
-		cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
-		memcpy(signature + 0, &ebx, 4);
-		memcpy(signature + 4, &ecx, 4);
-		memcpy(signature + 8, &edx, 4);
-		signature[12] = 0;
-
-		if (strcmp(signature, "KVMKVMKVM") == 0)
+		if (kvm_para_available_function(KVM_CPUID_SIGNATURE) ||
+		    kvm_para_available_function(KVM_CPUID_SIGNATURE_NEXT))
 			return true;
 	}

Nice catch. Just one small thing, you should loop until 0x40010000, as done in arch/x86/include/asm/xen/hypervisor.h, in case one day a third hypervisor implements three extensions (Hyper-V, KVM and its own set).

Any way we can centralize this stuff?

-hpa

maintainer
#3 lore raw

Il 19/07/2013 22:59, H. Peter Anvin ha scritto:

Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
arch/x86/include/asm/kvm_para.h +1 -7
-		cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
-		memcpy(signature + 0, &ebx, 4);
-		memcpy(signature + 4, &ecx, 4);
-		memcpy(signature + 8, &edx, 4);
-		signature[12] = 0;
-
-		if (strcmp(signature, "KVMKVMKVM") == 0)
+		if (kvm_para_available_function(KVM_CPUID_SIGNATURE) ||
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

arch/x86/include/asm/kvm_para.h static inline long kvm_hypercall4(unsigned int nr, unsigned long p1, · line 114

Quoted 3 lines
arch/x86/include/asm/kvm_para.h +1 -0
+		    kvm_para_available_function(KVM_CPUID_SIGNATURE_NEXT))
 			return true;
 	}

Nice catch. Just one small thing, you should loop until 0x40010000, as done in arch/x86/include/asm/xen/hypervisor.h, in case one day a third hypervisor implements three extensions (Hyper-V, KVM and its own set).

Quoted 1 line

Any way we can centralize this stuff?

I guess a function cpuid_hypervisor_base("KVMKVMKVM") that does the scan?

Paolo

#4 lore raw
Quoted 17 lines · Paolo Bonzini

Il 19/07/2013 22:59, H. Peter Anvin ha scritto:

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 11 lines

arch/x86/include/asm/kvm_para.h static inline long kvm_hypercall4(unsigned int nr, unsigned long p1, · line 114

Quoted 11 lines
arch/x86/include/asm/kvm_para.h +2 -7
-		cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
-		memcpy(signature + 0, &ebx, 4);
-		memcpy(signature + 4, &ecx, 4);
-		memcpy(signature + 8, &edx, 4);
-		signature[12] = 0;
-
-		if (strcmp(signature, "KVMKVMKVM") == 0)
+		if (kvm_para_available_function(KVM_CPUID_SIGNATURE) ||
+		    kvm_para_available_function(KVM_CPUID_SIGNATURE_NEXT))
 			return true;
 	}

Nice catch. Just one small thing, you should loop until 0x40010000, as done in arch/x86/include/asm/xen/hypervisor.h, in case one day a third hypervisor implements three extensions (Hyper-V, KVM and its own set).

Quoted 4 lines
Quoted 1 line

Any way we can centralize this stuff?

I guess a function cpuid_hypervisor_base("KVMKVMKVM") that does the scan?

Something like that.

-hpa