x86: introduce CONFIG_HYPERV and detection code
authorWei Liu <liuwe@microsoft.com>
Mon, 30 Sep 2019 13:34:50 +0000 (14:34 +0100)
committerWei Liu <wl@xen.org>
Wed, 4 Dec 2019 10:38:22 +0000 (10:38 +0000)
We use the same code structure as we did for Xen.

As starters, detect Hyper-V in probe routine. More complex
functionalities will be added later.

Signed-off-by: Wei Liu <liuwe@microsoft.com>
Reviewed-by: Paul Durrant <pdurrant@amazon.com>
Acked-by: Jan Beulich <jbeulich@suse.com>
xen/arch/x86/Kconfig
xen/arch/x86/guest/Makefile
xen/arch/x86/guest/hyperv/Makefile [new file with mode: 0644]
xen/arch/x86/guest/hyperv/hyperv.c [new file with mode: 0644]
xen/arch/x86/guest/hypervisor.c
xen/include/asm-x86/guest.h
xen/include/asm-x86/guest/hyperv.h [new file with mode: 0644]

index 18c8f6fd262c2a64b4b96b1d988c0c80fc08a98d..0a02b6ee3fd1879a39472cfbb8b3d11db2aedf59 100644 (file)
@@ -164,6 +164,14 @@ endchoice
 config GUEST
        bool
 
+config HYPERV_GUEST
+       bool "Hyper-V Guest"
+       select GUEST
+       ---help---
+         Support for Xen detecting when it is running under Hyper-V.
+
+         If unsure, say N.
+
 config XEN_GUEST
        bool "Xen Guest"
        select GUEST
index f63d64bbeef0f76fd0fa301e8269d0ab1cbefe84..f164196772e8db0a22fbd035befd1df12ffaa423 100644 (file)
@@ -1,3 +1,4 @@
 obj-y += hypervisor.o
 
+subdir-$(CONFIG_HYPERV_GUEST) += hyperv
 subdir-$(CONFIG_XEN_GUEST) += xen
diff --git a/xen/arch/x86/guest/hyperv/Makefile b/xen/arch/x86/guest/hyperv/Makefile
new file mode 100644 (file)
index 0000000..6817010
--- /dev/null
@@ -0,0 +1 @@
+obj-y += hyperv.o
diff --git a/xen/arch/x86/guest/hyperv/hyperv.c b/xen/arch/x86/guest/hyperv/hyperv.c
new file mode 100644 (file)
index 0000000..b82ae38
--- /dev/null
@@ -0,0 +1,54 @@
+/******************************************************************************
+ * arch/x86/guest/hyperv/hyperv.c
+ *
+ * Support for detecting and running under Hyper-V.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright (c) 2019 Microsoft.
+ */
+#include <xen/init.h>
+
+#include <asm/guest.h>
+
+static const struct hypervisor_ops ops = {
+    .name = "Hyper-V",
+};
+
+const struct hypervisor_ops *__init hyperv_probe(void)
+{
+    uint32_t eax, ebx, ecx, edx;
+
+    cpuid(0x40000000, &eax, &ebx, &ecx, &edx);
+    if ( !((ebx == 0x7263694d) &&  /* "Micr" */
+           (ecx == 0x666f736f) &&  /* "osof" */
+           (edx == 0x76482074)) )  /* "t Hv" */
+        return NULL;
+
+    cpuid(0x40000001, &eax, &ebx, &ecx, &edx);
+    if ( eax != 0x31237648 )    /* Hv#1 */
+        return NULL;
+
+    return &ops;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
index 5f643d6ad18ba1bc69c1f3c3cce80eb3921a99fa..4f27b987402ef3ffc9ec5eb306254a7d1336bef9 100644 (file)
@@ -35,6 +35,14 @@ const char *__init hypervisor_probe(void)
     if ( ops )
         return ops->name;
 
+    /*
+     * Detection of Hyper-V must come after Xen to avoid false positive due
+     * to viridian support
+     */
+    ops = hyperv_probe();
+    if ( ops )
+        return ops->name;
+
     return NULL;
 }
 
index 8e167165ae9115ae53bdc6660e6135fd8534c77d..94448606d4c68e949975384a616add94e9153b62 100644 (file)
@@ -20,6 +20,7 @@
 #define __X86_GUEST_H__
 
 #include <asm/guest/hypercall.h>
+#include <asm/guest/hyperv.h>
 #include <asm/guest/hypervisor.h>
 #include <asm/guest/pvh-boot.h>
 #include <asm/guest/xen.h>
diff --git a/xen/include/asm-x86/guest/hyperv.h b/xen/include/asm-x86/guest/hyperv.h
new file mode 100644 (file)
index 0000000..3f88b94
--- /dev/null
@@ -0,0 +1,43 @@
+/******************************************************************************
+ * asm-x86/guest/hyperv.h
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms and conditions of the GNU General Public
+ * License, version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright (c) 2019 Microsoft.
+ */
+
+#ifndef __X86_GUEST_HYPERV_H__
+#define __X86_GUEST_HYPERV_H__
+
+#ifdef CONFIG_HYPERV_GUEST
+
+#include <asm/guest/hypervisor.h>
+
+const struct hypervisor_ops *hyperv_probe(void);
+
+#else
+
+static inline const struct hypervisor_ops *hyperv_probe(void) { return NULL; }
+
+#endif /* CONFIG_HYPERV_GUEST */
+#endif /* __X86_GUEST_HYPERV_H__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */