Skip to contentSkip to conversation

More Hyper-V infrastructures

PATCH In review Wei Liu 4 participants 23 messages Feb 4, 2020 – Feb 5, 2020

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

v7 cover More Hyper-V infrastructures

This patch sereis implements several important functionalities to run Xen on top of Hyper-V. See individual patches for more details.

I've checked the assembly code as well as putting in a test patch to make sure the hypercall interface is implemented correctly.

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 (10):
x86/hypervisor: make hypervisor_ap_setup return an error code x86/smp: don't online cpu if hypervisor_ap_setup fails
x86: provide executable fixmap facility x86/hypervisor: provide hypervisor_fixup_e820 x86/hyperv: setup hypercall page
x86/hyperv: provide Hyper-V hypercall functions
DO NOT APPLY: x86/hyperv: issue an hypercall x86/hyperv: provide percpu hypercall input page x86/hyperv: retrieve vp_index from Hyper-V x86/hyperv: setup VP assist page

 MAINTAINERS                              |   1 +
 xen/arch/x86/boot/x86_64.S               |  15 ++-
 xen/arch/x86/e820.c                      |   4 +-
 xen/arch/x86/guest/hyperv/hyperv.c       | 154 ++++++++++++++++++++++-
 xen/arch/x86/guest/hyperv/private.h      |  31 +++++
 xen/arch/x86/guest/hypervisor.c          |  12 +-
 xen/arch/x86/guest/xen/xen.c             |  65 ++++++----
 xen/arch/x86/livepatch.c                 |   3 +-
 xen/arch/x86/mm.c                        |  14 ++-
 xen/arch/x86/smpboot.c                   |  12 +-
 xen/arch/x86/xen.lds.S                   |   5 +
 xen/include/asm-x86/asm_defns.h          |   5 +
 xen/include/asm-x86/config.h             |   2 +-
 xen/include/asm-x86/fixmap.h             |  24 ++++
 xen/include/asm-x86/guest/hyperv-hcall.h |  97 ++++++++++++++
 xen/include/asm-x86/guest/hyperv-tlfs.h  |   5 +-
 xen/include/asm-x86/guest/hyperv.h       |   3 +
 xen/include/asm-x86/guest/hypervisor.h   |  12 +-
 18 files changed, 419 insertions(+), 45 deletions(-)
 create mode 100644 xen/arch/x86/guest/hyperv/private.h
 create mode 100644 xen/include/asm-x86/guest/hyperv-hcall.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 v7 1/10 x86/hypervisor: make hypervisor_ap_setup return an error code

We want to be able to handle AP setup error in the upper layer.

For Xen, remove all panic() and BUG_ON() in init_evtchn and map_vcpuinfo. Only panic/BUG_ON when Xen can't fail gracefully.

---
v7:
1. Change init_evtchn

v6:
1. Change map_vcpuinfo as well
2. Make code shorter

---
 xen/arch/x86/guest/hypervisor.c        |  6 ++-
 xen/arch/x86/guest/xen/xen.c           | 58 ++++++++++++++++----------
 xen/include/asm-x86/guest/hypervisor.h |  6 +--
 3 files changed, 42 insertions(+), 28 deletions(-)
xen/arch/x86/guest/hypervisor.c +4 -2
index 4f27b98740..e72c92ffdf 100644
@@ -52,10 +52,12 @@ void __init hypervisor_setup(void)
         ops->setup();
 }
 
-void hypervisor_ap_setup(void)
+int hypervisor_ap_setup(void)
 {
     if ( ops && ops->ap_setup )
-        ops->ap_setup();
+        return ops->ap_setup();
+
+    return 0;
 }
 
 void hypervisor_resume(void)
xen/arch/x86/guest/xen/xen.c +35 -23

108 lines not on this page · raw message

xen/include/asm-x86/guest/hypervisor.h +3 -3
index 392f4b90ae..b503854c5b 100644
@@ -25,7 +25,7 @@ struct hypervisor_ops {
     /* Main setup routine */
     void (*setup)(void);
     /* AP setup */
-    void (*ap_setup)(void);
+    int (*ap_setup)(void);
     /* Resume from suspension */
     void (*resume)(void);
 };
@@ -34,7 +34,7 @@ struct hypervisor_ops {
 
 const char *hypervisor_probe(void);
 void hypervisor_setup(void);
-void hypervisor_ap_setup(void);
+int hypervisor_ap_setup(void);
 void hypervisor_resume(void);
 
 #else
@@ -44,7 +44,7 @@ void hypervisor_resume(void);
 
 static inline const char *hypervisor_probe(void) { return NULL; }
 static inline void hypervisor_setup(void) { ASSERT_UNREACHABLE(); }
-static inline void hypervisor_ap_setup(void) { ASSERT_UNREACHABLE(); }
+static inline int hypervisor_ap_setup(void) { ASSERT_UNREACHABLE(); return 0; }
 static inline void hypervisor_resume(void) { ASSERT_UNREACHABLE(); }
 
 #endif  /* CONFIG_GUEST */
Signature · 7 lines
2.20.1


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

Re: [Xen-devel] [PATCH v7 01/10] x86/hypervisor: make hypervisor_ap_setup return an error code

Quoted 7 lines · Wei Liu

We want to be able to handle AP setup error in the upper layer.

For Xen, remove all panic() and BUG_ON() in init_evtchn and map_vcpuinfo. Only panic/BUG_ON when Xen can't fail gracefully.

---

BTW I discover an issue: init_evtchn sets HVM_PARAM_CALLBACK_IRQ every time it is called. That's unnecessary for APs. Perhaps it would be best to break that function into two, one for setting HVM_PARAM_CALLBACK_IRQ, the other for allocating and setting callback. BSP needs to call both while APs only needs to call the latter.

This is out of scope for this series, but it is something to consider in the future.

Wei.

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

#3 lore raw

Re: [Xen-devel] [PATCH v7 01/10] x86/hypervisor: make hypervisor_ap_setup return an error code

Quoted 6 lines · Wei Liu
Quoted 5 lines · Wei Liu

We want to be able to handle AP setup error in the upper layer.

For Xen, remove all panic() and BUG_ON() in init_evtchn and map_vcpuinfo. Only panic/BUG_ON when Xen can't fail gracefully.

Quoted 8 lines
Quoted 2 lines

---

BTW I discover an issue: init_evtchn sets HVM_PARAM_CALLBACK_IRQ every time it is called. That's unnecessary for APs. Perhaps it would be best to break that function into two, one for setting HVM_PARAM_CALLBACK_IRQ, the other for allocating and setting callback. BSP needs to call both while APs only needs to call the latter.

We could gate the call to HVMOP_set_param on !smp_processor_id(), that way the BSP would be the only one to set HVM_PARAM_CALLBACK_IRQ. I'm not sure splitting this into a separate function is worth it.

Thanks, Roger.

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

author
#4 lore raw

Re: [Xen-devel] [PATCH v7 01/10] x86/hypervisor: make hypervisor_ap_setup return an error code

Quoted 16 lines · Roger Pau Monné
Quoted 6 lines · Wei Liu
Quoted 5 lines · Wei Liu

We want to be able to handle AP setup error in the upper layer.

For Xen, remove all panic() and BUG_ON() in init_evtchn and map_vcpuinfo. Only panic/BUG_ON when Xen can't fail gracefully.

Quoted 8 lines
Quoted 2 lines

---

BTW I discover an issue: init_evtchn sets HVM_PARAM_CALLBACK_IRQ every time it is called. That's unnecessary for APs. Perhaps it would be best to break that function into two, one for setting HVM_PARAM_CALLBACK_IRQ, the other for allocating and setting callback. BSP needs to call both while APs only needs to call the latter.

Quoted 3 lines

We could gate the call to HVMOP_set_param on !smp_processor_id(), that way the BSP would be the only one to set HVM_PARAM_CALLBACK_IRQ. I'm not sure splitting this into a separate function is worth it.

This works too. But again, something for another day.

Wei.

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

Reviewed-by on v7 1/10
#5 lore raw

Re: [Xen-devel] [PATCH v7 01/10] x86/hypervisor: make hypervisor_ap_setup return an error code

In reply to Wei Liu

xen/arch/x86/guest/xen/xen.c static void init_evtchn(void) · line 225

Quoted 8 lines · Wei Liu
xen/arch/x86/guest/xen/xen.c +3 -1
@@ -215,18 +220,19 @@ static void init_evtchn(void)
     rc = xen_hypercall_set_evtchn_upcall_vector(this_cpu(vcpu_id),
                                                 evtchn_upcall_vector);
     if ( rc )
-        panic("Unable to set evtchn upcall vector: %d\n", rc);
+    {
+        printk("Unable to set evtchn upcall vector: %d\n", rc);
+        goto out;

There's no need for "goto" here - "return rc" is all you need instead. As stated elsewhere, when there's complex cleanup or a fair risk of leaving out an important cleanup step, I can live with "goto" getting used. But I don't think it should be used to replace a simple "return".

With this

with one more (optional!) suggestion and one more remark:

Quoted 13 lines
xen/arch/x86/guest/xen/xen.c +4 -2
@@ -254,14 +260,20 @@ static void __init setup(void)
                XEN_LEGACY_MAX_VCPUS);
     }
 
