Skip to contentSkip to conversation

Port Xen to Hyper-V

PATCH Superseded Wei Liu 4 participants 28 messages Sep 30, 2019 – Nov 5, 2019

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

v2 cover Port Xen to Hyper-V

Hi all

This is version 2 of the patch series.

This is the very first stage for porting Xen to run on Hyper-V with all the goodies Hyper-V has to offer. With this series, Xen can successfully detect Hyper-V and prints out a message. I would like to first get the code structure and kconfig options agreed upon.

There are two major areas to be worked on:
* Make Dom0 able to use Hyper-V's synthetic devices.
* Make Xen use of the synthetic timer, reference TSC and enlightenment VMCS and other interfaces.

They aren't trivial, and time can be scarce on my side, so I intend to post patches piece meal when they are ready.

Questions and comments are welcome.

Thanks,
Wei.

---
Changes in v2:
1. Introduce and use a hypervisor framework
2. Keep memmap infra under Xen for now

Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
Cc Wei Liu <[email protected]>
Cc Roger Pau Monné <[email protected]>

Wei Liu (9):
x86: introduce CONFIG_GUEST and move code
x86: include asm_defns.h directly in hypercall.h
x86: drop hypervisor_cpuid_base
x86: include xen/lib.h in guest/hypercall.h
x86: introduce hypervisor framework
x86: rename hypervisor_{alloc,free}_unused_page
x86: switch xen implementation to use hypervisor framework
x86: be more verbose when running on a hypervisor
x86: introduce CONFIG_HYPERV and detection code

 xen/arch/x86/Kconfig                          | 13 +++
 xen/arch/x86/Makefile                         |  2 +-
 xen/arch/x86/guest/Makefile                   |  6 +-
 xen/arch/x86/guest/hyperv/Makefile            |  1 +
 xen/arch/x86/guest/hyperv/hyperv.c            | 69 ++++++++++++++++
 xen/arch/x86/guest/hypervisor.c               | 82 +++++++++++++++++++
 xen/arch/x86/guest/xen/Makefile               |  4 +
 xen/arch/x86/guest/{ => xen}/hypercall_page.S |  0
 xen/arch/x86/guest/{ => xen}/pvh-boot.c       |  2 +-
 xen/arch/x86/guest/{ => xen}/xen.c            | 39 ++++-----
 xen/arch/x86/pv/shim.c                        |  6 +-
 xen/arch/x86/setup.c                          |  6 +-
 xen/include/asm-x86/guest.h                   |  2 +
 xen/include/asm-x86/guest/hypercall.h         |  4 +
 xen/include/asm-x86/guest/hyperv.h            | 45 ++++++++++
 xen/include/asm-x86/guest/hypervisor.h        | 63 ++++++++++++++
 xen/include/asm-x86/guest/xen.h               | 24 ++----
 17 files changed, 321 insertions(+), 47 deletions(-)
 create mode 100644 xen/arch/x86/guest/hyperv/Makefile
 create mode 100644 xen/arch/x86/guest/hyperv/hyperv.c
 create mode 100644 xen/arch/x86/guest/hypervisor.c
 create mode 100644 xen/arch/x86/guest/xen/Makefile
 rename xen/arch/x86/guest/{ => xen}/hypercall_page.S (100%)
 rename xen/arch/x86/guest/{ => xen}/pvh-boot.c (99%)
 rename xen/arch/x86/guest/{ => xen}/xen.c (93%)
 create mode 100644 xen/include/asm-x86/guest/hyperv.h
 create mode 100644 xen/include/asm-x86/guest/hypervisor.h
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
author
#1 lore raw

PATCH v2 1/9 x86: introduce CONFIG_GUEST and move code

Xen is able to run as a guest on Xen. We plan to make it able to run on Hyper-V as well.

Introduce CONFIG_GUEST which is set to true if either running on Xen or Hyper-V is desired. Restructure code hierarchy for new code to come.

No functional change intended.

---
 xen/arch/x86/Kconfig                          | 4 ++++
 xen/arch/x86/Makefile                         | 2 +-
 xen/arch/x86/guest/Makefile                   | 5 +----
 xen/arch/x86/guest/xen/Makefile               | 4 ++++
 xen/arch/x86/guest/{ => xen}/hypercall_page.S | 0
 xen/arch/x86/guest/{ => xen}/pvh-boot.c       | 0
 xen/arch/x86/guest/{ => xen}/xen.c            | 0
 7 files changed, 10 insertions(+), 5 deletions(-)
 create mode 100644 xen/arch/x86/guest/xen/Makefile
 rename xen/arch/x86/guest/{ => xen}/hypercall_page.S (100%)
 rename xen/arch/x86/guest/{ => xen}/pvh-boot.c (100%)
 rename xen/arch/x86/guest/{ => xen}/xen.c (100%)
xen/arch/x86/Kconfig +4 -0
index 288dc6c042..584bdc1bb8 100644
@@ -160,8 +160,12 @@ config XEN_ALIGN_2M
 
 endchoice
 
+config GUEST
+	bool
+
 config XEN_GUEST
 	def_bool n
+	select GUEST
 	prompt "Xen Guest"
 	---help---
 	  Support for Xen detecting when it is running under Xen.
xen/arch/x86/Makefile +1 -1
index 2443fd2cc5..99a12d0090 100644
@@ -1,7 +1,7 @@
 subdir-y += acpi
 subdir-y += cpu
 subdir-y += genapic
-subdir-$(CONFIG_XEN_GUEST) += guest
+subdir-$(CONFIG_GUEST) += guest
 subdir-$(CONFIG_HVM) += hvm
 subdir-y += mm
 subdir-$(CONFIG_XENOPROF) += oprofile
xen/arch/x86/guest/Makefile +1 -4
index 26fb4b1007..6806f04947 100644
@@ -1,4 +1 @@
-obj-y += hypercall_page.o
-obj-y += xen.o
-
-obj-bin-$(CONFIG_PVH_GUEST) += pvh-boot.init.o
+subdir-$(CONFIG_XEN_GUEST) += xen
xen/arch/x86/guest/xen/Makefile new file +4 -0
new file mode 100644
index 0000000000..26fb4b1007
@@ -0,0 +1,4 @@
+obj-y += hypercall_page.o
+obj-y += xen.o
+
+obj-bin-$(CONFIG_PVH_GUEST) += pvh-boot.init.o
xen/arch/x86/guest/xen/hypercall_page.S renamed from xen/arch/x86/guest/hypercall_page.S +0 -0
similarity index 100%
rename from xen/arch/x86/guest/hypercall_page.S
rename to xen/arch/x86/guest/xen/hypercall_page.S
xen/arch/x86/guest/xen/pvh-boot.c renamed from xen/arch/x86/guest/pvh-boot.c +0 -0
similarity index 100%
rename from xen/arch/x86/guest/pvh-boot.c
rename to xen/arch/x86/guest/xen/pvh-boot.c
xen/arch/x86/guest/xen/xen.c renamed from xen/arch/x86/guest/xen.c +0 -0
similarity index 100%
rename from xen/arch/x86/guest/xen.c
rename to xen/arch/x86/guest/xen/xen.c
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
author
#2 lore raw

PATCH v2 2/9 x86: include asm_defns.h directly in hypercall.h

In reply to Wei Liu

ASM_CALL_CONSTRAINT is defined there.

No functional change.

---
 xen/include/asm-x86/guest/hypercall.h | 2 ++
 1 file changed, 2 insertions(+)
xen/include/asm-x86/guest/hypercall.h +2 -0
index d548816b30..c9deca6ffc 100644
@@ -23,6 +23,8 @@
 
 #include <xen/types.h>
 
+#include <asm/asm_defns.h>
+
 #include <public/xen.h>
 #include <public/sched.h>
 #include <public/hvm/hvm_op.h>
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
author
#3 lore raw

PATCH v2 3/9 x86: drop hypervisor_cpuid_base

In reply to Wei Liu

The only user is Xen specific code in PV shim. We can therefore export the variable directly.

---
 xen/arch/x86/guest/xen/xen.c    | 7 +------
 xen/arch/x86/pv/shim.c          | 2 +-
 xen/include/asm-x86/guest/xen.h | 2 +-
 3 files changed, 3 insertions(+), 8 deletions(-)
xen/arch/x86/guest/xen/xen.c +1 -6
index 7b7a5badab..78fc603996 100644
@@ -37,7 +37,7 @@
 
 bool __read_mostly xen_guest;
 
