Skip to contentSkip to conversation

Introduce /dev/mshv drivers

PATCH Superseded Nuno Das Neves 4 participants 36 messages Sep 22, 2023 – Sep 28, 2023

Subsystems (8)
Documentation x86 ACPI arm64 +4 more subsystems

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Introduce /dev/mshv drivers

This series introduces support for creating and running guest machines while running on the Microsoft Hypervisor. [0]
This is done via an IOCTL interface accessed through /dev/mshv, similar to /dev/kvm. Another series introducing this support was previously posted.
[1]

These interfaces support VMMs running in:
1. The root patition - provided in the mshv_root module, and
2. VTL 2 - provided in the mshv_vtl module [2]

Patches breakdown
-----------------
The first 7 patches are refactoring and adding some helper functions. They provide some benefit on their own and could be applied independently as cleanup patches.

Patches 8-12 just set things up for the driver code to come. These are very small. They come first so that the remaining patches are more self-contained.

The final 3 patches are the meat of the series:
- Patch 13 contains new header files used by the driver.
These are designed to mirror the ABI headers exported by Hyper-V. This is done to avoid polluting hyperv-tlfs.h and help track changes to the ABIs that are still unstable. (See FAQ below).
- Patch 14 conditionally includes these new header files into mshyperv.h and linux/hyperv.h, in order to be able to use these files in the new drivers while remaining independent from hyperv-tlfs.h.
- Patch 15 contains the new driver code located in drivers/hv. This is a large amount of code and new files, but it is mostly self-contained and all within drivers/hv - apart from the IOCTL interface itself in uapi.

Patch 15 is rather big and has bounced back from some mailing lists. If you did not get a copy in your inbox, you can view it here instead:
https://github.com/NunoDasNeves/linux/commit/2ae1cabd82257cb303296ab6af707d1bac867e42

FAQ on include/uapi/hyperv/*.h
------------------------------
Q:
Why not just add these definitions to hyperv-tlfs.h?
A:
The intention of hyperv-tlfs.h is to contain stable definitions documented in the public TLFS document. These new definitions don't fit that criteria, so they should be separate.

Q:
The new headers redefine many things that are already in hyperv-tlfs.h - why? A:
Some definitions are extended compared to what is documented in the TLFS. In order to avoid adding undocumented or unstable definitions to hyperv-tlfs.h, the new headers must compile independently.
Therefore, the new headers must redefine many things in hyperv-tlfs.h in order to compile.

Q:
Why are these files named hvgdk.h, hvgdk_mini.h, hvhdk.h and hvhdk_mini.h? A:
The precise meaning of the names reflects conventions used internally at Microsoft.
Naming them this way makes it easy to find where particular Hyper-V definitions come from, and check their correctness.
It also facilitates the future work of automatically generating these files.

Q:
Why are they in uapi?
A:
In short, to keep things simple. There are many definitions needed in both the kernel and the VMM in userspace. Separating them doesn't serve much purpose, and makes it more laborious to import definitions from Hyper-V code.

--------------------------
[0] "Hyper-V" is more well-known, but it really refers to the whole stack including the hypervisor and other components that run in Windows kernel and userspace.
[1] Previous /dev/mshv patch series and discussion:

    https://lore.kernel.org/linux-hyperv/1632853875-20261-1-git-send-email-nunodasneves@linux.microsoft.com/

[2] Virtual Secure Mode (VSM) and Virtual Trust Levels (VTL):
https://learn.microsoft.com/en-us/virtualization/hyper-v-on-windows/tlfs/vsm
--------------------------

Changes since v2:
* Fix some commit message wrapping
* Fix many checkpatch.pl --strict style issues
* Replace uapi ints with __s32
* Replace uapi enums with __u32
* Replace uapi pointers with __u64
* Add explicit padding to uapi structures
* Initialize status in get/set registers hypercall helpers
* Add missing return on error in get_vp_signaled_count
* Remove select TRANSPARENT_HUGEPAGES for mshv_vtl
* Use __func__ prefix consistently in printks
* Use single generic cpuid() to get all 4 registers instead of 4 calls
* Change hv_proximity_domain_info from union to struct for clarity
Changes since v1:
* Clean up formatting, capitalization in commit messages
* Add detail to commit message for patch 15
* Remove errant lines in Makefile and Kconfig in patch 15
* Move a reference to CONFIG_MSHV_VTL from patch 9 to 15

Nuno Das Neves (15):
hyperv-tlfs: Change shared HV_REGISTER_* defines to HV_MSR_*
mshyperv: Introduce hv_get_hypervisor_version function
mshyperv: Introduce numa_node_to_proximity_domain_info asm-generic/mshyperv: Introduce hv_recommend_using_aeoi()
hyperv: Move hv_connection_id to hyperv-tlfs
hyperv-tlfs: Introduce hv_status_to_string and hv_status_to_errno
Drivers: hv: Move hv_call_deposit_pages and hv_call_create_vp to
common code
Drivers: hv: Introduce per-cpu event ring tail
Drivers: hv: Introduce hv_output_arg_exists in hv_common.c
x86: hyperv: Add mshv_handler irq handler and setup function
Drivers: hv: export vmbus_isr, hv_context and hv_post_message
Documentation: Reserve ioctl number for mshv driver
uapi: hyperv: Add mshv driver headers defining hypervisor ABIs
asm-generic: hyperv: Use new Hyper-V headers conditionally.
Drivers: hv: Add modules to expose /dev/mshv to VMMs running on
Hyper-V

 .../userspace-api/ioctl/ioctl-number.rst      |    2 +
 arch/arm64/hyperv/mshyperv.c                  |   23 +-
 arch/arm64/include/asm/hyperv-tlfs.h          |   25 +
 arch/arm64/include/asm/mshyperv.h             |    2 +-
 arch/x86/hyperv/hv_init.c                     |    2 +-
 arch/x86/hyperv/hv_proc.c                     |  166 +-
 arch/x86/include/asm/hyperv-tlfs.h            |  137 +-
 arch/x86/include/asm/mshyperv.h               |   12 +-
 arch/x86/kernel/cpu/mshyperv.c                |   67 +-
 drivers/acpi/numa/srat.c                      |    1 +
 drivers/clocksource/hyperv_timer.c            |   24 +-
 drivers/hv/Kconfig                            |   49 +
 drivers/hv/Makefile                           |   20 +
 drivers/hv/hv.c                               |   50 +-
 drivers/hv/hv_call.c                          |  113 +
 drivers/hv/hv_common.c                        |  223 +-
 drivers/hv/hyperv_vmbus.h                     |    2 +-
 drivers/hv/mshv.h                             |  123 ++
 drivers/hv/mshv_eventfd.c                     |  761 +++++++
 drivers/hv/mshv_eventfd.h                     |   80 +
 drivers/hv/mshv_main.c                        |  208 ++
 drivers/hv/mshv_msi.c                         |  129 ++
 drivers/hv/mshv_portid_table.c                |   84 +
 drivers/hv/mshv_root.h                        |  193 ++
 drivers/hv/mshv_root_hv_call.c                | 1015 +++++++++
 drivers/hv/mshv_root_main.c                   | 1920 +++++++++++++++++
 drivers/hv/mshv_synic.c                       |  688 ++++++
 drivers/hv/mshv_vtl.h                         |   52 +
 drivers/hv/mshv_vtl_main.c                    | 1517 +++++++++++++
 drivers/hv/vmbus_drv.c                        |    3 +-
 drivers/hv/xfer_to_guest.c                    |   28 +
 include/asm-generic/hyperv-defs.h             |   26 +
 include/asm-generic/hyperv-tlfs.h             |   93 +-
 include/asm-generic/mshyperv.h                |   73 +-
 include/linux/hyperv.h                        |   11 +-
 include/uapi/hyperv/hvgdk.h                   |   41 +
 include/uapi/hyperv/hvgdk_mini.h              | 1076 +++++++++
 include/uapi/hyperv/hvhdk.h                   | 1342 ++++++++++++
 include/uapi/hyperv/hvhdk_mini.h              |  160 ++
 include/uapi/linux/mshv.h                     |  306 +++
 40 files changed, 10483 insertions(+), 364 deletions(-)
 create mode 100644 drivers/hv/hv_call.c
 create mode 100644 drivers/hv/mshv.h
 create mode 100644 drivers/hv/mshv_eventfd.c
 create mode 100644 drivers/hv/mshv_eventfd.h
 create mode 100644 drivers/hv/mshv_main.c
 create mode 100644 drivers/hv/mshv_msi.c
 create mode 100644 drivers/hv/mshv_portid_table.c
 create mode 100644 drivers/hv/mshv_root.h
 create mode 100644 drivers/hv/mshv_root_hv_call.c
 create mode 100644 drivers/hv/mshv_root_main.c
 create mode 100644 drivers/hv/mshv_synic.c
 create mode 100644 drivers/hv/mshv_vtl.h
 create mode 100644 drivers/hv/mshv_vtl_main.c
 create mode 100644 drivers/hv/xfer_to_guest.c
 create mode 100644 include/asm-generic/hyperv-defs.h
 create mode 100644 include/uapi/hyperv/hvgdk.h
 create mode 100644 include/uapi/hyperv/hvgdk_mini.h
 create mode 100644 include/uapi/hyperv/hvhdk.h
 create mode 100644 include/uapi/hyperv/hvhdk_mini.h
 create mode 100644 include/uapi/linux/mshv.h
Signature · 1 line
2.25.1
#1 lore raw

PATCH v3 1/15 hyperv-tlfs: Change shared HV_REGISTER_* defines to HV_MSR_*

In x86 hyperv-tlfs, HV_REGISTER_ prefix is used to indicate MSRs accessed via rdmsrl/wrmsrl. But in ARM64, HV_REGISTER_ instead indicates VP registers accessed via get/set vp registers hypercall.

This is due to HV_REGISTER_* names being used by hv_set/get_register, with the arch-specific version delegating to the appropriate mechanism.

The problem is, using prefix HV_REGISTER_ for MSRs will conflict with VP registers when they are introduced for x86 in future.

This patch solves the issue by:

1. Defining all the x86 MSRs with a consistent prefix: HV_X64_MSR_. This is so HV_REGISTER_ can be reserved for VP registers.

2. Change the non-arch-specific alias used by hv_set/get_register to HV_MSR_. This is also happens to be the same name HyperV uses for this purpose.

---
 arch/arm64/include/asm/hyperv-tlfs.h |  25 +++++
 arch/x86/include/asm/hyperv-tlfs.h   | 137 +++++++++++++--------------
 arch/x86/include/asm/mshyperv.h      |   8 +-
 arch/x86/kernel/cpu/mshyperv.c       |  22 ++---
 drivers/clocksource/hyperv_timer.c   |  24 ++---
 drivers/hv/hv.c                      |  36 +++----
 drivers/hv/hv_common.c               |  18 ++--
 include/asm-generic/mshyperv.h       |   2 +-
 8 files changed, 148 insertions(+), 124 deletions(-)
arch/arm64/include/asm/hyperv-tlfs.h +25 -0
index bc6c7ac934a1..a6e852c2fc3a 100644
@@ -64,6 +64,31 @@
 #define HV_REGISTER_STIMER0_CONFIG	0x000B0000
 #define HV_REGISTER_STIMER0_COUNT	0x000B0001
 
+/*
+ * To support non-arch-specific code calling hv_set/get_register:
+ * - On x86, HV_MSR_ indicates an MSR accessed via rdmsrl/wrmsrl
+ * - On ARM, HV_MSR_ indicates a VP register accessed via hypercall
+ */
+#define HV_MSR_VP_INDEX		(HV_REGISTER_VP_INDEX)
+#define HV_MSR_TIME_REF_COUNT	(HV_REGISTER_TIME_REF_COUNT)
+#define HV_MSR_REFERENCE_TS	(HV_REGISTER_REFERENCE_TSC)
+
+#define HV_MSR_STIMER0_CONFIG	(HV_REGISTER_STIMER0_CONFIG)
+#define HV_MSR_STIMER0_COUNT	(HV_REGISTER_STIMER0_COUNT)
+
+#define HV_MSR_SCONTROL		(HV_REGISTER_SCONTROL)
+#define HV_MSR_SIEFP		(HV_REGISTER_SIEFP)
+#define HV_MSR_SIMP		(HV_REGISTER_SIMP)
+#define HV_MSR_EOM		(HV_REGISTER_EOM)
+#define HV_MSR_SINT0		(HV_REGISTER_SINT0)
+
+#define HV_MSR_CRASH_P0		(HV_REGISTER_CRASH_P0)
+#define HV_MSR_CRASH_P1		(HV_REGISTER_CRASH_P1)
+#define HV_MSR_CRASH_P2		(HV_REGISTER_CRASH_P2)
+#define HV_MSR_CRASH_P3		(HV_REGISTER_CRASH_P3)
+#define HV_MSR_CRASH_P4		(HV_REGISTER_CRASH_P4)
+#define HV_MSR_CRASH_CTL	(HV_REGISTER_CRASH_CTL)
+
 union hv_msi_entry {
 	u64 as_uint64[2];
 	struct {
arch/x86/include/asm/hyperv-tlfs.h +68 -69

177 lines not on this page · raw message

arch/x86/include/asm/mshyperv.h +4 -4
index 66ca641a164a..a1d098505b60 100644
@@ -295,14 +295,14 @@ static inline void hv_ivm_msr_read(u64 msr, u64 *value) {}
 
 static inline bool hv_is_synic_reg(unsigned int reg)
 {
-	return (reg >= HV_REGISTER_SCONTROL) &&
-	       (reg <= HV_REGISTER_SINT15);
+	return (reg >= HV_X64_MSR_SCONTROL) &&
+	       (reg <= HV_X64_MSR_SINT15);
 }
 
 static inline bool hv_is_sint_reg(unsigned int reg)
 {
-	return (reg >= HV_REGISTER_SINT0) &&
-	       (reg <= HV_REGISTER_SINT15);
+	return (reg >= HV_X64_MSR_SINT0) &&
+	       (reg <= HV_X64_MSR_SINT15);
 }
 
 u64 hv_get_register(unsigned int reg);
arch/x86/kernel/cpu/mshyperv.c +11 -11
index b63590ffc777..7c82b96da11f 100644
@@ -48,19 +48,19 @@ EXPORT_SYMBOL_GPL(hyperv_paravisor_present);
 static inline unsigned int hv_get_nested_reg(unsigned int reg)
 {
 	if (hv_is_sint_reg(reg))
-		return reg - HV_REGISTER_SINT0 + HV_REGISTER_NESTED_SINT0;
+		return reg - HV_X64_MSR_SINT0 + HV_X64_MSR_NESTED_SINT0;
 
 	switch (reg) {
-	case HV_REGISTER_SIMP:
-		return HV_REGISTER_NESTED_SIMP;
-	case HV_REGISTER_SIEFP:
-		return HV_REGISTER_NESTED_SIEFP;
-	case HV_REGISTER_SVERSION:
-		return HV_REGISTER_NESTED_SVERSION;
-	case HV_REGISTER_SCONTROL:
-		return HV_REGISTER_NESTED_SCONTROL;
-	case HV_REGISTER_EOM:
-		return HV_REGISTER_NESTED_EOM;
+	case HV_X64_MSR_SIMP:
+		return HV_X64_MSR_NESTED_SIMP;
+	case HV_X64_MSR_SIEFP:
+		return HV_X64_MSR_NESTED_SIEFP;
+	case HV_X64_MSR_SVERSION:
+		return HV_X64_MSR_NESTED_SVERSION;
+	case HV_X64_MSR_SCONTROL:
+		return HV_X64_MSR_NESTED_SCONTROL;
+	case HV_X64_MSR_EOM:
+		return HV_X64_MSR_NESTED_EOM;
 	default:
 		return reg;
 	}
drivers/clocksource/hyperv_timer.c +12 -12
index 8ff7cd4e20bb..40832043dd21 100644
@@ -81,14 +81,14 @@ static int hv_ce_set_next_event(unsigned long delta,
 
 	current_tick = hv_read_reference_counter();
 	current_tick += delta;
-	hv_set_register(HV_REGISTER_STIMER0_COUNT, current_tick);
+	hv_set_register(HV_MSR_STIMER0_COUNT, current_tick);
 	return 0;
 }
 
 static int hv_ce_shutdown(struct clock_event_device *evt)
 {
-	hv_set_register(HV_REGISTER_STIMER0_COUNT, 0);
-	hv_set_register(HV_REGISTER_STIMER0_CONFIG, 0);
+	hv_set_register(HV_MSR_STIMER0_COUNT, 0);
+	hv_set_register(HV_MSR_STIMER0_CONFIG, 0);
 	if (direct_mode_enabled && stimer0_irq >= 0)
 		disable_percpu_irq(stimer0_irq);
 
@@ -119,7 +119,7 @@ static int hv_ce_set_oneshot(struct clock_event_device *evt)
 		timer_cfg.direct_mode = 0;
 		timer_cfg.sintx = stimer0_message_sint;
 	}
-	hv_set_register(HV_REGISTER_STIMER0_CONFIG, timer_cfg.as_uint64);
+	hv_set_register(HV_MSR_STIMER0_CONFIG, timer_cfg.as_uint64);
 	return 0;
 }
 
@@ -373,10 +373,10 @@ static __always_inline u64 read_hv_clock_msr(void)
 	 * nanosecond units.
 	 *
 	 * Use hv_raw_get_register() because this function is used from
-	 * noinstr. Notable; while HV_REGISTER_TIME_REF_COUNT is a synthetic
+	 * noinstr. Notable; while HV_MSR_TIME_REF_COUNT is a synthetic
 	 * register it doesn't need the GHCB path.
 	 */
-	return hv_raw_get_register(HV_REGISTER_TIME_REF_COUNT);
+	return hv_raw_get_register(HV_MSR_TIME_REF_COUNT);
 }
 
 /*
@@ -439,9 +439,9 @@ static void suspend_hv_clock_tsc(struct clocksource *arg)
 	union hv_reference_tsc_msr tsc_msr;
 
 	/* Disable the TSC page */
-	tsc_msr.as_uint64 = hv_get_register(HV_REGISTER_REFERENCE_TSC);
+	tsc_msr.as_uint64 = hv_get_register(HV_MSR_REFERENCE_TSC);
 	tsc_msr.enable = 0;
-	hv_set_register(HV_REGISTER_REFERENCE_TSC, tsc_msr.as_uint64);
+	hv_set_register(HV_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
 }
 
 
@@ -450,10 +450,10 @@ static void resume_hv_clock_tsc(struct clocksource *arg)
 	union hv_reference_tsc_msr tsc_msr;
 
 	/* Re-enable the TSC page */
-	tsc_msr.as_uint64 = hv_get_register(HV_REGISTER_REFERENCE_TSC);
+	tsc_msr.as_uint64 = hv_get_register(HV_MSR_REFERENCE_TSC);
 	tsc_msr.enable = 1;
 	tsc_msr.pfn = tsc_pfn;
-	hv_set_register(HV_REGISTER_REFERENCE_TSC, tsc_msr.as_uint64);
+	hv_set_register(HV_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
 }
 
 #ifdef HAVE_VDSO_CLOCKMODE_HVCLOCK
@@ -555,14 +555,14 @@ static void __init hv_init_tsc_clocksource(void)
 	 * thus TSC clocksource will work even without the real TSC page
 	 * mapped.
 	 */
-	tsc_msr.as_uint64 = hv_get_register(HV_REGISTER_REFERENCE_TSC);
+	tsc_msr.as_uint64 = hv_get_register(HV_MSR_REFERENCE_TSC);
 	if (hv_root_partition)
 		tsc_pfn = tsc_msr.pfn;
 	else
 		tsc_pfn = HVPFN_DOWN(virt_to_phys(tsc_page));
 	tsc_msr.enable = 1;
 	tsc_msr.pfn = tsc_pfn;
-	hv_set_register(HV_REGISTER_REFERENCE_TSC, tsc_msr.as_uint64);
+	hv_set_register(HV_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
 
 	clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
 
drivers/hv/hv.c +18 -18
index 51e5018ac9b2..5ea104c61fa7 100644
@@ -270,7 +270,7 @@ void hv_synic_enable_regs(unsigned int cpu)
 	union hv_synic_scontrol sctrl;
 
 	/* Setup the Synic's message page */
-	simp.as_uint64 = hv_get_register(HV_REGISTER_SIMP);
+	simp.as_uint64 = hv_get_register(HV_MSR_SIMP);
 	simp.simp_enabled = 1;
 
 	if (ms_hyperv.paravisor_present || hv_root_partition) {
@@ -286,10 +286,10 @@ void hv_synic_enable_regs(unsigned int cpu)
 			>> HV_HYP_PAGE_SHIFT;
 	}
 
-	hv_set_register(HV_REGISTER_SIMP, simp.as_uint64);
+	hv_set_register(HV_MSR_SIMP, simp.as_uint64);
 
 	/* Setup the Synic's event page */
-	siefp.as_uint64 = hv_get_register(HV_REGISTER_SIEFP);
+	siefp.as_uint64 = hv_get_register(HV_MSR_SIEFP);
 	siefp.siefp_enabled = 1;
 
 	if (ms_hyperv.paravisor_present || hv_root_partition) {
@@ -305,12 +305,12 @@ void hv_synic_enable_regs(unsigned int cpu)
 			>> HV_HYP_PAGE_SHIFT;
 	}
 
-	hv_set_register(HV_REGISTER_SIEFP, siefp.as_uint64);
+	hv_set_register(HV_MSR_SIEFP, siefp.as_uint64);
 
 	/* Setup the shared SINT. */
 	if (vmbus_irq != -1)
 		enable_percpu_irq(vmbus_irq, 0);
-	shared_sint.as_uint64 = hv_get_register(HV_REGISTER_SINT0 +
+	shared_sint.as_uint64 = hv_get_register(HV_MSR_SINT0 +
 					VMBUS_MESSAGE_SINT);
 
 	shared_sint.vector = vmbus_interrupt;
@@ -326,14 +326,14 @@ void hv_synic_enable_regs(unsigned int cpu)
 #else
 	shared_sint.auto_eoi = 0;
 #endif
-	hv_set_register(HV_REGISTER_SINT0 + VMBUS_MESSAGE_SINT,
-				shared_sint.as_uint64);
+	hv_set_register(HV_MSR_SINT0 + VMBUS_MESSAGE_SINT,
+			shared_sint.as_uint64);
 
 	/* Enable the global synic bit */
-	sctrl.as_uint64 = hv_get_register(HV_REGISTER_SCONTROL);
+	sctrl.as_uint64 = hv_get_register(HV_MSR_SCONTROL);
 	sctrl.enable = 1;
 
-	hv_set_register(HV_REGISTER_SCONTROL, sctrl.as_uint64);
+	hv_set_register(HV_MSR_SCONTROL, sctrl.as_uint64);
 }
 
 int hv_synic_init(unsigned int cpu)
@@ -357,17 +357,17 @@ void hv_synic_disable_regs(unsigned int cpu)
 	union hv_synic_siefp siefp;
 	union hv_synic_scontrol sctrl;
 
-	shared_sint.as_uint64 = hv_get_register(HV_REGISTER_SINT0 +
+	shared_sint.as_uint64 = hv_get_register(HV_MSR_SINT0 +
 					VMBUS_MESSAGE_SINT);
 
 	shared_sint.masked = 1;
 
 	/* Need to correctly cleanup in the case of SMP!!! */
 	/* Disable the interrupt */
-	hv_set_register(HV_REGISTER_SINT0 + VMBUS_MESSAGE_SINT,
-				shared_sint.as_uint64);
+	hv_set_register(HV_MSR_SINT0 + VMBUS_MESSAGE_SINT,
+			shared_sint.as_uint64);
 
-	simp.as_uint64 = hv_get_register(HV_REGISTER_SIMP);
+	simp.as_uint64 = hv_get_register(HV_MSR_SIMP);
 	/*
 	 * In Isolation VM, sim and sief pages are allocated by
 	 * paravisor. These pages also will be used by kdump
@@ -382,9 +382,9 @@ void hv_synic_disable_regs(unsigned int cpu)
 		simp.base_simp_gpa = 0;
 	}
 
-	hv_set_register(HV_REGISTER_SIMP, simp.as_uint64);
+	hv_set_register(HV_MSR_SIMP, simp.as_uint64);
 
-	siefp.as_uint64 = hv_get_register(HV_REGISTER_SIEFP);
+	siefp.as_uint64 = hv_get_register(HV_MSR_SIEFP);
 	siefp.siefp_enabled = 0;
 
 	if (ms_hyperv.paravisor_present || hv_root_partition) {
@@ -394,12 +394,12 @@ void hv_synic_disable_regs(unsigned int cpu)
 		siefp.base_siefp_gpa = 0;
 	}
 
-	hv_set_register(HV_REGISTER_SIEFP, siefp.as_uint64);
+	hv_set_register(HV_MSR_SIEFP, siefp.as_uint64);
 
 	/* Disable the global synic bit */
-	sctrl.as_uint64 = hv_get_register(HV_REGISTER_SCONTROL);
+	sctrl.as_uint64 = hv_get_register(HV_MSR_SCONTROL);
 	sctrl.enable = 0;
-	hv_set_register(HV_REGISTER_SCONTROL, sctrl.as_uint64);
+	hv_set_register(HV_MSR_SCONTROL, sctrl.as_uint64);
 
 	if (vmbus_irq != -1)
 		disable_percpu_irq(vmbus_irq);
drivers/hv/hv_common.c +9 -9
index 81aa8be3e0df..d5a46dacc03f 100644
@@ -228,17 +228,17 @@ static void hv_kmsg_dump(struct kmsg_dumper *dumper,
 	 * contain the size of the panic data in that page. Rest of the
 	 * registers are no-op when the NOTIFY_MSG flag is set.
 	 */
-	hv_set_register(HV_REGISTER_CRASH_P0, 0);
-	hv_set_register(HV_REGISTER_CRASH_P1, 0);
-	hv_set_register(HV_REGISTER_CRASH_P2, 0);
-	hv_set_register(HV_REGISTER_CRASH_P3, virt_to_phys(hv_panic_page));
-	hv_set_register(HV_REGISTER_CRASH_P4, bytes_written);
+	hv_set_register(HV_MSR_CRASH_P0, 0);
+	hv_set_register(HV_MSR_CRASH_P1, 0);
+	hv_set_register(HV_MSR_CRASH_P2, 0);
+	hv_set_register(HV_MSR_CRASH_P3, virt_to_phys(hv_panic_page));
+	hv_set_register(HV_MSR_CRASH_P4, bytes_written);
 
 	/*
 	 * Let Hyper-V know there is crash data available along with
 	 * the panic message.
 	 */
-	hv_set_register(HV_REGISTER_CRASH_CTL,
+	hv_set_register(HV_MSR_CRASH_CTL,
 			(HV_CRASH_CTL_CRASH_NOTIFY |
 			 HV_CRASH_CTL_CRASH_NOTIFY_MSG));
 }
@@ -311,7 +311,7 @@ int __init hv_common_init(void)
 		 * Register for panic kmsg callback only if the right
 		 * capability is supported by the hypervisor.
 		 */
-		hyperv_crash_ctl = hv_get_register(HV_REGISTER_CRASH_CTL);
+		hyperv_crash_ctl = hv_get_register(HV_MSR_CRASH_CTL);
 		if (hyperv_crash_ctl & HV_CRASH_CTL_CRASH_NOTIFY_MSG)
 			hv_kmsg_dump_register();
 
@@ -410,7 +410,7 @@ int hv_common_cpu_init(unsigned int cpu)
 		*inputarg = mem;
 	}
 
-	msr_vp_index = hv_get_register(HV_REGISTER_VP_INDEX);
+	msr_vp_index = hv_get_register(HV_MSR_VP_INDEX);
 
 	hv_vp_index[cpu] = msr_vp_index;
 
@@ -507,7 +507,7 @@ EXPORT_SYMBOL_GPL(hv_is_hibernation_supported);
  */
 static u64 __hv_read_ref_counter(void)
 {
-	return hv_get_register(HV_REGISTER_TIME_REF_COUNT);
+	return hv_get_register(HV_MSR_TIME_REF_COUNT);
 }
 
 u64 (*hv_read_reference_counter)(void) = __hv_read_ref_counter;
include/asm-generic/mshyperv.h +1 -1
index e7ecf03f675e..38fc48405d5f 100644
@@ -157,7 +157,7 @@ static inline void vmbus_signal_eom(struct hv_message *msg, u32 old_msg_type)
 		 * possibly deliver another msg from the
 		 * hypervisor
 		 */
-		hv_set_register(HV_REGISTER_EOM, 0);
+		hv_set_register(HV_MSR_EOM, 0);
 	}
 }
 
Signature · 1 line
2.25.1
maintainer Reviewed-by on v3 1/15
#2 lore raw

Re: [PATCH v3 01/15] hyperv-tlfs: Change shared HV_REGISTER_* defines to HV_MSR_*

Quoted 10 lines · Nuno Das Neves

In x86 hyperv-tlfs, HV_REGISTER_ prefix is used to indicate MSRs accessed via rdmsrl/wrmsrl. But in ARM64, HV_REGISTER_ instead indicates VP registers accessed via get/set vp registers hypercall.

This is due to HV_REGISTER_* names being used by hv_set/get_register, with the arch-specific version delegating to the appropriate mechanism.

The problem is, using prefix HV_REGISTER_ for MSRs will conflict with VP registers when they are introduced for x86 in future.

Quoted 8 lines

This patch solves the issue by:

1. Defining all the x86 MSRs with a consistent prefix: HV_X64_MSR_. This is so HV_REGISTER_ can be reserved for VP registers.

2. Change the non-arch-specific alias used by hv_set/get_register to HV_MSR_. This is also happens to be the same name HyperV uses for this purpose.

HyperV -> Hyper-V. I can fix this up when I apply this patch.

Quoted 2 lines
#3 lore raw

PATCH v3 2/15 mshyperv: Introduce hv_get_hypervisor_version function

In reply to Nuno Das Neves

x86_64 and arm64 implementations to get the hypervisor version information. Store these in hv_hypervisor_version_info structure to simplify parsing the fields.

Replace the existing parsing when printing the version numbers at boot.

---
 arch/arm64/hyperv/mshyperv.c      | 23 +++++++++++++-------
 arch/x86/kernel/cpu/mshyperv.c    | 36 +++++++++++++++++++------------
 include/asm-generic/hyperv-tlfs.h | 23 ++++++++++++++++++++
 include/asm-generic/mshyperv.h    |  2 ++
 4 files changed, 62 insertions(+), 22 deletions(-)
arch/arm64/hyperv/mshyperv.c +15 -8
index f1b8a04ee9f2..53bdd3bc81ac 100644
@@ -19,10 +19,19 @@
 
 static bool hyperv_initialized;
 
+int hv_get_hypervisor_version(union hv_hypervisor_version_info *info)
+{
+	hv_get_vpreg_128(HV_REGISTER_HYPERVISOR_VERSION,
+			 (struct hv_get_vp_registers_output *)info);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(hv_get_hypervisor_version);
+
 static int __init hyperv_init(void)
 {
-	struct hv_get_vp_registers_output	result;
-	u32	a, b, c, d;
+	struct hv_get_vp_registers_output result;
+	union hv_hypervisor_version_info version;
 	u64	guest_id;
 	int	ret;
 
@@ -55,13 +64,11 @@ static int __init hyperv_init(void)
 		ms_hyperv.misc_features);
 
 	/* Get information about the Hyper-V host version */
-	hv_get_vpreg_128(HV_REGISTER_HYPERVISOR_VERSION, &result);
-	a = result.as32.a;
-	b = result.as32.b;
-	c = result.as32.c;
-	d = result.as32.d;
+	hv_get_hypervisor_version(&version);
 	pr_info("Hyper-V: Host Build %d.%d.%d.%d-%d-%d\n",
-		b >> 16, b & 0xFFFF, a,	d & 0xFFFFFF, c, d >> 24);
+		version.major_version, version.minor_version,
+		version.build_number, version.service_number,
+		version.service_pack, version.service_branch);
 
 	ret = hv_common_init();
 	if (ret)
arch/x86/kernel/cpu/mshyperv.c +22 -14
index 7c82b96da11f..73fbcc003d68 100644
@@ -347,13 +347,26 @@ static void __init reduced_hw_init(void)
 	x86_init.irqs.pre_vector_init	= x86_init_noop;
 }
 
