Skip to contentSkip to conversation

More Hyper-V infrastructure

PATCH Superseded Wei Liu 5 participants 27 messages Dec 29, 2019 – Jan 3, 2020

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

cover More Hyper-V infrastructure

This patch sereis implements several important functionalities to run Xen on top of Hyper-V.

See individual patches for more details.

I can only test them lightly and look at disassembly output to check correctness at this stage. At the very last Xen on Hyper-V boots up okay, so it is not breaking anything.

Wei.

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

Wei Liu (8):
x86: include xen/lib.h in guest/pvh-boot.h x86/hyperv: detect absolutely necessary MSRs
x86: rename guest/hypercall.h to guest/xen-hypercall.h x86/hyperv: setup hypercall page
x86/hyperv: provide Hyper-V hypercall functions x86/hyperv: provide percpu hypercall input page x86/hyperv: retrieve vp_index from Hyper-V x86/hyperv: setup VP assist page

 xen/arch/x86/guest/hyperv/hyperv.c            | 125 +++++++++++++++++-
 xen/include/asm-x86/guest.h                   |   2 +-
 xen/include/asm-x86/guest/hyperv-hypercall.h  | 105 +++++++++++++++
 xen/include/asm-x86/guest/hyperv.h            |  10 ++
 xen/include/asm-x86/guest/pvh-boot.h          |   2 +
 .../guest/{hypercall.h => xen-hypercall.h}    |   2 +-
 6 files changed, 241 insertions(+), 5 deletions(-)
 create mode 100644 xen/include/asm-x86/guest/hyperv-hypercall.h
 rename xen/include/asm-x86/guest/{hypercall.h => xen-hypercall.h} (99%)
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 1/8 x86: include xen/lib.h in guest/pvh-boot.h

It needs ASSERT_UNREACHABLE.

---
 xen/include/asm-x86/guest/pvh-boot.h | 2 ++
 1 file changed, 2 insertions(+)
xen/include/asm-x86/guest/pvh-boot.h +2 -0
index b8a76c4eed..48ffd1a0b1 100644
@@ -30,6 +30,8 @@ void pvh_print_info(void);
 
 #else
 
+#include <xen/lib.h>
+
 #define pvh_boot 0
 
 static inline void pvh_init(multiboot_info_t **mbi, module_t **mod)
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 2/8 x86/hyperv: detect absolutely necessary MSRs

In reply to Wei Liu

If they are not available, disable Hyper-V related features.

---
 xen/arch/x86/guest/hyperv/hyperv.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