-static __read_mostly uint32_t xen_cpuid_base;
+__read_mostly uint32_t xen_cpuid_base;
 extern char hypercall_page[];
 static struct rangeset *mem;
 
@@ -301,11 +301,6 @@ int hypervisor_free_unused_page(mfn_t mfn)
     return rangeset_remove_range(mem, mfn_x(mfn), mfn_x(mfn));
 }
 
-uint32_t hypervisor_cpuid_base(void)
-{
-    return xen_cpuid_base;
-}
-
 static void ap_resume(void *unused)
 {
     map_vcpuinfo();
xen/arch/x86/pv/shim.c +1 -1
index 5edbcd9ac5..5441e89de2 100644
@@ -990,7 +990,7 @@ domid_t get_initial_domain_id(void)
     if ( !pv_shim )
         return 0;
 
-    cpuid(hypervisor_cpuid_base() + 4, &eax, &ebx, &ecx, &edx);
+    cpuid(xen_cpuid_base + 4, &eax, &ebx, &ecx, &edx);
 
     return (eax & XEN_HVM_CPUID_DOMID_PRESENT) ? ecx : 1;
 }
xen/include/asm-x86/guest/xen.h +1 -1
index 7e04e4a7ab..b015ed1883 100644
@@ -30,13 +30,13 @@
 
 extern bool xen_guest;
 extern bool pv_console;
+extern uint32_t xen_cpuid_base;
 
 void probe_hypervisor(void);
 void hypervisor_setup(void);
 void hypervisor_ap_setup(void);
 int hypervisor_alloc_unused_page(mfn_t *mfn);
 int hypervisor_free_unused_page(mfn_t mfn);
-uint32_t hypervisor_cpuid_base(void);
 void hypervisor_resume(void);
 
 DECLARE_PER_CPU(unsigned int, vcpu_id);
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
author
#4 lore raw

PATCH v2 4/9 x86: include xen/lib.h in guest/hypercall.h

In reply to Wei Liu

We need ASSERT_UNREACHABLE.

---
 xen/include/asm-x86/guest/hypercall.h | 2 ++
 1 file changed, 2 insertions(+)
xen/include/asm-x86/guest/hypercall.h +2 -0
index c9deca6ffc..d0d2f5022d 100644
@@ -182,6 +182,8 @@ static inline long xen_hypercall_set_evtchn_upcall_vector(
 
 #else /* CONFIG_XEN_GUEST */
 
+#include <xen/lib.h>
+
 #include <public/sched.h>
 
 static inline void xen_hypercall_console_write(
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
author
#6 lore raw

PATCH v2 5/9 x86: introduce hypervisor framework

In reply to Wei Liu

We will soon implement Hyper-V support for Xen. Add a framework for that.

This requires moving some of the hypervisor_* functions from xen.h to hypervisor.h.

---
 xen/arch/x86/guest/Makefile            |  2 +
 xen/arch/x86/guest/hypervisor.c        | 45 +++++++++++++++++++
 xen/include/asm-x86/guest.h            |  1 +
 xen/include/asm-x86/guest/hypervisor.h | 61 ++++++++++++++++++++++++++
 xen/include/asm-x86/guest/xen.h        | 12 -----
 5 files changed, 109 insertions(+), 12 deletions(-)
 create mode 100644 xen/arch/x86/guest/hypervisor.c
 create mode 100644 xen/include/asm-x86/guest/hypervisor.h
xen/arch/x86/guest/Makefile +2 -0
index 6806f04947..f63d64bbee 100644
@@ -1 +1,3 @@
+obj-y += hypervisor.o
+
 subdir-$(CONFIG_XEN_GUEST) += xen
xen/arch/x86/guest/hypervisor.c new file +45 -0

45 lines not on this page · raw message

xen/include/asm-x86/guest.h +1 -0
index a38c6b5b3f..8e167165ae 100644
@@ -20,6 +20,7 @@
 #define __X86_GUEST_H__
 
 #include <asm/guest/hypercall.h>
+#include <asm/guest/hypervisor.h>
 #include <asm/guest/pvh-boot.h>
 #include <asm/guest/xen.h>
 #include <asm/pv/shim.h>
xen/include/asm-x86/guest/hypervisor.h new file +61 -0

61 lines not on this page · raw message

xen/include/asm-x86/guest/xen.h +0 -12
index b015ed1883..3145f75361 100644
@@ -33,11 +33,8 @@ extern bool pv_console;
 extern uint32_t xen_cpuid_base;
 
 void probe_hypervisor(void);
-void hypervisor_setup(void);
-void hypervisor_ap_setup(void);
 int hypervisor_alloc_unused_page(mfn_t *mfn);
 int hypervisor_free_unused_page(mfn_t mfn);
-void hypervisor_resume(void);
 
 DECLARE_PER_CPU(unsigned int, vcpu_id);
 DECLARE_PER_CPU(struct vcpu_info *, vcpu_info);
@@ -49,15 +46,6 @@ DECLARE_PER_CPU(struct vcpu_info *, vcpu_info);
 
 static inline void probe_hypervisor(void) {}
 
-static inline void hypervisor_setup(void)
-{
-    ASSERT_UNREACHABLE();
-}
-static inline void hypervisor_ap_setup(void)
-{
-    ASSERT_UNREACHABLE();
-}
-
 #endif /* CONFIG_XEN_GUEST */
 #endif /* __X86_GUEST_XEN_H__ */
 
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
author
#7 lore raw

PATCH v2 6/9 x86: rename hypervisor_{alloc, free}_unused_page

In reply to Wei Liu

They are used in Xen code only.

No functional change.

---
 xen/arch/x86/guest/xen/xen.c    | 6 +++---
 xen/arch/x86/pv/shim.c          | 4 ++--
 xen/include/asm-x86/guest/xen.h | 4 ++--
 3 files changed, 7 insertions(+), 7 deletions(-)
xen/arch/x86/guest/xen/xen.c +3 -3
index 78fc603996..9895025d02 100644
@@ -97,7 +97,7 @@ static void map_shared_info(void)
     unsigned int i;
     unsigned long rc;
 
-    if ( hypervisor_alloc_unused_page(&mfn) )
+    if ( xen_alloc_unused_page(&mfn) )
         panic("unable to reserve shared info memory page\n");
 
     xatp.gpfn = mfn_x(mfn);
@@ -284,7 +284,7 @@ void hypervisor_ap_setup(void)
     init_evtchn();
 }
 
-int hypervisor_alloc_unused_page(mfn_t *mfn)
+int xen_alloc_unused_page(mfn_t *mfn)
 {
     unsigned long m;
     int rc;
@@ -296,7 +296,7 @@ int hypervisor_alloc_unused_page(mfn_t *mfn)
     return rc;
 }
 
-int hypervisor_free_unused_page(mfn_t mfn)
+int xen_free_unused_page(mfn_t mfn)
 {
     return rangeset_remove_range(mem, mfn_x(mfn), mfn_x(mfn));
 }
xen/arch/x86/pv/shim.c +2 -2
index 5441e89de2..41b4665649 100644
@@ -739,7 +739,7 @@ static long pv_shim_grant_table_op(unsigned int cmd,
                 };
                 mfn_t mfn;
 
-                rc = hypervisor_alloc_unused_page(&mfn);
+                rc = xen_alloc_unused_page(&mfn);
                 if ( rc )
                 {
                     gprintk(XENLOG_ERR,
@@ -751,7 +751,7 @@ static long pv_shim_grant_table_op(unsigned int cmd,
                 rc = xen_hypercall_memory_op(XENMEM_add_to_physmap, &xatp);
                 if ( rc )
                 {
-                    hypervisor_free_unused_page(mfn);
+                    xen_free_unused_page(mfn);
                     break;
                 }
 
xen/include/asm-x86/guest/xen.h +2 -2
index 3145f75361..8221fc1325 100644
@@ -33,8 +33,8 @@ extern bool pv_console;
 extern uint32_t xen_cpuid_base;
 
 void probe_hypervisor(void);
-int hypervisor_alloc_unused_page(mfn_t *mfn);
-int hypervisor_free_unused_page(mfn_t mfn);
+int xen_alloc_unused_page(mfn_t *mfn);
+int xen_free_unused_page(mfn_t mfn);
 
 DECLARE_PER_CPU(unsigned int, vcpu_id);
 DECLARE_PER_CPU(struct vcpu_info *, vcpu_info);
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
author
#9 lore raw

PATCH v2 7/9 x86: switch xen implementation to use hypervisor framework

In reply to Wei Liu

Take the chance to change probe_hypervisor to hypervisor_probe.

---
 xen/arch/x86/guest/hypervisor.c   | 31 +++++++++++++++++++++++++++++--
 xen/arch/x86/guest/xen/pvh-boot.c |  2 +-
 xen/arch/x86/guest/xen/xen.c      | 26 ++++++++++++++------------
 xen/arch/x86/setup.c              |  2 +-
 xen/include/asm-x86/guest/xen.h   |  6 ++++--
 5 files changed, 49 insertions(+), 18 deletions(-)
xen/arch/x86/guest/hypervisor.c +29 -2
index 89b9ae4de0..30453b6a7a 100644
@@ -22,7 +22,7 @@
 #include <xen/types.h>
 
 #include <asm/cache.h>
-#include <asm/guest/hypervisor.h>
+#include <asm/guest.h>
 
 static struct hypervisor_ops *hops __read_mostly;
 
@@ -31,7 +31,34 @@ bool hypervisor_probe(void)
     if ( hops )
         return true;
 
-    return false;
+    /* Too early to use cpu_has_hypervisor */
+    if ( !(cpuid_ecx(1) & cpufeat_mask(X86_FEATURE_HYPERVISOR)) )
+        return false;
+
+#ifdef CONFIG_XEN_GUEST
+    if ( xen_probe() )
+        hops = &xen_hypervisor_ops;
+#endif
+
+    return !!hops;
+}
+
+void hypervisor_setup(void)
+{
+    if ( hops && hops->setup )
+        hops->setup();
+}
+
+void hypervisor_ap_setup(void)
+{
+    if ( hops && hops->ap_setup )
+        hops->ap_setup();
+}
+
+void hypervisor_resume(void)
+{
+    if ( hops && hops->resume )
+        hops->resume();
 }
 
 /*
xen/arch/x86/guest/xen/pvh-boot.c +1 -1
index ca8e156f7d..498625eae0 100644
@@ -103,7 +103,7 @@ void __init pvh_init(multiboot_info_t **mbi, module_t **mod)
 {
     convert_pvh_info(mbi, mod);
 
-    probe_hypervisor();
+    hypervisor_probe();
     ASSERT(xen_guest);
 
     get_memory_map();
xen/arch/x86/guest/xen/xen.c +14 -12
index 9895025d02..a9d321e5eb 100644
@@ -67,24 +67,19 @@ static void __init find_xen_leaves(void)
     }
 }
 
-void __init probe_hypervisor(void)
+bool __init xen_probe(void)
 {
-    if ( xen_guest )
-        return;
-
-    /* Too early to use cpu_has_hypervisor */
-    if ( !(cpuid_ecx(1) & cpufeat_mask(X86_FEATURE_HYPERVISOR)) )
-        return;
-
     find_xen_leaves();
 
     if ( !xen_cpuid_base )
-        return;
+        return false;
 
     /* Fill the hypercall page. */
     wrmsrl(cpuid_ebx(xen_cpuid_base + 2), __pa(hypercall_page));
 
     xen_guest = true;
+
+    return true;
 }
 
 static void map_shared_info(void)
@@ -249,7 +244,7 @@ static void init_evtchn(void)
     }
 }
 