-    init_evtchn();
+    BUG_ON(init_evtchn());
 }
 
-static void ap_setup(void)
+static int ap_setup(void)
 {
+    int rc;
+

xen/arch/x86/guest/xen/xen.c static void __init setup(void) · line 277

Quoted 10 lines
xen/arch/x86/guest/xen/xen.c +6 -2
     set_vcpu_id();
-    map_vcpuinfo();
-    init_evtchn();
+
+    rc = map_vcpuinfo();
+    if ( rc )
+        return rc;
+
+    return init_evtchn();
 }

To avoid a local variable, how about

    return map_vcpuinfo() ?: init_evtchn();

?

xen/arch/x86/guest/xen/xen.c int xg_free_unused_page(mfn_t mfn) · line 300

Quoted 9 lines
xen/arch/x86/guest/xen/xen.c +2 -2
@@ -283,8 +295,8 @@ int xg_free_unused_page(mfn_t mfn)
 
 static void ap_resume(void *unused)
 {
-    map_vcpuinfo();
-    init_evtchn();
+    BUG_ON(map_vcpuinfo());
+    BUG_ON(init_evtchn());
 }

Current code structure calls for this, but in principle I don't think AP failure on resume should be any different from AP failure during boot. Nothing to be address here and now, of course.

Jan

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

author
#6 lore raw

Re: [Xen-devel] [PATCH v7 01/10] x86/hypervisor: make hypervisor_ap_setup return an error code

Quoted 15 lines · Jan Beulich

xen/arch/x86/guest/xen/xen.c static void init_evtchn(void) · line 225

Quoted 8 lines · Wei Liu
xen/arch/x86/guest/xen/xen.c +3 -1
@@ -215,18 +220,19 @@ static void init_evtchn(void)
     rc = xen_hypercall_set_evtchn_upcall_vector(this_cpu(vcpu_id),
                                                 evtchn_upcall_vector);
     if ( rc )
-        panic("Unable to set evtchn upcall vector: %d\n", rc);
+    {
+        printk("Unable to set evtchn upcall vector: %d\n", rc);
+        goto out;

There's no need for "goto" here - "return rc" is all you need instead. As stated elsewhere, when there's complex cleanup or a fair risk of leaving out an important cleanup step, I can live with "goto" getting used. But I don't think it should be used to replace a simple "return".

OK. That can be fixed.

Quoted 4 lines

With this

with one more (optional!) suggestion and one more remark:

Thanks.

Quoted 27 lines
Quoted 14 lines
xen/arch/x86/guest/xen/xen.c +4 -2
@@ -254,14 +260,20 @@ static void __init setup(void)
                XEN_LEGACY_MAX_VCPUS);
     }
 
-    init_evtchn();
+    BUG_ON(init_evtchn());
 }
 
-static void ap_setup(void)
+static int ap_setup(void)
 {
+    int rc;
+

xen/arch/x86/guest/xen/xen.c static void __init setup(void) · line 277

Quoted 10 lines
xen/arch/x86/guest/xen/xen.c +6 -2
     set_vcpu_id();
-    map_vcpuinfo();
-    init_evtchn();
+
+    rc = map_vcpuinfo();
+    if ( rc )
+        return rc;
+
+    return init_evtchn();
 }

To avoid a local variable, how about

Quoted 3 lines
    return map_vcpuinfo() ?: init_evtchn();

?

ISTR this is a GNU extension, but seeing that there is already quite a lot of it in hypercisor code, I will make the change.

Wei.

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

#7 lore raw

Re: [Xen-devel] [PATCH v7 01/10] x86/hypervisor: make hypervisor_ap_setup return an error code

Quoted 28 lines · Wei Liu
Quoted 27 lines · Jan Beulich
Quoted 13 lines · Wei Liu
xen/arch/x86/guest/xen/xen.c +4 -2
@@ -254,14 +260,20 @@ static void __init setup(void)
                XEN_LEGACY_MAX_VCPUS);
     }
 
-    init_evtchn();
+    BUG_ON(init_evtchn());
 }
 
-static void ap_setup(void)
+static int ap_setup(void)
 {
+    int rc;
+

xen/arch/x86/guest/xen/xen.c static void __init setup(void) · line 277

Quoted 10 lines
xen/arch/x86/guest/xen/xen.c +6 -2
     set_vcpu_id();
-    map_vcpuinfo();
-    init_evtchn();
+
+    rc = map_vcpuinfo();
+    if ( rc )
+        return rc;
+
+    return init_evtchn();
 }

To avoid a local variable, how about

Quoted 6 lines
Quoted 3 lines
    return map_vcpuinfo() ?: init_evtchn();

?

ISTR this is a GNU extension, but seeing that there is already quite a lot of it in hypercisor code, I will make the change.

In our own code using extensions is generally fine (as far as they're sufficiently backwards compatible). It's the public headers where we want to be more careful.

Jan

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

author
#8 lore raw

PATCH v7 2/10 x86/smp: don't online cpu if hypervisor_ap_setup fails

In reply to Wei Liu

Push hypervisor_ap_setup down to smp_callin.

Take the chance to replace xen_guest with cpu_has_hypervisor.

---
 xen/arch/x86/smpboot.c                 | 10 +++++++---
 xen/include/asm-x86/guest/hypervisor.h |  2 +-
 2 files changed, 8 insertions(+), 4 deletions(-)
xen/arch/x86/smpboot.c +7 -3
index c9d1ab4423..93b86a09e9 100644
@@ -199,6 +199,13 @@ static void smp_callin(void)
         goto halt;
     }
 