xen/arch/x86/guest/hyperv/hyperv.c +11 -0
index 2e70b4aa82..c6a26c5453 100644
@@ -32,6 +32,8 @@ static const struct hypervisor_ops ops = {
 const struct hypervisor_ops *__init hyperv_probe(void)
 {
     uint32_t eax, ebx, ecx, edx;
+    uint64_t required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
+        HV_X64_MSR_VP_INDEX_AVAILABLE;
 
     cpuid(0x40000000, &eax, &ebx, &ecx, &edx);
     if ( !((ebx == 0x7263694d) &&  /* "Micr" */
@@ -57,6 +59,15 @@ const struct hypervisor_ops *__init hyperv_probe(void)
     ms_hyperv.max_vp_index = eax;
     ms_hyperv.max_lp_index = ebx;
 
+    if ( (ms_hyperv.features & required_msrs) != required_msrs )
+    {
+        /* Oops, required MSRs are not available. Treat this as
+         * "Hyper-V is not available".
+         */
+        memset(&ms_hyperv, 0, sizeof(ms_hyperv));
+        return NULL;
+    }
+
     return &ops;
 }
 
Signature · 7 lines
2.20.1


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

Re: [Xen-devel] [PATCH 2/8] x86/hyperv: detect absolutely necessary MSRs

Quoted 22 lines · Wei Liu

If they are not available, disable Hyper-V related features.

---
 xen/arch/x86/guest/hyperv/hyperv.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
xen/arch/x86/guest/hyperv/hyperv.c +2 -0
index 2e70b4aa82..c6a26c5453 100644
@@ -32,6 +32,8 @@ static const struct hypervisor_ops ops = {
 const struct hypervisor_ops *__init hyperv_probe(void)
 {
     uint32_t eax, ebx, ecx, edx;
+    uint64_t required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
+        HV_X64_MSR_VP_INDEX_AVAILABLE;
 
     cpuid(0x40000000, &eax, &ebx, &ecx, &edx);
     if ( !((ebx == 0x7263694d) &&  /* "Micr" */

xen/arch/x86/guest/hyperv/hyperv.c const struct hypervisor_ops *__init hyperv_probe(void) · line 66

Quoted 8 lines
xen/arch/x86/guest/hyperv/hyperv.c +5 -0
@@ -57,6 +59,15 @@ const struct hypervisor_ops *__init hyperv_probe(void)
     ms_hyperv.max_vp_index = eax;
     ms_hyperv.max_lp_index = ebx;
 
+    if ( (ms_hyperv.features & required_msrs) != required_msrs )
+    {
+        /* Oops, required MSRs are not available. Treat this as
+         * "Hyper-V is not available".
+         */

That block comment violates default coding style.

Paul

Quoted 10 lines
xen/arch/x86/guest/hyperv/hyperv.c +4 -1
+        memset(&ms_hyperv, 0, sizeof(ms_hyperv));
+        return NULL;
+    }
+
     return &ops;
 }
 
--

2.20.1

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

Acked-by
#5 lore raw

Re: [Xen-devel] [PATCH 2/8] x86/hyperv: detect absolutely necessary MSRs

Quoted 16 lines · Paul Durrant
Quoted 15 lines · Wei Liu
xen/arch/x86/guest/hyperv/hyperv.c +2 -0
@@ -32,6 +32,8 @@ static const struct hypervisor_ops ops = {
 const struct hypervisor_ops *__init hyperv_probe(void)
 {
     uint32_t eax, ebx, ecx, edx;
+    uint64_t required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
+        HV_X64_MSR_VP_INDEX_AVAILABLE;
 
     cpuid(0x40000000, &eax, &ebx, &ecx, &edx);
     if ( !((ebx == 0x7263694d) &&  /* "Micr" */
@@ -57,6 +59,15 @@ const struct hypervisor_ops *__init hyperv_probe(void)
     ms_hyperv.max_vp_index = eax;
     ms_hyperv.max_lp_index = ebx;
 
Quoted 7 lines

xen/arch/x86/guest/hyperv/hyperv.c const struct hypervisor_ops *__init hyperv_probe(void) · line 66

Quoted 5 lines
xen/arch/x86/guest/hyperv/hyperv.c +5 -0
+    if ( (ms_hyperv.features & required_msrs) != required_msrs )
+    {
+        /* Oops, required MSRs are not available. Treat this as
+         * "Hyper-V is not available".
+         */

That block comment violates default coding style.

And with it corrected

Jan

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

author
#6 lore raw

PATCH 3/8 x86: rename guest/hypercall.h to guest/xen-hypercall.h

In reply to Wei Liu

We will provide a header file for Hyper-V hypercalls.

No functional change.

---
 xen/include/asm-x86/guest.h                                | 2 +-
 xen/include/asm-x86/guest/{hypercall.h => xen-hypercall.h} | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
 rename xen/include/asm-x86/guest/{hypercall.h => xen-hypercall.h} (99%)
xen/include/asm-x86/guest.h +1 -1
index 94448606d4..7f09c643d4 100644
@@ -19,11 +19,11 @@
 #ifndef __X86_GUEST_H__
 #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>
+#include <asm/guest/xen-hypercall.h>
 #include <asm/pv/shim.h>
 
 #endif /* __X86_GUEST_H__ */
xen/include/asm-x86/guest/xen-hypercall.h renamed from xen/include/asm-x86/guest/hypercall.h +1 -1
similarity index 99%
rename from xen/include/asm-x86/guest/hypercall.h
rename to xen/include/asm-x86/guest/xen-hypercall.h
index d0d2f5022d..681d3aecd7 100644
@@ -1,5 +1,5 @@
 /******************************************************************************
- * asm-x86/guest/hypercall.h
+ * asm-x86/guest/xen-hypercall.h
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms and conditions of the GNU General Public
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 3/8
#7 lore raw

Re: [Xen-devel] [PATCH 3/8] x86: rename guest/hypercall.h to guest/xen-hypercall.h

Quoted 6 lines · Wei Liu

We will provide a header file for Hyper-V hypercalls.

No functional change.

Quoted 38 lines
---
 xen/include/asm-x86/guest.h                                | 2 +-
 xen/include/asm-x86/guest/{hypercall.h => xen-hypercall.h} | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
 rename xen/include/asm-x86/guest/{hypercall.h => xen-hypercall.h} (99%)
xen/include/asm-x86/guest.h +1 -1
index 94448606d4..7f09c643d4 100644
@@ -19,11 +19,11 @@
 #ifndef __X86_GUEST_H__
 #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>
+#include <asm/guest/xen-hypercall.h>
 #include <asm/pv/shim.h>
 
 #endif /* __X86_GUEST_H__ */
xen/include/asm-x86/guest/xen-hypercall.h renamed from xen/include/asm-x86/guest/hypercall.h +1 -2
similarity index 99%
rename from xen/include/asm-x86/guest/hypercall.h
rename to xen/include/asm-x86/guest/xen-hypercall.h
index d0d2f5022d..681d3aecd7 100644
@@ -1,5 +1,5 @@
 /******************************************************************************
- * asm-x86/guest/hypercall.h
+ * asm-x86/guest/xen-hypercall.h
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms and conditions of the GNU General Public
--
Quoted 2 lines

2.20.1

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

Acked-by on v1 3/8
#8 lore raw

Re: [Xen-devel] [PATCH 3/8] x86: rename guest/hypercall.h to guest/xen-hypercall.h

In reply to Wei Liu
Quoted 5 lines · Wei Liu

We will provide a header file for Hyper-V hypercalls.

No functional change.

In principle

albeit ...

Quoted 3 lines
---
 xen/include/asm-x86/guest.h                                | 2 +-
 xen/include/asm-x86/guest/{hypercall.h => xen-hypercall.h} | 2 +-

... could we settle on guest/*-hcall.h to save on file name length without (hopefully) becoming ambiguous?

Jan

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

author
#9 lore raw

Re: [Xen-devel] [PATCH 3/8] x86: rename guest/hypercall.h to guest/xen-hypercall.h

Quoted 9 lines · Jan Beulich
Quoted 5 lines · Wei Liu

We will provide a header file for Hyper-V hypercalls.

No functional change.

In principle

Quoted 8 lines

albeit ...

Quoted 3 lines
---
 xen/include/asm-x86/guest.h                                | 2 +-
 xen/include/asm-x86/guest/{hypercall.h => xen-hypercall.h} | 2 +-

... could we settle on guest/*-hcall.h to save on file name length without (hopefully) becoming ambiguous?

That works for me.

Wei.

Quoted 2 lines

Jan

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

author
#10 lore raw

PATCH 4/8 x86/hyperv: setup hypercall page

In reply to Wei Liu
---
 xen/arch/x86/guest/hyperv/hyperv.c | 41 +++++++++++++++++++++++++++---
 1 file changed, 38 insertions(+), 3 deletions(-)
xen/arch/x86/guest/hyperv/hyperv.c +38 -3
index c6a26c5453..438910c8cb 100644
@@ -19,16 +19,17 @@
  * Copyright (c) 2019 Microsoft.
  */
 #include <xen/init.h>
+#include <xen/domain_page.h>
 
 #include <asm/guest.h>
 #include <asm/guest/hyperv-tlfs.h>
 
 struct ms_hyperv_info __read_mostly ms_hyperv;
 
-static const struct hypervisor_ops ops = {
-    .name = "Hyper-V",
-};
+void *hv_hypercall;
+static struct page_info *hv_hypercall_page;
 
+static const struct hypervisor_ops ops;
 const struct hypervisor_ops *__init hyperv_probe(void)
 {
     uint32_t eax, ebx, ecx, edx;
@@ -71,6 +72,40 @@ const struct hypervisor_ops *__init hyperv_probe(void)
     return &ops;
 }
 
+static void __init setup_hypercall_page(void)
+{
+    union hv_x64_msr_hypercall_contents hypercall_msr;
+
+    /* Unfortunately there isn't a really good way to unwind Xen to
+     * not use Hyper-V hooks, so panic if anything goes wrong.
+     *
+     * In practice if page allocation fails this early on it is
+     * unlikely we can get a working system later.
+     */
+    hv_hypercall_page = alloc_domheap_page(NULL, 0);
+    if ( !hv_hypercall_page )
+        panic("Failed to allocate Hyper-V hypercall page\n");
+
+    hv_hypercall = __map_domain_page_global(hv_hypercall_page);
+    if ( !hv_hypercall )
+        panic("Failed to map Hyper-V hypercall page\n");
+
+    rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+    hypercall_msr.enable = 1;
+    hypercall_msr.guest_physical_address = page_to_maddr(hv_hypercall_page);
+    wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+}
+
+static void __init setup(void)
+{
+    setup_hypercall_page();
+}
+
+static const struct hypervisor_ops ops = {
+    .name = "Hyper-V",
+    .setup = setup,
+};
+
 /*
  * Local variables:
  * mode: C
Signature · 7 lines
2.20.1


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

Re: [Xen-devel] [PATCH 4/8] x86/hyperv: setup hypercall page

From: Wei Liu <[email protected]> On Behalf Of Wei Liu Sent: Sunday, December 29, 2019 10:34 AM

Quoted 46 lines
---
 xen/arch/x86/guest/hyperv/hyperv.c | 41 +++++++++++++++++++++++++++---
 1 file changed, 38 insertions(+), 3 deletions(-)
xen/arch/x86/guest/hyperv/hyperv.c +15 -3
index c6a26c5453..438910c8cb 100644
@@ -19,16 +19,17 @@
  * Copyright (c) 2019 Microsoft.
  */
 #include <xen/init.h>
+#include <xen/domain_page.h>
 
 #include <asm/guest.h>
 #include <asm/guest/hyperv-tlfs.h>
 
 struct ms_hyperv_info __read_mostly ms_hyperv;
 
-static const struct hypervisor_ops ops = {
-    .name = "Hyper-V",
-};
+void *hv_hypercall;
+static struct page_info *hv_hypercall_page;
 
+static const struct hypervisor_ops ops;
 const struct hypervisor_ops *__init hyperv_probe(void)
 {
     uint32_t eax, ebx, ecx, edx;
@@ -71,6 +72,40 @@ const struct hypervisor_ops *__init hyperv_probe(void)
     return &ops;
 }
 
+static void __init setup_hypercall_page(void)
+{
+    union hv_x64_msr_hypercall_contents hypercall_msr;
+
+    /* Unfortunately there isn't a really good way to unwind Xen to
+     * not use Hyper-V hooks, so panic if anything goes wrong.
+     *
+     * In practice if page allocation fails this early on it is
+     * unlikely we can get a working system later.
+     */
+    hv_hypercall_page = alloc_domheap_page(NULL, 0);

xen/arch/x86/guest/hyperv/hyperv.c const struct hypervisor_ops *__init hyperv_probe(void) · line 95

Quoted 10 lines
xen/arch/x86/guest/hyperv/hyperv.c +10 -0
+    if ( !hv_hypercall_page )
+        panic("Failed to allocate Hyper-V hypercall page\n");
+
+    hv_hypercall = __map_domain_page_global(hv_hypercall_page);
+    if ( !hv_hypercall )
+        panic("Failed to map Hyper-V hypercall page\n");
+
+    rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+    hypercall_msr.enable = 1;
+    hypercall_msr.guest_physical_address = page_to_maddr(hv_hypercall_page);

The "guest_physical_address" field is actually the guest physical page number. So the physical address needs to be right shifted 12 bits before being stored here. I'd recommend using HV_HYP_PAGE_SHIFT from hyperv-tlfs.h as the shift value; it was introduced to deal with the possibility that the page size used and expected by the Hyper-V interface is different from the page size used by the guest VM (which can happen on ARM64, though not on x86).

Michael

Quoted 17 lines
xen/arch/x86/guest/hyperv/hyperv.c +13 -1
+    wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+}
+
+static void __init setup(void)
+{
+    setup_hypercall_page();
+}
+
+static const struct hypervisor_ops ops = {
+    .name = "Hyper-V",
+    .setup = setup,
+};
+
 /*
  * Local variables:
  * mode: C
--
Quoted 1 line

2.20.1

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

author
#12 lore raw

Re: [Xen-devel] [PATCH 4/8] x86/hyperv: setup hypercall page

On Sun, Dec 29, 2019 at 07:54:30PM +0000, Michael Kelley wrote:
[...]

Quoted 21 lines
Quoted 21 lines
xen/arch/x86/guest/hyperv/hyperv.c +20 -0
+static void __init setup_hypercall_page(void)
+{
+    union hv_x64_msr_hypercall_contents hypercall_msr;
+
+    /* Unfortunately there isn't a really good way to unwind Xen to
+     * not use Hyper-V hooks, so panic if anything goes wrong.
+     *
+     * In practice if page allocation fails this early on it is
+     * unlikely we can get a working system later.
+     */
+    hv_hypercall_page = alloc_domheap_page(NULL, 0);
+    if ( !hv_hypercall_page )
+        panic("Failed to allocate Hyper-V hypercall page\n");
+
+    hv_hypercall = __map_domain_page_global(hv_hypercall_page);
+    if ( !hv_hypercall )
+        panic("Failed to map Hyper-V hypercall page\n");
+
+    rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+    hypercall_msr.enable = 1;
Quoted 8 lines
Quoted 1 line

+ hypercall_msr.guest_physical_address = page_to_maddr(hv_hypercall_page);

The "guest_physical_address" field is actually the guest physical page number. So the physical address needs to be right shifted 12 bits before being stored here. I'd recommend using HV_HYP_PAGE_SHIFT from hyperv-tlfs.h as the shift value; it was introduced to deal with the possibility that the page size used and expected by the Hyper-V interface is different from the page size used by the guest VM (which can happen on ARM64, though not on x86).

Good catch, and thanks for the tip here.

I will fix this in the next version.

Wei.

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

#13 lore raw

Re: [Xen-devel] [PATCH 4/8] x86/hyperv: setup hypercall page

In reply to Wei Liu
Quoted 11 lines · Wei Liu
xen/arch/x86/guest/hyperv/hyperv.c +7 -0
@@ -71,6 +72,40 @@ const struct hypervisor_ops *__init hyperv_probe(void)
     return &ops;
 }
 
+static void __init setup_hypercall_page(void)
+{
+    union hv_x64_msr_hypercall_contents hypercall_msr;
+
+    /* Unfortunately there isn't a really good way to unwind Xen to
+     * not use Hyper-V hooks, so panic if anything goes wrong.
+     *

xen/arch/x86/guest/hyperv/hyperv.c const struct hypervisor_ops *__init hyperv_probe(void) · line 91

Quoted 10 lines
xen/arch/x86/guest/hyperv/hyperv.c +10 -0
+     * In practice if page allocation fails this early on it is
+     * unlikely we can get a working system later.
+     */
+    hv_hypercall_page = alloc_domheap_page(NULL, 0);
+    if ( !hv_hypercall_page )
+        panic("Failed to allocate Hyper-V hypercall page\n");
+
+    hv_hypercall = __map_domain_page_global(hv_hypercall_page);
+    if ( !hv_hypercall )
+        panic("Failed to map Hyper-V hypercall page\n");

I really hope this doesn't actually function correctly.  This should result in an NX mapping.

See feedback on the next patch for an alternative suggestion.

~Andrew

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

author
#14 lore raw

Re: [Xen-devel] [PATCH 4/8] x86/hyperv: setup hypercall page

Quoted 17 lines · Andrew Cooper
Quoted 16 lines · Wei Liu
xen/arch/x86/guest/hyperv/hyperv.c +12 -0
@@ -71,6 +72,40 @@ const struct hypervisor_ops *__init hyperv_probe(void)
     return &ops;
 }
 
+static void __init setup_hypercall_page(void)
+{
+    union hv_x64_msr_hypercall_contents hypercall_msr;
+
+    /* Unfortunately there isn't a really good way to unwind Xen to
+     * not use Hyper-V hooks, so panic if anything goes wrong.
+     *
+     * In practice if page allocation fails this early on it is
+     * unlikely we can get a working system later.
+     */
+    hv_hypercall_page = alloc_domheap_page(NULL, 0);
+    if ( !hv_hypercall_page )
Quoted 9 lines

xen/arch/x86/guest/hyperv/hyperv.c const struct hypervisor_ops *__init hyperv_probe(void) · line 91

Quoted 5 lines
xen/arch/x86/guest/hyperv/hyperv.c +5 -0
+        panic("Failed to allocate Hyper-V hypercall page\n");
+
+    hv_hypercall = __map_domain_page_global(hv_hypercall_page);
+    if ( !hv_hypercall )
+        panic("Failed to map Hyper-V hypercall page\n");

I really hope this doesn't actually function correctly.  This should result in an NX mapping.

Ah, stupid me. I had actually looked at Xen's implementation and thought "wouldn't it be nice to save one page in the image". I clearly missed that __map_domain_page_global makes the page NX.

Wei.

Quoted 3 lines

See feedback on the next patch for an alternative suggestion.

~Andrew

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

#15 lore raw

Re: [Xen-devel] [PATCH 4/8] x86/hyperv: setup hypercall page

Quoted 26 lines · Wei Liu
Quoted 16 lines · Andrew Cooper
Quoted 15 lines · Wei Liu
xen/arch/x86/guest/hyperv/hyperv.c +11 -0
@@ -71,6 +72,40 @@ const struct hypervisor_ops *__init hyperv_probe(void)
     return &ops;
 }
 
+static void __init setup_hypercall_page(void)
+{
+    union hv_x64_msr_hypercall_contents hypercall_msr;
+
+    /* Unfortunately there isn't a really good way to unwind Xen to
+     * not use Hyper-V hooks, so panic if anything goes wrong.
+     *
+     * In practice if page allocation fails this early on it is
+     * unlikely we can get a working system later.
+     */
+    hv_hypercall_page = alloc_domheap_page(NULL, 0);
Quoted 9 lines

xen/arch/x86/guest/hyperv/hyperv.c const struct hypervisor_ops *__init hyperv_probe(void) · line 91

Quoted 6 lines
xen/arch/x86/guest/hyperv/hyperv.c +6 -0
+    if ( !hv_hypercall_page )
+        panic("Failed to allocate Hyper-V hypercall page\n");
+
+    hv_hypercall = __map_domain_page_global(hv_hypercall_page);
+    if ( !hv_hypercall )
+        panic("Failed to map Hyper-V hypercall page\n");

I really hope this doesn't actually function correctly.  This should result in an NX mapping.

Quoted 2 lines

Ah, stupid me. I had actually looked at Xen's implementation and thought "wouldn't it be nice to save one page in the image".

Its 4k, and there is a lot to be said for not having random tiny critical bits of infrastructure spread dynamically around GFN space.

Quoted 1 line

I clearly missed that __map_domain_page_global makes the page NX.

It is hidden in the depths of PAGE_HYPERVISOR.

~Andrew

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

author
#16 lore raw

PATCH 5/8 x86/hyperv: provide Hyper-V hypercall functions

In reply to Wei Liu

These functions will be used later to make hypercalls to Hyper-V.

I couldn't find reference in TLFS that Hyper-V clobbers flags and r9-r11, but Linux's commit message says it does. Err on the safe side.

---
 xen/include/asm-x86/guest/hyperv-hypercall.h | 105 +++++++++++++++++++
 1 file changed, 105 insertions(+)
 create mode 100644 xen/include/asm-x86/guest/hyperv-hypercall.h
xen/include/asm-x86/guest/hyperv-hypercall.h new file +105 -0
new file mode 100644
index 0000000000..6017123be5
@@ -0,0 +1,105 @@
+/******************************************************************************
+ * asm-x86/guest/hyperv-hypercall.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_HYPERV_HYPERCALL_H__
+#define __X86_HYPERV_HYPERCALL_H__
+
+#include <xen/types.h>
+
+#include <asm/asm_defns.h>
+#include <asm/guest/hyperv-tlfs.h>
+#include <asm/page.h>
+
+extern void *hv_hypercall;
+
+static inline uint64_t hv_do_hypercall(uint64_t control, paddr_t input, paddr_t output)
+{
+    uint64_t status;
+
+    if ( !hv_hypercall )
+        return ~0ULL;
+
+    asm volatile ("mov %[output], %%r8\n"
+                  "call *%[hypercall_page]"
+                  : "=a" (status), "+c" (control),
+                    "+d" (input) ASM_CALL_CONSTRAINT
+                  : [output] "rm" (output),
+                    [hypercall_page] "m" (hv_hypercall)
+                  : "cc", "memory", "r8", "r9", "r10", "r11");
+
+    return status;
+}
+
+static inline uint64_t hv_do_fast_hypercall(uint16_t code,
+                                            uint64_t input1, uint64_t input2)
+{
+    uint64_t status;
+    uint64_t control = (uint64_t)code | HV_HYPERCALL_FAST_BIT;
+
+    if ( !hv_hypercall )
+        return ~0ULL;
+
+    asm volatile ("mov %[input2], %%r8\n"
+                  "call *%[hypercall_page]"
+                  : "=a" (status), "+c" (control),
+                    "+d" (input1) ASM_CALL_CONSTRAINT
+                  : [input2] "rm" (input2),
+                    [hypercall_page] "m" (hv_hypercall)
+                  : "cc", "r8", "r9", "r10", "r11");
+
+    return status;
+}
+
+static inline uint64_t hv_do_rep_hypercall(uint16_t code, uint16_t rep_count,
+                                           uint16_t varhead_size,
+                                           paddr_t input, paddr_t output)
+{
+    uint64_t control = code;
+    uint64_t status;
+    uint16_t rep_comp;
+
+    control |= (uint64_t)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET;
+    control |= (uint64_t)rep_count << HV_HYPERCALL_REP_COMP_OFFSET;
+
+    do {
+        status = hv_do_hypercall(control, input, output);
+        if ( (status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS )
+            break;
+
+        rep_comp = (status & HV_HYPERCALL_REP_COMP_MASK) >>
+            HV_HYPERCALL_REP_COMP_OFFSET;
+
+        control &= ~HV_HYPERCALL_REP_START_MASK;
+        control |= (uint64_t)rep_comp << HV_HYPERCALL_REP_COMP_OFFSET;
+
+    } while ( rep_comp < rep_count );
+
+    return status;
+}
+
+#endif /* __X86_HYPERV_HYPERCALL_H__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
Signature · 7 lines
2.20.1


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

Re: [Xen-devel] [PATCH 5/8] x86/hyperv: provide Hyper-V hypercall functions

Quoted 53 lines · Wei Liu

These functions will be used later to make hypercalls to Hyper-V.

I couldn't find reference in TLFS that Hyper-V clobbers flags and r9-r11, but Linux's commit message says it does. Err on the safe side.

---
 xen/include/asm-x86/guest/hyperv-hypercall.h | 105 +++++++++++++++++++
 1 file changed, 105 insertions(+)
 create mode 100644 xen/include/asm-x86/guest/hyperv-hypercall.h
xen/include/asm-x86/guest/hyperv-hypercall.h new file +36 -0
new file mode 100644
index 0000000000..6017123be5
@@ -0,0 +1,105 @@
+/******************************************************************************
+ * asm-x86/guest/hyperv-hypercall.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_HYPERV_HYPERCALL_H__
+#define __X86_HYPERV_HYPERCALL_H__
+
+#include <xen/types.h>
+
+#include <asm/asm_defns.h>
+#include <asm/guest/hyperv-tlfs.h>
+#include <asm/page.h>
+
+extern void *hv_hypercall;
+
+static inline uint64_t hv_do_hypercall(uint64_t control, paddr_t input, paddr_t output)
+{
+    uint64_t status;
+
+    if ( !hv_hypercall )
+        return ~0ULL;
+

xen/include/asm-x86/guest/hyperv-hypercall.h line 46

Quoted 10 lines
xen/include/asm-x86/guest/hyperv-hypercall.h new file +10 -0
+    asm volatile ("mov %[output], %%r8\n"
+                  "call *%[hypercall_page]"
+                  : "=a" (status), "+c" (control),
+                    "+d" (input) ASM_CALL_CONSTRAINT
+                  : [output] "rm" (output),
+                    [hypercall_page] "m" (hv_hypercall)
+                  : "cc", "memory", "r8", "r9", "r10", "r11");
+
+    return status;
+}

Indirect calls are expensive these days due to retpoline/IBRS, and in this case, unnecessary.

You want something like:

asm ( ".pushsection \".text.page_aligned\", \"ax\", @progbits\n\t" ".align 4096\n\t"
      ".globl hyperv_hypercall\n\t" "hyperv_hypercall:\n\t"
      "mov -1, %rax\n\t"
      "ret\n\t"
      ".align 4096;\n\t" );

Which will put one page worth of space in .text.page_aligned (so it gets mapped executable), at a location the linker can evaluate (so you can use a direct call, and the disassembly will be easier to follow), which is initialised to the "not ready yet" code so you don't need a runtime check in every hypercall that you didn't get the order of initialisation wrong at boot.

Alternatively, initialise to ud2 if some form of console can be reliably be arranged to work from the very start.

~Andrew

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

author
#18 lore raw

Re: [Xen-devel] [PATCH 5/8] x86/hyperv: provide Hyper-V hypercall functions

Quoted 78 lines · Andrew Cooper
Quoted 53 lines · Wei Liu

These functions will be used later to make hypercalls to Hyper-V.

I couldn't find reference in TLFS that Hyper-V clobbers flags and r9-r11, but Linux's commit message says it does. Err on the safe side.

---
 xen/include/asm-x86/guest/hyperv-hypercall.h | 105 +++++++++++++++++++
 1 file changed, 105 insertions(+)
 create mode 100644 xen/include/asm-x86/guest/hyperv-hypercall.h
xen/include/asm-x86/guest/hyperv-hypercall.h new file +36 -0
new file mode 100644
index 0000000000..6017123be5
@@ -0,0 +1,105 @@
+/******************************************************************************
+ * asm-x86/guest/hyperv-hypercall.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_HYPERV_HYPERCALL_H__
+#define __X86_HYPERV_HYPERCALL_H__
+
+#include <xen/types.h>
+
+#include <asm/asm_defns.h>
+#include <asm/guest/hyperv-tlfs.h>
+#include <asm/page.h>
+
+extern void *hv_hypercall;
+
+static inline uint64_t hv_do_hypercall(uint64_t control, paddr_t input, paddr_t output)
+{
+    uint64_t status;
+
+    if ( !hv_hypercall )
+        return ~0ULL;
+

xen/include/asm-x86/guest/hyperv-hypercall.h line 46

Quoted 10 lines
xen/include/asm-x86/guest/hyperv-hypercall.h new file +10 -0
+    asm volatile ("mov %[output], %%r8\n"
+                  "call *%[hypercall_page]"
+                  : "=a" (status), "+c" (control),
+                    "+d" (input) ASM_CALL_CONSTRAINT
+                  : [output] "rm" (output),
+                    [hypercall_page] "m" (hv_hypercall)
+                  : "cc", "memory", "r8", "r9", "r10", "r11");
+
+    return status;
+}

Indirect calls are expensive these days due to retpoline/IBRS, and in this case, unnecessary.

You want something like:

asm ( ".pushsection \".text.page_aligned\", \"ax\", @progbits\n\t" ".align 4096\n\t"
      ".globl hyperv_hypercall\n\t" "hyperv_hypercall:\n\t"
      "mov -1, %rax\n\t"
      "ret\n\t"
      ".align 4096;\n\t" );

Quoted 6 lines

Which will put one page worth of space in .text.page_aligned (so it gets mapped executable), at a location the linker can evaluate (so you can use a direct call, and the disassembly will be easier to follow), which is initialised to the "not ready yet" code so you don't need a runtime check in every hypercall that you didn't get the order of initialisation wrong at boot.

This works for me. Thanks.

Wei.

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

author
#19 lore raw

PATCH 6/8 x86/hyperv: provide percpu hypercall input page

In reply to Wei Liu

Hyper-V's input / output argument must be 8 bytes aligned an not cross page boundary. The easiest way to satisfy those requirements is to use percpu page.

For the foreseeable future we only need to provide input for TLB and APIC hypercalls, so skip setting up an output page.

The page tracking structure is not bound to hypercall because it is a common pattern for Xen to write guest physical address to Hyper-V while at the same time accessing the page via a pointer.

We will also need to provide an ap_setup hook for secondary cpus to setup its own input page.

---
 xen/arch/x86/guest/hyperv/hyperv.c | 26 ++++++++++++++++++++++++++
 xen/include/asm-x86/guest/hyperv.h |  8 ++++++++
 2 files changed, 34 insertions(+)
xen/arch/x86/guest/hyperv/hyperv.c +26 -0
index 438910c8cb..67667936e9 100644
@@ -28,6 +28,7 @@ struct ms_hyperv_info __read_mostly ms_hyperv;
 
 void *hv_hypercall;
 static struct page_info *hv_hypercall_page;
+DEFINE_PER_CPU_READ_MOSTLY(struct hyperv_pcpu_page, hv_pcpu_input_arg);
 
 static const struct hypervisor_ops ops;
 const struct hypervisor_ops *__init hyperv_probe(void)
@@ -96,14 +97,39 @@ static void __init setup_hypercall_page(void)
     wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
 }
 
+static void setup_hypercall_pcpu_arg(void)
+{
+    struct page_info *pg;
+    void *mapping;
+    unsigned int cpu = smp_processor_id();
+
+    pg = alloc_domheap_page(NULL, 0);
+    if ( !pg )
+        panic("Failed to setup hypercall input page for %u\n", cpu);
+
+    mapping = __map_domain_page_global(pg);
+    if ( !mapping )
+        panic("Failed to map hypercall input page for %u\n", cpu);
+
+    this_cpu(hv_pcpu_input_arg).maddr = page_to_maddr(pg);
+    this_cpu(hv_pcpu_input_arg).mapping = mapping;
+}
+
 static void __init setup(void)
 {
     setup_hypercall_page();
+    setup_hypercall_pcpu_arg();
+}
+
+static void ap_setup(void)
+{
+    setup_hypercall_pcpu_arg();
 }
 
 static const struct hypervisor_ops ops = {
     .name = "Hyper-V",
     .setup = setup,
+    .ap_setup = ap_setup,
 };
 
 /*
xen/include/asm-x86/guest/hyperv.h +8 -0
index c7a7f32bd5..83f297468f 100644
@@ -51,6 +51,8 @@ static inline uint64_t hv_scale_tsc(uint64_t tsc, uint64_t scale,
 
 #ifdef CONFIG_HYPERV_GUEST
 
+#include <xen/percpu.h>
+
 #include <asm/guest/hypervisor.h>
 
 struct ms_hyperv_info {
@@ -63,6 +65,12 @@ struct ms_hyperv_info {
 };
 extern struct ms_hyperv_info ms_hyperv;
 
+struct hyperv_pcpu_page {
+    paddr_t maddr;
+    void *mapping;
+};
+DECLARE_PER_CPU(struct hyperv_pcpu_page, hv_pcpu_input_arg);
+
 const struct hypervisor_ops *hyperv_probe(void);
 
 #else
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 6/8
#20 lore raw

Re: [Xen-devel] [PATCH 6/8] x86/hyperv: provide percpu hypercall input page

Quoted 8 lines · Wei Liu

Hyper-V's input / output argument must be 8 bytes aligned an not cross page boundary. The easiest way to satisfy those requirements is to use percpu page.

For the foreseeable future we only need to provide input for TLB and APIC hypercalls, so skip setting up an output page.

Quoted 8 lines

The page tracking structure is not bound to hypercall because it is a common pattern for Xen to write guest physical address to Hyper-V while at the same time accessing the page via a pointer.

We will also need to provide an ap_setup hook for secondary cpus to setup its own input page.

Quoted 84 lines
---
 xen/arch/x86/guest/hyperv/hyperv.c | 26 ++++++++++++++++++++++++++
 xen/include/asm-x86/guest/hyperv.h |  8 ++++++++
 2 files changed, 34 insertions(+)
xen/arch/x86/guest/hyperv/hyperv.c +26 -0
index 438910c8cb..67667936e9 100644
@@ -28,6 +28,7 @@ struct ms_hyperv_info __read_mostly ms_hyperv;
 
 void *hv_hypercall;
 static struct page_info *hv_hypercall_page;
+DEFINE_PER_CPU_READ_MOSTLY(struct hyperv_pcpu_page, hv_pcpu_input_arg);
 
 static const struct hypervisor_ops ops;
 const struct hypervisor_ops *__init hyperv_probe(void)
@@ -96,14 +97,39 @@ static void __init setup_hypercall_page(void)
     wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
 }
 
+static void setup_hypercall_pcpu_arg(void)
+{
+    struct page_info *pg;
+    void *mapping;
+    unsigned int cpu = smp_processor_id();
+
+    pg = alloc_domheap_page(NULL, 0);
+    if ( !pg )
+        panic("Failed to setup hypercall input page for %u\n", cpu);
+
+    mapping = __map_domain_page_global(pg);
+    if ( !mapping )
+        panic("Failed to map hypercall input page for %u\n", cpu);
+
+    this_cpu(hv_pcpu_input_arg).maddr = page_to_maddr(pg);
+    this_cpu(hv_pcpu_input_arg).mapping = mapping;
+}
+
 static void __init setup(void)
 {
     setup_hypercall_page();
+    setup_hypercall_pcpu_arg();
+}
+
+static void ap_setup(void)
+{
+    setup_hypercall_pcpu_arg();
 }
 
 static const struct hypervisor_ops ops = {
     .name = "Hyper-V",
     .setup = setup,
+    .ap_setup = ap_setup,
 };
 
 /*
xen/include/asm-x86/guest/hyperv.h +8 -1
index c7a7f32bd5..83f297468f 100644
@@ -51,6 +51,8 @@ static inline uint64_t hv_scale_tsc(uint64_t tsc, uint64_t scale,
 
 #ifdef CONFIG_HYPERV_GUEST
 
+#include <xen/percpu.h>
+
 #include <asm/guest/hypervisor.h>
 
 struct ms_hyperv_info {
@@ -63,6 +65,12 @@ struct ms_hyperv_info {
 };
 extern struct ms_hyperv_info ms_hyperv;
 
+struct hyperv_pcpu_page {
+    paddr_t maddr;
+    void *mapping;
+};
+DECLARE_PER_CPU(struct hyperv_pcpu_page, hv_pcpu_input_arg);
+
 const struct hypervisor_ops *hyperv_probe(void);
 
 #else
--
Quoted 2 lines

2.20.1

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

author
#21 lore raw

PATCH 7/8 x86/hyperv: retrieve vp_index from Hyper-V

In reply to Wei Liu

This will be useful when invoking hypercall that targets specific vcpu(s).

---
 xen/arch/x86/guest/hyperv/hyperv.c | 12 ++++++++++++
 xen/include/asm-x86/guest/hyperv.h |  1 +
 2 files changed, 13 insertions(+)
xen/arch/x86/guest/hyperv/hyperv.c +12 -0
index 67667936e9..da3a8cd85d 100644
@@ -29,6 +29,7 @@ struct ms_hyperv_info __read_mostly ms_hyperv;
 void *hv_hypercall;
 static struct page_info *hv_hypercall_page;
 DEFINE_PER_CPU_READ_MOSTLY(struct hyperv_pcpu_page, hv_pcpu_input_arg);
+DEFINE_PER_CPU_READ_MOSTLY(unsigned int, hv_vp_index);
 
 static const struct hypervisor_ops ops;
 const struct hypervisor_ops *__init hyperv_probe(void)
@@ -115,15 +116,26 @@ static void setup_hypercall_pcpu_arg(void)
     this_cpu(hv_pcpu_input_arg).mapping = mapping;
 }
 
+static void setup_vp_index(void)
+{
+    uint64_t vp_index_msr;
+
+    rdmsrl(HV_X64_MSR_VP_INDEX, vp_index_msr);
+
+    this_cpu(hv_vp_index) = vp_index_msr;
+}
+
 static void __init setup(void)
 {
     setup_hypercall_page();
     setup_hypercall_pcpu_arg();
+    setup_vp_index();
 }
 
 static void ap_setup(void)
 {
     setup_hypercall_pcpu_arg();
+    setup_vp_index();
 }
 
 static const struct hypervisor_ops ops = {
xen/include/asm-x86/guest/hyperv.h +1 -0
index 83f297468f..4b635829f3 100644
@@ -70,6 +70,7 @@ struct hyperv_pcpu_page {
     void *mapping;
 };
 DECLARE_PER_CPU(struct hyperv_pcpu_page, hv_pcpu_input_arg);
+DECLARE_PER_CPU(unsigned int, hv_vp_index);
 
 const struct hypervisor_ops *hyperv_probe(void);
 
Signature · 7 lines
2.20.1


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

Re: [Xen-devel] [PATCH 7/8] x86/hyperv: retrieve vp_index from Hyper-V

Quoted 24 lines · Wei Liu

This will be useful when invoking hypercall that targets specific vcpu(s).

---
 xen/arch/x86/guest/hyperv/hyperv.c | 12 ++++++++++++
 xen/include/asm-x86/guest/hyperv.h |  1 +
 2 files changed, 13 insertions(+)
xen/arch/x86/guest/hyperv/hyperv.c +1 -0
index 67667936e9..da3a8cd85d 100644
@@ -29,6 +29,7 @@ struct ms_hyperv_info __read_mostly ms_hyperv;
 void *hv_hypercall;
 static struct page_info *hv_hypercall_page;
 DEFINE_PER_CPU_READ_MOSTLY(struct hyperv_pcpu_page, hv_pcpu_input_arg);
+DEFINE_PER_CPU_READ_MOSTLY(unsigned int, hv_vp_index);
 
 static const struct hypervisor_ops ops;
 const struct hypervisor_ops *__init hyperv_probe(void)
@@ -115,15 +116,26 @@ static void setup_hypercall_pcpu_arg(void)
     this_cpu(hv_pcpu_input_arg).mapping = mapping;

xen/arch/x86/guest/hyperv/hyperv.c static void setup_hypercall_pcpu_arg(void) · line 126

Quoted 10 lines
xen/arch/x86/guest/hyperv/hyperv.c +8 -0
 }
 
+static void setup_vp_index(void)
+{
+    uint64_t vp_index_msr;
+
+    rdmsrl(HV_X64_MSR_VP_INDEX, vp_index_msr);
+
+    this_cpu(hv_vp_index) = vp_index_msr;
+}

Is it worth a separate function vs. bundling this into setup_hypercall_pcpu_arg()?

Paul

Quoted 28 lines
xen/arch/x86/guest/hyperv/hyperv.c +3 -0
+
 static void __init setup(void)
 {
     setup_hypercall_page();
     setup_hypercall_pcpu_arg();
+    setup_vp_index();
 }
 
 static void ap_setup(void)
 {
     setup_hypercall_pcpu_arg();
+    setup_vp_index();
 }
 
 static const struct hypervisor_ops ops = {
xen/include/asm-x86/guest/hyperv.h +1 -1
index 83f297468f..4b635829f3 100644
@@ -70,6 +70,7 @@ struct hyperv_pcpu_page {
     void *mapping;
 };
 DECLARE_PER_CPU(struct hyperv_pcpu_page, hv_pcpu_input_arg);
+DECLARE_PER_CPU(unsigned int, hv_vp_index);
 
 const struct hypervisor_ops *hyperv_probe(void);
 
--
Quoted 2 lines

2.20.1

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

author
#23 lore raw

Re: [Xen-devel] [PATCH 7/8] x86/hyperv: retrieve vp_index from Hyper-V

Quoted 27 lines · Paul Durrant
Quoted 26 lines · Wei Liu

This will be useful when invoking hypercall that targets specific vcpu(s).

---
 xen/arch/x86/guest/hyperv/hyperv.c | 12 ++++++++++++
 xen/include/asm-x86/guest/hyperv.h |  1 +
 2 files changed, 13 insertions(+)
xen/arch/x86/guest/hyperv/hyperv.c +1 -0
index 67667936e9..da3a8cd85d 100644
@@ -29,6 +29,7 @@ struct ms_hyperv_info __read_mostly ms_hyperv;
 void *hv_hypercall;
 static struct page_info *hv_hypercall_page;
 DEFINE_PER_CPU_READ_MOSTLY(struct hyperv_pcpu_page, hv_pcpu_input_arg);
+DEFINE_PER_CPU_READ_MOSTLY(unsigned int, hv_vp_index);
 
 static const struct hypervisor_ops ops;
 const struct hypervisor_ops *__init hyperv_probe(void)
@@ -115,15 +116,26 @@ static void setup_hypercall_pcpu_arg(void)
     this_cpu(hv_pcpu_input_arg).mapping = mapping;
 }
 
Quoted 11 lines

xen/arch/x86/guest/hyperv/hyperv.c static void setup_hypercall_pcpu_arg(void) · line 126

Quoted 8 lines
xen/arch/x86/guest/hyperv/hyperv.c +8 -0
+static void setup_vp_index(void)
+{
+    uint64_t vp_index_msr;
+
+    rdmsrl(HV_X64_MSR_VP_INDEX, vp_index_msr);
+
+    this_cpu(hv_vp_index) = vp_index_msr;
+}

Is it worth a separate function vs. bundling this into setup_hypercall_pcpu_arg()?

My thinking is this may be called in the resume path; while setup_hypercall_pcpu_arg doesn't (because they just set aside a bunch of guest pages).

That is just a precaution. I haven't found any text in TLFS that vp index will / can change after resume. This function can certainly be folded into the other if that's preferred.

Wei.

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

author
#24 lore raw

PATCH 8/8 x86/hyperv: setup VP assist page

In reply to Wei Liu

VP assist page is rather important as we need to toggle some bits in that page such that L1 guest can make hypercalls directly to L0 Hyper-V.

Preemptively split out set_vp_assist page which will be used in the resume path.

---
 xen/arch/x86/guest/hyperv/hyperv.c | 35 ++++++++++++++++++++++++++++++
 xen/include/asm-x86/guest/hyperv.h |  1 +
 2 files changed, 36 insertions(+)
xen/arch/x86/guest/hyperv/hyperv.c +35 -0
index da3a8cd85d..a88b9ae6d9 100644
@@ -30,6 +30,7 @@ void *hv_hypercall;
 static struct page_info *hv_hypercall_page;
 DEFINE_PER_CPU_READ_MOSTLY(struct hyperv_pcpu_page, hv_pcpu_input_arg);
 DEFINE_PER_CPU_READ_MOSTLY(unsigned int, hv_vp_index);
+DEFINE_PER_CPU_READ_MOSTLY(struct hyperv_pcpu_page, hv_vp_assist);
 
 static const struct hypervisor_ops ops;
 const struct hypervisor_ops *__init hyperv_probe(void)
@@ -125,17 +126,51 @@ static void setup_vp_index(void)
     this_cpu(hv_vp_index) = vp_index_msr;
 }
 
+static void set_vp_assist(void)
+{
+    uint64_t val = paddr_to_pfn(this_cpu(hv_vp_assist).maddr);
+
+    val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) |
+        HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
+
+    wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
+}
+
+static void setup_vp_assist(void)
+{
+    struct page_info *pg;
+    void *mapping;
+    unsigned int cpu = smp_processor_id();
+
+    pg = alloc_domheap_page(NULL, 0);
+    if ( !pg )
+        panic("Failed to allocate vp_assist page for %u\n", cpu);
+
+    mapping = __map_domain_page_global(pg);
+    if ( !mapping )
+        panic("Failed to map vp_assist page for %u\n", cpu);
+
+    clear_page(mapping);
+
+    this_cpu(hv_vp_assist).maddr = page_to_maddr(pg);
+    this_cpu(hv_vp_assist).mapping = mapping;
+
+    set_vp_assist();
+}
+
 static void __init setup(void)
 {
     setup_hypercall_page();
     setup_hypercall_pcpu_arg();
     setup_vp_index();
+    setup_vp_assist();
 }
 
 static void ap_setup(void)
 {
     setup_hypercall_pcpu_arg();
     setup_vp_index();
+    setup_vp_assist();
 }
 
 static const struct hypervisor_ops ops = {
xen/include/asm-x86/guest/hyperv.h +1 -0
index 4b635829f3..917f4e02c2 100644
@@ -71,6 +71,7 @@ struct hyperv_pcpu_page {
 };
 DECLARE_PER_CPU(struct hyperv_pcpu_page, hv_pcpu_input_arg);
 DECLARE_PER_CPU(unsigned int, hv_vp_index);
+DECLARE_PER_CPU(struct hyperv_pcpu_page, hv_vp_assist);
 
 const struct hypervisor_ops *hyperv_probe(void);
 
Signature · 7 lines
2.20.1


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

Re: [Xen-devel] [PATCH 8/8] x86/hyperv: setup VP assist page

From: Wei Liu <[email protected]> On Behalf Of Wei Liu Sent: Sunday, December 29, 2019 10:34 AM

Quoted 26 lines

VP assist page is rather important as we need to toggle some bits in that page such that L1 guest can make hypercalls directly to L0 Hyper-V.

Preemptively split out set_vp_assist page which will be used in the resume path.

---
 xen/arch/x86/guest/hyperv/hyperv.c | 35 ++++++++++++++++++++++++++++++
 xen/include/asm-x86/guest/hyperv.h |  1 +
 2 files changed, 36 insertions(+)
xen/arch/x86/guest/hyperv/hyperv.c +1 -0
index da3a8cd85d..a88b9ae6d9 100644
@@ -30,6 +30,7 @@ void *hv_hypercall;
 static struct page_info *hv_hypercall_page;
 DEFINE_PER_CPU_READ_MOSTLY(struct hyperv_pcpu_page, hv_pcpu_input_arg);
 DEFINE_PER_CPU_READ_MOSTLY(unsigned int, hv_vp_index);
+DEFINE_PER_CPU_READ_MOSTLY(struct hyperv_pcpu_page, hv_vp_assist);
 
 static const struct hypervisor_ops ops;
 const struct hypervisor_ops *__init hyperv_probe(void)

xen/arch/x86/guest/hyperv/hyperv.c static void setup_vp_index(void) · line 133

Quoted 8 lines
xen/arch/x86/guest/hyperv/hyperv.c +5 -0
@@ -125,17 +126,51 @@ static void setup_vp_index(void)
     this_cpu(hv_vp_index) = vp_index_msr;
 }
 
+static void set_vp_assist(void)
+{
+    uint64_t val = paddr_to_pfn(this_cpu(hv_vp_assist).maddr);
+
+    val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) | 

I'd recommend using HV_HYP_PAGE_SHIFT instead of HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT. On the Linux side, I'm planning to remove that #define and the similar *_PAGE_ADDRESS_MASK in favor of the newer HV_HYP_PAGE_* values. There's nothing special about the VP assist page, so using the generic #defines based on the Hyper-V page size is reasonable.

Michael

Quoted 56 lines
xen/arch/x86/guest/hyperv/hyperv.c +29 -0
+        HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
+
+    wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
+}
+
+static void setup_vp_assist(void)
+{
+    struct page_info *pg;
+    void *mapping;
+    unsigned int cpu = smp_processor_id();
+
+    pg = alloc_domheap_page(NULL, 0);
+    if ( !pg )
+        panic("Failed to allocate vp_assist page for %u\n", cpu);
+
+    mapping = __map_domain_page_global(pg);
+    if ( !mapping )
+        panic("Failed to map vp_assist page for %u\n", cpu);
+
+    clear_page(mapping);
+
+    this_cpu(hv_vp_assist).maddr = page_to_maddr(pg);
+    this_cpu(hv_vp_assist).mapping = mapping;
+
+    set_vp_assist();
+}
+
 static void __init setup(void)
 {
     setup_hypercall_page();
     setup_hypercall_pcpu_arg();
     setup_vp_index();
+    setup_vp_assist();
 }
 
 static void ap_setup(void)
 {
     setup_hypercall_pcpu_arg();
     setup_vp_index();
+    setup_vp_assist();
 }
 
 static const struct hypervisor_ops ops = {
xen/include/asm-x86/guest/hyperv.h +1 -1
index 4b635829f3..917f4e02c2 100644
@@ -71,6 +71,7 @@ struct hyperv_pcpu_page {
 };
 DECLARE_PER_CPU(struct hyperv_pcpu_page, hv_pcpu_input_arg);
 DECLARE_PER_CPU(unsigned int, hv_vp_index);
+DECLARE_PER_CPU(struct hyperv_pcpu_page, hv_vp_assist);
 
 const struct hypervisor_ops *hyperv_probe(void);
 
--
Quoted 1 line

2.20.1

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

author
#26 lore raw

Re: [Xen-devel] [PATCH 8/8] x86/hyperv: setup VP assist page

Quoted 30 lines · Michael Kelley

From: Wei Liu <[email protected]> On Behalf Of Wei Liu Sent: Sunday, December 29, 2019 10:34 AM

Quoted 29 lines

VP assist page is rather important as we need to toggle some bits in that page such that L1 guest can make hypercalls directly to L0 Hyper-V.

Preemptively split out set_vp_assist page which will be used in the resume path.

---
 xen/arch/x86/guest/hyperv/hyperv.c | 35 ++++++++++++++++++++++++++++++
 xen/include/asm-x86/guest/hyperv.h |  1 +
 2 files changed, 36 insertions(+)
xen/arch/x86/guest/hyperv/hyperv.c +1 -0
index da3a8cd85d..a88b9ae6d9 100644
@@ -30,6 +30,7 @@ void *hv_hypercall;
 static struct page_info *hv_hypercall_page;
 DEFINE_PER_CPU_READ_MOSTLY(struct hyperv_pcpu_page, hv_pcpu_input_arg);
 DEFINE_PER_CPU_READ_MOSTLY(unsigned int, hv_vp_index);
+DEFINE_PER_CPU_READ_MOSTLY(struct hyperv_pcpu_page, hv_vp_assist);
 
 static const struct hypervisor_ops ops;
 const struct hypervisor_ops *__init hyperv_probe(void)
@@ -125,17 +126,51 @@ static void setup_vp_index(void)
     this_cpu(hv_vp_index) = vp_index_msr;
 }
 
Quoted 12 lines

xen/arch/x86/guest/hyperv/hyperv.c static void setup_vp_index(void) · line 133

Quoted 5 lines
xen/arch/x86/guest/hyperv/hyperv.c +5 -0
+static void set_vp_assist(void)
+{
+    uint64_t val = paddr_to_pfn(this_cpu(hv_vp_assist).maddr);
+
+    val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) | 

I'd recommend using HV_HYP_PAGE_SHIFT instead of HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT. On the Linux side, I'm planning to remove that #define and the similar *_PAGE_ADDRESS_MASK in favor of the newer HV_HYP_PAGE_* values. There's nothing special about the VP assist page, so using the generic #defines based on the Hyper-V page size is reasonable.

Ack.

Wei.

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