-void __init hypervisor_setup(void)
+void __init xen_setup(void)
 {
     init_memmap();
 
@@ -277,7 +272,7 @@ void __init hypervisor_setup(void)
     init_evtchn();
 }
 
-void hypervisor_ap_setup(void)
+void xen_ap_setup(void)
 {
     set_vcpu_id();
     map_vcpuinfo();
@@ -307,7 +302,7 @@ static void ap_resume(void *unused)
     init_evtchn();
 }
 
-void hypervisor_resume(void)
+void xen_resume(void)
 {
     /* Reset shared info page. */
     map_shared_info();
@@ -330,6 +325,13 @@ void hypervisor_resume(void)
         pv_console_init();
 }
 
+struct hypervisor_ops xen_hypervisor_ops = {
+    .name = "Xen",
+    .setup = xen_setup,
+    .ap_setup = xen_ap_setup,
+    .resume = xen_resume,
+};
+
 /*
  * Local variables:
  * mode: C
xen/arch/x86/setup.c +1 -1
index dec60d0301..0ee11b15a6 100644
@@ -763,7 +763,7 @@ void __init noreturn __start_xen(unsigned long mbi_p)
      * allocing any xenheap structures wanted in lower memory. */
     kexec_early_calculations();
 
-    probe_hypervisor();
+    hypervisor_probe();
 
     parse_video_info();
 
xen/include/asm-x86/guest/xen.h +4 -2
index 8221fc1325..7401c80a1a 100644
@@ -23,6 +23,7 @@
 
 #include <asm/e820.h>
 #include <asm/fixmap.h>
+#include <asm/guest/hypervisor.h>
 
 #define XEN_shared_info ((struct shared_info *)fix_to_virt(FIX_XEN_SHARED_INFO))
 
@@ -31,8 +32,9 @@
 extern bool xen_guest;
 extern bool pv_console;
 extern uint32_t xen_cpuid_base;
+extern struct hypervisor_ops xen_hypervisor_ops;
 
-void probe_hypervisor(void);
+bool xen_probe(void);
 int xen_alloc_unused_page(mfn_t *mfn);
 int xen_free_unused_page(mfn_t mfn);
 
@@ -44,7 +46,7 @@ DECLARE_PER_CPU(struct vcpu_info *, vcpu_info);
 #define xen_guest 0
 #define pv_console 0
 
-static inline void probe_hypervisor(void) {}
+static inline bool xen_probe(void) { return false; }
 
 #endif /* CONFIG_XEN_GUEST */
 #endif /* __X86_GUEST_XEN_H__ */
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#10 lore raw

Re: [Xen-devel] [PATCH for-next v2 7/9] x86: switch xen implementation to use hypervisor framework

Quoted 1 line · Wei Liu

Take the chance to change probe_hypervisor to hypervisor_probe.

The implementation LGTM.

Quoted 26 lines
---
 xen/arch/x86/guest/hypervisor.c   | 31 +++++++++++++++++++++++++++++--
 xen/arch/x86/guest/xen/pvh-boot.c |  2 +-
 xen/arch/x86/guest/xen/xen.c      | 26 ++++++++++++++------------
 xen/arch/x86/setup.c              |  2 +-
 xen/include/asm-x86/guest/xen.h   |  6 ++++--
 5 files changed, 49 insertions(+), 18 deletions(-)
xen/arch/x86/guest/hypervisor.c +1 -1
index 89b9ae4de0..30453b6a7a 100644
@@ -22,7 +22,7 @@
 #include <xen/types.h>
 
 #include <asm/cache.h>
-#include <asm/guest/hypervisor.h>
+#include <asm/guest.h>
 
 static struct hypervisor_ops *hops __read_mostly;
 
@@ -31,7 +31,34 @@ bool hypervisor_probe(void)
     if ( hops )
         return true;

xen/arch/x86/guest/hypervisor.c bool hypervisor_probe(void) · line 41

Quoted 10 lines
xen/arch/x86/guest/hypervisor.c +8 -1
 
-    return false;
+    /* Too early to use cpu_has_hypervisor */
+    if ( !(cpuid_ecx(1) & cpufeat_mask(X86_FEATURE_HYPERVISOR)) )
+        return false;
+
+#ifdef CONFIG_XEN_GUEST
+    if ( xen_probe() )
+        hops = &xen_hypervisor_ops;
+#endif

I think you likely want something like:

    if ( xen_probe() )
    {
        hops = &xen_hypervisor_ops;
	return true;
    }
    if ( hyperv_probe() )
    {
        ....
	return true;
    }

    return false;

In order to return after a successful probe, or else you lose cycles probing for hypervisors when a match has been found, and also in the Xen case you risk detecting the HyperV support in Xen and thus using that instead of the Xen one.

Long term if we gain more guests support I would likely want to see hypervisor_ops turning into an array and gaining a probe function so that this can be done in a loop instead of having this function.

