Skip to contentSkip to conversation

Drivers: hv: Introduce new driver - mshv_vtl

PATCH Superseded Naman Jain 3 participants 7 messages May 12, 2025 – May 14, 2025

Subsystems (2)

v2 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Naman Jain

author
#0 lore raw

v2 cover Drivers: hv: Introduce new driver - mshv_vtl

Introduce a new mshv_vtl driver to provide an interface for Virtual Machine Monitor like OpenVMM and its use as OpenHCL paravisor to control VTL0 (Virtual trust Level).
Expose devices and support IOCTLs for features like VTL creation, VTL0 memory management, context switch, making hypercalls, mapping VTL0 address space to VTL2 userspace, getting new VMBus messages and channel events in VTL2 etc.

OpenVMM : https://openvmm.dev/guide/

Changes since v1:
https://lore.kernel.org/all/[email protected]/
Addressed Saurabh's comments:
* Split the patch in 2 to keep export symbols separate
* Make MSHV_VTL module tristate and fixed compilation warning that would come when HYPERV is compiled as a module.
* Remove the use of ref_count
* Split functionality of mshv_vtl_ioctl_get_set_regs to different functions mshv_vtl_ioctl_(get|set)_regs as it actually make things simpler
* Fixed use of copy_from_user in atomic context in mshv_vtl_hvcall_call. Added ToDo comment for info.
* Added extra code to free memory for vtl in error scenarios in mshv_ioctl_create_vtl()

Addressed Alok's comments regarding:
* Additional conditional checks
* corrected typo in HV_X64_REGISTER_MSR_MTRR_PHYS_MASKB case
* empty lines before return statement
* Added/edited comments, variable names, structure field names as suggested to improve documentation - no functional change here.

Naman Jain (2):
Drivers: hv: Export some symbols for mshv_vtl
Drivers: hv: Introduce mshv_vtl driver

 drivers/hv/Kconfig          |   21 +
 drivers/hv/Makefile         |    7 +-
 drivers/hv/hv.c             |    2 +
 drivers/hv/hyperv_vmbus.h   |    1 +
 drivers/hv/mshv_vtl.h       |   52 +
 drivers/hv/mshv_vtl_main.c  | 1783 +++++++++++++++++++++++++++++++++++
 drivers/hv/vmbus_drv.c      |    3 +-
 include/hyperv/hvgdk_mini.h |   81 ++
 include/hyperv/hvhdk.h      |    1 +
 include/uapi/linux/mshv.h   |   82 ++
 10 files changed, 2031 insertions(+), 2 deletions(-)
 create mode 100644 drivers/hv/mshv_vtl.h
 create mode 100644 drivers/hv/mshv_vtl_main.c

base-commit: ed61cb3d78d585209ec775933078e268544fe9a4

Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/2 Drivers: hv: Export some symbols for mshv_vtl

MSHV_VTL driver is going to be introduced, which is supposed to provide interface for Virtual Machine Monitors (VMMs) to control Virtual Trust Level (VTL). Export the symbols needed to make it work (vmbus_isr, hv_context and hv_post_message).

---
 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 308c8f279df8..11e8096fe840 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 0b450e53161e..b61f01fc1960 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 792bb614149d..55caac24d102 100644
@@ -1283,7 +1283,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);
@@ -1306,6 +1306,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.34.1
author
#2 lore raw

PATCH v2 2/2 Drivers: hv: Introduce mshv_vtl driver

In reply to Naman Jain

Provide an interface for Virtual Machine Monitor like OpenVMM and its use as OpenHCL paravisor to control VTL0 (Virtual trust Level). Expose devices and support IOCTLs for features like VTL creation, VTL0 memory management, context switch, making hypercalls, mapping VTL0 address space to VTL2 userspace, getting new VMBus messages and channel events in VTL2 etc.

---
 drivers/hv/Kconfig          |   21 +
 drivers/hv/Makefile         |    7 +-
 drivers/hv/mshv_vtl.h       |   52 +
 drivers/hv/mshv_vtl_main.c  | 1783 +++++++++++++++++++++++++++++++++++
 include/hyperv/hvgdk_mini.h |   81 ++
 include/hyperv/hvhdk.h      |    1 +
 include/uapi/linux/mshv.h   |   82 ++
 7 files changed, 2026 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hv/mshv_vtl.h
 create mode 100644 drivers/hv/mshv_vtl_main.c
drivers/hv/Kconfig +21 -0
index eefa0b559b73..05f990306d40 100644
@@ -72,4 +72,25 @@ config MSHV_ROOT
 
 	  If unsure, say N.
 
