Skip to contentSkip to conversation

x86: detect running on a HyperV system

PATCH In review Greg KH 4 participants 8 messages May 6, 2010 – May 8, 2010

Subsystems (3)
lore
Share & tools
lore

Greg KH

author
#0 lore raw

PATCH 1/1 x86: detect running on a HyperV system

From: Ky Srinivasan <[email protected]>

This patch integrates HyperV detection within the framework currently used by VmWare. With this patch, we can avoid having to replicate the HyperV detection code in each of the Microsoft HyperV drivers.

Reworked and tweaked by Greg K-H to build properly.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Vadim Rozenfeld <[email protected]>
Cc Avi Kivity <[email protected]>
Cc Gleb Natapov <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Alexey Dobriyan <[email protected]>
Cc "K.Prasad" <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Alan Cox <[email protected]>
Cc Haiyang Zhang <[email protected]>
Cc Hank Janssen <[email protected]>

---

KY, I fixed up the patch so it would build properly (the old one did not anymore) I also renamed the .c and .h file to "mshyperv.h". I really don't care about the name, but it shouldn't be "hyperv.h" as that is already used by the kernel. Hank, KY, any preference for the name?

x86-maintainers, we need this patch in the tree to get a -staging driver to work properly. If you don't object, can I take it through the staging tree? Or do you want to take it in -tip?

 arch/x86/include/asm/hyperv.h    |    6 +++
 arch/x86/include/asm/mshyperv.h  |    7 ++++
 arch/x86/include/asm/processor.h |    3 +
 arch/x86/kernel/cpu/Makefile     |    2 -
 arch/x86/kernel/cpu/hypervisor.c |   10 ++++-
 arch/x86/kernel/cpu/mshyperv.c   |   67 +++++++++++++++++++++++++++++++++++++++
 6 files changed, 91 insertions(+), 4 deletions(-)
arch/x86/include/asm/hyperv.h +6 -0
@@ -14,6 +14,9 @@
 #define HYPERV_CPUID_ENLIGHTMENT_INFO		0x40000004
 #define HYPERV_CPUID_IMPLEMENT_LIMITS		0x40000005
 
+#define HYPERV_HYPERVISOR_PRESENT_BIT		0x80000000
+#define HYPERV_CPUID_MIN			0x40000005
+
 /*
  * Feature identification. EAX indicates which features are available
  * to the partition based upon the current partition privileges.
@@ -129,6 +132,9 @@
 /* MSR used to provide vcpu index */
 #define HV_X64_MSR_VP_INDEX			0x40000002
 
+/* MSR used to read the per-partition time reference counter */
+#define HV_X64_MSR_TIME_REF_COUNT		0x40000020
+
 /* Define the virtual APIC registers */
 #define HV_X64_MSR_EOI				0x40000070
 #define HV_X64_MSR_ICR				0x40000071
arch/x86/include/asm/mshyperv.h new file +7 -0
@@ -0,0 +1,7 @@
+#ifndef ASM_X86__MSHYPER_H
+#define ASM_X86__MSHYPER_H
+
+int ms_hyperv_platform(void);
+void __cpuinit ms_hyperv_set_feature_bits(struct cpuinfo_x86 *c);
+
+#endif
arch/x86/include/asm/processor.h +3 -0
@@ -114,6 +114,8 @@ struct cpuinfo_x86 {
 	u16			cpu_index;
 #endif
 	unsigned int		x86_hyper_vendor;
+	/* The layout of this field is hypervisor specific */
+	unsigned int		x86_hyper_features;
 } __attribute__((__aligned__(SMP_CACHE_BYTES)));
 
 #define X86_VENDOR_INTEL	0