Quoted 88 lines
xen/arch/x86/guest/hypervisor.c +20 -0
+
+    return !!hops;
+}
+
+void hypervisor_setup(void)
+{
+    if ( hops && hops->setup )
+        hops->setup();
+}
+
+void hypervisor_ap_setup(void)
+{
+    if ( hops && hops->ap_setup )
+        hops->ap_setup();
+}
+
+void hypervisor_resume(void)
+{
+    if ( hops && hops->resume )
+        hops->resume();
 }
 
 /*
xen/arch/x86/guest/xen/pvh-boot.c +1 -1
index ca8e156f7d..498625eae0 100644
@@ -103,7 +103,7 @@ void __init pvh_init(multiboot_info_t **mbi, module_t **mod)
 {
     convert_pvh_info(mbi, mod);
 
-    probe_hypervisor();
+    hypervisor_probe();
     ASSERT(xen_guest);
 
     get_memory_map();
xen/arch/x86/guest/xen/xen.c +6 -11
index 9895025d02..a9d321e5eb 100644
@@ -67,24 +67,19 @@ static void __init find_xen_leaves(void)
     }
 }
 
-void __init probe_hypervisor(void)
+bool __init xen_probe(void)
 {
-    if ( xen_guest )
-        return;
-
-    /* Too early to use cpu_has_hypervisor */
-    if ( !(cpuid_ecx(1) & cpufeat_mask(X86_FEATURE_HYPERVISOR)) )
-        return;
-
     find_xen_leaves();
 
     if ( !xen_cpuid_base )
-        return;
+        return false;
 
     /* Fill the hypercall page. */
     wrmsrl(cpuid_ebx(xen_cpuid_base + 2), __pa(hypercall_page));
 
     xen_guest = true;
+
+    return true;
 }
 
 static void map_shared_info(void)
@@ -249,7 +244,7 @@ static void init_evtchn(void)
     }
 }
 
-void __init hypervisor_setup(void)
+void __init xen_setup(void)
 {
     init_memmap();
 
@@ -277,7 +272,7 @@ void __init hypervisor_setup(void)
     init_evtchn();
 }
 
-void hypervisor_ap_setup(void)
+void xen_ap_setup(void)
 {
     set_vcpu_id();
     map_vcpuinfo();

xen/arch/x86/guest/xen/xen.c static void ap_resume(void *unused) · line 305

Quoted 5 lines
xen/arch/x86/guest/xen/xen.c +1 -1
@@ -307,7 +302,7 @@ static void ap_resume(void *unused)
     init_evtchn();
 }
 
-void hypervisor_resume(void)
+void xen_resume(void)

I think xen_{setup/ap_setup/resume} can be made static now?

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#11 lore raw

Re: [Xen-devel] [PATCH for-next v2 7/9] x86: switch xen implementation to use hypervisor framework

On Mon, Oct 21, 2019 at 11:56:36AM +0200, Roger Pau Monné wrote:
[...]

Quoted 31 lines
Quoted 5 lines

static struct hypervisor_ops *hops __read_mostly;

xen/arch/x86/guest/hypervisor.c +0 -0
@@ -31,7 +31,34 @@ bool hypervisor_probe(void)
     if ( hops )
         return true;

xen/arch/x86/guest/hypervisor.c bool hypervisor_probe(void) · line 41

Quoted 10 lines
xen/arch/x86/guest/hypervisor.c +8 -1
 
-    return false;
+    /* Too early to use cpu_has_hypervisor */
+    if ( !(cpuid_ecx(1) & cpufeat_mask(X86_FEATURE_HYPERVISOR)) )
+        return false;
+
+#ifdef CONFIG_XEN_GUEST
+    if ( xen_probe() )
+        hops = &xen_hypervisor_ops;
+#endif

I think you likely want something like:

    if ( xen_probe() )
    {
        hops = &xen_hypervisor_ops;
	return true;
    }
    if ( hyperv_probe() )
    {
        ....
	return true;
    }

    return false;
Quoted 5 lines

In order to return after a successful probe, or else you lose cycles probing for hypervisors when a match has been found, and also in the Xen case you risk detecting the HyperV support in Xen and thus using that instead of the Xen one.

Good point.

Quoted 4 lines

Long term if we gain more guests support I would likely want to see hypervisor_ops turning into an array and gaining a probe function so that this can be done in a loop instead of having this function.

That was my plan from the get-go but Xen lacked appropriate infrastructure, hence I resorted to something akin to HVM hooks.

[...]

Quoted 18 lines
Quoted 18 lines
xen/arch/x86/guest/xen/xen.c +2 -2
-void __init hypervisor_setup(void)
+void __init xen_setup(void)
 {
     init_memmap();
@@ -277,7 +272,7 @@ void __init hypervisor_setup(void)
     init_evtchn();
 }
 
-void hypervisor_ap_setup(void)
+void xen_ap_setup(void)
 {
     set_vcpu_id();
     map_vcpuinfo();
@@ -307,7 +302,7 @@ static void ap_resume(void *unused)
     init_evtchn();
 }
 
Quoted 4 lines

xen/arch/x86/guest/xen/xen.c static void ap_resume(void *unused) · line 305

Quoted 2 lines
xen/arch/x86/guest/xen/xen.c +1 -1
-void hypervisor_resume(void)
+void xen_resume(void)

I think xen_{setup/ap_setup/resume} can be made static now?

Indeed. I will fix this.

Wei.

Quoted 2 lines

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#12 lore raw

PATCH v2 8/9 x86: be more verbose when running on a hypervisor

In reply to Wei Liu
---
 xen/arch/x86/guest/hypervisor.c        | 5 +++++
 xen/arch/x86/setup.c                   | 6 +++++-
 xen/include/asm-x86/guest/hypervisor.h | 2 ++
 3 files changed, 12 insertions(+), 1 deletion(-)
xen/arch/x86/guest/hypervisor.c +5 -0
index 30453b6a7a..8161b26c5a 100644
@@ -43,6 +43,11 @@ bool hypervisor_probe(void)
     return !!hops;
 }
 