+config MSHV_VTL
+	tristate "Microsoft Hyper-V VTL driver"
+	depends on HYPERV && X86_64
+	depends on TRANSPARENT_HUGEPAGE
+	depends on OF
+	# MTRRs are controlled by VTL0, and are not specific to individual VTLs.
+	# Therefore, do not attempt to access or modify MTRRs here.
+	depends on !MTRR
+	select CPUMASK_OFFSTACK
+	select HYPERV_VTL_MODE
+	default n
+	help
+	  Select this option to enable Hyper-V VTL driver support.
+	  This driver provides interfaces for Virtual Machine Manager (VMM) running in VTL2
+	  userspace to create VTLs and partitions, setup and manage VTL0 memory and
+	  allow userspace to make direct hypercalls. This also allows to map VTL0's address
+	  space to a usermode process in VTL2 and supports getting new VMBus messages and channel
+	  events in VTL2.
+
+	  If unsure, say N.
+
 endmenu
drivers/hv/Makefile +6 -1
index 976189c725dc..c53a0df746b7 100644
@@ -3,6 +3,7 @@ obj-$(CONFIG_HYPERV)		+= hv_vmbus.o
 obj-$(CONFIG_HYPERV_UTILS)	+= hv_utils.o
 obj-$(CONFIG_HYPERV_BALLOON)	+= hv_balloon.o
 obj-$(CONFIG_MSHV_ROOT)		+= mshv_root.o
+obj-$(CONFIG_MSHV_VTL)          += mshv_vtl.o
 
 CFLAGS_hv_trace.o = -I$(src)
 CFLAGS_hv_balloon.o = -I$(src)
@@ -14,7 +15,11 @@ hv_vmbus-$(CONFIG_HYPERV_TESTING)	+= hv_debugfs.o
 hv_utils-y := hv_util.o hv_kvp.o hv_snapshot.o hv_utils_transport.o
 mshv_root-y := mshv_root_main.o mshv_synic.o mshv_eventfd.o mshv_irq.o \
 	       mshv_root_hv_call.o mshv_portid_table.o
+mshv_vtl-y := mshv_vtl_main.o
 
 # Code that must be built-in
 obj-$(subst m,y,$(CONFIG_HYPERV)) += hv_common.o
-obj-$(subst m,y,$(CONFIG_MSHV_ROOT)) += hv_proc.o mshv_common.o
+obj-$(subst m,y,$(CONFIG_MSHV_ROOT)) += hv_proc.o
+ifneq ($(CONFIG_MSHV_ROOT) $(CONFIG_MSHV_VTL),)
+    obj-y += mshv_common.o
+endif
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 +1783 -0

1,783 lines not on this page · raw message

include/hyperv/hvgdk_mini.h +81 -0

93 lines not on this page · raw message