@@ -129,6 +131,7 @@ struct cpuinfo_x86 {
 
 #define X86_HYPER_VENDOR_NONE  0
 #define X86_HYPER_VENDOR_VMWARE 1
+#define X86_HYPER_VENDOR_MSFT	2
 
 /*
  * capabilities of CPUs
arch/x86/kernel/cpu/Makefile +1 -1
@@ -14,7 +14,7 @@ CFLAGS_common.o		:= $(nostackp)
 
 obj-y			:= intel_cacheinfo.o addon_cpuid_features.o
 obj-y			+= proc.o capflags.o powerflags.o common.o
-obj-y			+= vmware.o hypervisor.o sched.o
+obj-y			+= vmware.o hypervisor.o sched.o mshyperv.o
 
 obj-$(CONFIG_X86_32)	+= bugs.o cmpxchg.o
 obj-$(CONFIG_X86_64)	+= bugs_64.o
arch/x86/kernel/cpu/hypervisor.c +7 -3
@@ -23,6 +23,7 @@
 
 #include <asm/processor.h>
 #include <asm/vmware.h>
+#include <asm/mshyperv.h>
 #include <asm/hypervisor.h>
 
 static inline void __cpuinit
@@ -30,6 +31,8 @@ detect_hypervisor_vendor(struct cpuinfo_
 {
 	if (vmware_platform())
 		c->x86_hyper_vendor = X86_HYPER_VENDOR_VMWARE;
+	else if (ms_hyperv_platform())
+		c->x86_hyper_vendor = X86_HYPER_VENDOR_MSFT;
 	else
 		c->x86_hyper_vendor = X86_HYPER_VENDOR_NONE;
 }
@@ -37,10 +40,11 @@ detect_hypervisor_vendor(struct cpuinfo_
 static inline void __cpuinit
 hypervisor_set_feature_bits(struct cpuinfo_x86 *c)
 {
-	if (boot_cpu_data.x86_hyper_vendor == X86_HYPER_VENDOR_VMWARE) {
+	if (boot_cpu_data.x86_hyper_vendor == X86_HYPER_VENDOR_VMWARE)
 		vmware_set_feature_bits(c);
-		return;
-	}
+	else if (boot_cpu_data.x86_hyper_vendor == X86_HYPER_VENDOR_MSFT)
+		ms_hyperv_set_feature_bits(c);
+	return;
 }
 
 void __cpuinit init_hypervisor(struct cpuinfo_x86 *c)
arch/x86/kernel/cpu/mshyperv.c new file +67 -0

67 lines not on this page · raw message

#1 lore raw
Quoted 5 lines · Greg KH

x86-maintainers, we need this patch in the tree to get a -staging driver to work properly. If you don't object, can I take it through the staging tree? Or do you want to take it in -tip?

I'd prefer to take it in -tip, partly because I would like to rework a few things -- including preexisting braindamage like c->x86_hyper_vendor (since when was hypervisor vendor a per-cpu property!?)

I'll try to get to it today, if not feel free to bug me.

-hpa

Signature · 2 lines
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.
author
#2 lore raw
Quoted 10 lines · H. Peter Anvin
Quoted 5 lines · Greg KH

x86-maintainers, we need this patch in the tree to get a -staging driver to work properly. If you don't object, can I take it through the staging tree? Or do you want to take it in -tip?

I'd prefer to take it in -tip, partly because I would like to rework a few things -- including preexisting braindamage like c->x86_hyper_vendor (since when was hypervisor vendor a per-cpu property!?)

Heh, good point, but where should it live?

And I have no objection to taking it in -tip, thanks for doing so.

Quoted 1 line

I'll try to get to it today, if not feel free to bug me.

Will do :)

thanks again,

greg k-h

#3 lore raw
Quoted 7 lines · Greg KH
Quoted 4 lines

I'd prefer to take it in -tip, partly because I would like to rework a few things -- including preexisting braindamage like c->x86_hyper_vendor (since when was hypervisor vendor a per-cpu property!?)

Heh, good point, but where should it live?

Probably just make it a global variable, or somesuch.

-hpa

#4 lore raw
In reply to Greg KH
Quoted 14 lines · Greg KH
Quoted 10 lines · H. Peter Anvin
Quoted 5 lines · Greg KH

x86-maintainers, we need this patch in the tree to get a -staging driver to work properly. If you don't object, can I take it through the staging tree? Or do you want to take it in -tip?

I'd prefer to take it in -tip, partly because I would like to rework a few things -- including preexisting braindamage like c->x86_hyper_vendor (since when was hypervisor vendor a per-cpu property!?)

Heh, good point, but where should it live?

Quoted 8 lines

And I have no objection to taking it in -tip, thanks for doing so.

Quoted 1 line

I'll try to get to it today, if not feel free to bug me.

Will do :)

thanks again,

Okay... we write something called x86_hyper_features, but it isn't actually used for anything. I'm assuming based on what I can see in <asm/hyperv.h> that it is almost certainly not anything CPU-based. I really don't like a random field that can be stuffed with information from random locations, and it doesn't look like it's even a flag field in any meaningful way. Worse, the field is just used to mix bits from two different CPUID levels at what appears to be a completely arbitrary boundary:

        /*
         * Extract the features, recommendations etc.
         * The first 9 bits will be used to track hypervisor features.
         * The next 6 bits will be used to track the hypervisor
         * recommendations.
         */
        cpuid(HYPERV_CPUID_FEATURES, &eax, &ebx, &ecx, &edx);
        c->x86_hyper_features |= (eax & 0x1ff);

        cpuid(HYPERV_CPUID_ENLIGHTMENT_INFO, &eax, &ebx, &ecx, &edx);
        c->x86_hyper_features |= ((eax & 0x3f) << 9);

Why 9? Why 6? The only thing that I can think of that justifies those particular numbers is what the *current* version of Hyper-V does.

It would be better to just have a global variable where these two levels are separately stuffed away instead of arbitrarily commingled (again, I'm assuming none of the bits are actually per CPU.)

What does this driver of yours do with them?

-hpa

author
#5 lore raw

[KY added to the To:, sorry for not including him earlier, he wrote this code]

Quoted 45 lines · H. Peter Anvin
Quoted 14 lines · Greg KH
Quoted 10 lines · H. Peter Anvin
Quoted 5 lines · Greg KH

x86-maintainers, we need this patch in the tree to get a -staging driver to work properly. If you don't object, can I take it through the staging tree? Or do you want to take it in -tip?

I'd prefer to take it in -tip, partly because I would like to rework a few things -- including preexisting braindamage like c->x86_hyper_vendor (since when was hypervisor vendor a per-cpu property!?)

Heh, good point, but where should it live?

Quoted 8 lines

And I have no objection to taking it in -tip, thanks for doing so.

Quoted 1 line

I'll try to get to it today, if not feel free to bug me.

Will do :)

thanks again,

Okay... we write something called x86_hyper_features, but it isn't actually used for anything. I'm assuming based on what I can see in <asm/hyperv.h> that it is almost certainly not anything CPU-based. I really don't like a random field that can be stuffed with information from random locations, and it doesn't look like it's even a flag field in any meaningful way. Worse, the field is just used to mix bits from two different CPUID levels at what appears to be a completely arbitrary boundary:

        /*
         * Extract the features, recommendations etc.
         * The first 9 bits will be used to track hypervisor features.
         * The next 6 bits will be used to track the hypervisor
         * recommendations.
         */
        cpuid(HYPERV_CPUID_FEATURES, &eax, &ebx, &ecx, &edx);
        c->x86_hyper_features |= (eax & 0x1ff);

        cpuid(HYPERV_CPUID_ENLIGHTMENT_INFO, &eax, &ebx, &ecx, &edx);
        c->x86_hyper_features |= ((eax & 0x3f) << 9);
Quoted 2 lines

Why 9? Why 6? The only thing that I can think of that justifies those particular numbers is what the *current* version of Hyper-V does.

I think that is what Hyper-V is saying it will always do. Hank? Any help here?

Quoted 3 lines

It would be better to just have a global variable where these two levels are separately stuffed away instead of arbitrarily commingled (again, I'm assuming none of the bits are actually per CPU.)

Sure, that would be fine, that's all we really need.

Quoted 1 line

What does this driver of yours do with them?

It just tests to see if the functionality is present. I've included the driver below to show this.

thanks,

greg k-h

--------
From: K. Y. Srinivasan <[email protected]>
Subject: Staging: A clocksource for Linux guests hosted on HyperV.

This patch is a clocksource implementation suitable for guests hosted on HyperV. Time keeping in Linux guests hosted on HyperV is unstable. This clocksource driver fixes the problem.

---
 drivers/staging/hv/Makefile        |    2 
 drivers/staging/hv/hv_timesource.c |   94 +++++++++++++++++++++++++++++++++++++
 2 files changed, 95 insertions(+), 1 deletion(-)
drivers/staging/hv/Makefile +1 -1
@@ -1,4 +1,4 @@
-obj-$(CONFIG_HYPERV)		+= hv_vmbus.o
+obj-$(CONFIG_HYPERV)		+= hv_vmbus.o hv_timesource.o
 obj-$(CONFIG_HYPERV_STORAGE)	+= hv_storvsc.o
 obj-$(CONFIG_HYPERV_BLOCK)	+= hv_blkvsc.o
 obj-$(CONFIG_HYPERV_NET)	+= hv_netvsc.o
drivers/staging/hv/hv_timesource.c new file +94 -0

94 lines not on this page · raw message

#6 lore raw

SNIP SNIP SNIP----------

Quoted 4 lines

From: Greg KH [[email protected]] - Friday, May 07, 2010 8:46 AM

[KY added to the To:, sorry for not including him earlier, he wrote this code]

Quoted 21 lines · H. Peter Anvin
Quoted 21 lines · Greg KH
Quoted 2 lines · H. Peter Anvin
Quoted 1 line · Greg KH

Okay... we write something called x86_hyper_features, but it isn't actually used for anything. I'm assuming based on what I can see in <asm/hyperv.h> that it is almost certainly not anything CPU-based. I really don't like a random field that can be stuffed with information from random locations, and it doesn't look like it's even a flag field in any meaningful way. Worse, the field is just used to mix bits from two different CPUID levels at what appears to be a completely arbitrary boundary:

/*
* Extract the features, recommendations etc.
* The first 9 bits will be used to track hypervisor features.
* The next 6 bits will be used to track the hypervisor
* recommendations.
*/
cpuid(HYPERV_CPUID_FEATURES, &eax, &ebx, &ecx, &edx); c->x86_hyper_features |= (eax & 0x1ff);

Quoted 8 lines
Quoted 5 lines
        cpuid(HYPERV_CPUID_ENLIGHTMENT_INFO, &eax, &ebx, &ecx, &edx);
        c->x86_hyper_features |= ((eax & 0x3f) << 9);

Why 9? Why 6? The only thing that I can think of that justifies those particular numbers is what the *current* version of Hyper-V does.

I think that is what Hyper-V is saying it will always do. Hank? Any help here?

That is correct. Hyper-V will always do this.

For functionality like a timesource, we need to get to hyper-v the way Ky did in his code. There is no messaging for it like IDE/SCSI/Network and the like.

My understanding is that more information is going to be contained in these areas once Windows 8 is more defined. I know Windows 8 will give us changes but we do not know yet what they are. And I will not untill much later in the year. They are designing some of the low level stuff there right now.

I know that some of the messaging protocols will change regarding networking.

Hank.

#7 lore raw

[tip:x86/cpu] x86: Detect running on a Microsoft HyperV system

In reply to Greg KH
Commit-ID:  a2a47c6c3d1a7c01da4464b3b7be93b924f874c1
Gitweb:     http://git.kernel.org/tip/a2a47c6c3d1a7c01da4464b3b7be93b924f874c1
Author:     Ky Srinivasan <[email protected]>
AuthorDate: Thu, 6 May 2010 12:08:41 -0700
Committer:  H. Peter Anvin <[email protected]>
CommitDate: Thu, 6 May 2010 18:24:15 -0700

x86: Detect running on a Microsoft HyperV system

This patch integrates HyperV detection within the framework currently used by VmWare. With this patch, we can avoid having to replicate the HyperV detection code in each of the Microsoft HyperV drivers.

Reworked and tweaked by Greg K-H to build properly.

LKML-Reference: <[email protected]>

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Vadim Rozenfeld <[email protected]>
Cc Avi Kivity <[email protected]>
Cc Gleb Natapov <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Alexey Dobriyan <[email protected]>
Cc "K.Prasad" <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Alan Cox <[email protected]>
Cc Haiyang Zhang <[email protected]>
Cc Hank Janssen <[email protected]>
---
 arch/x86/include/asm/hyperv.h    |    6 +++
 arch/x86/include/asm/mshyperv.h  |    7 ++++
 arch/x86/include/asm/processor.h |    3 ++
 arch/x86/kernel/cpu/Makefile     |    2 +-
 arch/x86/kernel/cpu/hypervisor.c |   10 ++++--
 arch/x86/kernel/cpu/mshyperv.c   |   67 ++++++++++++++++++++++++++++++++++++++
 6 files changed, 91 insertions(+), 4 deletions(-)
arch/x86/include/asm/hyperv.h +6 -0
index e153a2b..4604047 100644
@@ -14,6 +14,9 @@
 #define HYPERV_CPUID_ENLIGHTMENT_INFO		0x40000004
 #define HYPERV_CPUID_IMPLEMENT_LIMITS		0x40000005
 
+#define HYPERV_HYPERVISOR_PRESENT_BIT		0x80000000
+#define HYPERV_CPUID_MIN			0x40000005
+
 /*
  * Feature identification. EAX indicates which features are available
  * to the partition based upon the current partition privileges.
@@ -129,6 +132,9 @@
 /* MSR used to provide vcpu index */
 #define HV_X64_MSR_VP_INDEX			0x40000002
 
+/* MSR used to read the per-partition time reference counter */
+#define HV_X64_MSR_TIME_REF_COUNT		0x40000020
+
 /* Define the virtual APIC registers */
 #define HV_X64_MSR_EOI				0x40000070
 #define HV_X64_MSR_ICR				0x40000071
arch/x86/include/asm/mshyperv.h new file +7 -0
new file mode 100644
index 0000000..6cd8101
@@ -0,0 +1,7 @@
+#ifndef ASM_X86__MSHYPER_H
+#define ASM_X86__MSHYPER_H
+
+int ms_hyperv_platform(void);
+void __cpuinit ms_hyperv_set_feature_bits(struct cpuinfo_x86 *c);
+
+#endif
arch/x86/include/asm/processor.h +3 -0
index b753ea5..597c041 100644
@@ -114,6 +114,8 @@ struct cpuinfo_x86 {
 	u16			cpu_index;
 #endif
 	unsigned int		x86_hyper_vendor;
+	/* The layout of this field is hypervisor specific */
+	unsigned int		x86_hyper_features;
 } __attribute__((__aligned__(SMP_CACHE_BYTES)));
 
 #define X86_VENDOR_INTEL	0
@@ -129,6 +131,7 @@ struct cpuinfo_x86 {
 
 #define X86_HYPER_VENDOR_NONE  0
 #define X86_HYPER_VENDOR_VMWARE 1
+#define X86_HYPER_VENDOR_MSFT	2
 
 /*
  * capabilities of CPUs
arch/x86/kernel/cpu/Makefile +1 -1
index c202b62..3a785da 100644
@@ -14,7 +14,7 @@ CFLAGS_common.o		:= $(nostackp)
 
 obj-y			:= intel_cacheinfo.o addon_cpuid_features.o
 obj-y			+= proc.o capflags.o powerflags.o common.o
-obj-y			+= vmware.o hypervisor.o sched.o
+obj-y			+= vmware.o hypervisor.o sched.o mshyperv.o
 
 obj-$(CONFIG_X86_32)	+= bugs.o cmpxchg.o
 obj-$(CONFIG_X86_64)	+= bugs_64.o
arch/x86/kernel/cpu/hypervisor.c +7 -3
index 08be922..de3f4e0 100644
@@ -23,6 +23,7 @@
 
 #include <asm/processor.h>
 #include <asm/vmware.h>
+#include <asm/mshyperv.h>
 #include <asm/hypervisor.h>
 
 static inline void __cpuinit
@@ -30,6 +31,8 @@ detect_hypervisor_vendor(struct cpuinfo_x86 *c)
 {
 	if (vmware_platform())
 		c->x86_hyper_vendor = X86_HYPER_VENDOR_VMWARE;
+	else if (ms_hyperv_platform())
+		c->x86_hyper_vendor = X86_HYPER_VENDOR_MSFT;
 	else
 		c->x86_hyper_vendor = X86_HYPER_VENDOR_NONE;
 }
@@ -37,10 +40,11 @@ detect_hypervisor_vendor(struct cpuinfo_x86 *c)
 static inline void __cpuinit
 hypervisor_set_feature_bits(struct cpuinfo_x86 *c)
 {
-	if (boot_cpu_data.x86_hyper_vendor == X86_HYPER_VENDOR_VMWARE) {
+	if (boot_cpu_data.x86_hyper_vendor == X86_HYPER_VENDOR_VMWARE)
 		vmware_set_feature_bits(c);
-		return;
-	}
+	else if (boot_cpu_data.x86_hyper_vendor == X86_HYPER_VENDOR_MSFT)
+		ms_hyperv_set_feature_bits(c);
+	return;
 }
 
 void __cpuinit init_hypervisor(struct cpuinfo_x86 *c)
arch/x86/kernel/cpu/mshyperv.c new file +67 -0

67 lines not on this page · raw message