Skip to contentSkip to conversation

x86/hyperv: use dynamically allocated page for hypercalls

PATCH In review Ariadne Conill 2 participants 2 messages Jul 16 – Jul 17

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 x86/hyperv: use dynamically allocated page for hypercalls

Previously, the Hyper-V guest code placed the hypercall page at a fixed, hardcoded location: the top-most MFN of the guest physical address space (HV_HCALL_MFN). To make this work, an e820_fixup hook reserved that range as E820_RESERVED so nothing else would claim it.

This assumes the top-most physical frame is actually backed by RAM (or at least mapped to something), which is not guaranteed. On Azure compute guests it is not: the top-most MFN is not physically mapped to anything. When we program HV_X64_MSR_HYPERCALL to point at that frame and subsequently execute through the hypercall page via the fixmap, the access faults and Xen panics during early boot.

Instead of guessing at a physical address, allocate a real xenheap page and use its MFN for the hypercall page. This frame is always backed and mapped, so the fault no longer occurs. The e820_fixup hook and the HV_HCALL_MFN definition are no longer needed and are removed.

---
 xen/arch/x86/guest/hyperv/hyperv.c      | 19 +++++++++----------
 xen/arch/x86/include/asm/guest/hyperv.h |  3 ---
 2 files changed, 9 insertions(+), 13 deletions(-)
xen/arch/x86/guest/hyperv/hyperv.c +9 -10
index 90757e0793..d98d02fb80 100644
@@ -99,10 +99,18 @@ static void __init setup_hypercall_page(void)
     rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
     if ( !hypercall_msr.enable )
     {
-        mfn = HV_HCALL_MFN;
+        void *hcall_page = alloc_xenheap_page();
+
+        if ( !hcall_page )
+            panic("Hyper-V: Failed to allocate hypercall trampoline page\n");
+
+        mfn = virt_to_mfn(hcall_page);
         hypercall_msr.enable = 1;
         hypercall_msr.guest_physical_address = mfn;
         wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+
+        dprintk(XENLOG_INFO,
+                "Hyper-V: Allocated hypercall page at MFN %lx\n", mfn);
     }
     else
         mfn = hypercall_msr.guest_physical_address;
@@ -188,14 +196,6 @@ static int cf_check ap_setup(void)
     return setup_vp_assist();
 }
 
-static void __init cf_check e820_fixup(void)
-{
-    uint64_t s = HV_HCALL_MFN << PAGE_SHIFT;
-
-    if ( !e820_add_range(s, s + PAGE_SIZE, E820_RESERVED) )
-        panic("Unable to reserve Hyper-V hypercall range\n");
-}
-
 static int cf_check flush_tlb(
     const cpumask_t *mask, const void *va, unsigned int flags)
 {
@@ -212,7 +212,6 @@ static const struct hypervisor_ops __initconst_cf_clobber ops = {
     .name = "Hyper-V",
     .setup = setup,
     .ap_setup = ap_setup,
-    .e820_fixup = e820_fixup,
     .flush_tlb = flush_tlb,
 };
 
xen/arch/x86/include/asm/guest/hyperv.h +0 -3
index dabc62727b..5792e77104 100644
@@ -10,9 +10,6 @@
 
 #include <xen/types.h>
 
-/* Use top-most MFN for hypercall page */
-#define HV_HCALL_MFN   (((1ULL << paddr_bits) - 1) >> HV_HYP_PAGE_SHIFT)
-
 /*
  * The specification says: "The partition reference time is computed
  * by the following formula:
Signature · 1 line
2.54.0
#1 lore raw
Quoted 12 lines · Ariadne Conill

Previously, the Hyper-V guest code placed the hypercall page at a fixed, hardcoded location: the top-most MFN of the guest physical address space (HV_HCALL_MFN). To make this work, an e820_fixup hook reserved that range as E820_RESERVED so nothing else would claim it.

This assumes the top-most physical frame is actually backed by RAM (or at least mapped to something), which is not guaranteed. On Azure compute guests it is not: the top-most MFN is not physically mapped to anything. When we program HV_X64_MSR_HYPERCALL to point at that frame and subsequently execute through the hypercall page via the fixmap, the access faults and Xen panics during early boot.

Quoted 6 lines

Instead of guessing at a physical address, allocate a real xenheap page and use its MFN for the hypercall page. This frame is always backed and mapped, so the fault no longer occurs. The e820_fixup hook and the HV_HCALL_MFN definition are no longer needed and are removed.

This is certainly nicer than before.  But, it was intentional to be a page not backed by RAM, and "out of the way".

IIRC, the Viridian hypercall page is described as an overlay which comes into place, and shadows whatever is behind it.  It is also mapped read-only which means it will shatter superpages (hence putting it "out of the way"), and yield's #GP when trying to write to it (this is not so easy to achieve, so is clearly deliberate).

From the commit message, you seem to be saying that selecting a page which isn't actually RAM doesn't appear to work?  What fault precisely do you get?

Quoted 10 lines
---
 xen/arch/x86/guest/hyperv/hyperv.c      | 19 +++++++++----------
 xen/arch/x86/include/asm/guest/hyperv.h |  3 ---
 2 files changed, 9 insertions(+), 13 deletions(-)
xen/arch/x86/guest/hyperv/hyperv.c +0 -0
index 90757e0793..d98d02fb80 100644

xen/arch/x86/guest/hyperv/hyperv.c static void __init setup_hypercall_page(void) · line 102

Quoted 5 lines
xen/arch/x86/guest/hyperv/hyperv.c +1 -1
@@ -99,10 +99,18 @@ static void __init setup_hypercall_page(void)
     rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
     if ( !hypercall_msr.enable )
     {
-        mfn = HV_HCALL_MFN;
+        void *hcall_page = alloc_xenheap_page();

We never access the page via it's main alias.  We configure FIXMAP_X to point at it.

So I think this is safe to be domheap page not a xenheap page.

~Andrew