+    if ( cpu_has_hypervisor && (rc = hypervisor_ap_setup()) != 0 )
+    {
+        printk("CPU%d: Failed to initialise hypervisor functions. Not coming online.\n", cpu);
+        cpu_error = rc;
+        goto halt;
+    }
+
     if ( (rc = hvm_cpu_up()) != 0 )
     {
         printk("CPU%d: Failed to initialise HVM. Not coming online.\n", cpu);
@@ -371,9 +378,6 @@ void start_secondary(void *unused)
 
     tsx_init(); /* Needs microcode.  May change HLE/RTM feature bits. */
 
-    if ( xen_guest )
-        hypervisor_ap_setup();
-
     smp_callin();
 
     set_cpu_sibling_map(cpu);
xen/include/asm-x86/guest/hypervisor.h +1 -1
index b503854c5b..64383f0c3d 100644
@@ -44,7 +44,7 @@ void hypervisor_resume(void);
 
 static inline const char *hypervisor_probe(void) { return NULL; }
 static inline void hypervisor_setup(void) { ASSERT_UNREACHABLE(); }
-static inline int hypervisor_ap_setup(void) { ASSERT_UNREACHABLE(); return 0; }
+static inline int hypervisor_ap_setup(void) { return 0; }
 static inline void hypervisor_resume(void) { ASSERT_UNREACHABLE(); }
 
 #endif  /* CONFIG_GUEST */
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 v7 3/10 x86: provide executable fixmap facility

In reply to Wei Liu

This allows us to set aside some address space for executable mapping. This fixed map range starts from XEN_VIRT_END so that it is within reach of the .text section.

Shift the percpu stub range and shrink livepatch range accordingly.

---
v7:
1. Introduce ASM_CONSTANT

v6:
1. Move symbol generation snippet to arch_init_memory and use %P0

v5:
1. drop __virt_to_fix_x
2. also check FIX*_RESERVED in __set_fixmap*
3. generate global symbol to be used in linker script
4. address other misc comments

---
 xen/arch/x86/boot/x86_64.S      | 15 ++++++++++++---
 xen/arch/x86/livepatch.c        |  3 ++-
 xen/arch/x86/mm.c               | 14 +++++++++++++-
 xen/arch/x86/smpboot.c          |  2 +-
 xen/arch/x86/xen.lds.S          |  1 +
 xen/include/asm-x86/asm_defns.h |  5 +++++
 xen/include/asm-x86/config.h    |  2 +-
 xen/include/asm-x86/fixmap.h    | 24 ++++++++++++++++++++++++
 8 files changed, 59 insertions(+), 7 deletions(-)
xen/arch/x86/boot/x86_64.S +12 -3
index 1cbf5acdfb..314a32a19f 100644
@@ -81,11 +81,20 @@ GLOBAL(l2_directmap)
         .size l2_directmap, . - l2_directmap
 
 /*
- * L2 mapping the Xen text/data/bss region, constructed dynamically.  Uses 1x
- * 4k page.
+ * L2 mapping the Xen text/data/bss region, constructed dynamically.
+ * Executable fixmap is hooked up statically.
+ * Uses 1x 4k page.
  */
 GLOBAL(l2_xenmap)
-        .fill L2_PAGETABLE_ENTRIES, 8, 0
+        idx = 0
+        .rept L2_PAGETABLE_ENTRIES
+        .if idx == l2_table_offset(FIXADDR_X_TOP - 1)
+        .quad sym_offs(l1_fixmap_x) + __PAGE_HYPERVISOR
+        .else
+        .quad 0
+        .endif
+        idx = idx + 1
+        .endr
         .size l2_xenmap, . - l2_xenmap
 
 /* L2 mapping the fixmap.  Uses 1x 4k page. */
xen/arch/x86/livepatch.c +2 -1
index 2749cbc5cf..513b0f3841 100644
@@ -12,6 +12,7 @@
 #include <xen/livepatch.h>
 #include <xen/sched.h>
 
+#include <asm/fixmap.h>
 #include <asm/nmi.h>
 #include <asm/livepatch.h>
 
@@ -311,7 +312,7 @@ void __init arch_livepatch_init(void)
     void *start, *end;
 
     start = (void *)xen_virt_end;
-    end = (void *)(XEN_VIRT_END - NR_CPUS * PAGE_SIZE);
+    end = (void *)(XEN_VIRT_END - FIXADDR_X_SIZE - NR_CPUS * PAGE_SIZE);
 
     BUG_ON(end <= start);
 
xen/arch/x86/mm.c +13 -1
index a05a713276..9b33829084 100644
@@ -158,6 +158,8 @@
 /* Mapping of the fixmap space needed early. */
 l1_pgentry_t __section(".bss.page_aligned") __aligned(PAGE_SIZE)
     l1_fixmap[L1_PAGETABLE_ENTRIES];
+l1_pgentry_t __section(".bss.page_aligned") __aligned(PAGE_SIZE)
+    l1_fixmap_x[L1_PAGETABLE_ENTRIES];
 
 paddr_t __read_mostly mem_hotplug;
 
@@ -373,6 +375,9 @@ void __init arch_init_memory(void)
         }
     }
 #endif
+
+    /* Generate a symbol to be used in linker script */
+    ASM_CONSTANT(FIXADDR_X_SIZE, FIXADDR_X_SIZE);
 }
 
 int page_is_ram_type(unsigned long mfn, unsigned long mem_type)
@@ -5719,10 +5724,17 @@ int destroy_xen_mappings(unsigned long s, unsigned long e)
 void __set_fixmap(
     enum fixed_addresses idx, unsigned long mfn, unsigned long flags)
 {
-    BUG_ON(idx >= __end_of_fixed_addresses);
+    BUG_ON(idx >= __end_of_fixed_addresses || idx <= FIX_RESERVED);
     map_pages_to_xen(__fix_to_virt(idx), _mfn(mfn), 1, flags);
 }
 
+void __set_fixmap_x(
+    enum fixed_addresses_x idx, unsigned long mfn, unsigned long flags)
+{
+    BUG_ON(idx >= __end_of_fixed_addresses_x || idx <= FIX_X_RESERVED);
+    map_pages_to_xen(__fix_x_to_virt(idx), _mfn(mfn), 1, flags);
+}
+
 void *__init arch_vmap_virt_end(void)
 {
     return fix_to_virt(__end_of_fixed_addresses);
xen/arch/x86/smpboot.c +1 -1
index 93b86a09e9..e83e4564a4 100644
@@ -644,7 +644,7 @@ unsigned long alloc_stub_page(unsigned int cpu, unsigned long *mfn)
         unmap_domain_page(memset(__map_domain_page(pg), 0xcc, PAGE_SIZE));
     }
 
-    stub_va = XEN_VIRT_END - (cpu + 1) * PAGE_SIZE;
+    stub_va = XEN_VIRT_END - FIXADDR_X_SIZE - (cpu + 1) * PAGE_SIZE;
     if ( map_pages_to_xen(stub_va, page_to_mfn(pg), 1,
                           PAGE_HYPERVISOR_RX | MAP_SMALL_PAGES) )
     {
xen/arch/x86/xen.lds.S +1 -0
index f5730ffe93..44fda616d5 100644
@@ -352,6 +352,7 @@ SECTIONS
 }
 
 ASSERT(__2M_rwdata_end <= XEN_VIRT_END - XEN_VIRT_START + __XEN_VIRT_START -
+                          FIXADDR_X_SIZE -
                           NR_CPUS * PAGE_SIZE,
        "Xen image overlaps stubs area")
 
xen/include/asm-x86/asm_defns.h +5 -0
index 370f239c50..98cd49a89b 100644
@@ -392,4 +392,9 @@ label: .long (val);                         \
     .size label, . - label;                 \
     .type label, @object
 
+
+#define ASM_CONSTANT(name, value)                \
+    asm ( ".equ " #name ", %P0; .global " #name  \
+          :: "i" ((value)) );
+
 #endif /* __X86_ASM_DEFNS_H__ */
xen/include/asm-x86/config.h +1 -1
index d0cfbb70a8..a34053c4c0 100644
@@ -218,7 +218,7 @@ extern unsigned char boot_edid_info[128];
 /* Slot 261: high read-only compat machine-to-phys conversion table (1GB). */
 #define HIRO_COMPAT_MPT_VIRT_START RDWR_COMPAT_MPT_VIRT_END
 #define HIRO_COMPAT_MPT_VIRT_END (HIRO_COMPAT_MPT_VIRT_START + GB(1))
-/* Slot 261: xen text, static data and bss (1GB). */
+/* Slot 261: xen text, static data, bss, per-cpu stubs and executable fixmap (1GB). */
 #define XEN_VIRT_START          (HIRO_COMPAT_MPT_VIRT_END)
 #define XEN_VIRT_END            (XEN_VIRT_START + GB(1))
 
xen/include/asm-x86/fixmap.h +24 -0
index 9fb2f47946..8330097a74 100644
@@ -15,6 +15,7 @@
 #include <asm/page.h>
 
 #define FIXADDR_TOP (VMAP_VIRT_END - PAGE_SIZE)
+#define FIXADDR_X_TOP (XEN_VIRT_END - PAGE_SIZE)
 
 #ifndef __ASSEMBLY__
 
@@ -89,6 +90,29 @@ static inline unsigned long virt_to_fix(const unsigned long vaddr)
     return __virt_to_fix(vaddr);
 }
 
+enum fixed_addresses_x {
+    /* Index 0 is reserved since fix_x_to_virt(0) == FIXADDR_X_TOP. */
+    FIX_X_RESERVED,
+#ifdef CONFIG_HYPERV_GUEST
+    FIX_X_HYPERV_HCALL,
+#endif
+    __end_of_fixed_addresses_x
+};
+
+#define FIXADDR_X_SIZE  (__end_of_fixed_addresses_x << PAGE_SHIFT)
+#define FIXADDR_X_START (FIXADDR_X_TOP - FIXADDR_X_SIZE)
+
+extern void __set_fixmap_x(
+    enum fixed_addresses_x idx, unsigned long mfn, unsigned long flags);
+
+#define set_fixmap_x(idx, phys) \
+    __set_fixmap_x(idx, (phys)>>PAGE_SHIFT, PAGE_HYPERVISOR_RX | MAP_SMALL_PAGES)
+
+#define clear_fixmap_x(idx) __set_fixmap_x(idx, 0, 0)
+
+#define __fix_x_to_virt(x) (FIXADDR_X_TOP - ((x) << PAGE_SHIFT))
+#define fix_x_to_virt(x)   ((void *)__fix_x_to_virt(x))
+
 #endif /* __ASSEMBLY__ */
 
 #endif
Signature · 7 lines
2.20.1


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

Re: [Xen-devel] [PATCH v7 03/10] x86: provide executable fixmap facility

Quoted 7 lines · Wei Liu

This allows us to set aside some address space for executable mapping. This fixed map range starts from XEN_VIRT_END so that it is within reach of the .text section.

Shift the percpu stub range and shrink livepatch range accordingly.

with ...

xen/include/asm-x86/asm_defns.h label: .long (val); \ · line 400

Quoted 12 lines
xen/include/asm-x86/asm_defns.h +5 -0
@@ -392,4 +392,9 @@ label: .long (val);                         \
     .size label, . - label;                 \
     .type label, @object
 
+
+#define ASM_CONSTANT(name, value)                \
+    asm ( ".equ " #name ", %P0; .global " #name  \
+          :: "i" ((value)) );
+
 #endif /* __X86_ASM_DEFNS_H__ */

... no double blank lines introduced here.

Jan

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

author
#11 lore raw

PATCH v7 4/10 x86/hypervisor: provide hypervisor_fixup_e820

In reply to Wei Liu

And implement the hook for Xen guest.

---
v7:
1. Drop bogus ASSERT_UNREACHABLE from stub
2. Add Jan's Rb, considering #1 doesn't change the meat of the patch

---
 xen/arch/x86/e820.c                    | 4 ++--
 xen/arch/x86/guest/hypervisor.c        | 6 ++++++
 xen/arch/x86/guest/xen/xen.c           | 7 +++++++
 xen/include/asm-x86/guest/hypervisor.h | 6 ++++++
 4 files changed, 21 insertions(+), 2 deletions(-)
xen/arch/x86/e820.c +2 -2
index b9f589cac3..160f029edd 100644
@@ -691,8 +691,8 @@ unsigned long __init init_e820(const char *str, struct e820map *raw)
 
     machine_specific_memory_setup(raw);
 
-    if ( pv_shim )
-        pv_shim_fixup_e820(&e820);
+    if ( cpu_has_hypervisor )
+        hypervisor_e820_fixup(&e820);
 
     printk("%s RAM map:\n", str);
     print_e820_memory_map(e820.map, e820.nr_map);
xen/arch/x86/guest/hypervisor.c +6 -0
index e72c92ffdf..5fd433c8d4 100644
@@ -66,6 +66,12 @@ void hypervisor_resume(void)
         ops->resume();
 }
 
+void __init hypervisor_e820_fixup(struct e820map *e820)
+{
+    if ( ops && ops->e820_fixup )
+        ops->e820_fixup(e820);
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/x86/guest/xen/xen.c +7 -0
index 1cf09886da..5a7f3cce42 100644
@@ -322,11 +322,18 @@ static void resume(void)
         pv_console_init();
 }
 
+static void __init e820_fixup(struct e820map *e820)
+{
+    if ( pv_shim )
+        pv_shim_fixup_e820(e820);
+}
+
 static const struct hypervisor_ops ops = {
     .name = "Xen",
     .setup = setup,
     .ap_setup = ap_setup,
     .resume = resume,
+    .e820_fixup = e820_fixup,
 };
 
 const struct hypervisor_ops *__init xg_probe(void)
xen/include/asm-x86/guest/hypervisor.h +6 -0
index 64383f0c3d..ade10e74ea 100644
@@ -19,6 +19,8 @@
 #ifndef __X86_HYPERVISOR_H__
 #define __X86_HYPERVISOR_H__
 
+#include <asm/e820.h>
+
 struct hypervisor_ops {
     /* Name of the hypervisor */
     const char *name;
@@ -28,6 +30,8 @@ struct hypervisor_ops {
     int (*ap_setup)(void);
     /* Resume from suspension */
     void (*resume)(void);
+    /* Fix up e820 map */
+    void (*e820_fixup)(struct e820map *e820);
 };
 
 #ifdef CONFIG_GUEST
@@ -36,6 +40,7 @@ const char *hypervisor_probe(void);
 void hypervisor_setup(void);
 int hypervisor_ap_setup(void);
 void hypervisor_resume(void);
+void hypervisor_e820_fixup(struct e820map *e820);
 
 #else
 
@@ -46,6 +51,7 @@ static inline const char *hypervisor_probe(void) { return NULL; }
 static inline void hypervisor_setup(void) { ASSERT_UNREACHABLE(); }
 static inline int hypervisor_ap_setup(void) { return 0; }
 static inline void hypervisor_resume(void) { ASSERT_UNREACHABLE(); }
+static inline void hypervisor_e820_fixup(struct e820map *e820) {}
 
 #endif  /* CONFIG_GUEST */
 
Signature · 7 lines
2.20.1


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

PATCH v7 5/10 x86/hyperv: setup hypercall page

In reply to Wei Liu

Hyper-V uses a technique called overlay page for its hypercall page. It will insert a backing page to the guest when the hypercall functionality is enabled. That means we can use a page that is not backed by real memory for hypercall page.

To avoid shattering L0 superpages and treading on any MMIO areas residing in low addresses, use the top-most addressable page for that purpose. Adjust e820 map accordingly.

We also need to register Xen's guest OS ID to Hyper-V. Use 0x3 as the vendor ID. Fix the comment in hyperv-tlfs.h while at it.

---
v7:
1. Fix a style issue
2. Initialise ID to 0
3. Update commit message

v6:
1. Use hv_guest_os_id
2. Use new e820_fixup hook
3. Add a BUILD_BUG_ON

v5:
1. use hypervisor_reserve_top_pages
2. add a macro for hypercall page mfn
3. address other misc comments

v4:
1. Use fixmap
2. Follow routines listed in TLFS

---
 xen/arch/x86/guest/hyperv/hyperv.c      | 69 +++++++++++++++++++++++--
 xen/include/asm-x86/guest/hyperv-tlfs.h |  5 +-
 xen/include/asm-x86/guest/hyperv.h      |  3 ++
 3 files changed, 72 insertions(+), 5 deletions(-)
xen/arch/x86/guest/hyperv/hyperv.c +66 -3
index 8d38313d7a..2e20a96f30 100644
@@ -19,15 +19,27 @@
  * Copyright (c) 2019 Microsoft.
  */
 #include <xen/init.h>
+#include <xen/version.h>
 
+#include <asm/fixmap.h>
 #include <asm/guest.h>
 #include <asm/guest/hyperv-tlfs.h>
+#include <asm/processor.h>
 
 struct ms_hyperv_info __read_mostly ms_hyperv;
 
-static const struct hypervisor_ops ops = {
-    .name = "Hyper-V",
-};
+static uint64_t generate_guest_id(void)
+{
+    union hv_guest_os_id id = {};
+
+    id.vendor = HV_XEN_VENDOR_ID;
+    id.major = xen_major_version();
+    id.minor = xen_minor_version();
+
+    return id.raw;
+}
+
+static const struct hypervisor_ops ops;
 
 const struct hypervisor_ops *__init hyperv_probe(void)
 {
@@ -72,6 +84,57 @@ 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;
+    union hv_guest_os_id guest_id;
+    unsigned long mfn;
+
+    BUILD_BUG_ON(HV_HYP_PAGE_SHIFT != PAGE_SHIFT);
+
+    rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id.raw);
+    if ( !guest_id.raw )
+    {
+        guest_id.raw = generate_guest_id();
+        wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id.raw);
+    }
+
+    rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+    if ( !hypercall_msr.enable )
+    {
+        mfn = HV_HCALL_MFN;
+        hypercall_msr.enable = 1;
+        hypercall_msr.guest_physical_address = mfn;
+        wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+    }
+    else
+        mfn = hypercall_msr.guest_physical_address;
+
+    rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+    BUG_ON(!hypercall_msr.enable);
+
+    set_fixmap_x(FIX_X_HYPERV_HCALL, mfn << PAGE_SHIFT);
+}
+
+static void __init setup(void)
+{
+    setup_hypercall_page();
+}
+
+static void __init e820_fixup(struct e820map *e820)
+{
+    uint64_t s = HV_HCALL_MFN << PAGE_SHIFT;
+
+    if ( !e820_add_range(e820, s, s + PAGE_SIZE, E820_RESERVED) )
+        panic("Unable to reserve Hyper-V hypercall range\n");
+}
+
+static const struct hypervisor_ops ops = {
+    .name = "Hyper-V",
+    .setup = setup,
+    .e820_fixup = e820_fixup,
+};
+
 /*
  * Local variables:
  * mode: C
xen/include/asm-x86/guest/hyperv-tlfs.h +3 -2
index 091e25cdd1..0a0f3398c1 100644
@@ -318,15 +318,16 @@ struct ms_hyperv_tsc_page {
  *
  * Bit(s)
  * 63 - Indicates if the OS is Open Source or not; 1 is Open Source
- * 62:56 - Os Type; Linux is 0x100
+ * 62:56 - Os Type; Linux 0x1, FreeBSD 0x2, Xen 0x3
  * 55:48 - Distro specific identification
- * 47:16 - Linux kernel version number
+ * 47:16 - Guest OS version number
  * 15:0  - Distro specific identification
  *
  *
  */
 
 #define HV_LINUX_VENDOR_ID              0x8100
+#define HV_XEN_VENDOR_ID                0x8300
 union hv_guest_os_id
 {
     uint64_t raw;
xen/include/asm-x86/guest/hyperv.h +3 -0
index c7a7f32bd5..1a1b47831c 100644
@@ -21,6 +21,9 @@
 
 #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 · 7 lines
2.20.1


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

Re: [Xen-devel] [PATCH v7 05/10] x86/hyperv: setup hypercall page

Quoted 5 lines · Wei Liu

Hyper-V uses a technique called overlay page for its hypercall page. It will insert a backing page to the guest when the hypercall functionality is enabled. That means we can use a page that is not backed by real memory for hypercall page.

Quoted 8 lines

To avoid shattering L0 superpages and treading on any MMIO areas residing in low addresses, use the top-most addressable page for that purpose. Adjust e820 map accordingly.

We also need to register Xen's guest OS ID to Hyper-V. Use 0x3 as the vendor ID. Fix the comment in hyperv-tlfs.h while at it.

Quoted 79 lines

---
v7:
1. Fix a style issue
2. Initialise ID to 0
3. Update commit message

v6:
1. Use hv_guest_os_id
2. Use new e820_fixup hook
3. Add a BUILD_BUG_ON

v5:
1. use hypervisor_reserve_top_pages
2. add a macro for hypercall page mfn
3. address other misc comments

v4:
1. Use fixmap
2. Follow routines listed in TLFS

---
 xen/arch/x86/guest/hyperv/hyperv.c      | 69 +++++++++++++++++++++++--
 xen/include/asm-x86/guest/hyperv-tlfs.h |  5 +-
 xen/include/asm-x86/guest/hyperv.h      |  3 ++
 3 files changed, 72 insertions(+), 5 deletions(-)
xen/arch/x86/guest/hyperv/hyperv.c +30 -3
index 8d38313d7a..2e20a96f30 100644
@@ -19,15 +19,27 @@
  * Copyright (c) 2019 Microsoft.
  */
 #include <xen/init.h>
+#include <xen/version.h>
 
+#include <asm/fixmap.h>
 #include <asm/guest.h>
 #include <asm/guest/hyperv-tlfs.h>
+#include <asm/processor.h>
 
 struct ms_hyperv_info __read_mostly ms_hyperv;
 
-static const struct hypervisor_ops ops = {
-    .name = "Hyper-V",
-};
+static uint64_t generate_guest_id(void)
+{
+    union hv_guest_os_id id = {};
+
+    id.vendor = HV_XEN_VENDOR_ID;
+    id.major = xen_major_version();
+    id.minor = xen_minor_version();
+
+    return id.raw;
+}
+
+static const struct hypervisor_ops ops;
 
 const struct hypervisor_ops *__init hyperv_probe(void)
 {
@@ -72,6 +84,57 @@ 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;
+    union hv_guest_os_id guest_id;
+    unsigned long mfn;
+
+    BUILD_BUG_ON(HV_HYP_PAGE_SHIFT != PAGE_SHIFT);
+
+    rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id.raw);
+    if ( !guest_id.raw )
+    {
+        guest_id.raw = generate_guest_id();
+        wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id.raw);
+    }
+

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

Quoted 10 lines
xen/arch/x86/guest/hyperv/hyperv.c +10 -0
+    rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+    if ( !hypercall_msr.enable )
+    {
+        mfn = HV_HCALL_MFN;
+        hypercall_msr.enable = 1;
+        hypercall_msr.guest_physical_address = mfn;
+        wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+    }
+    else
+        mfn = hypercall_msr.guest_physical_address;

Is it expected that the guest ID or the hypercal page is already setup?

Ie: would virtual firmware setup any of this? (and not clean it up afterwards)

Thanks, Roger.

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

author
#14 lore raw

Re: [Xen-devel] [PATCH v7 05/10] x86/hyperv: setup hypercall page

On Wed, Feb 05, 2020 at 03:04:16PM +0100, Roger Pau Monné wrote:
[...]

Quoted 29 lines
Quoted 15 lines
xen/arch/x86/guest/hyperv/hyperv.c +15 -0
+static void __init setup_hypercall_page(void)
+{
+    union hv_x64_msr_hypercall_contents hypercall_msr;
+    union hv_guest_os_id guest_id;
+    unsigned long mfn;
+
+    BUILD_BUG_ON(HV_HYP_PAGE_SHIFT != PAGE_SHIFT);
+
+    rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id.raw);
+    if ( !guest_id.raw )
+    {
+        guest_id.raw = generate_guest_id();
+        wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id.raw);
+    }
+

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

Quoted 10 lines
xen/arch/x86/guest/hyperv/hyperv.c +10 -0
+    rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+    if ( !hypercall_msr.enable )
+    {
+        mfn = HV_HCALL_MFN;
+        hypercall_msr.enable = 1;
+        hypercall_msr.guest_physical_address = mfn;
+        wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+    }
+    else
+        mfn = hypercall_msr.guest_physical_address;

Is it expected that the guest ID or the hypercal page is already setup?

Quoted 2 lines

Ie: would virtual firmware setup any of this? (and not clean it up afterwards)

There is no definitive answer to why/when this happens in TLFS, but it does require guest to respect that is already in the MSR.

Wei.

Quoted 2 lines

Thanks, Roger.

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

author
#15 lore raw

Re: [Xen-devel] [PATCH v7 05/10] x86/hyperv: setup hypercall page

Quoted 31 lines · Wei Liu

On Wed, Feb 05, 2020 at 03:04:16PM +0100, Roger Pau Monné wrote:
[...]

Quoted 29 lines
Quoted 15 lines
xen/arch/x86/guest/hyperv/hyperv.c +15 -0
+static void __init setup_hypercall_page(void)
+{
+    union hv_x64_msr_hypercall_contents hypercall_msr;
+    union hv_guest_os_id guest_id;
+    unsigned long mfn;
+
+    BUILD_BUG_ON(HV_HYP_PAGE_SHIFT != PAGE_SHIFT);
+
+    rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id.raw);
+    if ( !guest_id.raw )
+    {
+        guest_id.raw = generate_guest_id();
+        wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id.raw);
+    }
+

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

Quoted 10 lines
xen/arch/x86/guest/hyperv/hyperv.c +10 -0
+    rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+    if ( !hypercall_msr.enable )
+    {
+        mfn = HV_HCALL_MFN;
+        hypercall_msr.enable = 1;
+        hypercall_msr.guest_physical_address = mfn;
+        wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+    }
+    else
+        mfn = hypercall_msr.guest_physical_address;

Is it expected that the guest ID or the hypercal page is already setup?

Quoted 5 lines
Quoted 2 lines

Ie: would virtual firmware setup any of this? (and not clean it up afterwards)

There is no definitive answer to why/when this happens in TLFS, but it does require guest to respect that is already in the MSR.

My guess would be after migration Hyper-V sets up the same MSR/page FWIW.

Wei.

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

Acked-by Paul Durrant on v7 5/10
#16 lore raw

Re: [Xen-devel] [PATCH v7 05/10] x86/hyperv: setup hypercall page

In reply to Wei Liu
Quoted 15 lines

-----Original Message-----
From: Wei Liu <[email protected]> On Behalf Of Wei Liu
Sent: 04 February 2020 15:37
To: Xen Development List <[email protected]>
Cc: Durrant, Paul <[email protected]>; Michael Kelley <[email protected]>; Wei Liu <[email protected]>; Wei Liu <[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>
Subject: [PATCH v7 05/10] x86/hyperv: setup hypercall page

Hyper-V uses a technique called overlay page for its hypercall page. It will insert a backing page to the guest when the hypercall functionality is enabled. That means we can use a page that is not backed by real memory for hypercall page.

Quoted 8 lines

To avoid shattering L0 superpages and treading on any MMIO areas residing in low addresses, use the top-most addressable page for that purpose. Adjust e820 map accordingly.

We also need to register Xen's guest OS ID to Hyper-V. Use 0x3 as the vendor ID. Fix the comment in hyperv-tlfs.h while at it.

Quoted 159 lines

---
v7:
1. Fix a style issue
2. Initialise ID to 0
3. Update commit message

v6:
1. Use hv_guest_os_id
2. Use new e820_fixup hook
3. Add a BUILD_BUG_ON

v5:
1. use hypervisor_reserve_top_pages
2. add a macro for hypercall page mfn
3. address other misc comments

v4:
1. Use fixmap
2. Follow routines listed in TLFS

---
 xen/arch/x86/guest/hyperv/hyperv.c      | 69 +++++++++++++++++++++++--
 xen/include/asm-x86/guest/hyperv-tlfs.h |  5 +-
 xen/include/asm-x86/guest/hyperv.h      |  3 ++
 3 files changed, 72 insertions(+), 5 deletions(-)
xen/arch/x86/guest/hyperv/hyperv.c +66 -3
index 8d38313d7a..2e20a96f30 100644
@@ -19,15 +19,27 @@
  * Copyright (c) 2019 Microsoft.
  */
 #include <xen/init.h>
+#include <xen/version.h>
 
+#include <asm/fixmap.h>
 #include <asm/guest.h>
 #include <asm/guest/hyperv-tlfs.h>
+#include <asm/processor.h>
 
 struct ms_hyperv_info __read_mostly ms_hyperv;
 
-static const struct hypervisor_ops ops = {
-    .name = "Hyper-V",
-};
+static uint64_t generate_guest_id(void)
+{
+    union hv_guest_os_id id = {};
+
+    id.vendor = HV_XEN_VENDOR_ID;
+    id.major = xen_major_version();
+    id.minor = xen_minor_version();
+
+    return id.raw;
+}
+
+static const struct hypervisor_ops ops;
 
 const struct hypervisor_ops *__init hyperv_probe(void)
 {
@@ -72,6 +84,57 @@ 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;
+    union hv_guest_os_id guest_id;
+    unsigned long mfn;
+
+    BUILD_BUG_ON(HV_HYP_PAGE_SHIFT != PAGE_SHIFT);
+
+    rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id.raw);
+    if ( !guest_id.raw )
+    {
+        guest_id.raw = generate_guest_id();
+        wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id.raw);
+    }
+
+    rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+    if ( !hypercall_msr.enable )
+    {
+        mfn = HV_HCALL_MFN;
+        hypercall_msr.enable = 1;
+        hypercall_msr.guest_physical_address = mfn;
+        wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+    }
+    else
+        mfn = hypercall_msr.guest_physical_address;
+
+    rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+    BUG_ON(!hypercall_msr.enable);
+
+    set_fixmap_x(FIX_X_HYPERV_HCALL, mfn << PAGE_SHIFT);
+}
+
+static void __init setup(void)
+{
+    setup_hypercall_page();
+}
+
+static void __init e820_fixup(struct e820map *e820)
+{
+    uint64_t s = HV_HCALL_MFN << PAGE_SHIFT;
+
+    if ( !e820_add_range(e820, s, s + PAGE_SIZE, E820_RESERVED) )
+        panic("Unable to reserve Hyper-V hypercall range\n");
+}
+
+static const struct hypervisor_ops ops = {
+    .name = "Hyper-V",
+    .setup = setup,
+    .e820_fixup = e820_fixup,
+};
+
 /*
  * Local variables:
  * mode: C
xen/include/asm- renamed from xen/include/asm-x86/guest/hyperv-tlfs.h +0 -0

x86/guest/hyperv-tlfs.h
index 091e25cdd1..0a0f3398c1 100644

xen/include/asm-x86/guest/hyperv-tlfs.h +3 -2
@@ -318,15 +318,16 @@ struct ms_hyperv_tsc_page {
  *
  * Bit(s)
  * 63 - Indicates if the OS is Open Source or not; 1 is Open Source
- * 62:56 - Os Type; Linux is 0x100
+ * 62:56 - Os Type; Linux 0x1, FreeBSD 0x2, Xen 0x3
  * 55:48 - Distro specific identification
- * 47:16 - Linux kernel version number
+ * 47:16 - Guest OS version number
  * 15:0  - Distro specific identification
  *
  *
  */
 
 #define HV_LINUX_VENDOR_ID              0x8100