include/hyperv/hvhdk.h +1 -0
index b4067ada02cf..9b890126e8e8 100644
@@ -479,6 +479,7 @@ struct hv_connection_info {
 #define HV_EVENT_FLAGS_COUNT		(256 * 8)
 #define HV_EVENT_FLAGS_BYTE_COUNT	(256)
 #define HV_EVENT_FLAGS32_COUNT		(256 / sizeof(u32))
+#define HV_EVENT_FLAGS_LONG_COUNT	(HV_EVENT_FLAGS_BYTE_COUNT / sizeof(__u64))
 
 /* linux side we create long version of flags to use long bit ops on flags */
 #define HV_EVENT_FLAGS_UL_COUNT		(256 / sizeof(ulong))
include/uapi/linux/mshv.h +82 -0

86 lines not on this page · raw message

Signature · 1 line
2.34.1
#3 lore raw

RE: [PATCH v2 2/2] Drivers: hv: Introduce mshv_vtl driver

Quoted 32 lines

Provide an interface for Virtual Machine Monitor like OpenVMM and its use as OpenHCL paravisor to control VTL0 (Virtual trust Level). Expose devices and support IOCTLs for features like VTL creation, VTL0 memory management, context switch, making hypercalls, mapping VTL0 address space to VTL2 userspace, getting new VMBus messages and channel events in VTL2 etc.

---
 drivers/hv/Kconfig          |   21 +
 drivers/hv/Makefile         |    7 +-
 drivers/hv/mshv_vtl.h       |   52 +
 drivers/hv/mshv_vtl_main.c  | 1783
+++++++++++++++++++++++++++++++++++
 include/hyperv/hvgdk_mini.h |   81 ++
 include/hyperv/hvhdk.h      |    1 +
 include/uapi/linux/mshv.h   |   82 ++
 7 files changed, 2026 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hv/mshv_vtl.h
 create mode 100644 drivers/hv/mshv_vtl_main.c
drivers/hv/Kconfig +0 -0
index eefa0b559b73..05f990306d40 100644

drivers/hv/Kconfig config MSHV_ROOT · line 79

Quoted 8 lines
drivers/hv/Kconfig +5 -0
@@ -72,4 +72,25 @@ config MSHV_ROOT
 
 	  If unsure, say N.
 
+config MSHV_VTL
+	tristate "Microsoft Hyper-V VTL driver"
+	depends on HYPERV && X86_64
+	depends on TRANSPARENT_HUGEPAGE
+	depends on OF

I am aware that OpenHCL works only with DeviceTree and hence we have to enable CONFIG_OF, but I am not aware of any dependency in this driver, why we need to enable CONFIG_OF by this driver.

We can always enable CONFIG_OF separately in OpenHCL kernel configs. Please check.

- Saurabh

author
#4 lore raw

Re: [PATCH v2 2/2] Drivers: hv: Introduce mshv_vtl driver

Quoted 35 lines · Saurabh Singh Sengar
Quoted 35 lines

Provide an interface for Virtual Machine Monitor like OpenVMM and its use as OpenHCL paravisor to control VTL0 (Virtual trust Level). Expose devices and support IOCTLs for features like VTL creation, VTL0 memory management, context switch, making hypercalls, mapping VTL0 address space to VTL2 userspace, getting new VMBus messages and channel events in VTL2 etc.

---
  drivers/hv/Kconfig          |   21 +
  drivers/hv/Makefile         |    7 +-
  drivers/hv/mshv_vtl.h       |   52 +
  drivers/hv/mshv_vtl_main.c  | 1783
+++++++++++++++++++++++++++++++++++
  include/hyperv/hvgdk_mini.h |   81 ++
  include/hyperv/hvhdk.h      |    1 +
  include/uapi/linux/mshv.h   |   82 ++
  7 files changed, 2026 insertions(+), 1 deletion(-)

create mode 100644 drivers/hv/mshv_vtl.h
create mode 100644 drivers/hv/mshv_vtl_main.c

drivers/hv/Kconfig +0 -0
index eefa0b559b73..05f990306d40 100644
@@ -72,4 +72,25 @@ config MSHV_ROOT
 
  	  If unsure, say N.
 
Quoted 7 lines

drivers/hv/Kconfig config MSHV_ROOT · line 79

Quoted 5 lines
drivers/hv/Kconfig +5 -0
+config MSHV_VTL
+	tristate "Microsoft Hyper-V VTL driver"
+	depends on HYPERV && X86_64
+	depends on TRANSPARENT_HUGEPAGE
+	depends on OF

I am aware that OpenHCL works only with DeviceTree and hence we have to

yes, that was the reason for keeping it.

Quoted 6 lines

enable CONFIG_OF, but I am not aware of any dependency in this driver, why we need to enable CONFIG_OF by this driver.

We can always enable CONFIG_OF separately in OpenHCL kernel configs. Please check.

- Saurabh

But this is right, I'll remove it. Just FYI, rest of the configs and header files are required for it to compile.

Regards,
Naman

Reviewed-by on v2 2/2
#5 lore raw

Re: [PATCH v2 2/2] Drivers: hv: Introduce mshv_vtl driver

In reply to Naman Jain
Quoted 51 lines
drivers/hv/mshv_vtl_main.c new file +50 -0
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2023, Microsoft Corporation.
+ *
+ * Author:
+ *   Roman Kisel <[email protected]>
+ *   Saurabh Sengar <[email protected]>
+ *   Naman Jain <[email protected]>
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/miscdevice.h>
+#include <linux/anon_inodes.h>
+#include <linux/pfn_t.h>
+#include <linux/cpuhotplug.h>
+#include <linux/count_zeros.h>
+#include <linux/eventfd.h>
+#include <linux/poll.h>
+#include <linux/file.h>
+#include <linux/vmalloc.h>
+#include <asm/debugreg.h>
+#include <asm/mshyperv.h>
+#include <trace/events/ipi.h>
+#include <uapi/asm/mtrr.h>
+#include <uapi/linux/mshv.h>
+#include <hyperv/hvhdk.h>
+
+#include "../../kernel/fpu/legacy.h"
+#include "mshv.h"
+#include "mshv_vtl.h"
+#include "hyperv_vmbus.h"
+
+MODULE_AUTHOR("Microsoft");
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Microsoft Hyper-V VTL Driver");
+
+#define MSHV_ENTRY_REASON_LOWER_VTL_CALL     0x1
+#define MSHV_ENTRY_REASON_INTERRUPT          0x2
+#define MSHV_ENTRY_REASON_INTERCEPT          0x3
+
+#define MAX_GUEST_MEM_SIZE	BIT_ULL(40)
+#define MSHV_PG_OFF_CPU_MASK	0xFFFF
+#define MSHV_REAL_OFF_SHIFT	16
+#define MSHV_RUN_PAGE_OFFSET	0
+#define MSHV_REG_PAGE_OFFSET	1
+#define VTL2_VMBUS_SINT_INDEX	7
+
+static struct device *mem_dev;
+

drivers/hv/mshv_vtl_main.c line 60

Quoted 10 lines
drivers/hv/mshv_vtl_main.c new file +10 -0
+static struct tasklet_struct msg_dpc;
+static wait_queue_head_t fd_wait_queue;
+static bool has_message;
+static struct eventfd_ctx *flag_eventfds[HV_EVENT_FLAGS_COUNT];
+static DEFINE_MUTEX(flag_lock);
+static bool __read_mostly mshv_has_reg_page;
+
+struct mshv_vtl_hvcall_fd {
+	u64 allow_bitmap[2 * PAGE_SIZE];
+	bool allow_map_intialized;

typo allow_map_intialized -> allow_map_initialized

Quoted 5 lines
drivers/hv/mshv_vtl_main.c new file +5 -0
+	/*
+	 * Used to protect hvcall setup in IOCTLs
+	 */
+	struct mutex init_mutex;
+	struct miscdevice *dev;

drivers/hv/mshv_vtl_main.c line 75

Quoted 10 lines
drivers/hv/mshv_vtl_main.c new file +10 -0
+};
+
+struct mshv_vtl_poll_file {
+	struct file *file;
+	wait_queue_entry_t wait;
+	wait_queue_head_t *wqh;
+	poll_table pt;
+	int cpu;
+};
+

[clip]

Quoted 16 lines
drivers/hv/mshv_vtl_main.c new file +16 -0
+
+static int mshv_vtl_hvcall_setup(struct mshv_vtl_hvcall_fd *fd,
+				 struct mshv_vtl_hvcall_setup __user *hvcall_setup_user)
+{
+	int ret = 0;
+	struct mshv_vtl_hvcall_setup hvcall_setup;
+
+	mutex_lock(&fd->init_mutex);
+
+	if (fd->allow_map_intialized) {
+		dev_err(fd->dev->this_device,
+			"Hypercall allow map has already been set, pid %d\n",
+			current->pid);
+		ret = -EINVAL;
+		goto exit;
+	}

drivers/hv/mshv_vtl_main.c line 1474

Quoted 10 lines
drivers/hv/mshv_vtl_main.c new file +10 -0
+
+	if (copy_from_user(&hvcall_setup, hvcall_setup_user,
+			   sizeof(struct mshv_vtl_hvcall_setup))) {
+		ret = -EFAULT;
+		goto exit;
+	}
+	if (hvcall_setup.bitmap_size > ARRAY_SIZE(fd->allow_bitmap)) {
+		ret = -EINVAL;
+		goto exit;
+	}

is this valid case if hvcall_setup.bitmap_size == 0 ?

drivers/hv/mshv_vtl_main.c line 1481

Quoted 7 lines
drivers/hv/mshv_vtl_main.c new file +7 -0
+	if (copy_from_user(&fd->allow_bitmap,
+			   (void __user *)hvcall_setup.allow_bitmap_ptr,
+			   hvcall_setup.bitmap_size)) {
+		ret = -EFAULT;
+		goto exit;
+	}
+

[clip]

Thanks,
Alok

author
#6 lore raw

Re: [PATCH v2 2/2] Drivers: hv: Introduce mshv_vtl driver

Quoted 55 lines · ALOK TIWARI
Quoted 55 lines
drivers/hv/mshv_vtl_main.c new file +54 -0
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2023, Microsoft Corporation.
+ *
+ * Author:
+ *   Roman Kisel <[email protected]>
+ *   Saurabh Sengar <[email protected]>
+ *   Naman Jain <[email protected]>
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/miscdevice.h>
+#include <linux/anon_inodes.h>
+#include <linux/pfn_t.h>
+#include <linux/cpuhotplug.h>
+#include <linux/count_zeros.h>
+#include <linux/eventfd.h>
+#include <linux/poll.h>
+#include <linux/file.h>
+#include <linux/vmalloc.h>
+#include <asm/debugreg.h>
+#include <asm/mshyperv.h>
+#include <trace/events/ipi.h>
+#include <uapi/asm/mtrr.h>
+#include <uapi/linux/mshv.h>
+#include <hyperv/hvhdk.h>
+
+#include "../../kernel/fpu/legacy.h"
+#include "mshv.h"
+#include "mshv_vtl.h"
+#include "hyperv_vmbus.h"
+
+MODULE_AUTHOR("Microsoft");
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Microsoft Hyper-V VTL Driver");
+
+#define MSHV_ENTRY_REASON_LOWER_VTL_CALL     0x1
+#define MSHV_ENTRY_REASON_INTERRUPT          0x2
+#define MSHV_ENTRY_REASON_INTERCEPT          0x3
+
+#define MAX_GUEST_MEM_SIZE    BIT_ULL(40)
+#define MSHV_PG_OFF_CPU_MASK    0xFFFF
+#define MSHV_REAL_OFF_SHIFT    16
+#define MSHV_RUN_PAGE_OFFSET    0
+#define MSHV_REG_PAGE_OFFSET    1
+#define VTL2_VMBUS_SINT_INDEX    7
+
+static struct device *mem_dev;
+
+static struct tasklet_struct msg_dpc;
+static wait_queue_head_t fd_wait_queue;
+static bool has_message;
+static struct eventfd_ctx *flag_eventfds[HV_EVENT_FLAGS_COUNT];
Quoted 9 lines

drivers/hv/mshv_vtl_main.c line 60

Quoted 6 lines
drivers/hv/mshv_vtl_main.c new file +6 -0
+static DEFINE_MUTEX(flag_lock);
+static bool __read_mostly mshv_has_reg_page;
+
+struct mshv_vtl_hvcall_fd {
+    u64 allow_bitmap[2 * PAGE_SIZE];
+    bool allow_map_intialized;

typo allow_map_intialized -> allow_map_initialized

Noted. Will change it here and at other places. Thanks.

Quoted 36 lines
Quoted 5 lines

+    /*
+     * Used to protect hvcall setup in IOCTLs
+     */
+    struct mutex init_mutex;
+    struct miscdevice *dev;

drivers/hv/mshv_vtl_main.c line 75

Quoted 10 lines
drivers/hv/mshv_vtl_main.c new file +10 -0
+};
+
+struct mshv_vtl_poll_file {
+    struct file *file;
+    wait_queue_entry_t wait;
+    wait_queue_head_t *wqh;
+    poll_table pt;
+    int cpu;
+};
+

[clip]

Quoted 20 lines
drivers/hv/mshv_vtl_main.c new file +20 -0
+
+static int mshv_vtl_hvcall_setup(struct mshv_vtl_hvcall_fd *fd,
+                 struct mshv_vtl_hvcall_setup __user *hvcall_setup_user)
+{
+    int ret = 0;
+    struct mshv_vtl_hvcall_setup hvcall_setup;
+
+    mutex_lock(&fd->init_mutex);
+
+    if (fd->allow_map_intialized) {
+        dev_err(fd->dev->this_device,
+            "Hypercall allow map has already been set, pid %d\n",
+            current->pid);
+        ret = -EINVAL;
+        goto exit;
+    }
+
+    if (copy_from_user(&hvcall_setup, hvcall_setup_user,
+               sizeof(struct mshv_vtl_hvcall_setup))) {
+        ret = -EFAULT;
Quoted 8 lines
Quoted 6 lines

+        goto exit;
+    }
+    if (hvcall_setup.bitmap_size > ARRAY_SIZE(fd->allow_bitmap)) { +        ret = -EINVAL;
+        goto exit;
+    }

is this valid case if hvcall_setup.bitmap_size == 0 ?

If bitmap_size is 0, then nothing will be copied in copy_from_user() to .allow_bitmap and then mshv_vtl_hvcall_is_allowed() will start returning 0/false for further hvcalls . This sounds reasonable to me.

Quoted 8 lines
Quoted 8 lines

+    if (copy_from_user(&fd->allow_bitmap,
+               (void __user *)hvcall_setup.allow_bitmap_ptr, +               hvcall_setup.bitmap_size)) {
+        ret = -EFAULT;
+        goto exit;
+    }
+

Quoted 8 lines

[clip]

Thanks,
Alok

Thanks again for reviewing.

Regards,
Naman