+int hv_get_hypervisor_version(union hv_hypervisor_version_info *info)
+{
+	unsigned int hv_max_functions;
+
+	hv_max_functions = cpuid_eax(HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS);
+	if (hv_max_functions < HYPERV_CPUID_VERSION) {
+		pr_err("%s: Could not detect Hyper-V version\n", __func__);
+		return -ENODEV;
+	}
+
+	cpuid(HYPERV_CPUID_VERSION, &info->eax, &info->ebx, &info->ecx, &info->edx);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(hv_get_hypervisor_version);
+
 static void __init ms_hyperv_init_platform(void)
 {
 	int hv_max_functions_eax;
-	int hv_host_info_eax;
-	int hv_host_info_ebx;
-	int hv_host_info_ecx;
-	int hv_host_info_edx;
+	union hv_hypervisor_version_info version;
 
 #ifdef CONFIG_PARAVIRT
 	pv_info.name = "Hyper-V";
@@ -407,16 +420,11 @@ static void __init ms_hyperv_init_platform(void)
 	/*
 	 * Extract host information.
 	 */
-	if (hv_max_functions_eax >= HYPERV_CPUID_VERSION) {
-		hv_host_info_eax = cpuid_eax(HYPERV_CPUID_VERSION);
-		hv_host_info_ebx = cpuid_ebx(HYPERV_CPUID_VERSION);
-		hv_host_info_ecx = cpuid_ecx(HYPERV_CPUID_VERSION);
-		hv_host_info_edx = cpuid_edx(HYPERV_CPUID_VERSION);
-
-		pr_info("Hyper-V: Host Build %d.%d.%d.%d-%d-%d\n",
-			hv_host_info_ebx >> 16, hv_host_info_ebx & 0xFFFF,
-			hv_host_info_eax, hv_host_info_edx & 0xFFFFFF,
-			hv_host_info_ecx, hv_host_info_edx >> 24);
+	if (hv_get_hypervisor_version(&version) == 0) {
+		pr_info("Hyper-V Host Build:%d-%d.%d-%d-%d.%d\n",
+			version.build_number, version.major_version,
+			version.minor_version, version.service_pack,
+			version.service_branch, version.service_number);
 	}
 
 	if (ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS &&
include/asm-generic/hyperv-tlfs.h +23 -0
index fdac4a1714ec..f63b3704d89e 100644
@@ -787,6 +787,29 @@ struct hv_input_unmap_device_interrupt {
 #define HV_SOURCE_SHADOW_NONE               0x0
 #define HV_SOURCE_SHADOW_BRIDGE_BUS_RANGE   0x1
 
+/*
+ * Version info reported by hypervisor
+ */
+union hv_hypervisor_version_info {
+	struct {
+		u32 build_number;
+
+		u32 minor_version : 16;
+		u32 major_version : 16;
+
+		u32 service_pack;
+
+		u32 service_number : 24;
+		u32 service_branch : 8;
+	};
+	struct {
+		u32 eax;
+		u32 ebx;
+		u32 ecx;
+		u32 edx;
+	};
+};
+
 /*
  * The whole argument should fit in a page to be able to pass to the hypervisor
  * in one hypercall.
include/asm-generic/mshyperv.h +2 -0
index 38fc48405d5f..5b3512e5a72e 100644
@@ -161,6 +161,8 @@ static inline void vmbus_signal_eom(struct hv_message *msg, u32 old_msg_type)
 	}
 }
 
+int hv_get_hypervisor_version(union hv_hypervisor_version_info *info);
+
 void hv_setup_vmbus_handler(void (*handler)(void));
 void hv_remove_vmbus_handler(void);
 void hv_setup_stimer0_handler(void (*handler)(void));
Signature · 1 line
2.25.1
maintainer Acked-by on v3 2/15
#4 lore raw

Re: [PATCH v3 02/15] mshyperv: Introduce hv_get_hypervisor_version function

Quoted 7 lines · Nuno Das Neves

x86_64 and arm64 implementations to get the hypervisor version information. Store these in hv_hypervisor_version_info structure to simplify parsing the fields.

Replace the existing parsing when printing the version numbers at boot.

#5 lore raw

PATCH v3 3/15 mshyperv: Introduce numa_node_to_proximity_domain_info

In reply to Nuno Das Neves

Factor out logic for converting numa node to proximity domain info into a helper function, and export it.

Change hv_proximity_domain_info to a struct to simplify the code.

---
 arch/x86/hyperv/hv_proc.c         |  8 ++------
 drivers/acpi/numa/srat.c          |  1 +
 include/asm-generic/hyperv-tlfs.h | 14 +++++---------
 include/asm-generic/mshyperv.h    | 15 +++++++++++++++
 4 files changed, 23 insertions(+), 15 deletions(-)
arch/x86/hyperv/hv_proc.c +2 -6
index 68a0843d4750..5ba5ca1b2089 100644
@@ -121,7 +121,6 @@ int hv_call_add_logical_proc(int node, u32 lp_index, u32 apic_id)
 	u64 status;
 	unsigned long flags;
 	int ret = HV_STATUS_SUCCESS;
-	int pxm = node_to_pxm(node);
 
 	/*
 	 * When adding a logical processor, the hypervisor may return
@@ -137,11 +136,8 @@ int hv_call_add_logical_proc(int node, u32 lp_index, u32 apic_id)
 
 		input->lp_index = lp_index;
 		input->apic_id = apic_id;
-		input->flags = 0;
-		input->proximity_domain_info.domain_id = pxm;
-		input->proximity_domain_info.flags.reserved = 0;
-		input->proximity_domain_info.flags.proximity_info_valid = 1;
-		input->proximity_domain_info.flags.proximity_preferred = 1;
+		input->proximity_domain_info =
+			numa_node_to_proximity_domain_info(node);
 		status = hv_do_hypercall(HVCALL_ADD_LOGICAL_PROCESSOR,
 					 input, output);
 		local_irq_restore(flags);
drivers/acpi/numa/srat.c +1 -0
index 1f4fc5f8a819..0cf9f0574495 100644
@@ -48,6 +48,7 @@ int node_to_pxm(int node)
 		return PXM_INVAL;
 	return node_to_pxm_map[node];
 }
+EXPORT_SYMBOL(node_to_pxm);
 
 static void __acpi_map_pxm_to_node(int pxm, int node)
 {
include/asm-generic/hyperv-tlfs.h +5 -9
index f63b3704d89e..a6dffb346bf2 100644
@@ -512,13 +512,9 @@ struct hv_proximity_domain_flags {
 	u32 proximity_info_valid : 1;
 } __packed;
 
-/* Not a union in windows but useful for zeroing */
-union hv_proximity_domain_info {
-	struct {
-		u32 domain_id;
-		struct hv_proximity_domain_flags flags;
-	};
-	u64 as_uint64;
+struct hv_proximity_domain_info {
+	u32 domain_id;
+	struct hv_proximity_domain_flags flags;
 } __packed;
 
 struct hv_lp_startup_status {
@@ -535,7 +531,7 @@ struct hv_lp_startup_status {
 struct hv_add_logical_processor_in {
 	u32 lp_index;
 	u32 apic_id;
-	union hv_proximity_domain_info proximity_domain_info;
+	struct hv_proximity_domain_info proximity_domain_info;
 	u64 flags;
 } __packed;
 
@@ -560,7 +556,7 @@ struct hv_create_vp {
 	u8 padding[3];
 	u8 subnode_type;
 	u64 subnode_id;
-	union hv_proximity_domain_info proximity_domain_info;
+	struct hv_proximity_domain_info proximity_domain_info;
 	u64 flags;
 } __packed;
 
include/asm-generic/mshyperv.h +15 -0
index 5b3512e5a72e..e16557523b6e 100644
@@ -21,6 +21,7 @@
 #include <linux/types.h>
 #include <linux/atomic.h>
 #include <linux/bitops.h>
+#include <acpi/acpi_numa.h>
 #include <linux/cpumask.h>
 #include <linux/nmi.h>
 #include <asm/ptrace.h>
@@ -28,6 +29,20 @@
 
 #define VTPM_BASE_ADDRESS 0xfed40000
 
+static inline struct hv_proximity_domain_info
+numa_node_to_proximity_domain_info(int node)
+{
+	struct hv_proximity_domain_info proximity_domain_info = {};
+
+	if (node != NUMA_NO_NODE) {
+		proximity_domain_info.domain_id = node_to_pxm(node);
+		proximity_domain_info.flags.proximity_info_valid = 1;
+		proximity_domain_info.flags.proximity_preferred = 1;
+	}
+
+	return proximity_domain_info;
+}
+
 struct ms_hyperv_info {
 	u32 features;
 	u32 priv_high;
Signature · 1 line
2.25.1
#6 lore raw

PATCH v3 4/15 asm-generic/mshyperv: Introduce hv_recommend_using_aeoi()

In reply to Nuno Das Neves

Factor out logic for determining if we should set the auto eoi flag in SINT register.

---
 drivers/hv/hv.c                | 12 +-----------
 include/asm-generic/mshyperv.h | 13 +++++++++++++
 2 files changed, 14 insertions(+), 11 deletions(-)
drivers/hv/hv.c +1 -11
index 5ea104c61fa7..d7869205dcbe 100644
@@ -315,17 +315,7 @@ void hv_synic_enable_regs(unsigned int cpu)
 
 	shared_sint.vector = vmbus_interrupt;
 	shared_sint.masked = false;
-
-	/*
-	 * On architectures where Hyper-V doesn't support AEOI (e.g., ARM64),
-	 * it doesn't provide a recommendation flag and AEOI must be disabled.
-	 */
-#ifdef HV_DEPRECATING_AEOI_RECOMMENDED
-	shared_sint.auto_eoi =
-			!(ms_hyperv.hints & HV_DEPRECATING_AEOI_RECOMMENDED);
-#else
-	shared_sint.auto_eoi = 0;
-#endif
+	shared_sint.auto_eoi = hv_recommend_using_aeoi();
 	hv_set_register(HV_MSR_SINT0 + VMBUS_MESSAGE_SINT,
 			shared_sint.as_uint64);
 
include/asm-generic/mshyperv.h +13 -0
index e16557523b6e..c9e166d73fca 100644
@@ -82,6 +82,19 @@ extern u64 hv_do_fast_hypercall8(u16 control, u64 input8);
 bool hv_isolation_type_snp(void);
 bool hv_isolation_type_tdx(void);
 
+/*
+ * On architectures where Hyper-V doesn't support AEOI (e.g., ARM64),
+ * it doesn't provide a recommendation flag and AEOI must be disabled.
+ */
+static inline bool hv_recommend_using_aeoi(void)
+{
+#ifdef HV_DEPRECATING_AEOI_RECOMMENDED
+	return !(ms_hyperv.hints & HV_DEPRECATING_AEOI_RECOMMENDED);
+#else
+	return false;
+#endif
+}
+
 /* Helper functions that provide a consistent pattern for checking Hyper-V hypercall status. */
 static inline int hv_result(u64 status)
 {
Signature · 1 line
2.25.1
#7 lore raw

PATCH v3 5/15 hyperv: Move hv_connection_id to hyperv-tlfs

In reply to Nuno Das Neves

The definition conflicts with one added in hvgdk.h as part of the mshv driver so must be moved to hyperv-tlfs.h.

This structure should be in hyperv-tlfs.h anyway, since it is part of the TLFS document.

---
 include/asm-generic/hyperv-tlfs.h | 9 +++++++++
 include/linux/hyperv.h            | 9 ---------
 2 files changed, 9 insertions(+), 9 deletions(-)
include/asm-generic/hyperv-tlfs.h +9 -0
index a6dffb346bf2..1316584983c1 100644
@@ -842,4 +842,13 @@ struct hv_mmio_write_input {
 	u8 data[HV_HYPERCALL_MMIO_MAX_DATA_LENGTH];
 } __packed;
 
+/* Define connection identifier type. */
+union hv_connection_id {
+	u32 asu32;
+	struct {
+		u32 id:24;
+		u32 reserved:8;
+	} u;
+};
+
 #endif
include/linux/hyperv.h +0 -9
index a922d4526de4..2d8568708d90 100644
@@ -748,15 +748,6 @@ struct vmbus_close_msg {
 	struct vmbus_channel_close_channel msg;
 };
 
-/* Define connection identifier type. */
-union hv_connection_id {
-	u32 asu32;
-	struct {
-		u32 id:24;
-		u32 reserved:8;
-	} u;
-};
-
 enum vmbus_device_type {
 	HV_IDE = 0,
 	HV_SCSI,
Signature · 1 line
2.25.1
#8 lore raw

PATCH v3 6/15 hyperv-tlfs: Introduce hv_status_to_string and hv_status_to_errno

In reply to Nuno Das Neves

hv_status_to_errno translates hyperv statuses to linux error codes. This is useful for returning something linux-friendly from a hypercall helper function.

hv_status_to_string improves clarity of error messages.

---
 arch/x86/hyperv/hv_init.c         |  2 +-
 arch/x86/hyperv/hv_proc.c         |  6 ++--
 include/asm-generic/hyperv-tlfs.h | 47 ++++++++++++++++++++++---------
 include/asm-generic/mshyperv.h    | 33 ++++++++++++++++++++++
 4 files changed, 71 insertions(+), 17 deletions(-)
arch/x86/hyperv/hv_init.c +1 -1
index 2b0124394e24..5b679bfbc7f7 100644
@@ -386,7 +386,7 @@ static void __init hv_get_partition_id(void)
 	status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page);
 	if (!hv_result_success(status)) {
 		/* No point in proceeding if this failed */
-		pr_err("Failed to get partition ID: %lld\n", status);
+		pr_err("Failed to get partition ID: %s\n", hv_status_to_string(status));
 		BUG();
 	}
 	hv_current_partition_id = output_page->partition_id;
arch/x86/hyperv/hv_proc.c +3 -3
index 5ba5ca1b2089..ed80da64649e 100644
@@ -144,9 +144,9 @@ int hv_call_add_logical_proc(int node, u32 lp_index, u32 apic_id)
 
 		if (hv_result(status) != HV_STATUS_INSUFFICIENT_MEMORY) {
 			if (!hv_result_success(status)) {
-				pr_err("%s: cpu %u apic ID %u, %lld\n", __func__,
-				       lp_index, apic_id, status);
-				ret = hv_result(status);
+				pr_err("%s: cpu %u apic ID %u, %s\n", __func__,
+				       lp_index, apic_id, hv_status_to_string(status));
+				ret = hv_status_to_errno(status);
 			}
 			break;
 		}
include/asm-generic/hyperv-tlfs.h +34 -13

53 lines not on this page · raw message

include/asm-generic/mshyperv.h +33 -0
index c9e166d73fca..81bacd4bce66 100644
@@ -305,6 +305,39 @@ static inline int cpumask_to_vpset_skip(struct hv_vpset *vpset,
 	return __cpumask_to_vpset(vpset, cpus, func);
 }
 
+
+static inline int hv_status_to_errno(u64 hv_status)
+{
+	switch (hv_result(hv_status)) {
+	case HV_STATUS_SUCCESS:
+		return 0;
+	case HV_STATUS_INVALID_PARAMETER:
+	case HV_STATUS_UNKNOWN_PROPERTY:
+	case HV_STATUS_PROPERTY_VALUE_OUT_OF_RANGE:
+	case HV_STATUS_INVALID_VP_INDEX:
+	case HV_STATUS_INVALID_REGISTER_VALUE:
+	case HV_STATUS_INVALID_LP_INDEX:
+	case HV_STATUS_PROCESSOR_FEATURE_NOT_SUPPORTED:
+		return -EINVAL;
+	case HV_STATUS_ACCESS_DENIED:
+	case HV_STATUS_OPERATION_DENIED:
+		return -EACCES;
+	case HV_STATUS_NOT_ACKNOWLEDGED:
+	case HV_STATUS_INVALID_VP_STATE:
+	case HV_STATUS_INVALID_PARTITION_STATE:
+		return -EBADFD;
+	}
+	return -ENOTRECOVERABLE;
+}
+
+static inline const char *hv_status_to_string(u64 hv_status)
+{
+	switch (hv_result(hv_status)) {
+	__HV_STATUS_DEF(__HV_MAKE_HV_STATUS_CASE)
+	default : return "Unknown";
+	}
+}
+
 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die);
 bool hv_is_hyperv_initialized(void);
 bool hv_is_hibernation_supported(void);
Signature · 1 line
2.25.1
#9 lore raw

PATCH v3 7/15 Drivers: hv: Move hv_call_deposit_pages and hv_call_create_vp to common code

In reply to Nuno Das Neves

These hypercalls are not arch-specific. Move them to common code.

---
 arch/x86/hyperv/hv_proc.c       | 152 --------------------------------
 arch/x86/include/asm/mshyperv.h |   1 -
 drivers/hv/hv_common.c          | 147 ++++++++++++++++++++++++++++++
 include/asm-generic/mshyperv.h  |   2 +
 4 files changed, 149 insertions(+), 153 deletions(-)
arch/x86/hyperv/hv_proc.c +0 -152
index ed80da64649e..0a35cb865427 100644
@@ -3,7 +3,6 @@
 #include <linux/vmalloc.h>
 #include <linux/mm.h>
 #include <linux/clockchips.h>
-#include <linux/acpi.h>
 #include <linux/hyperv.h>
 #include <linux/slab.h>
 #include <linux/cpuhotplug.h>
@@ -14,106 +13,6 @@
 
 #include <asm/trace/hyperv.h>
 
-/*
- * See struct hv_deposit_memory. The first u64 is partition ID, the rest
- * are GPAs.
- */
-#define HV_DEPOSIT_MAX (HV_HYP_PAGE_SIZE / sizeof(u64) - 1)
-
-/* Deposits exact number of pages. Must be called with interrupts enabled.  */
-int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
-{
-	struct page **pages, *page;
-	int *counts;
-	int num_allocations;
-	int i, j, page_count;
-	int order;
-	u64 status;
-	int ret;
-	u64 base_pfn;
-	struct hv_deposit_memory *input_page;
-	unsigned long flags;
-
-	if (num_pages > HV_DEPOSIT_MAX)
-		return -E2BIG;
-	if (!num_pages)
-		return 0;
-
-	/* One buffer for page pointers and counts */
-	page = alloc_page(GFP_KERNEL);
-	if (!page)
-		return -ENOMEM;
-	pages = page_address(page);
-
-	counts = kcalloc(HV_DEPOSIT_MAX, sizeof(int), GFP_KERNEL);
-	if (!counts) {
-		free_page((unsigned long)pages);
-		return -ENOMEM;
-	}
-
-	/* Allocate all the pages before disabling interrupts */
-	i = 0;
-
-	while (num_pages) {
-		/* Find highest order we can actually allocate */
-		order = 31 - __builtin_clz(num_pages);
-
-		while (1) {
-			pages[i] = alloc_pages_node(node, GFP_KERNEL, order);
-			if (pages[i])
-				break;
-			if (!order) {
-				ret = -ENOMEM;
-				num_allocations = i;
-				goto err_free_allocations;
-			}
-			--order;
-		}
-
-		split_page(pages[i], order);
-		counts[i] = 1 << order;
-		num_pages -= counts[i];
-		i++;
-	}
-	num_allocations = i;
-
-	local_irq_save(flags);
-
-	input_page = *this_cpu_ptr(hyperv_pcpu_input_arg);
-
-	input_page->partition_id = partition_id;
-
-	/* Populate gpa_page_list - these will fit on the input page */
-	for (i = 0, page_count = 0; i < num_allocations; ++i) {
-		base_pfn = page_to_pfn(pages[i]);
-		for (j = 0; j < counts[i]; ++j, ++page_count)
-			input_page->gpa_page_list[page_count] = base_pfn + j;
-	}
-	status = hv_do_rep_hypercall(HVCALL_DEPOSIT_MEMORY,
-				     page_count, 0, input_page, NULL);
-	local_irq_restore(flags);
-	if (!hv_result_success(status)) {
-		pr_err("Failed to deposit pages: %lld\n", status);
-		ret = hv_result(status);
-		goto err_free_allocations;
-	}
-
-	ret = 0;
-	goto free_buf;
-
-err_free_allocations:
-	for (i = 0; i < num_allocations; ++i) {
-		base_pfn = page_to_pfn(pages[i]);
-		for (j = 0; j < counts[i]; ++j)
-			__free_page(pfn_to_page(base_pfn + j));
-	}
-
-free_buf:
-	free_page((unsigned long)pages);
-	kfree(counts);
-	return ret;
-}
-
 int hv_call_add_logical_proc(int node, u32 lp_index, u32 apic_id)
 {
 	struct hv_add_logical_processor_in *input;
@@ -156,54 +55,3 @@ int hv_call_add_logical_proc(int node, u32 lp_index, u32 apic_id)
 	return ret;
 }
 
-int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags)
-{
-	struct hv_create_vp *input;
-	u64 status;
-	unsigned long irq_flags;
-	int ret = HV_STATUS_SUCCESS;
-	int pxm = node_to_pxm(node);
-
-	/* Root VPs don't seem to need pages deposited */
-	if (partition_id != hv_current_partition_id) {
-		/* The value 90 is empirically determined. It may change. */
-		ret = hv_call_deposit_pages(node, partition_id, 90);
-		if (ret)
-			return ret;
-	}
-
-	do {
-		local_irq_save(irq_flags);
-
-		input = *this_cpu_ptr(hyperv_pcpu_input_arg);
-
-		input->partition_id = partition_id;
-		input->vp_index = vp_index;
-		input->flags = flags;
-		input->subnode_type = HvSubnodeAny;
-		if (node != NUMA_NO_NODE) {
-			input->proximity_domain_info.domain_id = pxm;
-			input->proximity_domain_info.flags.reserved = 0;
-			input->proximity_domain_info.flags.proximity_info_valid = 1;
-			input->proximity_domain_info.flags.proximity_preferred = 1;
-		} else {
-			input->proximity_domain_info.as_uint64 = 0;
-		}
-		status = hv_do_hypercall(HVCALL_CREATE_VP, input, NULL);
-		local_irq_restore(irq_flags);
-
-		if (hv_result(status) != HV_STATUS_INSUFFICIENT_MEMORY) {
-			if (!hv_result_success(status)) {
-				pr_err("%s: vcpu %u, lp %u, %lld\n", __func__,
-				       vp_index, flags, status);
-				ret = hv_result(status);
-			}
-			break;
-		}
-		ret = hv_call_deposit_pages(node, partition_id, 1);
-
-	} while (!ret);
-
-	return ret;
-}
-
arch/x86/include/asm/mshyperv.h +0 -1
index a1d098505b60..6e96d3420a9d 100644
@@ -62,7 +62,6 @@ u64 hv_tdx_hypercall(u64 control, u64 param1, u64 param2);
 
 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
 int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
-int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
 
 /*
  * If the hypercall involves no input or output parameters, the hypervisor
drivers/hv/hv_common.c +147 -0

150 lines not on this page · raw message

include/asm-generic/mshyperv.h +2 -0
index 81bacd4bce66..5be3e3402e37 100644
@@ -349,6 +349,8 @@ u64 hv_tdx_hypercall(u64 control, u64 param1, u64 param2);
 void hyperv_cleanup(void);
 bool hv_query_ext_cap(u64 cap_query);
 void hv_setup_dma_ops(struct device *dev, bool coherent);
+int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
+int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
 #else /* CONFIG_HYPERV */
 static inline bool hv_is_hyperv_initialized(void) { return false; }
 static inline bool hv_is_hibernation_supported(void) { return false; }
Signature · 1 line
2.25.1
#10 lore raw

PATCH v3 8/15 Drivers: hv: Introduce per-cpu event ring tail

In reply to Nuno Das Neves

Add a pointer hv_synic_eventring_tail to track the tail pointer for the SynIC event ring buffer for each SINT.

This will be used by the mshv driver, but must be tracked independently since the driver module could be removed and re-inserted.

---
 drivers/hv/hv_common.c         | 28 ++++++++++++++++++++++++++--
 include/asm-generic/mshyperv.h |  2 ++
 2 files changed, 28 insertions(+), 2 deletions(-)
drivers/hv/hv_common.c +26 -2
index 8c6ab60d5387..d346ce6adf00 100644
@@ -62,6 +62,16 @@ static void hv_kmsg_dump_unregister(void);
 
 static struct ctl_table_header *hv_ctl_table_hdr;
 
+/*
+ * Per-cpu array holding the tail pointer for the SynIC event ring buffer
+ * for each SINT.
+ *
+ * We cannot maintain this in mshv driver because the tail pointer should
+ * persist even if the mshv driver is unloaded.
+ */
+u8 __percpu **hv_synic_eventring_tail;
+EXPORT_SYMBOL_GPL(hv_synic_eventring_tail);
+
 /*
  * Hyper-V specific initialization and shutdown code that is
  * common across all architectures.  Called from architecture
@@ -84,6 +94,9 @@ void __init hv_common_free(void)
 
 	free_percpu(hyperv_pcpu_input_arg);
 	hyperv_pcpu_input_arg = NULL;
+
+	free_percpu(hv_synic_eventring_tail);
+	hv_synic_eventring_tail = NULL;
 }
 
 /*
@@ -333,6 +346,8 @@ int __init hv_common_init(void)
 	if (hv_root_partition) {
 		hyperv_pcpu_output_arg = alloc_percpu(void *);
 		BUG_ON(!hyperv_pcpu_output_arg);
+		hv_synic_eventring_tail = alloc_percpu(u8 *);
+		BUG_ON(hv_synic_eventring_tail == NULL);
 	}
 
 	hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
@@ -357,6 +372,7 @@ int __init hv_common_init(void)
 int hv_common_cpu_init(unsigned int cpu)
 {
 	void **inputarg, **outputarg;
+	u8 **synic_eventring_tail;
 	u64 msr_vp_index;
 	gfp_t flags;
 	int pgcount = hv_root_partition ? 2 : 1;
@@ -369,8 +385,8 @@ int hv_common_cpu_init(unsigned int cpu)
 	inputarg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
 
 	/*
-	 * hyperv_pcpu_input_arg and hyperv_pcpu_output_arg memory is already
-	 * allocated if this CPU was previously online and then taken offline
+	 * The per-cpu memory is already allocated if this CPU was previously
+	 * online and then taken offline
 	 */
 	if (!*inputarg) {
 		mem = kmalloc(pgcount * HV_HYP_PAGE_SIZE, flags);
@@ -380,6 +396,14 @@ int hv_common_cpu_init(unsigned int cpu)
 		if (hv_root_partition) {
 			outputarg = (void **)this_cpu_ptr(hyperv_pcpu_output_arg);
 			*outputarg = (char *)mem + HV_HYP_PAGE_SIZE;
+			synic_eventring_tail = (u8 **)this_cpu_ptr(hv_synic_eventring_tail);
+			*synic_eventring_tail = kcalloc(HV_SYNIC_SINT_COUNT, sizeof(u8),
+							flags);
+
+			if (unlikely(!*synic_eventring_tail)) {
+				kfree(mem);
+				return -ENOMEM;
+			}
 		}
 
 		if (!ms_hyperv.paravisor_present &&
include/asm-generic/mshyperv.h +2 -0
index 5be3e3402e37..5a12e5754e97 100644
@@ -77,6 +77,8 @@ extern bool hv_nested;
 extern void * __percpu *hyperv_pcpu_input_arg;
 extern void * __percpu *hyperv_pcpu_output_arg;
 
+extern u8 __percpu **hv_synic_eventring_tail;
+
 extern u64 hv_do_hypercall(u64 control, void *inputaddr, void *outputaddr);
 extern u64 hv_do_fast_hypercall8(u16 control, u64 input8);
 bool hv_isolation_type_snp(void);
Signature · 1 line
2.25.1
#11 lore raw

PATCH v3 9/15 Drivers: hv: Introduce hv_output_arg_exists in hv_common.c

In reply to Nuno Das Neves

This is a more flexible approach for determining whether to allocate the output page.

---
 drivers/hv/hv_common.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
drivers/hv/hv_common.c +17 -4
index d346ce6adf00..89f5c1fb2a35 100644
@@ -58,6 +58,14 @@ EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
 void * __percpu *hyperv_pcpu_output_arg;
 EXPORT_SYMBOL_GPL(hyperv_pcpu_output_arg);
 
+/*
+ * Determine whether output arg is needed
+ */
+static inline bool hv_output_arg_exists(void)
+{
+	return hv_root_partition ? true : false;
+}
+
 static void hv_kmsg_dump_unregister(void);
 
 static struct ctl_table_header *hv_ctl_table_hdr;
@@ -342,10 +350,12 @@ int __init hv_common_init(void)
 	hyperv_pcpu_input_arg = alloc_percpu(void  *);
 	BUG_ON(!hyperv_pcpu_input_arg);
 
-	/* Allocate the per-CPU state for output arg for root */
-	if (hv_root_partition) {
+	if (hv_output_arg_exists()) {
 		hyperv_pcpu_output_arg = alloc_percpu(void *);
 		BUG_ON(!hyperv_pcpu_output_arg);
+	}
+
+	if (hv_root_partition) {
 		hv_synic_eventring_tail = alloc_percpu(u8 *);
 		BUG_ON(hv_synic_eventring_tail == NULL);
 	}
@@ -375,7 +385,7 @@ int hv_common_cpu_init(unsigned int cpu)
 	u8 **synic_eventring_tail;
 	u64 msr_vp_index;
 	gfp_t flags;
-	int pgcount = hv_root_partition ? 2 : 1;
+	int pgcount = hv_output_arg_exists() ? 2 : 1;
 	void *mem;
 	int ret;
 
@@ -393,9 +403,12 @@ int hv_common_cpu_init(unsigned int cpu)
 		if (!mem)
 			return -ENOMEM;
 
-		if (hv_root_partition) {
+		if (hv_output_arg_exists()) {
 			outputarg = (void **)this_cpu_ptr(hyperv_pcpu_output_arg);
 			*outputarg = (char *)mem + HV_HYP_PAGE_SIZE;
+		}
+
+		if (hv_root_partition) {
 			synic_eventring_tail = (u8 **)this_cpu_ptr(hv_synic_eventring_tail);
 			*synic_eventring_tail = kcalloc(HV_SYNIC_SINT_COUNT, sizeof(u8),
 							flags);
Signature · 1 line
2.25.1
#12 lore raw

PATCH v3 10/15 x86: hyperv: Add mshv_handler irq handler and setup function

In reply to Nuno Das Neves

This will handle SYNIC interrupts such as intercepts, doorbells, and scheduling messages intended for the mshv driver.

---
 arch/x86/kernel/cpu/mshyperv.c | 9 +++++++++
 drivers/hv/hv_common.c         | 5 +++++
 include/asm-generic/mshyperv.h | 2 ++
 3 files changed, 16 insertions(+)
arch/x86/kernel/cpu/mshyperv.c +9 -0
index 73fbcc003d68..b8c3177d6389 100644
@@ -110,6 +110,7 @@ void hv_set_register(unsigned int reg, u64 value)
 }
 EXPORT_SYMBOL_GPL(hv_set_register);
 
+static void (*mshv_handler)(void);
 static void (*vmbus_handler)(void);
 static void (*hv_stimer0_handler)(void);
 static void (*hv_kexec_handler)(void);
@@ -120,6 +121,9 @@ DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_callback)
 	struct pt_regs *old_regs = set_irq_regs(regs);
 
 	inc_irq_stat(irq_hv_callback_count);
+	if (mshv_handler)
+		mshv_handler();
+
 	if (vmbus_handler)
 		vmbus_handler();
 
@@ -129,6 +133,11 @@ DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_callback)
 	set_irq_regs(old_regs);
 }
 
+void hv_setup_mshv_irq(void (*handler)(void))
+{
+	mshv_handler = handler;
+}
+
 void hv_setup_vmbus_handler(void (*handler)(void))
 {
 	vmbus_handler = handler;
drivers/hv/hv_common.c +5 -0
index 89f5c1fb2a35..092b0c727db9 100644
@@ -585,6 +585,11 @@ void __weak hv_remove_vmbus_handler(void)
 }
 EXPORT_SYMBOL_GPL(hv_remove_vmbus_handler);
 
+void __weak hv_setup_mshv_irq(void (*handler)(void))
+{
+}
+EXPORT_SYMBOL_GPL(hv_setup_mshv_irq);
+
 void __weak hv_setup_kexec_handler(void (*handler)(void))
 {
 }
include/asm-generic/mshyperv.h +2 -0
index 5a12e5754e97..0a9e8ff31b73 100644
@@ -198,6 +198,8 @@ void hv_remove_vmbus_handler(void);
 void hv_setup_stimer0_handler(void (*handler)(void));
 void hv_remove_stimer0_handler(void);
 
+void hv_setup_mshv_irq(void (*handler)(void));
+
 void hv_setup_kexec_handler(void (*handler)(void));
 void hv_remove_kexec_handler(void);
 void hv_setup_crash_handler(void (*handler)(struct pt_regs *regs));
Signature · 1 line
2.25.1
#13 lore raw

PATCH v3 11/15 Drivers: hv: export vmbus_isr, hv_context and hv_post_message

In reply to Nuno Das Neves

These will be used by the mshv_vtl driver.

---
 drivers/hv/hv.c           | 2 ++
 drivers/hv/hyperv_vmbus.h | 1 +
 drivers/hv/vmbus_drv.c    | 3 ++-
 3 files changed, 5 insertions(+), 1 deletion(-)
drivers/hv/hv.c +2 -0
index d7869205dcbe..c60a4fb55f3c 100644
@@ -25,6 +25,7 @@
 
 /* The one and only */
 struct hv_context hv_context;
+EXPORT_SYMBOL_GPL(hv_context);
 
 /*
  * hv_init - Main initialization routine.
@@ -93,6 +94,7 @@ int hv_post_message(union hv_connection_id connection_id,
 
 	return hv_result(status);
 }
+EXPORT_SYMBOL_GPL(hv_post_message);
 
 int hv_synic_alloc(void)
 {
drivers/hv/hyperv_vmbus.h +1 -0
index f6b1e710f805..09792eb4ffed 100644
@@ -32,6 +32,7 @@
  */
 #define HV_UTIL_NEGO_TIMEOUT 55
 
+void vmbus_isr(void);
 
 /* Definitions for the monitored notification facility */
 union hv_monitor_trigger_group {
drivers/hv/vmbus_drv.c +2 -1
index edbb38f6956b..6d27597af8bf 100644
@@ -1286,7 +1286,7 @@ static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)
 	}
 }
 
-static void vmbus_isr(void)
+void vmbus_isr(void)
 {
 	struct hv_per_cpu_context *hv_cpu
 		= this_cpu_ptr(hv_context.cpu_context);
@@ -1309,6 +1309,7 @@ static void vmbus_isr(void)
 
 	add_interrupt_randomness(vmbus_interrupt);
 }
+EXPORT_SYMBOL_GPL(vmbus_isr);
 
 static irqreturn_t vmbus_percpu_isr(int irq, void *dev_id)
 {
Signature · 1 line
2.25.1
#14 lore raw

PATCH v3 12/15 Documentation: Reserve ioctl number for mshv driver

In reply to Nuno Das Neves
---
 Documentation/userspace-api/ioctl/ioctl-number.rst | 2 ++
 1 file changed, 2 insertions(+)
Documentation/userspace-api/ioctl/ioctl-number.rst +2 -0
index 4ea5b837399a..71e6d23070ca 100644
@@ -355,6 +355,8 @@ Code  Seq#    Include File                                           Comments
 0xB6  all    linux/fpga-dfl.h
 0xB7  all    uapi/linux/remoteproc_cdev.h                            <mailto:[email protected]>
 0xB7  all    uapi/linux/nsfs.h                                       <mailto:Andrei Vagin <[email protected]>>
+0xB8  all    uapi/linux/mshv.h                                       Microsoft Hypervisor VM management APIs
+                                                                     <mailto:[email protected]>
 0xC0  00-0F  linux/usb/iowarrior.h
 0xCA  00-0F  uapi/misc/cxl.h
 0xCA  10-2F  uapi/misc/ocxl.h
Signature · 1 line
2.25.1
#16 lore raw

PATCH v3 13/15 uapi: hyperv: Add mshv driver headers defining hypervisor ABIs

In reply to Nuno Das Neves

These must be in uapi because they will be used in the mshv ioctl API.

Version numbers for each file:
hvhdk.h 25212
hvhdk_mini.h 25294
hvgdk.h 25125
hvgdk_mini.h 25294

These are unstable interfaces and as such must be compiled independently from published interfaces found in hyperv-tlfs.h.

---
 include/uapi/hyperv/hvgdk.h      |   41 +
 include/uapi/hyperv/hvgdk_mini.h | 1076 ++++++++++++++++++++++++
 include/uapi/hyperv/hvhdk.h      | 1342 ++++++++++++++++++++++++++++++
 include/uapi/hyperv/hvhdk_mini.h |  160 ++++
 4 files changed, 2619 insertions(+)
 create mode 100644 include/uapi/hyperv/hvgdk.h
 create mode 100644 include/uapi/hyperv/hvgdk_mini.h
 create mode 100644 include/uapi/hyperv/hvhdk.h
 create mode 100644 include/uapi/hyperv/hvhdk_mini.h
include/uapi/hyperv/hvgdk.h new file +41 -0
new file mode 100644
index 000000000000..9bcbb7d902b2
@@ -0,0 +1,41 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright (c) 2023, Microsoft Corporation.
+ *
+ * These files (hvhdk.h, hvhdk_mini.h, hvgdk.h, hvgdk_mini.h) define APIs for
+ * communicating with the Microsoft Hypervisor.
+ *
+ * These definitions are subject to change across hypervisor versions, and as
+ * such are separate and independent from hyperv-tlfs.h.
+ *
+ * The naming of these headers reflects conventions used in the Microsoft
+ * Hypervisor.
+ */
+#ifndef _UAPI_HV_HVGDK_H
+#define _UAPI_HV_HVGDK_H
+
+#include "hvgdk_mini.h"
+
+enum hv_unimplemented_msr_action {
+	HV_UNIMPLEMENTED_MSR_ACTION_FAULT = 0,
+	HV_UNIMPLEMENTED_MSR_ACTION_IGNORE_WRITE_READ_ZERO = 1,
+	HV_UNIMPLEMENTED_MSR_ACTION_COUNT = 2,
+};
+
+/* Define connection identifier type. */
+union hv_connection_id {
+	__u32 asu32;
+	struct {
+		__u32 id:24;
+		__u32 reserved:8;
+	} __packed u;
+};
+
+struct hv_input_unmap_gpa_pages {
+	__u64 target_partition_id;
+	__u64 target_gpa_base;
+	__u32 unmap_flags;
+	__u32 padding;
+} __packed;
+
+#endif /* #ifndef _UAPI_HV_HVGDK_H */
include/uapi/hyperv/hvgdk_mini.h new file +1076 -0

1,076 lines not on this page · raw message

include/uapi/hyperv/hvhdk.h new file +1342 -0

1,342 lines not on this page · raw message

include/uapi/hyperv/hvhdk_mini.h new file +160 -0

160 lines not on this page · raw message

Signature · 1 line
2.25.1
#17 lore raw

PATCH v3 14/15 asm-generic: hyperv: Use new Hyper-V headers conditionally.

In reply to Nuno Das Neves

Add asm-generic/hyperv-defs.h. It includes hyperv-tlfs.h or hvhdk.h depending on compile-time constant HV_HYPERV_DEFS which will be defined in the mshv driver.

This is needed to keep unstable Hyper-V interfaces independent of hyperv-tlfs.h. This ensures hvhdk.h replaces hyperv-tlfs.h in the mshv driver, even via indirect includes.

---
 arch/arm64/include/asm/mshyperv.h |  2 +-
 arch/x86/include/asm/mshyperv.h   |  3 +--
 drivers/hv/hyperv_vmbus.h         |  1 -
 include/asm-generic/hyperv-defs.h | 26 ++++++++++++++++++++++++++
 include/asm-generic/mshyperv.h    |  2 +-
 include/linux/hyperv.h            |  2 +-
 6 files changed, 30 insertions(+), 6 deletions(-)
 create mode 100644 include/asm-generic/hyperv-defs.h
arch/arm64/include/asm/mshyperv.h +1 -1
index 20070a847304..8ec14caf3d4f 100644
@@ -20,7 +20,7 @@
 
 #include <linux/types.h>
 #include <linux/arm-smccc.h>
-#include <asm/hyperv-tlfs.h>
+#include <asm-generic/hyperv-defs.h>
 
 /*
  * Declare calls to get and set Hyper-V VP register values on ARM64, which
arch/x86/include/asm/mshyperv.h +1 -2
index 6e96d3420a9d..e19fc6140a31 100644
@@ -6,10 +6,9 @@
 #include <linux/nmi.h>
 #include <linux/msi.h>
 #include <asm/io.h>
-#include <asm/hyperv-tlfs.h>
 #include <asm/nospec-branch.h>
 #include <asm/paravirt.h>
-#include <asm/mshyperv.h>
+#include <asm-generic/hyperv-defs.h>
 
 /*
  * Hyper-V always provides a single IO-APIC at this MMIO address.
drivers/hv/hyperv_vmbus.h +0 -1
index 09792eb4ffed..0e4bc18a13fa 100644
@@ -15,7 +15,6 @@
 #include <linux/list.h>
 #include <linux/bitops.h>
 #include <asm/sync_bitops.h>
-#include <asm/hyperv-tlfs.h>
 #include <linux/atomic.h>
 #include <linux/hyperv.h>
 #include <linux/interrupt.h>
include/asm-generic/hyperv-defs.h new file +26 -0
new file mode 100644
index 000000000000..ac6fcba35c8c
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_GENERIC_HYPERV_DEFS_H
+#define _ASM_GENERIC_HYPERV_DEFS_H
+
+/*
+ * There are cases where Microsoft Hypervisor ABIs are needed which may not be
+ * stable or present in the Hyper-V TLFS document. E.g. the mshv_root driver.
+ *
+ * As these interfaces are unstable and may differ from hyperv-tlfs.h, they
+ * must be kept separate and independent.
+ *
+ * However, code from files that depend on hyperv-tlfs.h (such as mshyperv.h)
+ * is still needed, so work around the issue by conditionally including the
+ * correct definitions.
+ *
+ * Note: Since they are independent of each other, there are many definitions
+ * duplicated in both hyperv-tlfs.h and uapi/hyperv/hv*.h files.
+ */
+#ifdef HV_HYPERV_DEFS
+#include <uapi/hyperv/hvhdk.h>
+#else
+#include <asm/hyperv-tlfs.h>
+#endif
+
+#endif /* _ASM_GENERIC_HYPERV_DEFS_H */
+
include/asm-generic/mshyperv.h +1 -1
index 0a9e8ff31b73..d3864ba64e47 100644
@@ -25,7 +25,7 @@
 #include <linux/cpumask.h>
 #include <linux/nmi.h>
 #include <asm/ptrace.h>
-#include <asm/hyperv-tlfs.h>
+#include <asm-generic/hyperv-defs.h>
 
 #define VTPM_BASE_ADDRESS 0xfed40000
 
include/linux/hyperv.h +1 -1
index 2d8568708d90..d0bc3d34241c 100644
@@ -24,7 +24,7 @@
 #include <linux/mod_devicetable.h>
 #include <linux/interrupt.h>
 #include <linux/reciprocal_div.h>
-#include <asm/hyperv-tlfs.h>
+#include <asm-generic/hyperv-defs.h>
 
 #define MAX_PAGE_BUFFER_COUNT				32
 #define MAX_MULTIPAGE_BUFFER_COUNT			32 /* 128K */
Signature · 1 line
2.25.1
#18 lore raw

PATCH v3 15/15 Drivers: hv: Add modules to expose /dev/mshv to VMMs running on Hyper-V

In reply to Nuno Das Neves

Add mshv, mshv_root, and mshv_vtl modules:

Module mshv is the parent module to the other two. It provides /dev/mshv, plus some common hypercall helper code. When one of the child modules is loaded, it is registered with the mshv module, which then provides entry point(s) to the child module via the IOCTLs defined in uapi/linux/mshv.h.

E.g. When the mshv_root module is loaded, it registers itself, and the MSHV_CREATE_PARTITION IOCTL becomes available in /dev/mshv. That is used to get a partition fd managed by mshv_root.

Similarly for mshv_vtl module, there is MSHV_CREATE_VTL, which creates an fd representing the lower vtl, managed by mshv_vtl.

Module mshv_root provides APIs for creating and managing child partitions. It defines abstractions for partitions (vms), vps (vcpus), and other things related to running a guest. It exposes the userspace interfaces for a VMM to manage the guest.

Module mshv_vtl provides VTL (Virtual Trust Level) support for VMMs. In this scenario, the host kernel and VMM run in a higher trust level than the guest, but within the same partition. This provides better isolation and performance.

---
 drivers/hv/Kconfig             |   49 +
 drivers/hv/Makefile            |   20 +
 drivers/hv/hv_call.c           |  113 ++
 drivers/hv/hv_common.c         |    4 +
 drivers/hv/mshv.h              |  123 ++
 drivers/hv/mshv_eventfd.c      |  761 +++++++++++++
 drivers/hv/mshv_eventfd.h      |   80 ++
 drivers/hv/mshv_main.c         |  208 ++++
 drivers/hv/mshv_msi.c          |  129 +++
 drivers/hv/mshv_portid_table.c |   84 ++
 drivers/hv/mshv_root.h         |  193 ++++
 drivers/hv/mshv_root_hv_call.c | 1015 +++++++++++++++++
 drivers/hv/mshv_root_main.c    | 1920 ++++++++++++++++++++++++++++++++
 drivers/hv/mshv_synic.c        |  688 ++++++++++++
 drivers/hv/mshv_vtl.h          |   52 +
 drivers/hv/mshv_vtl_main.c     | 1517 +++++++++++++++++++++++++
 drivers/hv/xfer_to_guest.c     |   28 +
 include/uapi/linux/mshv.h      |  306 +++++
 18 files changed, 7290 insertions(+)
 create mode 100644 drivers/hv/hv_call.c
 create mode 100644 drivers/hv/mshv.h
 create mode 100644 drivers/hv/mshv_eventfd.c
 create mode 100644 drivers/hv/mshv_eventfd.h
 create mode 100644 drivers/hv/mshv_main.c
 create mode 100644 drivers/hv/mshv_msi.c
 create mode 100644 drivers/hv/mshv_portid_table.c
 create mode 100644 drivers/hv/mshv_root.h
 create mode 100644 drivers/hv/mshv_root_hv_call.c
 create mode 100644 drivers/hv/mshv_root_main.c
 create mode 100644 drivers/hv/mshv_synic.c
 create mode 100644 drivers/hv/mshv_vtl.h
 create mode 100644 drivers/hv/mshv_vtl_main.c
 create mode 100644 drivers/hv/xfer_to_guest.c
 create mode 100644 include/uapi/linux/mshv.h
drivers/hv/Kconfig +49 -0
index 00242107d62e..f9d9b8883ba4 100644
@@ -54,4 +54,53 @@ config HYPERV_BALLOON
 	help
 	  Select this option to enable Hyper-V Balloon driver.
 
+config MSHV
+	tristate "Microsoft Hypervisor root partition interfaces: /dev/mshv"
+	depends on X86_64 && HYPERV
+	select EVENTFD
+	select MSHV_XFER_TO_GUEST_WORK
+	help
+	  Select this option to enable core functionality for managing guest
+	  virtual machines running under the Microsoft Hypervisor.
+
+	  The interfaces are provided via a device named /dev/mshv.
+
+	  To compile this as a module, choose M here.
+
+	  If unsure, say N.
+
+config MSHV_ROOT
+	tristate "Microsoft Hyper-V root partition APIs driver"
+	depends on MSHV
+	help
+	  Select this option to provide /dev/mshv interfaces specific to
+	  running as the root partition on Microsoft Hypervisor.
+
+	  To compile this as a module, choose M here.
+
+	  If unsure, say N.
+
+config MSHV_VTL
+	tristate "Microsoft Hyper-V VTL driver"
+	depends on MSHV
+	select HYPERV_VTL_MODE
+	help
+	  Select this option to enable Hyper-V VTL driver.
+	  Virtual Secure Mode (VSM) is a set of hypervisor capabilities and
+	  enlightenments offered to host and guest partitions which enables
+	  the creation and management of new security boundaries within
+	  operating system software.
+
+	  VSM achieves and maintains isolation through Virtual Trust Levels
+	  (VTLs). Virtual Trust Levels are hierarchical, with higher levels
+	  being more privileged than lower levels. VTL0 is the least privileged
+	  level, and currently only other level supported is VTL2.
+
+	  To compile this as a module, choose M here.
+
+	  If unsure, say N.
+
+config MSHV_XFER_TO_GUEST_WORK
+	bool
+
 endmenu
drivers/hv/Makefile +20 -0
index d76df5c8c2a9..da7aa7542b05 100644
@@ -2,10 +2,30 @@
 obj-$(CONFIG_HYPERV)		+= hv_vmbus.o
 obj-$(CONFIG_HYPERV_UTILS)	+= hv_utils.o
 obj-$(CONFIG_HYPERV_BALLOON)	+= hv_balloon.o
+obj-$(CONFIG_MSHV)			+= mshv.o
+obj-$(CONFIG_MSHV_VTL)		+= mshv_vtl.o
+obj-$(CONFIG_MSHV_ROOT)		+= mshv_root.o
 
 CFLAGS_hv_trace.o = -I$(src)
 CFLAGS_hv_balloon.o = -I$(src)
 
+CFLAGS_mshv_main.o			= -DHV_HYPERV_DEFS
+CFLAGS_hv_call.o			= -DHV_HYPERV_DEFS
+CFLAGS_mshv_root_main.o		= -DHV_HYPERV_DEFS
+CFLAGS_mshv_root_hv_call.o	= -DHV_HYPERV_DEFS
+CFLAGS_mshv_synic.o			= -DHV_HYPERV_DEFS
+CFLAGS_mshv_portid_table.o	= -DHV_HYPERV_DEFS
+CFLAGS_mshv_eventfd.o		= -DHV_HYPERV_DEFS
+CFLAGS_mshv_msi.o			= -DHV_HYPERV_DEFS
+CFLAGS_mshv_vtl_main.o		= -DHV_HYPERV_DEFS
+
+mshv-y				+= mshv_main.o
+mshv_root-y			:= mshv_root_main.o mshv_synic.o mshv_portid_table.o \
+						mshv_eventfd.o mshv_msi.o mshv_root_hv_call.o hv_call.o
+mshv_vtl-y			:= mshv_vtl_main.o hv_call.o
+
+obj-$(CONFIG_MSHV_XFER_TO_GUEST_WORK) += xfer_to_guest.o
+
 hv_vmbus-y := vmbus_drv.o \
 		 hv.o connection.o channel.o \
 		 channel_mgmt.o ring_buffer.o hv_trace.o
drivers/hv/hv_call.c new file +113 -0

113 lines not on this page · raw message

drivers/hv/hv_common.c +4 -0
index 092b0c727db9..251d8de3180d 100644
@@ -63,7 +63,11 @@ EXPORT_SYMBOL_GPL(hyperv_pcpu_output_arg);
  */
 static inline bool hv_output_arg_exists(void)
 {
+#ifdef CONFIG_MSHV_VTL
+	return true;
+#else
 	return hv_root_partition ? true : false;
+#endif
 }
 
 static void hv_kmsg_dump_unregister(void);
drivers/hv/mshv.h new file +123 -0

123 lines not on this page · raw message

drivers/hv/mshv_eventfd.c new file +761 -0

761 lines not on this page · raw message

drivers/hv/mshv_eventfd.h new file +80 -0

80 lines not on this page · raw message

drivers/hv/mshv_main.c new file +208 -0

208 lines not on this page · raw message

drivers/hv/mshv_msi.c new file +129 -0

129 lines not on this page · raw message

drivers/hv/mshv_portid_table.c new file +84 -0

84 lines not on this page · raw message

drivers/hv/mshv_root.h new file +193 -0

193 lines not on this page · raw message

drivers/hv/mshv_root_hv_call.c new file +1015 -0

1,015 lines not on this page · raw message

drivers/hv/mshv_root_main.c new file +1920 -0

200 lines not on this page · raw message

drivers/hv/mshv_synic.c new file +688 -0

688 lines not on this page · raw message

drivers/hv/mshv_vtl.h new file +52 -0

52 lines not on this page · raw message

drivers/hv/mshv_vtl_main.c new file +1517 -0

1,517 lines not on this page · raw message

drivers/hv/xfer_to_guest.c new file +28 -0
new file mode 100644
index 000000000000..4a3901d44209

Lines of this file are not shown. View the raw message.

include/uapi/linux/mshv.h new file +306 -0

306 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.25.1

Part of the diff is not shown here. View the raw message.

maintainer Acked-by on v3 15/15
#19 lore raw

Re: [PATCH v3 15/15] Drivers: hv: Add modules to expose /dev/mshv to VMMs running on Hyper-V

Quoted 19 lines · Nuno Das Neves

Add mshv, mshv_root, and mshv_vtl modules:

Module mshv is the parent module to the other two. It provides /dev/mshv, plus some common hypercall helper code. When one of the child modules is loaded, it is registered with the mshv module, which then provides entry point(s) to the child module via the IOCTLs defined in uapi/linux/mshv.h.

E.g. When the mshv_root module is loaded, it registers itself, and the MSHV_CREATE_PARTITION IOCTL becomes available in /dev/mshv. That is used to get a partition fd managed by mshv_root.

Similarly for mshv_vtl module, there is MSHV_CREATE_VTL, which creates an fd representing the lower vtl, managed by mshv_vtl.

Module mshv_root provides APIs for creating and managing child partitions. It defines abstractions for partitions (vms), vps (vcpus), and other things related to running a guest. It exposes the userspace interfaces for a VMM to manage the guest.

Quoted 6 lines

Module mshv_vtl provides VTL (Virtual Trust Level) support for VMMs. In this scenario, the host kernel and VMM run in a higher trust level than the guest, but within the same partition. This provides better isolation and performance.

As far as I can tell, all my comments from the previous version are addressed. I believe Saurabh and Boqun's comments are addressed, too.

The code looks good to me, so:

I will wait for some time for others to chime in, just in case the community has more comments.

#20 lore raw

Re: [PATCH v3 15/15] Drivers: hv: Add modules to expose /dev/mshv to VMMs running on Hyper-V

In reply to Nuno Das Neves

drivers/hv/mshv_vtl_main.c line 1455

Quoted 11 lines · Nuno Das Neves
drivers/hv/mshv_vtl_main.c new file +11 -0
+static int __init mshv_vtl_init(void)
+{
+	int ret;
+
+	tasklet_init(&msg_dpc, mshv_vtl_sint_on_msg_dpc, 0);
+	init_waitqueue_head(&fd_wait_queue);
+
+	if (mshv_vtl_get_vsm_regs()) {
+		pr_emerg("%s: Unable to get VSM capabilities !!\n", __func__);
+		BUG();
+	}

So you crash the whole kernel if someone loads this module on a non-mshv system?

That seems quite excessive and hostile :(

Or am I somehow reading this incorrectly?

thanks,

greg k-h

maintainer
#21 lore raw

Re: [PATCH v3 15/15] Drivers: hv: Add modules to expose /dev/mshv to VMMs running on Hyper-V

Greg, thank you for looking at the code.

Quoted 17 lines · Greg KH

drivers/hv/mshv_vtl_main.c line 1455

Quoted 11 lines · Nuno Das Neves
drivers/hv/mshv_vtl_main.c new file +11 -0
+static int __init mshv_vtl_init(void)
+{
+	int ret;
+
+	tasklet_init(&msg_dpc, mshv_vtl_sint_on_msg_dpc, 0);
+	init_waitqueue_head(&fd_wait_queue);
+
+	if (mshv_vtl_get_vsm_regs()) {
+		pr_emerg("%s: Unable to get VSM capabilities !!\n", __func__);
+		BUG();
+	}

So you crash the whole kernel if someone loads this module on a non-mshv system?

Quoted 2 lines

That seems quite excessive and hostile :(

I agree this can be more lenient. It should be safe to just return an error code (ENODEV) and let user space decide the next action.

Saurabh, let me know what you think.

Thanks,
Wei.

Quoted 6 lines

Or am I somehow reading this incorrectly?

thanks,

greg k-h

#22 lore raw

Re: [PATCH v3 15/15] Drivers: hv: Add modules to expose /dev/mshv to VMMs running on Hyper-V

Quoted 20 lines · Wei Liu

Greg, thank you for looking at the code.

Quoted 17 lines · Greg KH

drivers/hv/mshv_vtl_main.c line 1455

Quoted 11 lines · Nuno Das Neves
drivers/hv/mshv_vtl_main.c new file +11 -0
+static int __init mshv_vtl_init(void)
+{
+	int ret;
+
+	tasklet_init(&msg_dpc, mshv_vtl_sint_on_msg_dpc, 0);
+	init_waitqueue_head(&fd_wait_queue);
+
+	if (mshv_vtl_get_vsm_regs()) {
+		pr_emerg("%s: Unable to get VSM capabilities !!\n", __func__);
+		BUG();
+	}

So you crash the whole kernel if someone loads this module on a non-mshv system?

Quoted 7 lines
Quoted 2 lines

That seems quite excessive and hostile :(

I agree this can be more lenient. It should be safe to just return an error code (ENODEV) and let user space decide the next action.

Saurabh, let me know what you think.

Thanks for reporting this. I agree, returning an error makes more sense here. We should do this for both BUG() in this init.

- Saurabh

Quoted 10 lines

Thanks,
Wei.

Quoted 6 lines

Or am I somehow reading this incorrectly?

thanks,

greg k-h

#23 lore raw

Re: [PATCH v3 15/15] Drivers: hv: Add modules to expose /dev/mshv to VMMs running on Hyper-V

In reply to Nuno Das Neves
Quoted 12 lines · Nuno Das Neves
drivers/hv/mshv_vtl_main.c new file +12 -0
+static int mshv_vtl_get_vsm_regs(void)
+{
+	struct hv_register_assoc registers[2];
+	union hv_input_vtl input_vtl;
+	int ret, count = 2;
+
+	input_vtl.as_uint8 = 0;
+	registers[0].name = HV_REGISTER_VSM_CODE_PAGE_OFFSETS;
+	registers[1].name = HV_REGISTER_VSM_CAPABILITIES;
+
+	ret = hv_call_get_vp_registers(HV_VP_INDEX_SELF, HV_PARTITION_ID_SELF,
+				       count, input_vtl, registers);

drivers/hv/mshv_vtl_main.c line 218

Quoted 10 lines
drivers/hv/mshv_vtl_main.c new file +10 -0
+	if (ret)
+		return ret;
+
+	mshv_vsm_page_offsets.as_uint64 = registers[0].value.reg64;
+	mshv_vsm_capabilities.as_uint64 = registers[1].value.reg64;
+
+	pr_debug("%s: VSM code page offsets: %#016llx\n", __func__,
+		 mshv_vsm_page_offsets.as_uint64);
+	pr_info("%s: VSM capabilities: %#016llx\n", __func__,
+		mshv_vsm_capabilities.as_uint64);

When drivers are working properly, they are quiet. This is very noisy and probably is leaking memory addresses to userspace?

Also, there is NEVER a need for __func__ in a pr_debug() line, it has that for you automatically.

Also, drivers should never call pr_*() calls, always use the proper dev_*() calls instead.

Quoted 9 lines
drivers/hv/mshv_vtl_main.c new file +9 -0
+
+	return ret;
+}
+
+static int mshv_vtl_configure_vsm_partition(void)
+{
+	union hv_register_vsm_partition_config config;
+	struct hv_register_assoc reg_assoc;
+	union hv_input_vtl input_vtl;

drivers/hv/mshv_vtl_main.c line 237

Quoted 10 lines
drivers/hv/mshv_vtl_main.c new file +10 -0
+
+	config.as_u64 = 0;
+	config.default_vtl_protection_mask = HV_MAP_GPA_PERMISSIONS_MASK;
+	config.enable_vtl_protection = 1;
+	config.zero_memory_on_reset = 1;
+	config.intercept_vp_startup = 1;
+	config.intercept_cpuid_unimplemented = 1;
+
+	if (mshv_vsm_capabilities.intercept_page_available) {
+		pr_debug("%s: using intercept page", __func__);

Again, __func__ is not needed, you are providing it twice here for no real reason except to waste storage space :)

drivers/hv/mshv_vtl_main.c line 246

Quoted 9 lines
drivers/hv/mshv_vtl_main.c new file +9 -0
+		config.intercept_page = 1;
+	}
+
+	reg_assoc.name = HV_REGISTER_VSM_PARTITION_CONFIG;
+	reg_assoc.value.reg64 = config.as_u64;
+	input_vtl.as_uint8 = 0;
+
+	return hv_call_set_vp_registers(HV_VP_INDEX_SELF, HV_PARTITION_ID_SELF,
+				       1, input_vtl, &reg_assoc);

None of this needs to be unwound if initialization fails later on?

thanks,

greg k-h

#24 lore raw

Re: [PATCH v3 15/15] Drivers: hv: Add modules to expose /dev/mshv to VMMs running on Hyper-V

Resend in plain text instead of HTML - oops!

Quoted 18 lines · Greg KH
Quoted 17 lines · Nuno Das Neves
drivers/hv/mshv_vtl_main.c new file +17 -0
+static int mshv_vtl_get_vsm_regs(void)
+{
+	struct hv_register_assoc registers[2];
+	union hv_input_vtl input_vtl;
+	int ret, count = 2;
+
+	input_vtl.as_uint8 = 0;
+	registers[0].name = HV_REGISTER_VSM_CODE_PAGE_OFFSETS;
+	registers[1].name = HV_REGISTER_VSM_CAPABILITIES;
+
+	ret = hv_call_get_vp_registers(HV_VP_INDEX_SELF, HV_PARTITION_ID_SELF,
+				       count, input_vtl, registers);
+	if (ret)
+		return ret;
+
+	mshv_vsm_page_offsets.as_uint64 = registers[0].value.reg64;
+	mshv_vsm_capabilities.as_uint64 = registers[1].value.reg64;
Quoted 9 lines

drivers/hv/mshv_vtl_main.c line 218

Quoted 5 lines
drivers/hv/mshv_vtl_main.c new file +5 -0
+
+	pr_debug("%s: VSM code page offsets: %#016llx\n", __func__,
+		 mshv_vsm_page_offsets.as_uint64);
+	pr_info("%s: VSM capabilities: %#016llx\n", __func__,
+		mshv_vsm_capabilities.as_uint64);

When drivers are working properly, they are quiet. This is very noisy and probably is leaking memory addresses to userspace?

I will remove these, thanks.

Quoted 3 lines

Also, there is NEVER a need for __func__ in a pr_debug() line, it has that for you automatically.

Thank you, I didn't know this.

Quoted 3 lines

Also, drivers should never call pr_*() calls, always use the proper dev_*() calls instead.

We only use struct device in one place in this driver, I think that is the only place it makes sense to use dev_*() over pr_*() calls.

Quoted 16 lines
Quoted 16 lines
drivers/hv/mshv_vtl_main.c new file +14 -0
+
+	return ret;
+}
+
+static int mshv_vtl_configure_vsm_partition(void)
+{
+	union hv_register_vsm_partition_config config;
+	struct hv_register_assoc reg_assoc;
+	union hv_input_vtl input_vtl;
+
+	config.as_u64 = 0;
+	config.default_vtl_protection_mask = HV_MAP_GPA_PERMISSIONS_MASK;
+	config.enable_vtl_protection = 1;
+	config.zero_memory_on_reset = 1;
Quoted 9 lines

drivers/hv/mshv_vtl_main.c line 237

Quoted 5 lines
drivers/hv/mshv_vtl_main.c new file +5 -0
+	config.intercept_vp_startup = 1;
+	config.intercept_cpuid_unimplemented = 1;
+
+	if (mshv_vsm_capabilities.intercept_page_available) {
+		pr_debug("%s: using intercept page", __func__);

Again, __func__ is not needed, you are providing it twice here for no real reason except to waste storage space :)

Thanks, I will review all the uses of pr_debug().

Quoted 4 lines
Quoted 4 lines
drivers/hv/mshv_vtl_main.c new file +4 -0
+		config.intercept_page = 1;
+	}
+
+	reg_assoc.name = HV_REGISTER_VSM_PARTITION_CONFIG;
Quoted 9 lines

drivers/hv/mshv_vtl_main.c line 246

Quoted 5 lines
drivers/hv/mshv_vtl_main.c new file +5 -0
+	reg_assoc.value.reg64 = config.as_u64;
+	input_vtl.as_uint8 = 0;
+
+	return hv_call_set_vp_registers(HV_VP_INDEX_SELF, HV_PARTITION_ID_SELF,
+				       1, input_vtl, &reg_assoc);

None of this needs to be unwound if initialization fails later on?

I think unwinding this is not needed, not 100% sure. Saurabh, can you comment?

Thanks,
Nuno

Quoted 3 lines

thanks,

greg k-h

#25 lore raw

Re: [PATCH v3 15/15] Drivers: hv: Add modules to expose /dev/mshv to VMMs running on Hyper-V

Quoted 7 lines · Nuno Das Neves
Quoted 3 lines · Greg KH

Also, drivers should never call pr_*() calls, always use the proper dev_*() calls instead.

We only use struct device in one place in this driver, I think that is the only place it makes sense to use dev_*() over pr_*() calls.

Then the driver needs to be fixed to use struct device properly so that you do have access to it when you want to print messages. That's a valid reason to pass around your device structure when needed.

thanks,

greg k-h

maintainer
#26 lore raw

Re: [PATCH v3 15/15] Drivers: hv: Add modules to expose /dev/mshv to VMMs running on Hyper-V

Quoted 6 lines · Greg KH
Quoted 5 lines · Nuno Das Neves
Quoted 3 lines · Greg KH

Also, drivers should never call pr_*() calls, always use the proper dev_*() calls instead.

Quoted 6 lines
Quoted 2 lines

We only use struct device in one place in this driver, I think that is the only place it makes sense to use dev_*() over pr_*() calls.

Then the driver needs to be fixed to use struct device properly so that you do have access to it when you want to print messages. That's a valid reason to pass around your device structure when needed.

Greg, ACRN and Nitro drivers do not pass around the device structure. Instead, they rely on a global struct device. We can follow the same.

Nuno, I checked our code. We already have a misc device. We can use that for dev_* calls.

Something like this:

dev_warn(mshv_dev.this_device, "this is a warning message");

This should resolve Greg's concern.

Thanks,
Wei.

Quoted 5 lines

thanks,

greg k-h

#27 lore raw

Re: [PATCH v3 15/15] Drivers: hv: Add modules to expose /dev/mshv to VMMs running on Hyper-V

Quoted 10 lines · Wei Liu
Quoted 9 lines · Greg KH
Quoted 7 lines · Nuno Das Neves
Quoted 3 lines · Greg KH

Also, drivers should never call pr_*() calls, always use the proper dev_*() calls instead.

We only use struct device in one place in this driver, I think that is the only place it makes sense to use dev_*() over pr_*() calls.

Quoted 6 lines
Quoted 3 lines

Then the driver needs to be fixed to use struct device properly so that you do have access to it when you want to print messages. That's a valid reason to pass around your device structure when needed.

Greg, ACRN and Nitro drivers do not pass around the device structure. Instead, they rely on a global struct device. We can follow the same.

A single global struct device is wrong, please don't do that.

Don't copy bad design patterns from other drivers, be better :)

thanks,

greg k-h

maintainer
#28 lore raw

Re: [PATCH v3 15/15] Drivers: hv: Add modules to expose /dev/mshv to VMMs running on Hyper-V

Quoted 15 lines · Greg KH
Quoted 14 lines · Wei Liu
Quoted 12 lines · Greg KH
Quoted 7 lines · Nuno Das Neves
Quoted 3 lines · Greg KH

Also, drivers should never call pr_*() calls, always use the proper dev_*() calls instead.

We only use struct device in one place in this driver, I think that is the only place it makes sense to use dev_*() over pr_*() calls.

Then the driver needs to be fixed to use struct device properly so that you do have access to it when you want to print messages. That's a valid reason to pass around your device structure when needed.

Quoted 7 lines
Quoted 2 lines

Greg, ACRN and Nitro drivers do not pass around the device structure. Instead, they rely on a global struct device. We can follow the same.

A single global struct device is wrong, please don't do that.

Don't copy bad design patterns from other drivers, be better :)

If we're working with real devices like network cards or graphics cards I would agree -- it is easy to imagine that we have several cards of the same model in the system -- but in real world there won't be two hypervisor instances running on the same hardware.

We can stash the struct device inside some private data fields, but that doesn't change the fact that we're still having one instance of the structure. Is this what you want? Or do you have something else in mind?

Thanks,
Wei.

Quoted 4 lines

thanks,

greg k-h

#29 lore raw

Re: [PATCH v3 15/15] Drivers: hv: Add modules to expose /dev/mshv to VMMs running on Hyper-V

Quoted 24 lines · Wei Liu
Quoted 15 lines · Greg KH
Quoted 14 lines · Wei Liu
Quoted 12 lines · Greg KH
Quoted 7 lines · Nuno Das Neves
Quoted 3 lines · Greg KH

Also, drivers should never call pr_*() calls, always use the proper dev_*() calls instead.

We only use struct device in one place in this driver, I think that is the only place it makes sense to use dev_*() over pr_*() calls.

Then the driver needs to be fixed to use struct device properly so that you do have access to it when you want to print messages. That's a valid reason to pass around your device structure when needed.

Quoted 7 lines
Quoted 2 lines

Greg, ACRN and Nitro drivers do not pass around the device structure. Instead, they rely on a global struct device. We can follow the same.

A single global struct device is wrong, please don't do that.

Don't copy bad design patterns from other drivers, be better :)

Quoted 8 lines

If we're working with real devices like network cards or graphics cards I would agree -- it is easy to imagine that we have several cards of the same model in the system -- but in real world there won't be two hypervisor instances running on the same hardware.

We can stash the struct device inside some private data fields, but that doesn't change the fact that we're still having one instance of the structure. Is this what you want? Or do you have something else in mind?

You have a real device, it's how userspace interacts with your subsystem. Please use that, it is dynamically created and handled and is the correct representation here.

thanks,

greg k-h

#30 lore raw

Re: [PATCH v3 15/15] Drivers: hv: Add modules to expose /dev/mshv to VMMs running on Hyper-V

Quoted 10 lines · Greg KH
Quoted 9 lines · Wei Liu
Quoted 8 lines · Greg KH
Quoted 7 lines · Wei Liu
Quoted 6 lines · Greg KH
Quoted 5 lines · Nuno Das Neves
Quoted 3 lines · Greg KH

Also, drivers should never call pr_*() calls, always use the proper dev_*() calls instead.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

We only use struct device in one place in this driver, I think that is the only place it makes sense to use dev_*() over pr_*() calls.

Then the driver needs to be fixed to use struct device properly so that you do have access to it when you want to print messages. That's a valid reason to pass around your device structure when needed.

What is the tangible benefit of using dev_*() over pr_*()? As I said, our use of struct device is very limited compared to all the places we may need to log errors.

pr_*() is used by many, many drivers; it seems to be the norm. We can certainly add a pr_fmt to improve the logging.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

Greg, ACRN and Nitro drivers do not pass around the device structure. Instead, they rely on a global struct device. We can follow the same.

A single global struct device is wrong, please don't do that.

Don't copy bad design patterns from other drivers, be better :)

What makes it a bad pattern? It seems to be well-established, and is also used by KVM which this driver is loosely modeled after:
https://elixir.bootlin.com/linux/v6.5.5/source/virt/kvm/kvm_main.c#L5128

Quoted 6 lines
Quoted 6 lines

If we're working with real devices like network cards or graphics cards I would agree -- it is easy to imagine that we have several cards of the same model in the system -- but in real world there won't be two hypervisor instances running on the same hardware.

Quoted 8 lines
Quoted 3 lines

We can stash the struct device inside some private data fields, but that doesn't change the fact that we're still having one instance of the structure. Is this what you want? Or do you have something else in mind?

You have a real device, it's how userspace interacts with your subsystem. Please use that, it is dynamically created and handled and is the correct representation here.

Are you referring to the struct device we get from calling misc_register? If not, please be more specific.

How would you suggest we get a reference to that device via e.g. open() or ioctl() without keeping a global reference to it?

Thanks,
Nuno

#31 lore raw

Re: [PATCH v3 15/15] Drivers: hv: Add modules to expose /dev/mshv to VMMs running on Hyper-V

Quoted 14 lines · Nuno Das Neves
Quoted 13 lines · Greg KH
Quoted 12 lines · Wei Liu
Quoted 11 lines · Greg KH
Quoted 10 lines · Wei Liu
Quoted 9 lines · Greg KH
Quoted 7 lines · Nuno Das Neves
Quoted 3 lines · Greg KH

Also, drivers should never call pr_*() calls, always use the proper dev_*() calls instead.

We only use struct device in one place in this driver, I think that is the only place it makes sense to use dev_*() over pr_*() calls.

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

Then the driver needs to be fixed to use struct device properly so that you do have access to it when you want to print messages. That's a valid reason to pass around your device structure when needed.

What is the tangible benefit of using dev_*() over pr_*()?

Unified reporting and handling of userspace of kernel log messages so they can be classified properly as well as dealing correctly with the dynamic debugging kernel infrastructure.

Why wouldn't you want to use it?

Quoted 3 lines

As I said,
our use of struct device is very limited compared to all the places we may need to log errors.

Then please fix that.

Quoted 1 line

pr_*() is used by many, many drivers; it seems to be the norm.

Not at all, it is not.

Quoted 1 line

We can certainly add a pr_fmt to improve the logging.

Please do it correctly so you don't have to go and add support for it later when your tools people ask you why they can't properly parse your driver's kernel log messages.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

If we're working with real devices like network cards or graphics cards I would agree -- it is easy to imagine that we have several cards of the same model in the system -- but in real world there won't be two hypervisor instances running on the same hardware.

We can stash the struct device inside some private data fields, but that doesn't change the fact that we're still having one instance of the structure. Is this what you want? Or do you have something else in mind?

Quoted 7 lines
Quoted 4 lines

You have a real device, it's how userspace interacts with your subsystem. Please use that, it is dynamically created and handled and is the correct representation here.

Are you referring to the struct device we get from calling misc_register?

Yes.

Quoted 2 lines

How would you suggest we get a reference to that device via e.g. open() or ioctl() without keeping a global reference to it?

You explicitly have it in your open() and ioctl() call, you never need a global reference for it the kernel gives it to you!

thanks,

greg k-h

maintainer
#32 lore raw

Re: [PATCH v3 15/15] Drivers: hv: Add modules to expose /dev/mshv to VMMs running on Hyper-V

Hi Greg

It is past midnight here, so I may not be able to articulate my thoughts very well. I want to avoid waiting for another day for another round trip of emails though. We can look at your reply in the morning and reply again.

On Wed, Sep 27, 2023 at 08:01:01AM +0200, Greg KH wrote:
[...]

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 8 lines

If we're working with real devices like network cards or graphics cards I would agree -- it is easy to imagine that we have several cards of the same model in the system -- but in real world there won't be two hypervisor instances running on the same hardware.

We can stash the struct device inside some private data fields, but that doesn't change the fact that we're still having one instance of the structure. Is this what you want? Or do you have something else in mind?

You have a real device, it's how userspace interacts with your subsystem. Please use that, it is dynamically created and handled and is the correct representation here.

Quoted 5 lines
Quoted 2 lines

Are you referring to the struct device we get from calling misc_register?

Yes.

We know about this, please see below. And we plan to use this.

Quoted 6 lines
Quoted 2 lines

How would you suggest we get a reference to that device via e.g. open() or ioctl() without keeping a global reference to it?

You explicitly have it in your open() and ioctl() call, you never need a global reference for it the kernel gives it to you!

This is what I don't follow.

Nuno and I discussed this today offline. We looked at the code before and looked again today (well, yesterday now).

Here are the two functions:

int vfs_open(const struct path *path, struct file *file) long vfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)

Or, if we provide an open function in our file_operations struct, we get an additional struct inode pointer.

    int (*open) (struct inode *, struct file *);

Neither struct file nor struct inode contains a reference to struct device.

Then in vfs.rst, there is a section about open:

``open``
called by the VFS when an inode should be opened. When the VFS opens a file, it creates a new "struct file". It then calls the open method for the newly allocated file structure. You might think that the open method really belongs in "struct inode_operations", and you may be right. I think it's done the way it is because it makes filesystems simpler to implement. The open() method is a good place to initialize the "private_data" member in the file structure if you want to point to a device structure

So, the driver is supposed to stash a pointer to struct device in private_data. That's what I alluded to in my previous reply. The core driver framework or the VFS doesn't give us a reference to struct device. We have to do it ourselves.

We can do that for sure, but the struct device we stash into private_data is going to be the one that is returned from misc_register, which at the same time is already stashed inside a static variable in our driver by our own code (Note that this is a pervasive pattern in the kernel).

I hope this is clear. If we're missing something extremely obvious, that somehow we can get a reference to struct device from the VFS while opening the file or doing ioctl without stashing it ourselves in our own code, please let us know.

At this point I feel like either I'm extremely stupid or we're just talking past each other. If you tell me it is the former and help me understand how we can achieve what you described, I am more than happy to learn new things I don't know or understand. :-)

If we have to propagate that reference ourselves, that then leads to next question whether it will just be more convenient to use the stashed value in the static variable directly like other drivers do, instead of stashing and propagating it around, knowing 100% it is the same object. I don't feel too strongly about this. As long as we are on the same page about getting a reference to struct device, we can do it either way.

Thanks,
Wei.

Quoted 4 lines

thanks,

greg k-h

#33 lore raw

Re: [PATCH v3 15/15] Drivers: hv: Add modules to expose /dev/mshv to VMMs running on Hyper-V

Quoted 61 lines · Wei Liu

On Wed, Sep 27, 2023 at 08:01:01AM +0200, Greg KH wrote:
[...]

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 8 lines

If we're working with real devices like network cards or graphics cards I would agree -- it is easy to imagine that we have several cards of the same model in the system -- but in real world there won't be two hypervisor instances running on the same hardware.

We can stash the struct device inside some private data fields, but that doesn't change the fact that we're still having one instance of the structure. Is this what you want? Or do you have something else in mind?

You have a real device, it's how userspace interacts with your subsystem. Please use that, it is dynamically created and handled and is the correct representation here.

Quoted 5 lines
Quoted 2 lines

Are you referring to the struct device we get from calling misc_register?

Yes.

We know about this, please see below. And we plan to use this.

Quoted 6 lines
Quoted 2 lines

How would you suggest we get a reference to that device via e.g. open() or ioctl() without keeping a global reference to it?

You explicitly have it in your open() and ioctl() call, you never need a global reference for it the kernel gives it to you!

This is what I don't follow.

Nuno and I discussed this today offline. We looked at the code before and looked again today (well, yesterday now).

Here are the two functions:

int vfs_open(const struct path *path, struct file *file) long vfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)

Or, if we provide an open function in our file_operations struct, we get an additional struct inode pointer.

    int (*open) (struct inode *, struct file *);

Neither struct file nor struct inode contains a reference to struct device.

Then in vfs.rst, there is a section about open:

``open``
called by the VFS when an inode should be opened. When the VFS opens a file, it creates a new "struct file". It then calls the open method for the newly allocated file structure. You might think that the open method really belongs in "struct inode_operations", and you may be right. I think it's done the way it is because it makes filesystems simpler to implement. The open() method is a good place to initialize the "private_data" member in the file structure if you want to point to a device structure

Quoted 4 lines

So, the driver is supposed to stash a pointer to struct device in private_data. That's what I alluded to in my previous reply. The core driver framework or the VFS doesn't give us a reference to struct device. We have to do it ourselves.

Please read Linux Device Drivers, 3rd edition, chapter 3, for how to do this properly. The book is free online.

Also look at the zillion in-kernel example drivers that use the misc device api, container_of() is your friend...

Quoted 5 lines

We can do that for sure, but the struct device we stash into private_data is going to be the one that is returned from misc_register, which at the same time is already stashed inside a static variable in our driver by our own code (Note that this is a pervasive pattern in the kernel).

Again, don't make this static, there's no requirement to do so.

But even if you do, sure, use it this way, you have a device. But I would strongly discourage you from having a static variable, there is no need to do this at all, and no one else should do so either.

thanks,

greg k-h

#34 lore raw

Re: [PATCH v3 15/15] Drivers: hv: Add modules to expose /dev/mshv to VMMs running on Hyper-V

Quoted 9 lines · Greg KH
Quoted 4 lines · Wei Liu

So, the driver is supposed to stash a pointer to struct device in private_data. That's what I alluded to in my previous reply. The core driver framework or the VFS doesn't give us a reference to struct device. We have to do it ourselves.

Please read Linux Device Drivers, 3rd edition, chapter 3, for how to do this properly. The book is free online.

Thanks, the issue that confused us was how to get the miscdevice. I eventually found the answer in the misc_register() documentation:

"By default, an open() syscall to the device sets file->private_data to point to the structure."

That's good - when we create a guest, we will have the miscdevice
in private_data already. Then we can just put it in our per-guest data structure. That will let us retrieve the device in the other ioctls so we can call dev_*().

Thanks,
Nuno

#35 lore raw

Re: [PATCH v3 15/15] Drivers: hv: Add modules to expose /dev/mshv to VMMs running on Hyper-V

In reply to Nuno Das Neves
Quoted 87 lines · Nuno Das Neves

Resend in plain text instead of HTML - oops!

Quoted 18 lines · Greg KH
Quoted 17 lines · Nuno Das Neves
drivers/hv/mshv_vtl_main.c new file +17 -0
+static int mshv_vtl_get_vsm_regs(void)
+{
+	struct hv_register_assoc registers[2];
+	union hv_input_vtl input_vtl;
+	int ret, count = 2;
+
+	input_vtl.as_uint8 = 0;
+	registers[0].name = HV_REGISTER_VSM_CODE_PAGE_OFFSETS;
+	registers[1].name = HV_REGISTER_VSM_CAPABILITIES;
+
+	ret = hv_call_get_vp_registers(HV_VP_INDEX_SELF, HV_PARTITION_ID_SELF,
+				       count, input_vtl, registers);
+	if (ret)
+		return ret;
+
+	mshv_vsm_page_offsets.as_uint64 = registers[0].value.reg64;
+	mshv_vsm_capabilities.as_uint64 = registers[1].value.reg64;
Quoted 9 lines

drivers/hv/mshv_vtl_main.c line 218

Quoted 5 lines
drivers/hv/mshv_vtl_main.c new file +5 -0
+
+	pr_debug("%s: VSM code page offsets: %#016llx\n", __func__,
+		 mshv_vsm_page_offsets.as_uint64);
+	pr_info("%s: VSM capabilities: %#016llx\n", __func__,
+		mshv_vsm_capabilities.as_uint64);

When drivers are working properly, they are quiet. This is very noisy and probably is leaking memory addresses to userspace?

I will remove these, thanks.

Quoted 3 lines

Also, there is NEVER a need for __func__ in a pr_debug() line, it has that for you automatically.

Thank you, I didn't know this.

Quoted 3 lines

Also, drivers should never call pr_*() calls, always use the proper dev_*() calls instead.

We only use struct device in one place in this driver, I think that is the only place it makes sense to use dev_*() over pr_*() calls.

Quoted 16 lines
Quoted 16 lines
drivers/hv/mshv_vtl_main.c new file +14 -0
+
+	return ret;
+}
+
+static int mshv_vtl_configure_vsm_partition(void)
+{
+	union hv_register_vsm_partition_config config;
+	struct hv_register_assoc reg_assoc;
+	union hv_input_vtl input_vtl;
+
+	config.as_u64 = 0;
+	config.default_vtl_protection_mask = HV_MAP_GPA_PERMISSIONS_MASK;
+	config.enable_vtl_protection = 1;
+	config.zero_memory_on_reset = 1;
Quoted 9 lines

drivers/hv/mshv_vtl_main.c line 237

Quoted 5 lines
drivers/hv/mshv_vtl_main.c new file +5 -0
+	config.intercept_vp_startup = 1;
+	config.intercept_cpuid_unimplemented = 1;
+
+	if (mshv_vsm_capabilities.intercept_page_available) {
+		pr_debug("%s: using intercept page", __func__);

Again, __func__ is not needed, you are providing it twice here for no real reason except to waste storage space :)

Thanks, I will review all the uses of pr_debug().

Quoted 4 lines
Quoted 4 lines
drivers/hv/mshv_vtl_main.c new file +4 -0
+		config.intercept_page = 1;
+	}
+
+	reg_assoc.name = HV_REGISTER_VSM_PARTITION_CONFIG;
Quoted 9 lines

drivers/hv/mshv_vtl_main.c line 246

Quoted 5 lines
drivers/hv/mshv_vtl_main.c new file +5 -0
+	reg_assoc.value.reg64 = config.as_u64;
+	input_vtl.as_uint8 = 0;
+
+	return hv_call_set_vp_registers(HV_VP_INDEX_SELF, HV_PARTITION_ID_SELF,
+				       1, input_vtl, &reg_assoc);

None of this needs to be unwound if initialization fails later on?

Quoted 2 lines

I think unwinding this is not needed, not 100% sure. Saurabh, can you comment?

Yes unwinding is not useful here, as these are synthetic register and there is no other use case of VSM supporting platforms other than VSM configuration.

In a non VSM supported platform hv_call_set_vp_registers itself will fail for HV_REGISTER_VSM_PARTITION_CONFIG.

- Saurabh

Quoted 8 lines

Thanks,
Nuno

Quoted 3 lines

thanks,

greg k-h