+#define HV_XEN_VENDOR_ID                0x8300
 union hv_guest_os_id
 {
     uint64_t raw;
xen/include/asm- renamed from xen/include/asm-x86/guest/hyperv.h +0 -0

x86/guest/hyperv.h
index c7a7f32bd5..1a1b47831c 100644

xen/include/asm-x86/guest/hyperv.h +3 -1
@@ -21,6 +21,9 @@
 
 #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:
--
Quoted 1 line

2.20.1

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

author
#17 lore raw

PATCH v7 6/10 x86/hyperv: provide Hyper-V hypercall functions

In reply to Wei Liu

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

---
v7:
1. Use ASM_CONSTANT and put it in hyperv.c

v6:
1. Use asm(...) to generate symbol
2. Add a comment regarding volatile registers

v5:
1. Switch back to direct call
2. Fix some issues pointed out by Jan

---
 MAINTAINERS                              |  1 +
 xen/arch/x86/guest/hyperv/hyperv.c       |  2 +
 xen/arch/x86/xen.lds.S                   |  4 +
 xen/include/asm-x86/guest/hyperv-hcall.h | 97 ++++++++++++++++++++++++
 4 files changed, 104 insertions(+)
 create mode 100644 xen/include/asm-x86/guest/hyperv-hcall.h
MAINTAINERS +1 -0
index 04d91482cd..d0a5ed635b 100644
@@ -519,6 +519,7 @@ S:	Supported
 F:	xen/arch/x86/guest/hyperv/
 F:	xen/arch/x86/hvm/viridian/
 F:	xen/include/asm-x86/guest/hyperv.h
+F:	xen/include/asm-x86/guest/hyperv-hcall.h
 F:	xen/include/asm-x86/guest/hyperv-tlfs.h
 F:	xen/include/asm-x86/hvm/viridian.h
 
xen/arch/x86/guest/hyperv/hyperv.c +2 -0
index 2e20a96f30..888bda25b0 100644
@@ -118,6 +118,8 @@ static void __init setup_hypercall_page(void)
 
 static void __init setup(void)
 {
+    ASM_CONSTANT(HV_HCALL_PAGE, __fix_x_to_virt(FIX_X_HYPERV_HCALL));
+
     setup_hypercall_page();
 }
 
xen/arch/x86/xen.lds.S +4 -0
index 44fda616d5..7f9459d683 100644
@@ -326,6 +326,10 @@ SECTIONS
   efi = .;
 #endif
 
+#ifdef CONFIG_HYPERV_GUEST
+  hv_hcall_page = ABSOLUTE(HV_HCALL_PAGE);
+#endif
+
   /* Sections to be discarded */
   /DISCARD/ : {
        *(.exit.text)
xen/include/asm-x86/guest/hyperv-hcall.h new file +97 -0

97 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
Acked-by on v7 6/10
#18 lore raw

Re: [Xen-devel] [PATCH v7 06/10] x86/hyperv: provide Hyper-V hypercall functions

Quoted 4 lines · Wei Liu

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

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

author
#19 lore raw

PATCH v7 7/10 DO NOT APPLY: x86/hyperv: issue an hypercall

In reply to Wei Liu

Test if the infrastructure works.

---
 xen/arch/x86/guest/hyperv/hyperv.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
xen/arch/x86/guest/hyperv/hyperv.c +14 -0
index 888bda25b0..2a2afcb363 100644
@@ -23,6 +23,7 @@
 
 #include <asm/fixmap.h>
 #include <asm/guest.h>
+#include <asm/guest/hyperv-hcall.h>
 #include <asm/guest/hyperv-tlfs.h>
 #include <asm/processor.h>
 
@@ -114,6 +115,19 @@ static void __init setup_hypercall_page(void)
     BUG_ON(!hypercall_msr.enable);
 
     set_fixmap_x(FIX_X_HYPERV_HCALL, mfn << PAGE_SHIFT);
+
+    /* XXX Wei: Issue an hypercall here to make sure things are set up
+     * correctly.  When there is actual use of the hypercall facility,
+     * this can be removed.
+     */
+    {
+        uint16_t r = hv_do_hypercall(0xffff, 0, 0);
+        BUG_ON(r != HV_STATUS_INVALID_HYPERCALL_CODE);
+        r = hv_do_fast_hypercall(0xffff, 0, 0);
+        BUG_ON(r != HV_STATUS_INVALID_HYPERCALL_CODE);
+
+        printk("Successfully issued Hyper-V hypercalls\n");
+    }
 }
 
 static void __init setup(void)
Signature · 7 lines
2.20.1


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

PATCH v7 8/10 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. One 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.

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

---
v6:
1. Make code shorter
2. Change variable name

v5:
1. Adjust to new ap_setup
2. Change variable name to hv_pcpu_input_page

v4:
1. Change wording in commit message
2. Prevent leak
3. Introduce a private header

v3:
1. Use xenheap page instead
2. Drop page tracking structure
3. Drop Paul's review tag

address comments

---
 xen/arch/x86/guest/hyperv/hyperv.c  | 28 ++++++++++++++++++++++++++++
 xen/arch/x86/guest/hyperv/private.h | 29 +++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)
 create mode 100644 xen/arch/x86/guest/hyperv/private.h
xen/arch/x86/guest/hyperv/hyperv.c +28 -0
index 2a2afcb363..6b58065c78 100644
@@ -27,7 +27,10 @@
 #include <asm/guest/hyperv-tlfs.h>
 #include <asm/processor.h>
 
+#include "private.h"
+
 struct ms_hyperv_info __read_mostly ms_hyperv;
+DEFINE_PER_CPU_READ_MOSTLY(void *, hv_input_page);
 
 static uint64_t generate_guest_id(void)
 {
@@ -130,11 +133,35 @@ static void __init setup_hypercall_page(void)
     }
 }
 
+static int setup_hypercall_pcpu_arg(void)
+{
+    if ( this_cpu(hv_input_page) )
+        return 0;
+
+    this_cpu(hv_input_page) = alloc_xenheap_page();
+    if ( !this_cpu(hv_input_page) )
+    {
+        printk("CPU%u: Failed to allocate hypercall input page\n",
+               smp_processor_id());
+        return -ENOMEM;
+    }
+
+    return 0;
+}
+
 static void __init setup(void)
 {
     ASM_CONSTANT(HV_HCALL_PAGE, __fix_x_to_virt(FIX_X_HYPERV_HCALL));
 
     setup_hypercall_page();
+
+    if ( setup_hypercall_pcpu_arg() )
+        panic("Hyper-V hypercall percpu arg setup failed\n");
+}
+
+static int ap_setup(void)
+{
+    return setup_hypercall_pcpu_arg();
 }
 
 static void __init e820_fixup(struct e820map *e820)
@@ -148,6 +175,7 @@ static void __init e820_fixup(struct e820map *e820)
 static const struct hypervisor_ops ops = {
     .name = "Hyper-V",
     .setup = setup,
+    .ap_setup = ap_setup,
     .e820_fixup = e820_fixup,
 };
 
xen/arch/x86/guest/hyperv/private.h new file +29 -0
new file mode 100644
index 0000000000..093985a94b
@@ -0,0 +1,29 @@
+/******************************************************************************
+ * arch/x86/guest/hyperv/private.h
+ *
+ * Definitions / declarations only useful to Hyper-V code.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright (c) 2020 Microsoft.
+ */
+
+#ifndef __XEN_HYPERV_PRIVIATE_H__
+#define __XEN_HYPERV_PRIVIATE_H__
+
+#include <xen/percpu.h>
+
+DECLARE_PER_CPU(void *, hv_input_page);
+
+#endif /* __XEN_HYPERV_PRIVIATE_H__  */
Signature · 7 lines
2.20.1


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

PATCH v7 9/10 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).

---
v5:
1. Add Jan's Ack.

v4:
1. Use private.h
2. Add Paul's review tag

v2:
1. Fold into setup_pcpu_arg function

---
 xen/arch/x86/guest/hyperv/hyperv.c  | 6 ++++++
 xen/arch/x86/guest/hyperv/private.h | 1 +
 2 files changed, 7 insertions(+)
xen/arch/x86/guest/hyperv/hyperv.c +6 -0
index 6b58065c78..e31c4b30b8 100644
@@ -31,6 +31,7 @@
 
 struct ms_hyperv_info __read_mostly ms_hyperv;
 DEFINE_PER_CPU_READ_MOSTLY(void *, hv_input_page);
+DEFINE_PER_CPU_READ_MOSTLY(unsigned int, hv_vp_index);
 
 static uint64_t generate_guest_id(void)
 {
@@ -135,6 +136,8 @@ static void __init setup_hypercall_page(void)
 
 static int setup_hypercall_pcpu_arg(void)
 {
+    uint64_t vp_index_msr;
+
     if ( this_cpu(hv_input_page) )
         return 0;
 
@@ -146,6 +149,9 @@ static int setup_hypercall_pcpu_arg(void)
         return -ENOMEM;
     }
 
+    rdmsrl(HV_X64_MSR_VP_INDEX, vp_index_msr);
+    this_cpu(hv_vp_index) = vp_index_msr;
+
     return 0;
 }
 
xen/arch/x86/guest/hyperv/private.h +1 -0
index 093985a94b..d1765d4f23 100644
@@ -25,5 +25,6 @@
 #include <xen/percpu.h>
 
 DECLARE_PER_CPU(void *, hv_input_page);
+DECLARE_PER_CPU(unsigned int, hv_vp_index);
 
 #endif /* __XEN_HYPERV_PRIVIATE_H__  */
Signature · 7 lines
2.20.1


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

PATCH v7 10/10 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 it for efficient nested virtualisation.

---
v6:
1. Use hv_vp_assist_page_msr
2. Make code shorter
3. Preserve rsvdP fields

v5:
1. Deal with error properly instead of always panicking
2. Swap percpu variables declarations' location

v4:
1. Use private.h
2. Prevent leak

v3:
1. Use xenheap page
2. Drop set_vp_assist

v2:
1. Use HV_HYP_PAGE_SHIFT instead

---
 xen/arch/x86/guest/hyperv/hyperv.c  | 37 ++++++++++++++++++++++++++++-
 xen/arch/x86/guest/hyperv/private.h |  1 +
 2 files changed, 37 insertions(+), 1 deletion(-)
xen/arch/x86/guest/hyperv/hyperv.c +36 -1
index e31c4b30b8..248f854d6e 100644
@@ -31,6 +31,7 @@
 
 struct ms_hyperv_info __read_mostly ms_hyperv;
 DEFINE_PER_CPU_READ_MOSTLY(void *, hv_input_page);
+DEFINE_PER_CPU_READ_MOSTLY(void *, hv_vp_assist);
 DEFINE_PER_CPU_READ_MOSTLY(unsigned int, hv_vp_index);
 
 static uint64_t generate_guest_id(void)
@@ -155,6 +156,31 @@ static int setup_hypercall_pcpu_arg(void)
     return 0;
 }
 
+static int setup_vp_assist(void)
+{
+    union hv_vp_assist_page_msr msr;
+
+    if ( !this_cpu(hv_vp_assist) )
+    {
+        this_cpu(hv_vp_assist) = alloc_xenheap_page();
+        if ( !this_cpu(hv_vp_assist) )
+        {
+            printk("CPU%u: Failed to allocate vp_assist page\n",
+                   smp_processor_id());
+            return -ENOMEM;
+        }
+
+        clear_page(this_cpu(hv_vp_assist));
+    }
+
+    rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.raw);
+    msr.pfn = virt_to_mfn(this_cpu(hv_vp_assist));
+    msr.enabled = 1;
+    wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.raw);
+
+    return 0;
+}
+
 static void __init setup(void)
 {
     ASM_CONSTANT(HV_HCALL_PAGE, __fix_x_to_virt(FIX_X_HYPERV_HCALL));
@@ -163,11 +189,20 @@ static void __init setup(void)
 
     if ( setup_hypercall_pcpu_arg() )
         panic("Hyper-V hypercall percpu arg setup failed\n");
+
+    if ( setup_vp_assist() )
+        panic("VP assist page setup failed\n");
 }
 
 static int ap_setup(void)
 {
-    return setup_hypercall_pcpu_arg();
+    int rc;
+
+    rc = setup_hypercall_pcpu_arg();
+    if ( rc )
+        return rc;
+
+    return setup_vp_assist();
 }
 
 static void __init e820_fixup(struct e820map *e820)
xen/arch/x86/guest/hyperv/private.h +1 -0
index d1765d4f23..956eff831f 100644
@@ -25,6 +25,7 @@
 #include <xen/percpu.h>
 
 DECLARE_PER_CPU(void *, hv_input_page);
+DECLARE_PER_CPU(void *, hv_vp_assist);
 DECLARE_PER_CPU(unsigned int, hv_vp_index);
 
 #endif /* __XEN_HYPERV_PRIVIATE_H__  */
Signature · 7 lines
2.20.1


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