+const char *hypervisor_name(void)
+{
+    return hops->name;
+}
+
 void hypervisor_setup(void)
 {
     if ( hops && hops->setup )
xen/arch/x86/setup.c +5 -1
index 0ee11b15a6..cf5a7b8e1e 100644
@@ -689,6 +689,7 @@ void __init noreturn __start_xen(unsigned long mbi_p)
     int i, j, e820_warn = 0, bytes = 0;
     bool acpi_boot_table_init_done = false, relocated = false;
     int ret;
+    bool running_on_hypervisor;
     struct ns16550_defaults ns16550 = {
         .data_bits = 8,
         .parity    = 'n',
@@ -763,7 +764,7 @@ void __init noreturn __start_xen(unsigned long mbi_p)
      * allocing any xenheap structures wanted in lower memory. */
     kexec_early_calculations();
 
-    hypervisor_probe();
+    running_on_hypervisor = hypervisor_probe();
 
     parse_video_info();
 
@@ -789,6 +790,9 @@ void __init noreturn __start_xen(unsigned long mbi_p)
 
     printk("Xen image load base address: %#lx\n", xen_phys_start);
 
+    if ( running_on_hypervisor )
+        printk("Running on %s\n", hypervisor_name());
+
 #ifdef CONFIG_VIDEO
     printk("Video information:\n");
 
xen/include/asm-x86/guest/hypervisor.h +2 -0
index 38344e2e89..a7d75bf9cf 100644
@@ -36,6 +36,7 @@ bool hypervisor_probe(void);
 void hypervisor_setup(void);
 void hypervisor_ap_setup(void);
 void hypervisor_resume(void);
+const char *hypervisor_name(void);
 
 #else
 
@@ -45,6 +46,7 @@ static inline bool hypervisor_probe(void) { return false; }
 static inline void hypervisor_setup(void) {}
 static inline void hypervisor_ap_setup(void) {}
 static inline void hypervisor_resume(void) {}
+static inline char *hypervisor_name(void) { return NULL; }
 
 #endif  /* CONFIG_GUEST */
 
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#13 lore raw

Re: [Xen-devel] [PATCH for-next v2 8/9] x86: be more verbose when running on a hypervisor

Quoted 12 lines · Wei Liu
---
 xen/arch/x86/guest/hypervisor.c        | 5 +++++
 xen/arch/x86/setup.c                   | 6 +++++-
 xen/include/asm-x86/guest/hypervisor.h | 2 ++
 3 files changed, 12 insertions(+), 1 deletion(-)
xen/arch/x86/guest/hypervisor.c +0 -0
index 30453b6a7a..8161b26c5a 100644

xen/arch/x86/guest/hypervisor.c bool hypervisor_probe(void) · line 47

Quoted 5 lines
xen/arch/x86/guest/hypervisor.c +2 -0
@@ -43,6 +43,11 @@ bool hypervisor_probe(void)
     return !!hops;
 }
 
+const char *hypervisor_name(void)
+{

I would maybe add ASSERT(hops);

Quoted 50 lines
xen/arch/x86/guest/hypervisor.c +3 -0
+    return hops->name;
+}
+
 void hypervisor_setup(void)
 {
     if ( hops && hops->setup )
xen/arch/x86/setup.c +5 -1
index 0ee11b15a6..cf5a7b8e1e 100644
@@ -689,6 +689,7 @@ void __init noreturn __start_xen(unsigned long mbi_p)
     int i, j, e820_warn = 0, bytes = 0;
     bool acpi_boot_table_init_done = false, relocated = false;
     int ret;
+    bool running_on_hypervisor;
     struct ns16550_defaults ns16550 = {
         .data_bits = 8,
         .parity    = 'n',
@@ -763,7 +764,7 @@ void __init noreturn __start_xen(unsigned long mbi_p)
      * allocing any xenheap structures wanted in lower memory. */
     kexec_early_calculations();
 
-    hypervisor_probe();
+    running_on_hypervisor = hypervisor_probe();
 
     parse_video_info();
 
@@ -789,6 +790,9 @@ void __init noreturn __start_xen(unsigned long mbi_p)
 
     printk("Xen image load base address: %#lx\n", xen_phys_start);
 
+    if ( running_on_hypervisor )
+        printk("Running on %s\n", hypervisor_name());
+
 #ifdef CONFIG_VIDEO
     printk("Video information:\n");
 
xen/include/asm-x86/guest/hypervisor.h +1 -0
index 38344e2e89..a7d75bf9cf 100644
@@ -36,6 +36,7 @@ bool hypervisor_probe(void);
 void hypervisor_setup(void);
 void hypervisor_ap_setup(void);
 void hypervisor_resume(void);
+const char *hypervisor_name(void);
 
 #else
 

xen/include/asm-x86/guest/hypervisor.h static inline bool hypervisor_probe(void) { return false; } · line 49

Quoted 4 lines
xen/include/asm-x86/guest/hypervisor.h +1 -0
@@ -45,6 +46,7 @@ static inline bool hypervisor_probe(void) { return false; }
 static inline void hypervisor_setup(void) {}
 static inline void hypervisor_ap_setup(void) {}
 static inline void hypervisor_resume(void) {}
+static inline char *hypervisor_name(void) { return NULL; }

I think you want an ASSERT_UNREACHABLE here, since hypervisor_name shouldn't be called unless Xen has detected that's running as a guest, which can only happen if CONFIG_GUEST is selected.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#14 lore raw

Re: [Xen-devel] [PATCH for-next v2 8/9] x86: be more verbose when running on a hypervisor

Quoted 24 lines · Roger Pau Monné
Quoted 12 lines · Wei Liu
---
 xen/arch/x86/guest/hypervisor.c        | 5 +++++
 xen/arch/x86/setup.c                   | 6 +++++-
 xen/include/asm-x86/guest/hypervisor.h | 2 ++
 3 files changed, 12 insertions(+), 1 deletion(-)
xen/arch/x86/guest/hypervisor.c +0 -0
index 30453b6a7a..8161b26c5a 100644

xen/arch/x86/guest/hypervisor.c bool hypervisor_probe(void) · line 47

Quoted 5 lines
xen/arch/x86/guest/hypervisor.c +2 -0
@@ -43,6 +43,11 @@ bool hypervisor_probe(void)
     return !!hops;
 }
 
+const char *hypervisor_name(void)
+{

I would maybe add ASSERT(hops);

Quoted 3 lines
xen/arch/x86/guest/hypervisor.c +3 -0
+    return hops->name;
+}
+

[...]

Quoted 12 lines
Quoted 12 lines
xen/include/asm-x86/guest/hypervisor.h +1 -0
index 38344e2e89..a7d75bf9cf 100644
@@ -36,6 +36,7 @@ bool hypervisor_probe(void);
 void hypervisor_setup(void);
 void hypervisor_ap_setup(void);
 void hypervisor_resume(void);
+const char *hypervisor_name(void);
 
 #else
 
Quoted 9 lines

xen/include/asm-x86/guest/hypervisor.h static inline bool hypervisor_probe(void) { return false; } · line 49

Quoted 5 lines
xen/include/asm-x86/guest/hypervisor.h +1 -0
@@ -45,6 +46,7 @@ static inline bool hypervisor_probe(void) { return false; }
 static inline void hypervisor_setup(void) {}
 static inline void hypervisor_ap_setup(void) {}
 static inline void hypervisor_resume(void) {}
+static inline char *hypervisor_name(void) { return NULL; }

I think you want an ASSERT_UNREACHABLE here, since hypervisor_name shouldn't be called unless Xen has detected that's running as a guest, which can only happen if CONFIG_GUEST is selected.

Ack to both comments.

Wei.

Quoted 2 lines

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#15 lore raw

Re: [Xen-devel] [PATCH for-next v2 8/9] x86: be more verbose when running on a hypervisor

In reply to Roger Pau Monné
Quoted 11 lines · Roger Pau Monné
Quoted 10 lines · Wei Liu
xen/include/asm-x86/guest/hypervisor.h +1 -0
@@ -36,6 +36,7 @@ bool hypervisor_probe(void);
 void hypervisor_setup(void);
 void hypervisor_ap_setup(void);
 void hypervisor_resume(void);
+const char *hypervisor_name(void);
 
 #else
 
Quoted 9 lines

xen/include/asm-x86/guest/hypervisor.h static inline bool hypervisor_probe(void) { return false; } · line 49

Quoted 5 lines
xen/include/asm-x86/guest/hypervisor.h +1 -0
@@ -45,6 +46,7 @@ static inline bool hypervisor_probe(void) { return false; }
 static inline void hypervisor_setup(void) {}
 static inline void hypervisor_ap_setup(void) {}
 static inline void hypervisor_resume(void) {}
+static inline char *hypervisor_name(void) { return NULL; }

I think you want an ASSERT_UNREACHABLE here, since hypervisor_name shouldn't be called unless Xen has detected that's running as a guest, which can only happen if CONFIG_GUEST is selected.

And please bring prototype and stub in sync return-type-wise.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#16 lore raw

Re: [Xen-devel] [PATCH for-next v2 8/9] x86: be more verbose when running on a hypervisor

Quoted 22 lines · Jan Beulich
Quoted 11 lines · Roger Pau Monné
Quoted 10 lines · Wei Liu
xen/include/asm-x86/guest/hypervisor.h +1 -0
@@ -36,6 +36,7 @@ bool hypervisor_probe(void);
 void hypervisor_setup(void);
 void hypervisor_ap_setup(void);
 void hypervisor_resume(void);
+const char *hypervisor_name(void);
 
 #else
 
Quoted 9 lines

xen/include/asm-x86/guest/hypervisor.h static inline bool hypervisor_probe(void) { return false; } · line 49

Quoted 5 lines
xen/include/asm-x86/guest/hypervisor.h +1 -0
@@ -45,6 +46,7 @@ static inline bool hypervisor_probe(void) { return false; }
 static inline void hypervisor_setup(void) {}
 static inline void hypervisor_ap_setup(void) {}
 static inline void hypervisor_resume(void) {}
+static inline char *hypervisor_name(void) { return NULL; }

I think you want an ASSERT_UNREACHABLE here, since hypervisor_name shouldn't be called unless Xen has detected that's running as a guest, which can only happen if CONFIG_GUEST is selected.

Quoted 1 line

And please bring prototype and stub in sync return-type-wise.

Missed this comment.

I will handle this if I haven't already.

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#17 lore raw

PATCH v2 9/9 x86: introduce CONFIG_HYPERV and detection code

In reply to Wei Liu

We use the same code structure as we die for Xen.

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

---
 xen/arch/x86/Kconfig               |  9 ++++
 xen/arch/x86/guest/Makefile        |  1 +
 xen/arch/x86/guest/hyperv/Makefile |  1 +
 xen/arch/x86/guest/hyperv/hyperv.c | 69 ++++++++++++++++++++++++++++++
 xen/arch/x86/guest/hypervisor.c    |  5 +++
 xen/include/asm-x86/guest.h        |  1 +
 xen/include/asm-x86/guest/hyperv.h | 45 +++++++++++++++++++
 7 files changed, 131 insertions(+)
 create mode 100644 xen/arch/x86/guest/hyperv/Makefile
 create mode 100644 xen/arch/x86/guest/hyperv/hyperv.c
 create mode 100644 xen/include/asm-x86/guest/hyperv.h
xen/arch/x86/Kconfig +9 -0
index 584bdc1bb8..c5a93babfe 100644
@@ -163,6 +163,15 @@ endchoice
 config GUEST
 	bool
 
+config HYPERV_GUEST
+	def_bool n
+	select GUEST
+	prompt "Hyper-V Guest"
+	---help---
+	  Support for Xen detecting when it is running under Hyper-V.
+
+	  If unsure, say N.
+
 config XEN_GUEST
 	def_bool n
 	select GUEST
xen/arch/x86/guest/Makefile +1 -0
index f63d64bbee..f164196772 100644
@@ -1,3 +1,4 @@
 obj-y += hypervisor.o
 
+subdir-$(CONFIG_HYPERV_GUEST) += hyperv
 subdir-$(CONFIG_XEN_GUEST) += xen
xen/arch/x86/guest/hyperv/Makefile new file +1 -0
new file mode 100644
index 0000000000..68170109a9
@@ -0,0 +1 @@
+obj-y += hyperv.o
xen/arch/x86/guest/hyperv/hyperv.c new file +69 -0

69 lines not on this page · raw message

xen/arch/x86/guest/hypervisor.c +5 -0
index 8161b26c5a..87a195e888 100644
@@ -40,6 +40,11 @@ bool hypervisor_probe(void)
         hops = &xen_hypervisor_ops;
 #endif
 
+#ifdef CONFIG_HYPERV_GUEST
+    if ( hyperv_probe() )
+        hops = &hyperv_hypervisor_ops;
+#endif
+
     return !!hops;
 }
 
xen/include/asm-x86/guest.h +1 -0
index 8e167165ae..94448606d4 100644
@@ -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>
xen/include/asm-x86/guest/hyperv.h new file +45 -0

45 lines not on this page · raw message

Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#19 lore raw

Re: [Xen-devel] [PATCH for-next v2 9/9] x86: introduce CONFIG_HYPERV and detection code

In reply to Wei Liu
Quoted 79 lines · Wei Liu

We use the same code structure as we die for Xen.

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

---
 xen/arch/x86/Kconfig               |  9 ++++
 xen/arch/x86/guest/Makefile        |  1 +
 xen/arch/x86/guest/hyperv/Makefile |  1 +
 xen/arch/x86/guest/hyperv/hyperv.c | 69 ++++++++++++++++++++++++++++++
 xen/arch/x86/guest/hypervisor.c    |  5 +++
 xen/include/asm-x86/guest.h        |  1 +
 xen/include/asm-x86/guest/hyperv.h | 45 +++++++++++++++++++
 7 files changed, 131 insertions(+)
 create mode 100644 xen/arch/x86/guest/hyperv/Makefile
 create mode 100644 xen/arch/x86/guest/hyperv/hyperv.c
 create mode 100644 xen/include/asm-x86/guest/hyperv.h
xen/arch/x86/Kconfig +9 -0
index 584bdc1bb8..c5a93babfe 100644
@@ -163,6 +163,15 @@ endchoice
 config GUEST
 	bool
 
+config HYPERV_GUEST
+	def_bool n
+	select GUEST
+	prompt "Hyper-V Guest"
+	---help---
+	  Support for Xen detecting when it is running under Hyper-V.
+
+	  If unsure, say N.
+
 config XEN_GUEST
 	def_bool n
 	select GUEST
xen/arch/x86/guest/Makefile +1 -0
index f63d64bbee..f164196772 100644
@@ -1,3 +1,4 @@
 obj-y += hypervisor.o
 
+subdir-$(CONFIG_HYPERV_GUEST) += hyperv
 subdir-$(CONFIG_XEN_GUEST) += xen
xen/arch/x86/guest/hyperv/Makefile new file +1 -0
new file mode 100644
index 0000000000..68170109a9
@@ -0,0 +1 @@
+obj-y += hyperv.o
xen/arch/x86/guest/hyperv/hyperv.c new file +18 -0
new file mode 100644
index 0000000000..4494b87fe8
@@ -0,0 +1,69 @@
+/******************************************************************************
+ * 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/>.
+ *

xen/arch/x86/guest/hyperv/hyperv.c line 28

Quoted 10 lines
xen/arch/x86/guest/hyperv/hyperv.c new file +10 -0
+ * Copyright (c) 2019 Microsoft.
+ */
+#include <xen/init.h>
+
+#include <asm/guest.h>
+
+bool __init hyperv_probe(void)
+{
+    uint32_t eax, ebx, ecx, edx;
+    bool hyperv_guest = false;

I don't think you need this local variable, you can return true in if the if condition matches, and false otherwise.

xen/arch/x86/guest/hyperv/hyperv.c line 33

Quoted 5 lines
xen/arch/x86/guest/hyperv/hyperv.c new file +5 -0
+
+    cpuid(0x40000000, &eax, &ebx, &ecx, &edx);
+    if ( (ebx == 0x7263694d) && /* "Micr" */
+         (ecx == 0x666f736f) && /* "osof" */
+         (edx == 0x76482074) )  /* "t Hv" */

I guess there are no HyperV headers to import that have those values defined?

Alternatively you could do something like the following I think:

static const char hyperv_sig[] __initconst = "Microsoft Hv";

bool __init hyperv_probe(void)

{
    uint32_t eax, sig[3];

    cpuid(0x40000000, &eax, &sig[0], &sig[1], &sig[2]);
    if ( !strncmp(hyperv_sig, sig, strncmp(hyperv_sig) )
        return true;

    return false;
}
Quoted 9 lines
xen/arch/x86/guest/hyperv/hyperv.c new file +9 -0
+        hyperv_guest = true;
+
+    return hyperv_guest;
+}
+
+void __init hyperv_setup(void)
+{
+    /* Nothing yet */
+}

xen/arch/x86/guest/hyperv/hyperv.c line 52

Quoted 10 lines
xen/arch/x86/guest/hyperv/hyperv.c new file +10 -0
+
+void hyperv_ap_setup(void)
+{
+    /* Nothing yet */
+}
+
+void hyperv_resume(void)
+{
+    /* Nothing yet */
+}

There's no need to introduce such dummy functions, just leaving the pointers as NULL should work fine, and the functions can be introduced when there's logic in them.

Quoted 22 lines
xen/arch/x86/guest/hyperv/hyperv.c new file +17 -0
+
+struct hypervisor_ops hyperv_hypervisor_ops = {
+    .name = "Hyper-V",
+    .setup = hyperv_setup,
+    .ap_setup = hyperv_ap_setup,
+    .resume = hyperv_resume,
+};
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/x86/guest/hypervisor.c +0 -0
index 8161b26c5a..87a195e888 100644

xen/arch/x86/guest/hypervisor.c bool hypervisor_probe(void) · line 46

Quoted 7 lines
xen/arch/x86/guest/hypervisor.c +4 -0
@@ -40,6 +40,11 @@ bool hypervisor_probe(void)
         hops = &xen_hypervisor_ops;
 #endif
 
+#ifdef CONFIG_HYPERV_GUEST
+    if ( hyperv_probe() )
+        hops = &hyperv_hypervisor_ops;
+#endif

This won't work correctly if Xen has viridian extensions enabled, since the HyperV probe will overwrite the Xen one.

Quoted 38 lines
xen/arch/x86/guest/hypervisor.c +1 -0
+
     return !!hops;
 }
 
xen/include/asm-x86/guest.h +1 -0
index 8e167165ae..94448606d4 100644
@@ -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>
xen/include/asm-x86/guest/hyperv.h new file +16 -0
new file mode 100644
index 0000000000..a2d8ee8987
@@ -0,0 +1,45 @@
+/******************************************************************************
+ * 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.

xen/include/asm-x86/guest/hyperv.h line 26

Quoted 10 lines
xen/include/asm-x86/guest/hyperv.h new file +10 -0
+ */
+
+#ifndef __X86_GUEST_HYPERV_H__
+#define __X86_GUEST_HYPERV_H__
+
+#ifdef CONFIG_HYPERV_GUEST
+
+#include <asm/guest/hypervisor.h>
+
+extern struct hypervisor_ops hyperv_hypervisor_ops;

hyperv_ops would be fine by me, seems kind of redundant to have 'hyper' twice in a name.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#20 lore raw

Re: [Xen-devel] [PATCH for-next v2 9/9] x86: introduce CONFIG_HYPERV and detection code

Quoted 105 lines · Roger Pau Monné
Quoted 79 lines · Wei Liu

We use the same code structure as we die for Xen.

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

---
 xen/arch/x86/Kconfig               |  9 ++++
 xen/arch/x86/guest/Makefile        |  1 +
 xen/arch/x86/guest/hyperv/Makefile |  1 +
 xen/arch/x86/guest/hyperv/hyperv.c | 69 ++++++++++++++++++++++++++++++
 xen/arch/x86/guest/hypervisor.c    |  5 +++
 xen/include/asm-x86/guest.h        |  1 +
 xen/include/asm-x86/guest/hyperv.h | 45 +++++++++++++++++++
 7 files changed, 131 insertions(+)
 create mode 100644 xen/arch/x86/guest/hyperv/Makefile
 create mode 100644 xen/arch/x86/guest/hyperv/hyperv.c
 create mode 100644 xen/include/asm-x86/guest/hyperv.h
xen/arch/x86/Kconfig +9 -0
index 584bdc1bb8..c5a93babfe 100644
@@ -163,6 +163,15 @@ endchoice
 config GUEST
 	bool
 
+config HYPERV_GUEST
+	def_bool n
+	select GUEST
+	prompt "Hyper-V Guest"
+	---help---
+	  Support for Xen detecting when it is running under Hyper-V.
+
+	  If unsure, say N.
+
 config XEN_GUEST
 	def_bool n
 	select GUEST
xen/arch/x86/guest/Makefile +1 -0
index f63d64bbee..f164196772 100644
@@ -1,3 +1,4 @@
 obj-y += hypervisor.o
 
+subdir-$(CONFIG_HYPERV_GUEST) += hyperv
 subdir-$(CONFIG_XEN_GUEST) += xen
xen/arch/x86/guest/hyperv/Makefile new file +1 -0
new file mode 100644
index 0000000000..68170109a9
@@ -0,0 +1 @@
+obj-y += hyperv.o
xen/arch/x86/guest/hyperv/hyperv.c new file +18 -0
new file mode 100644
index 0000000000..4494b87fe8
@@ -0,0 +1,69 @@
+/******************************************************************************
+ * 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/>.
+ *

xen/arch/x86/guest/hyperv/hyperv.c line 28

Quoted 10 lines
xen/arch/x86/guest/hyperv/hyperv.c new file +10 -0
+ * Copyright (c) 2019 Microsoft.
+ */
+#include <xen/init.h>
+
+#include <asm/guest.h>
+
+bool __init hyperv_probe(void)
+{
+    uint32_t eax, ebx, ecx, edx;
+    bool hyperv_guest = false;

I don't think you need this local variable, you can return true in if the if condition matches, and false otherwise.

xen/arch/x86/guest/hyperv/hyperv.c line 33

Quoted 5 lines
xen/arch/x86/guest/hyperv/hyperv.c new file +5 -0
+
+    cpuid(0x40000000, &eax, &ebx, &ecx, &edx);
+    if ( (ebx == 0x7263694d) && /* "Micr" */
+         (ecx == 0x666f736f) && /* "osof" */
+         (edx == 0x76482074) )  /* "t Hv" */

I guess there are no HyperV headers to import that have those values defined?

Alternatively you could do something like the following I think:

Quoted 8 lines

static const char hyperv_sig[] __initconst = "Microsoft Hv";

bool __init hyperv_probe(void)

{
    uint32_t eax, sig[3];

    cpuid(0x40000000, &eax, &sig[0], &sig[1], &sig[2]);
    if ( !strncmp(hyperv_sig, sig, strncmp(hyperv_sig) )

Urg, I've made a mistake here, the line should be:

!strncmp(hyperv_sig, sig, strlen(hyperv_sig))

And you can likely declare hyperv_sig inside the probe function also.

Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#21 lore raw

Re: [Xen-devel] [PATCH for-next v2 9/9] x86: introduce CONFIG_HYPERV and detection code

Quoted 16 lines · Roger Pau Monné
Quoted 16 lines

xen/arch/x86/guest/hyperv/hyperv.c line 33

Quoted 5 lines
xen/arch/x86/guest/hyperv/hyperv.c new file +5 -0
+
+    cpuid(0x40000000, &eax, &ebx, &ecx, &edx);
+    if ( (ebx == 0x7263694d) && /* "Micr" */
+         (ecx == 0x666f736f) && /* "osof" */
+         (edx == 0x76482074) )  /* "t Hv" */

I guess there are no HyperV headers to import that have those values defined?

Alternatively you could do something like the following I think:

static const char hyperv_sig[] __initconst = "Microsoft Hv";

bool __init hyperv_probe(void)

{
    uint32_t eax, sig[3];
Quoted 5 lines
Quoted 2 lines

cpuid(0x40000000, &eax, &sig[0], &sig[1], &sig[2]); if ( !strncmp(hyperv_sig, sig, strncmp(hyperv_sig) )

Urg, I've made a mistake here, the line should be:

!strncmp(hyperv_sig, sig, strlen(hyperv_sig))

Just because the leaves form an ascii string, doesn't mean that using string comparisons are the sane way to check.  3x 32bit compares are substantially more efficient, and far harder to get wrong.

Wei: On your detection algorithm, you also need to find HV#1 in 0x40000001.eax to detect conformance to the viridian spec.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#22 lore raw

Re: [Xen-devel] [PATCH for-next v2 9/9] x86: introduce CONFIG_HYPERV and detection code

Quoted 21 lines · Andrew Cooper
Quoted 20 lines · Roger Pau Monné
Quoted 10 lines

xen/arch/x86/guest/hyperv/hyperv.c line 33

Quoted 5 lines
xen/arch/x86/guest/hyperv/hyperv.c new file +5 -0
+
+    cpuid(0x40000000, &eax, &ebx, &ecx, &edx);
+    if ( (ebx == 0x7263694d) && /* "Micr" */
+         (ecx == 0x666f736f) && /* "osof" */
+         (edx == 0x76482074) )  /* "t Hv" */

I guess there are no HyperV headers to import that have those values defined?

Alternatively you could do something like the following I think:

Quoted 8 lines

static const char hyperv_sig[] __initconst = "Microsoft Hv";

bool __init hyperv_probe(void)

{
    uint32_t eax, sig[3];

    cpuid(0x40000000, &eax, &sig[0], &sig[1], &sig[2]);
    if ( !strncmp(hyperv_sig, sig, strncmp(hyperv_sig) )

Urg, I've made a mistake here, the line should be:

Quoted 8 lines
Quoted 1 line

!strncmp(hyperv_sig, sig, strlen(hyperv_sig))

Just because the leaves form an ascii string, doesn't mean that using string comparisons are the sane way to check.  3x 32bit compares are substantially more efficient, and far harder to get wrong.

Wei: On your detection algorithm, you also need to find HV#1 in 0x40000001.eax to detect conformance to the viridian spec.

Sure I can do that.

I'm not sure it matters that much in practice though.

Wei.

Quoted 2 lines

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#23 lore raw

Re: [Xen-devel] [PATCH for-next v2 9/9] x86: introduce CONFIG_HYPERV and detection code

In reply to Roger Pau Monné

On Mon, Oct 21, 2019 at 12:22:25PM +0200, Roger Pau Monné wrote:
[...]

Quoted 8 lines

xen/arch/x86/guest/hyperv/hyperv.c line 28

Quoted 4 lines
xen/arch/x86/guest/hyperv/hyperv.c new file +4 -0
+bool __init hyperv_probe(void)
+{
+    uint32_t eax, ebx, ecx, edx;
+    bool hyperv_guest = false;

I don't think you need this local variable, you can return true in if the if condition matches, and false otherwise.

Sure. I can drop it for now and reintroduce it when necessary.

Quoted 9 lines

xen/arch/x86/guest/hyperv/hyperv.c line 33

Quoted 5 lines
xen/arch/x86/guest/hyperv/hyperv.c new file +5 -0
+
+    cpuid(0x40000000, &eax, &ebx, &ecx, &edx);
+    if ( (ebx == 0x7263694d) && /* "Micr" */
+         (ecx == 0x666f736f) && /* "osof" */
+         (edx == 0x76482074) )  /* "t Hv" */

I guess there are no HyperV headers to import that have those values defined?

Not yet. I have plan to import a header from Linux. When that's done these will be replaced by some macros.

So I will keep this as-is for now.

[...]

Quoted 12 lines

xen/include/asm-x86/guest/hyperv.h line 26

Quoted 8 lines
xen/include/asm-x86/guest/hyperv.h new file +8 -0
+#ifndef __X86_GUEST_HYPERV_H__
+#define __X86_GUEST_HYPERV_H__
+
+#ifdef CONFIG_HYPERV_GUEST
+
+#include <asm/guest/hypervisor.h>
+
+extern struct hypervisor_ops hyperv_hypervisor_ops;

hyperv_ops would be fine by me, seems kind of redundant to have 'hyper' twice in a name.

In that case I will also change xen_hypervisor_ops to xen_ops to remain consistent.

Wei.

Quoted 1 line

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#24 lore raw

Re: [Xen-devel] [PATCH for-next v2 9/9] x86: introduce CONFIG_HYPERV and detection code

Quoted 19 lines · Wei Liu

On Mon, Oct 21, 2019 at 12:22:25PM +0200, Roger Pau Monné wrote:
[...]

Quoted 8 lines

xen/arch/x86/guest/hyperv/hyperv.c line 28

Quoted 4 lines
xen/arch/x86/guest/hyperv/hyperv.c new file +4 -0
+bool __init hyperv_probe(void)
+{
+    uint32_t eax, ebx, ecx, edx;
+    bool hyperv_guest = false;

I don't think you need this local variable, you can return true in if the if condition matches, and false otherwise.

Sure. I can drop it for now and reintroduce it when necessary.

Quoted 6 lines
Quoted 5 lines
xen/arch/x86/guest/hyperv/hyperv.c new file +5 -0
+
+    cpuid(0x40000000, &eax, &ebx, &ecx, &edx);
+    if ( (ebx == 0x7263694d) && /* "Micr" */
+         (ecx == 0x666f736f) && /* "osof" */
+         (edx == 0x76482074) )  /* "t Hv" */
Quoted 8 lines
Quoted 3 lines

I guess there are no HyperV headers to import that have those values defined?

Not yet. I have plan to import a header from Linux. When that's done these will be replaced by some macros.

So I will keep this as-is for now.

Is it really cumbersome to introduce the header now?

IMO it would be better to avoid deferring this to when you introduce the header, since it's easy to miss it.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#25 lore raw

Re: [Xen-devel] [PATCH for-next v2 9/9] x86: introduce CONFIG_HYPERV and detection code

Quoted 27 lines · Roger Pau Monné
Quoted 26 lines · Wei Liu

On Mon, Oct 21, 2019 at 12:22:25PM +0200, Roger Pau Monné wrote:
[...]

Quoted 8 lines

xen/arch/x86/guest/hyperv/hyperv.c line 28

Quoted 4 lines
xen/arch/x86/guest/hyperv/hyperv.c new file +4 -0
+bool __init hyperv_probe(void)
+{
+    uint32_t eax, ebx, ecx, edx;
+    bool hyperv_guest = false;

I don't think you need this local variable, you can return true in if the if condition matches, and false otherwise.

Sure. I can drop it for now and reintroduce it when necessary.

Quoted 9 lines

xen/arch/x86/guest/hyperv/hyperv.c line 33

Quoted 5 lines
xen/arch/x86/guest/hyperv/hyperv.c new file +5 -0
+
+    cpuid(0x40000000, &eax, &ebx, &ecx, &edx);
+    if ( (ebx == 0x7263694d) && /* "Micr" */
+         (ecx == 0x666f736f) && /* "osof" */
+         (edx == 0x76482074) )  /* "t Hv" */

I guess there are no HyperV headers to import that have those values defined?

Not yet. I have plan to import a header from Linux. When that's done these will be replaced by some macros.

Quoted 6 lines
Quoted 1 line

So I will keep this as-is for now.

Is it really cumbersome to introduce the header now?

IMO it would be better to avoid deferring this to when you introduce the header, since it's easy to miss it.

The header in Linux is not without its problems. It certainly doesn't have the signature values in it yet, so whether importing it now or later is immaterial to this issue at hand. I will have to go through that header file first but -ETIME.

Wei.

Quoted 2 lines

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#26 lore raw

cover Port Xen to Hyper-V

In reply to Wei Liu

Hi all

In case you're wondering, I can already run a fully fledged Xen system on Hyper-V with emulated disk and network.

This is the very first stage for porting Xen to run on Hyper-V with all the goodies Hyper-V has to offer. With this series, Xen can successfully detect Hyper-V and prints out a message. I would like to first get the directory structure and kconfig options agreed upon.

There are two major areas to be worked on:
* Make Dom0 able to use Hyper-V's synthetic devices.
* Make Xen use of the synthetic timer, reference TSC and enlightenment VMCS and other interfaces.

They aren't trivial, and time can be scarce on my side, so I intend to post patches piece meal when they are ready.

Questions and comments are welcome.

Thanks,
Wei.

Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
Cc Wei Liu <[email protected]>
Cc Roger Pau Monné <[email protected]>

Wei Liu (8):
x86: introduce CONFIG_GUEST and move code
x86: include asm_defns.h directly in hypercall.h
x86: drop hypervisor_cpuid_base
x86: factor out xen variants for hypervisor setup code
x86: factor out hypervisor agnostic code
x86: make probe_xen return boolean value
x86: introduce CONFIG_HYPERV and hyperv directory
x86: be more verbose when running nested

 xen/arch/x86/Kconfig                          |  13 ++
 xen/arch/x86/Makefile                         |   2 +-
 xen/arch/x86/guest/Makefile                   |   6 +-
 xen/arch/x86/guest/hyperv/Makefile            |   1 +
 xen/arch/x86/guest/hyperv/hyperv.c            |  63 ++++++++++
 xen/arch/x86/guest/hypervisor.c               | 116 ++++++++++++++++++
 xen/arch/x86/guest/xen/Makefile               |   4 +
 xen/arch/x86/guest/{ => xen}/hypercall_page.S |   0
 xen/arch/x86/guest/{ => xen}/pvh-boot.c       |   0
 xen/arch/x86/guest/{ => xen}/xen.c            |  73 ++---------
 xen/arch/x86/pv/shim.c                        |   2 +-
 xen/arch/x86/setup.c                          |  11 ++
 xen/include/asm-x86/guest.h                   |   2 +
 xen/include/asm-x86/guest/hypercall.h         |   2 +
 xen/include/asm-x86/guest/hyperv.h            |  48 ++++++++
 xen/include/asm-x86/guest/hypervisor.h        |  58 +++++++++
 xen/include/asm-x86/guest/xen.h               |  23 +---
 17 files changed, 337 insertions(+), 87 deletions(-)
 create mode 100644 xen/arch/x86/guest/hyperv/Makefile
 create mode 100644 xen/arch/x86/guest/hyperv/hyperv.c
 create mode 100644 xen/arch/x86/guest/hypervisor.c
 create mode 100644 xen/arch/x86/guest/xen/Makefile
 rename xen/arch/x86/guest/{ => xen}/hypercall_page.S (100%)
 rename xen/arch/x86/guest/{ => xen}/pvh-boot.c (100%)
 rename xen/arch/x86/guest/{ => xen}/xen.c (80%)
 create mode 100644 xen/include/asm-x86/guest/hyperv.h
 create mode 100644 xen/include/asm-x86/guest/hypervisor.h
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
author
#27 lore raw
In reply to Wei Liu
Quoted 19 lines · Wei Liu

Hi all

This is version 2 of the patch series.

This is the very first stage for porting Xen to run on Hyper-V with all the goodies Hyper-V has to offer. With this series, Xen can successfully detect Hyper-V and prints out a message. I would like to first get the code structure and kconfig options agreed upon.

There are two major areas to be worked on:
* Make Dom0 able to use Hyper-V's synthetic devices.
* Make Xen use of the synthetic timer, reference TSC and enlightenment VMCS and other interfaces.

They aren't trivial, and time can be scarce on my side, so I intend to post patches piece meal when they are ready.

Questions and comments are welcome.

Quoted 7 lines

Thanks,
Wei.

---
Changes in v2:
1. Introduce and use a hypervisor framework
2. Keep memmap infra under Xen for now

Ping?

Can I get an high level agreement on the code structure such that I can continue building on top of this series?

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel