Skip to contentSkip to conversation

Drivers: hv: Introduce new driver - mshv_vtl

PATCH Superseded Naman Jain 4 participants 14 messages Nov 10, 2025 – Nov 12, 2025

Subsystems (4)

v11 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Naman Jain

author
#0 lore raw

v11 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 v10:
https://lore.kernel.org/all/[email protected]/
* Addressed Peter's comments, by changing EXPORT_SYMBOL_GPL to EXPORT_SYMBOL_FOR_MODULES.
* Add a dependency on HYPERV_VMBUS for MSHV_VTL after recent changes to separate out HYPERV_VMBUS config and making it tristate. Without this change, compilation would fail if HYPERV_VMBUS is 'm' and 'MSHV_VTL' is 'y'.
* Rebased to linux-next tag: next-20251110.

Changes since v9:
https://lore.kernel.org/all/[email protected]/
* Fixed CR2 restore logic in VTL return assembly code
* Fixed an issue with rbp register clobbering in the wrapper function mshv_vtl_return_hypercall and marked it with STACK_FRAME_NON_STANDARD_FP to prevent objtool warning. This addresses an issue which manifested as a crash in VTL0, as the rbp register value set up by VTL2 before VTL transition changes in this wrapper function as part of save restore of the stack pointer.
* Minor checkpatch fix for use of extern function in hv_vtl.c
* Rebased to tag: next-20251028

Changes since v8:
https://lore.kernel.org/all/[email protected]/
Addressed Sean's comments:
* Removed forcing SIGPENDING, and other minor changes, in mshv_vtl_ioctl_return_to_lower_vtl after referring to Sean's earlier changes for xfer_to_guest_mode_handle_work.

* Rebased and resolved merge conflicts, compilation errors on latest linux-next kernel tip, after Roman's Confidential VM changes, which merged recently. No functional changes.
https://lore.kernel.org/all/[email protected]/

Changes since v7:
https://lore.kernel.org/all/[email protected]/
Addressed Peter's concerns. Thanks Peter, Paolo, Sean for valuable inputs. Discussion- https://lore.kernel.org/all/[email protected]/
* moved assembly code to arch/x86/
* This prevents the need to export hv_hypercall_pg
* Will make it easier to add support for other architectures in the future
* moved assembly code to a separate .S file (arch/x86/hyperv/mshv_vtl_asm.S)
* Used noinstr for this new function in .S file
* Fixed save/restore logic of callee registers, rbp to fix previous objtool warning
* used static call instead of indirect call
* used asm offsets similar to KVM code in assembly file (arch/x86/hyperv/mshv-asm-offsets.c)
* Removed the usage of STACK_FRAME_NON_STANDARD.

Other changes-
* Changed logic to use xfer_to_guest_mode_handle_work and VIRT_XFER_TO_GUEST_WORK after recently merged changes.
* Removed Reviewed-by Tags after recent changes.

Changes since v6:
https://lore.kernel.org/all/[email protected]/
Addressed Michael's comments:
* Corrected MAX_BITMAP_SIZE size - finally
* Added missing __packed to hv_synic_overlay_page_msr
* Fixed typo in comment, added CPU hotplug info in comments
* Reverted to mutex_lock/unlock in mshv_vtl_ioctl_set_poll_file
* Unified mshv_vtl_set_reg and mshv_vtl_get_reg
* Dynamic to static allocation of reg in mshv_vtl_ioctl_(get|set)_regs
* Fixed error handling in mshv_vtl_sint_ioctl_signal_event()

Changes since v5:
https://lore.kernel.org/all/[email protected]/
Addressed Michael Kelley's suggestions:
* Added "depends on HYPERV_VTL_MODE", removed "depends on HYPERV" in Kconfig
* Removed unused macro MAX_GUEST_MEM_SIZE
* Made macro dependency explicit: MSHV_PG_OFF_CPU_MASK and MSHV_REAL_OFF_SHIFT
* Refactored and corrected how allow_bitmap is used and defined. Removed PAGE_SIZE dependency.
* Added __packed for structure definitions wherever it was missing.
* Moved hv_register_vsm_* union definitions to hvgdk_mini.h, kept mshv_synic_overlay_page_msr in the driver, renamed it and added a comment. (Nuno)
* Introduced global variables input_vtl_zero and input_vtl_normal and used them everywhere these were defined locally
* s/"page_to_phys(reg_page) >> HV_HYP_PAGE_SHIFT"/"page_to_hvpfn(reg_page)" in mshv_vtl_configure_reg_page
* Refactored mshv_vtl_vmbus_isr() to reduce complexity in finding and resetting bits similar to how vmbus_chan_sched is implemented.
* Used __get_free_page() instead in mshv_vtl_alloc_context()
* Added fallback hv_setup_vmbus_handler(vmbus_isr) in hv_vtl_setup_synic() and in hv_vtl_remove_synic().
* Maintained symmetry of functions in hv_vtl_remove_synic
* Added a note for explanation of excluding last PFN in the range provided in mshv_vtl_ioctl_add_vtl0_mem()
* Added comments for hotplug being not supported, wherever cpu_online() was used to check if CPU is online or not.
* Added a check for input.cpu to make sure it's less than nr_cpu_ids in mshv_vtl_ioctl_set_poll_file()
* Removed switch-case and implemented static tables in mshv_vtl_(get|set)_reg for reducing LOC
* Simplified mshv_vtl_ioctl_(get|set)_regs to process one register at a time, and fixed earlier bug with array of registers processing.
* Used hv_result_to_errno() in mshv_vtl_sint_ioctl_signal_event()
* Added a READ_ONCE() while reading old_eventfd in mshv_vtl_sint_ioctl_set_eventfd()
* Renamed mshv_vtl_hvcall and mshv_vtl_hvcall_setup to remove ambiguity
* Took care of latest mm patches regarding PFN_DEV, pfn_t deprecation
* Few other minor changes while reorganizing code.

Addressed Markus Elfring's suggestions:
* Used guard(mutex) for better mutex handling.

Changes since v4:
https://lore.kernel.org/all/[email protected]/
* Fixed warnings from kernel test robot for missing export.h when the kernel is compiled with W=1 option.
Some recent changes in kernel flags these warnings and that's why it was not seen in previous runs. Warnings in other Hyper-V drivers will be fixed separately.
* No functional changes

Changes since v3:
https://lore.kernel.org/all/[email protected]/
Addressed Stanislav's, Nuno's comments.
* Change data types for different variables, excluding the ones in uapi headers
* Added comment for the need of HUGEPAGES config in Kconfig.
* generalized new IOCTL names by removing VTL in their name.

* Rebased and added Saurabh's Reviewed-by tag

Changes since v2:
https://lore.kernel.org/all/[email protected]/
* Removed CONFIG_OF dependency (addressed Saurabh's comments)
* Fixed typo in "allow_map_intialized" variable name

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

 arch/x86/hyperv/Makefile           |   10 +-
 arch/x86/hyperv/hv_vtl.c           |   43 +
 arch/x86/hyperv/mshv-asm-offsets.c |   37 +
 arch/x86/hyperv/mshv_vtl_asm.S     |   98 ++
 arch/x86/include/asm/mshyperv.h    |   34 +
 drivers/hv/Kconfig                 |   27 +-
 drivers/hv/Makefile                |    7 +-
 drivers/hv/hv.c                    |    3 +
 drivers/hv/hyperv_vmbus.h          |    1 +
 drivers/hv/mshv_vtl.h              |   25 +
 drivers/hv/mshv_vtl_main.c         | 1392 ++++++++++++++++++++++++++++
 drivers/hv/vmbus_drv.c             |    4 +-
 include/hyperv/hvgdk_mini.h        |  106 +++
 include/uapi/linux/mshv.h          |   80 ++
 14 files changed, 1863 insertions(+), 4 deletions(-)
 create mode 100644 arch/x86/hyperv/mshv-asm-offsets.c
 create mode 100644 arch/x86/hyperv/mshv_vtl_asm.S
 create mode 100644 drivers/hv/mshv_vtl.h
 create mode 100644 drivers/hv/mshv_vtl_main.c

base-commit: ab40c92c74c6b0c611c89516794502b3a3173966

Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v11 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           | 3 +++
 drivers/hv/hyperv_vmbus.h | 1 +
 drivers/hv/vmbus_drv.c    | 4 +++-
 3 files changed, 7 insertions(+), 1 deletion(-)
drivers/hv/hv.c +3 -0
index 936c5f310df6..c100f04b3581 100644
@@ -18,6 +18,7 @@
 #include <linux/clockchips.h>
 #include <linux/delay.h>
 #include <linux/interrupt.h>
+#include <linux/export.h>
 #include <clocksource/hyperv_timer.h>
 #include <asm/mshyperv.h>
 #include <linux/set_memory.h>
@@ -25,6 +26,7 @@
 
 /* The one and only */
 struct hv_context hv_context;
+EXPORT_SYMBOL_FOR_MODULES(hv_context, "mshv_vtl");
 
 /*
  * hv_init - Main initialization routine.
@@ -104,6 +106,7 @@ int hv_post_message(union hv_connection_id connection_id,
 
 	return hv_result(status);
 }
+EXPORT_SYMBOL_FOR_MODULES(hv_post_message, "mshv_vtl");
 
 static int hv_alloc_page(void **page, bool decrypt, const char *note)
 {
drivers/hv/hyperv_vmbus.h +1 -0
index f7fc2630c054..b2862e0a317a 100644
@@ -33,6 +33,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 +3 -1
index 0dc4692b411a..47fcab38398a 100644
@@ -36,6 +36,7 @@
 #include <linux/syscore_ops.h>
 #include <linux/dma-map-ops.h>
 #include <linux/pci.h>
+#include <linux/export.h>
 #include <clocksource/hyperv_timer.h>
 #include <asm/mshyperv.h>
 #include "hyperv_vmbus.h"
@@ -1349,7 +1350,7 @@ static void vmbus_message_sched(struct hv_per_cpu_context *hv_cpu, void *message
 	}
 }
 
-static void vmbus_isr(void)
+void vmbus_isr(void)
 {
 	struct hv_per_cpu_context *hv_cpu
 		= this_cpu_ptr(hv_context.cpu_context);
@@ -1362,6 +1363,7 @@ static void vmbus_isr(void)
 
 	add_interrupt_randomness(vmbus_interrupt);
 }
+EXPORT_SYMBOL_FOR_MODULES(vmbus_isr, "mshv_vtl");
 
 static irqreturn_t vmbus_percpu_isr(int irq, void *dev_id)
 {
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v11 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.

---
 arch/x86/hyperv/Makefile           |   10 +-
 arch/x86/hyperv/hv_vtl.c           |   43 +
 arch/x86/hyperv/mshv-asm-offsets.c |   37 +
 arch/x86/hyperv/mshv_vtl_asm.S     |   98 ++
 arch/x86/include/asm/mshyperv.h    |   34 +
 drivers/hv/Kconfig                 |   27 +-
 drivers/hv/Makefile                |    7 +-
 drivers/hv/mshv_vtl.h              |   25 +
 drivers/hv/mshv_vtl_main.c         | 1392 ++++++++++++++++++++++++++++
 include/hyperv/hvgdk_mini.h        |  106 +++
 include/uapi/linux/mshv.h          |   80 ++
 11 files changed, 1856 insertions(+), 3 deletions(-)
 create mode 100644 arch/x86/hyperv/mshv-asm-offsets.c
 create mode 100644 arch/x86/hyperv/mshv_vtl_asm.S
 create mode 100644 drivers/hv/mshv_vtl.h
 create mode 100644 drivers/hv/mshv_vtl_main.c
arch/x86/hyperv/Makefile +9 -1
index 6f5d97cddd80..56292102af62 100644
@@ -1,7 +1,12 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-y			:= hv_init.o mmu.o nested.o irqdomain.o ivm.o
 obj-$(CONFIG_X86_64)	+= hv_apic.o
-obj-$(CONFIG_HYPERV_VTL_MODE)	+= hv_vtl.o
+obj-$(CONFIG_HYPERV_VTL_MODE)	+= hv_vtl.o mshv_vtl_asm.o
+
+$(obj)/mshv_vtl_asm.o: $(obj)/mshv-asm-offsets.h
+
+$(obj)/mshv-asm-offsets.h: $(obj)/mshv-asm-offsets.s FORCE
+	$(call filechk,offsets,__MSHV_ASM_OFFSETS_H__)
 
 ifdef CONFIG_X86_64
 obj-$(CONFIG_PARAVIRT_SPINLOCKS)	+= hv_spinlock.o
@@ -12,3 +17,6 @@ obj-$(CONFIG_PARAVIRT_SPINLOCKS)	+= hv_spinlock.o
   obj-$(CONFIG_CRASH_DUMP)      += hv_crash.o hv_trampoline.o
  endif
 endif
+
+targets += mshv-asm-offsets.s
+clean-files += mshv-asm-offsets.h
arch/x86/hyperv/hv_vtl.c +43 -0

58 lines not on this page · raw message

arch/x86/hyperv/mshv-asm-offsets.c new file +37 -0
new file mode 100644
index 000000000000..882c1db6df16
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Generate definitions needed by assembly language modules.
+ * This code generates raw asm output which is post-processed to extract
+ * and format the required data.
+ *
+ * Copyright (c) 2025, Microsoft Corporation.
+ *
+ * Author:
+ *   Naman Jain <[email protected]>
+ */
+#define COMPILE_OFFSETS
+
+#include <linux/kbuild.h>
+#include <asm/mshyperv.h>
+
+static void __used common(void)
+{
+	if (IS_ENABLED(CONFIG_HYPERV_VTL_MODE)) {
+		OFFSET(MSHV_VTL_CPU_CONTEXT_rax, mshv_vtl_cpu_context, rax);
+		OFFSET(MSHV_VTL_CPU_CONTEXT_rcx, mshv_vtl_cpu_context, rcx);
+		OFFSET(MSHV_VTL_CPU_CONTEXT_rdx, mshv_vtl_cpu_context, rdx);
+		OFFSET(MSHV_VTL_CPU_CONTEXT_rbx, mshv_vtl_cpu_context, rbx);
+		OFFSET(MSHV_VTL_CPU_CONTEXT_rbp, mshv_vtl_cpu_context, rbp);
+		OFFSET(MSHV_VTL_CPU_CONTEXT_rsi, mshv_vtl_cpu_context, rsi);
+		OFFSET(MSHV_VTL_CPU_CONTEXT_rdi, mshv_vtl_cpu_context, rdi);
+		OFFSET(MSHV_VTL_CPU_CONTEXT_r8,  mshv_vtl_cpu_context, r8);
+		OFFSET(MSHV_VTL_CPU_CONTEXT_r9,  mshv_vtl_cpu_context, r9);
+		OFFSET(MSHV_VTL_CPU_CONTEXT_r10, mshv_vtl_cpu_context, r10);
+		OFFSET(MSHV_VTL_CPU_CONTEXT_r11, mshv_vtl_cpu_context, r11);
+		OFFSET(MSHV_VTL_CPU_CONTEXT_r12, mshv_vtl_cpu_context, r12);
+		OFFSET(MSHV_VTL_CPU_CONTEXT_r13, mshv_vtl_cpu_context, r13);
+		OFFSET(MSHV_VTL_CPU_CONTEXT_r14, mshv_vtl_cpu_context, r14);
+		OFFSET(MSHV_VTL_CPU_CONTEXT_r15, mshv_vtl_cpu_context, r15);
+		OFFSET(MSHV_VTL_CPU_CONTEXT_cr2, mshv_vtl_cpu_context, cr2);
+	}
+}
arch/x86/hyperv/mshv_vtl_asm.S new file +98 -0

98 lines not on this page · raw message

arch/x86/include/asm/mshyperv.h +34 -0
index 1342d55c2545..10037125099a 100644
@@ -11,6 +11,7 @@
 #include <asm/paravirt.h>
 #include <asm/msr.h>
 #include <hyperv/hvhdk.h>
+#include <asm/fpu/types.h>
 
 /*
  * Hyper-V always provides a single IO-APIC at this MMIO address.
@@ -269,13 +270,46 @@ static inline u64 hv_get_non_nested_msr(unsigned int reg) { return 0; }
 static inline int hv_apicid_to_vp_index(u32 apic_id) { return -EINVAL; }
 #endif /* CONFIG_HYPERV */
 
+struct mshv_vtl_cpu_context {
+	union {
+		struct {
+			u64 rax;
+			u64 rcx;
+			u64 rdx;
+			u64 rbx;
+			u64 cr2;
+			u64 rbp;
+			u64 rsi;
+			u64 rdi;
+			u64 r8;
+			u64 r9;
+			u64 r10;
+			u64 r11;
+			u64 r12;
+			u64 r13;
+			u64 r14;
+			u64 r15;
+		};
+		u64 gp_regs[16];
+	};
+
+	struct fxregs_state fx_state;
+};
 
 #ifdef CONFIG_HYPERV_VTL_MODE
 void __init hv_vtl_init_platform(void);
 int __init hv_vtl_early_init(void);
+void mshv_vtl_return_call(struct mshv_vtl_cpu_context *vtl0);
+void mshv_vtl_return_call_init(u64 vtl_return_offset);
+void mshv_vtl_return_hypercall(void);
+void __mshv_vtl_return_call(struct mshv_vtl_cpu_context *vtl0);
 #else
 static inline void __init hv_vtl_init_platform(void) {}
 static inline int __init hv_vtl_early_init(void) { return 0; }
+static inline void mshv_vtl_return_call(struct mshv_vtl_cpu_context *vtl0) {}
+static inline void mshv_vtl_return_call_init(u64 vtl_return_offset) {}
+static inline void mshv_vtl_return_hypercall(void) {}
+static inline void __mshv_vtl_return_call(struct mshv_vtl_cpu_context *vtl0) {}
 #endif
 
 #include <asm-generic/mshyperv.h>
drivers/hv/Kconfig +26 -1
index 0b8c391a0342..d4a8d349200c 100644
@@ -17,7 +17,8 @@ config HYPERV
 
 config HYPERV_VTL_MODE
 	bool "Enable Linux to boot in VTL context"
-	depends on (X86_64 || ARM64) && HYPERV
+	depends on (X86_64 && HAVE_STATIC_CALL) || ARM64
+	depends on HYPERV
 	depends on SMP
 	default n
 	help
@@ -82,4 +83,28 @@ config MSHV_ROOT
 
 	  If unsure, say N.
 
+config MSHV_VTL
+	tristate "Microsoft Hyper-V VTL driver"
+	depends on X86_64 && HYPERV_VTL_MODE
+	depends on HYPERV_VMBUS
+	# Mapping VTL0 memory to a userspace process in VTL2 is supported in OpenHCL.
+	# VTL2 for OpenHCL makes use of Huge Pages to improve performance on VMs,
+	# specially with large memory requirements.
+	depends on TRANSPARENT_HUGEPAGE
+	# 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 VIRT_XFER_TO_GUEST_WORK
+	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 1a1677bf4dac..58b8d07639f3 100644
@@ -3,6 +3,7 @@ obj-$(CONFIG_HYPERV_VMBUS)	+= 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-$(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 +25 -0
new file mode 100644
index 000000000000..a6eea52f7aa2
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+#ifndef _MSHV_VTL_H
+#define _MSHV_VTL_H
+
+#include <linux/mshv.h>
+#include <linux/types.h>
+
+struct mshv_vtl_run {
+	u32 cancel;
+	u32 vtl_ret_action_size;
+	u32 pad[2];
+	char exit_message[MSHV_MAX_RUN_MSG_SIZE];
+	union {
+		struct mshv_vtl_cpu_context cpu_context;
+
+		/*
+		 * Reserving room for the cpu context to grow and to maintain compatibility
+		 * with user mode.
+		 */
+		char reserved[1024];
+	};
+	char vtl_ret_actions[MSHV_MAX_RUN_MSG_SIZE];
+};
+
+#endif /* _MSHV_VTL_H */
drivers/hv/mshv_vtl_main.c new file +1392 -0

1,392 lines not on this page · raw message

include/hyperv/hvgdk_mini.h +106 -0

118 lines not on this page · raw message

include/uapi/linux/mshv.h +80 -0

84 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#3 lore raw

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

Quoted 45 lines · 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.

arch/x86/hyperv/hv_vtl.c +16 -0
index 042e8712d8de..dba27e1bcc10 100644
@@ -249,3 +253,42 @@ int __init hv_vtl_early_init(void)
 
 	return 0;
 }
+
+DEFINE_STATIC_CALL_NULL(__mshv_vtl_return_hypercall, void (*)(void));
+
+noinstr void mshv_vtl_return_hypercall(void)
+{
+	asm volatile ("call " STATIC_CALL_TRAMP_STR(__mshv_vtl_return_hypercall));
+}
+
+/*
+ * ASM_CALL_CONSTRAINT is intentionally not used in above asm block before making a call to
+ * __mshv_vtl_return_hypercall, to avoid rbp clobbering before actual VTL return happens.
+ * This however leads to objtool complain about "call without frame pointer save/setup".
+ * To ignore that warning, and inform objtool about this non-standard function,
+ * STACK_FRAME_NON_STANDARD_FP is used.
+ */
+STACK_FRAME_NON_STANDARD_FP(mshv_vtl_return_hypercall);
arch/x86/hyperv/mshv_vtl_asm.S new file +10 -0
@@ -0,0 +1,98 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Assembly level code for mshv_vtl VTL transition
+ *
+ * Copyright (c) 2025, Microsoft Corporation.
+ *
+ * Author:
+ *   Naman Jain <[email protected]>
+ */
+

arch/x86/hyperv/mshv_vtl_asm.S line 20

Quoted 10 lines
arch/x86/hyperv/mshv_vtl_asm.S new file +10 -0
+#include <linux/linkage.h>
+#include <asm/asm.h>
+#include <asm/asm-offsets.h>
+#include <asm/frame.h>
+#include "mshv-asm-offsets.h"
+
+	.text
+	.section .noinstr.text, "ax"
+/*
+ * void __mshv_vtl_return_call(struct mshv_vtl_cpu_context *vtl0)

Can we please get a few words on the magical context here? Like no NMIs and #DB traps and the like. Because if any of them were possible this code would be horribly broken.

Quoted 30 lines
arch/x86/hyperv/mshv_vtl_asm.S new file +30 -0
+ */
+SYM_FUNC_START(__mshv_vtl_return_call)
+	/* Push callee save registers */
+	pushq %rbp
+	mov %rsp, %rbp
+	pushq %r12
+	pushq %r13
+	pushq %r14
+	pushq %r15
+	pushq %rbx
+
+	/* register switch to VTL0 clobbers all registers except rax/rcx */
+	mov %_ASM_ARG1, %rax
+
+	/* grab rbx/rbp/rsi/rdi/r8-r15 */
+	mov MSHV_VTL_CPU_CONTEXT_rbx(%rax), %rbx
+	mov MSHV_VTL_CPU_CONTEXT_rbp(%rax), %rbp
+	mov MSHV_VTL_CPU_CONTEXT_rsi(%rax), %rsi
+	mov MSHV_VTL_CPU_CONTEXT_rdi(%rax), %rdi
+	mov MSHV_VTL_CPU_CONTEXT_r8(%rax), %r8
+	mov MSHV_VTL_CPU_CONTEXT_r9(%rax), %r9
+	mov MSHV_VTL_CPU_CONTEXT_r10(%rax), %r10
+	mov MSHV_VTL_CPU_CONTEXT_r11(%rax), %r11
+	mov MSHV_VTL_CPU_CONTEXT_r12(%rax), %r12
+	mov MSHV_VTL_CPU_CONTEXT_r13(%rax), %r13
+	mov MSHV_VTL_CPU_CONTEXT_r14(%rax), %r14
+	mov MSHV_VTL_CPU_CONTEXT_r15(%rax), %r15
+
+	mov MSHV_VTL_CPU_CONTEXT_cr2(%rax), %rdx
+	mov %rdx, %cr2

arch/x86/hyperv/mshv_vtl_asm.S line 60

Quoted 10 lines
arch/x86/hyperv/mshv_vtl_asm.S new file +10 -0
+	mov MSHV_VTL_CPU_CONTEXT_rdx(%rax), %rdx
+
+	/* stash host registers on stack */
+	pushq %rax
+	pushq %rcx
+
+	xor %ecx, %ecx
+
+	/* make a hypercall to switch VTL */
+	call mshv_vtl_return_hypercall

Yuck!

This seems to build for me.

---

arch/x86/hyperv/hv_vtl.c +0 -14
@@ -256,20 +256,6 @@ int __init hv_vtl_early_init(void)
 
 DEFINE_STATIC_CALL_NULL(__mshv_vtl_return_hypercall, void (*)(void));
 
-noinstr void mshv_vtl_return_hypercall(void)
-{
-	asm volatile ("call " STATIC_CALL_TRAMP_STR(__mshv_vtl_return_hypercall));
-}
-
-/*
- * ASM_CALL_CONSTRAINT is intentionally not used in above asm block before making a call to
- * __mshv_vtl_return_hypercall, to avoid rbp clobbering before actual VTL return happens.
- * This however leads to objtool complain about "call without frame pointer save/setup".
- * To ignore that warning, and inform objtool about this non-standard function,
- * STACK_FRAME_NON_STANDARD_FP is used.
- */
-STACK_FRAME_NON_STANDARD_FP(mshv_vtl_return_hypercall);
-
 void mshv_vtl_return_call_init(u64 vtl_return_offset)
 {
 	static_call_update(__mshv_vtl_return_hypercall,
arch/x86/hyperv/mshv_vtl_asm.S +2 -1
@@ -9,6 +9,7 @@
  */
 
 #include <linux/linkage.h>
+#include <linux/static_call_types.h>
 #include <asm/asm.h>
 #include <asm/asm-offsets.h>
 #include <asm/frame.h>
@@ -57,7 +58,7 @@ SYM_FUNC_START(__mshv_vtl_return_call)
 	xor %ecx, %ecx
 
 	/* make a hypercall to switch VTL */
-	call mshv_vtl_return_hypercall
+	call STATIC_CALL_TRAMP_STR(__mshv_vtl_return_hypercall)
 
 	/* stash guest registers on stack, restore saved host copies */
 	pushq %rax
include/linux/compiler_types.h +4 -4
@@ -11,6 +11,10 @@
 #define __has_builtin(x) (0)
 #endif
 
+/* Indirect macros required for expanded argument pasting, eg. __LINE__. */
+#define ___PASTE(a,b) a##b
+#define __PASTE(a,b) ___PASTE(a,b)
+
 #ifndef __ASSEMBLY__
 
 /*
@@ -79,10 +83,6 @@ static inline void __chk_io_ptr(const vo
 # define __builtin_warning(x, y...) (1)
 #endif /* __CHECKER__ */
 
-/* Indirect macros required for expanded argument pasting, eg. __LINE__. */
-#define ___PASTE(a,b) a##b
-#define __PASTE(a,b) ___PASTE(a,b)
-
 #ifdef __KERNEL__
 
 /* Attributes */
include/linux/static_call_types.h +4 -0
@@ -25,6 +25,8 @@
 #define STATIC_CALL_SITE_INIT 2UL	/* init section */
 #define STATIC_CALL_SITE_FLAGS 3UL
 
+#ifndef __ASSEMBLY__
+
 /*
  * The static call site table needs to be created by external tooling (objtool
  * or a compiler plugin).
@@ -100,4 +102,6 @@ struct static_call_key {
 
 #endif /* CONFIG_HAVE_STATIC_CALL */
 
+#endif /* __ASSEMBLY__ */
+
 #endif /* _STATIC_CALL_TYPES_H */
author
#4 lore raw

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

Quoted 52 lines · Peter Zijlstra
Quoted 51 lines · 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.

arch/x86/hyperv/hv_vtl.c +16 -0
index 042e8712d8de..dba27e1bcc10 100644
@@ -249,3 +253,42 @@ int __init hv_vtl_early_init(void)
 
  	return 0;
  }
+
+DEFINE_STATIC_CALL_NULL(__mshv_vtl_return_hypercall, void (*)(void));
+
+noinstr void mshv_vtl_return_hypercall(void)
+{
+	asm volatile ("call " STATIC_CALL_TRAMP_STR(__mshv_vtl_return_hypercall));
+}
+
+/*
+ * ASM_CALL_CONSTRAINT is intentionally not used in above asm block before making a call to
+ * __mshv_vtl_return_hypercall, to avoid rbp clobbering before actual VTL return happens.
+ * This however leads to objtool complain about "call without frame pointer save/setup".
+ * To ignore that warning, and inform objtool about this non-standard function,
+ * STACK_FRAME_NON_STANDARD_FP is used.
+ */
+STACK_FRAME_NON_STANDARD_FP(mshv_vtl_return_hypercall);
arch/x86/hyperv/mshv_vtl_asm.S new file +16 -0
@@ -0,0 +1,98 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Assembly level code for mshv_vtl VTL transition
+ *
+ * Copyright (c) 2025, Microsoft Corporation.
+ *
+ * Author:
+ *   Naman Jain <[email protected]>
+ */
+
+#include <linux/linkage.h>
+#include <asm/asm.h>
+#include <asm/asm-offsets.h>
+#include <asm/frame.h>
+#include "mshv-asm-offsets.h"
+
Quoted 8 lines

arch/x86/hyperv/mshv_vtl_asm.S line 20

Quoted 4 lines
arch/x86/hyperv/mshv_vtl_asm.S new file +4 -0
+	.text
+	.section .noinstr.text, "ax"
+/*
+ * void __mshv_vtl_return_call(struct mshv_vtl_cpu_context *vtl0)

Can we please get a few words on the magical context here? Like no NMIs and #DB traps and the like. Because if any of them were possible this code would be horribly broken.

Sure, I can add below comment:

This function is marked as 'noinstr' to prevent against instrumentation and debugging facilities. NMIs aren't a problem because the NMI handler saves/restores CR2 specifically to guard against #PFs in NMI context clobbering the guest state.

Quoted 44 lines
Quoted 31 lines
arch/x86/hyperv/mshv_vtl_asm.S new file +30 -0
+ */
+SYM_FUNC_START(__mshv_vtl_return_call)
+	/* Push callee save registers */
+	pushq %rbp
+	mov %rsp, %rbp
+	pushq %r12
+	pushq %r13
+	pushq %r14
+	pushq %r15
+	pushq %rbx
+
+	/* register switch to VTL0 clobbers all registers except rax/rcx */
+	mov %_ASM_ARG1, %rax
+
+	/* grab rbx/rbp/rsi/rdi/r8-r15 */
+	mov MSHV_VTL_CPU_CONTEXT_rbx(%rax), %rbx
+	mov MSHV_VTL_CPU_CONTEXT_rbp(%rax), %rbp
+	mov MSHV_VTL_CPU_CONTEXT_rsi(%rax), %rsi
+	mov MSHV_VTL_CPU_CONTEXT_rdi(%rax), %rdi
+	mov MSHV_VTL_CPU_CONTEXT_r8(%rax), %r8
+	mov MSHV_VTL_CPU_CONTEXT_r9(%rax), %r9
+	mov MSHV_VTL_CPU_CONTEXT_r10(%rax), %r10
+	mov MSHV_VTL_CPU_CONTEXT_r11(%rax), %r11
+	mov MSHV_VTL_CPU_CONTEXT_r12(%rax), %r12
+	mov MSHV_VTL_CPU_CONTEXT_r13(%rax), %r13
+	mov MSHV_VTL_CPU_CONTEXT_r14(%rax), %r14
+	mov MSHV_VTL_CPU_CONTEXT_r15(%rax), %r15
+
+	mov MSHV_VTL_CPU_CONTEXT_cr2(%rax), %rdx
+	mov %rdx, %cr2

arch/x86/hyperv/mshv_vtl_asm.S line 60

Quoted 10 lines
arch/x86/hyperv/mshv_vtl_asm.S new file +10 -0
+	mov MSHV_VTL_CPU_CONTEXT_rdx(%rax), %rdx
+
+	/* stash host registers on stack */
+	pushq %rax
+	pushq %rcx
+
+	xor %ecx, %ecx
+
+	/* make a hypercall to switch VTL */
+	call mshv_vtl_return_hypercall

Yuck!

Quoted 1 line

This seems to build for me.

This would have been the cleanest approach. We discussed this before and unfortunately it didn't work. Please find the link to this discussion:

https://lore.kernel.org/all/[email protected]/

To summarize above discussion, I see below compilation error with this from objtool. You may have CONFIG_X86_KERNEL_IBT enabled in your workspace, which would have masked this.

AS arch/x86/hyperv/mshv_vtl_asm.o
arch/x86/hyperv/mshv_vtl_asm.o: error: objtool: static_call: can't find static_call_key symbol: __SCK____mshv_vtl_return_hypercall make[4]: *** [scripts/Makefile.build:430: arch/x86/hyperv/mshv_vtl_asm.o] Error 255
make[4]: *** Deleting file 'arch/x86/hyperv/mshv_vtl_asm.o'
make[3]: *** [scripts/Makefile.build:556: arch/x86/hyperv] Error 2

Because of this compilation error, I had to implement this wrapper function (mshv_vtl_return_hypercall) which makes this static call in C.

Quoted 28 lines

---

arch/x86/hyperv/hv_vtl.c +0 -14
@@ -256,20 +256,6 @@ int __init hv_vtl_early_init(void)
 
  DEFINE_STATIC_CALL_NULL(__mshv_vtl_return_hypercall, void (*)(void));
 
-noinstr void mshv_vtl_return_hypercall(void)
-{
-	asm volatile ("call " STATIC_CALL_TRAMP_STR(__mshv_vtl_return_hypercall));
-}
-
-/*
- * ASM_CALL_CONSTRAINT is intentionally not used in above asm block before making a call to
- * __mshv_vtl_return_hypercall, to avoid rbp clobbering before actual VTL return happens.
- * This however leads to objtool complain about "call without frame pointer save/setup".
- * To ignore that warning, and inform objtool about this non-standard function,
- * STACK_FRAME_NON_STANDARD_FP is used.
- */
-STACK_FRAME_NON_STANDARD_FP(mshv_vtl_return_hypercall)
-
  void mshv_vtl_return_call_init(u64 vtl_return_offset)
  {
  	static_call_update(__mshv_vtl_return_hypercall,

arch/x86/hyperv/mshv_vtl_asm.S line 9

Quoted 1 line
arch/x86/hyperv/mshv_vtl_asm.S +0 -0
@@ -9,6 +9,7 @@
   */

<snip>

Regards,
Naman

maintainer
#5 lore raw

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

Quoted 8 lines · Naman Jain

This would have been the cleanest approach. We discussed this before and unfortunately it didn't work. Please find the link to this discussion:

https://lore.kernel.org/all/[email protected]/

To summarize above discussion, I see below compilation error with this from objtool. You may have CONFIG_X86_KERNEL_IBT enabled in your workspace, which would have masked this.

IBT isn't the problem, the thing is running objtool on vmlinux.o vs the individual translation units. vmlinux.o will have that symbol, while your .S file doesn't.

Quoted 3 lines

AS arch/x86/hyperv/mshv_vtl_asm.o
arch/x86/hyperv/mshv_vtl_asm.o: error: objtool: static_call: can't find static_call_key symbol: __SCK____mshv_vtl_return_hypercall

Right, and I said you had to do that ADDRESSABLE thing. So I added a DECLARE_STATIC_CALL() and a static_call() in hv.c, compiled it so .s and stole the bits.

And then you get something like the below. See symbol 5, that's the entry we need.

# readelf -sW defconfig-build/arch/x86/hyperv/mshv_vtl_asm.o

Symbol table '.symtab' contains 8 entries:

   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000000000     8 OBJECT  LOCAL  DEFAULT    6 __UNIQUE_ID_addressable___SCK____mshv_vtl_return_hypercall_662.0
     2: 0000000000000000     0 SECTION LOCAL  DEFAULT    4 .noinstr.text
     3: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND __SCT____mshv_vtl_return_hypercall
     4: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND __x86_return_thunk
     5: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND __SCK____mshv_vtl_return_hypercall
     6: 0000000000000010   179 FUNC    GLOBAL DEFAULT    4 __mshv_vtl_return_call
     7: 0000000000000000    16 FUNC    GLOBAL DEFAULT    4 __pfx___mshv_vtl_return_call

---

arch/x86/hyperv/hv_vtl.c +0 -14
@@ -256,20 +256,6 @@ int __init hv_vtl_early_init(void)
 
 DEFINE_STATIC_CALL_NULL(__mshv_vtl_return_hypercall, void (*)(void));
 
-noinstr void mshv_vtl_return_hypercall(void)
-{
-	asm volatile ("call " STATIC_CALL_TRAMP_STR(__mshv_vtl_return_hypercall));
-}
-
-/*
- * ASM_CALL_CONSTRAINT is intentionally not used in above asm block before making a call to
- * __mshv_vtl_return_hypercall, to avoid rbp clobbering before actual VTL return happens.
- * This however leads to objtool complain about "call without frame pointer save/setup".
- * To ignore that warning, and inform objtool about this non-standard function,
- * STACK_FRAME_NON_STANDARD_FP is used.
- */
-STACK_FRAME_NON_STANDARD_FP(mshv_vtl_return_hypercall);
-
 void mshv_vtl_return_call_init(u64 vtl_return_offset)
 {
 	static_call_update(__mshv_vtl_return_hypercall,
arch/x86/hyperv/mshv_vtl_asm.S +9 -1
@@ -9,6 +9,7 @@
  */
 
 #include <linux/linkage.h>
+#include <linux/static_call_types.h>
 #include <asm/asm.h>
 #include <asm/asm-offsets.h>
 #include <asm/frame.h>
@@ -57,7 +58,7 @@ SYM_FUNC_START(__mshv_vtl_return_call)
 	xor %ecx, %ecx
 
 	/* make a hypercall to switch VTL */
-	call mshv_vtl_return_hypercall
+	call STATIC_CALL_TRAMP_STR(__mshv_vtl_return_hypercall)
 
 	/* stash guest registers on stack, restore saved host copies */
 	pushq %rax
@@ -96,3 +97,10 @@ SYM_FUNC_START(__mshv_vtl_return_call)
 	pop %rbp
 	RET
 SYM_FUNC_END(__mshv_vtl_return_call)
+
+	.section	.discard.addressable,"aw"
+	.align 8
+	.type	__UNIQUE_ID_addressable___SCK____mshv_vtl_return_hypercall_662.0, @object
+	.size	__UNIQUE_ID_addressable___SCK____mshv_vtl_return_hypercall_662.0, 8
+__UNIQUE_ID_addressable___SCK____mshv_vtl_return_hypercall_662.0:
+	.quad	__SCK____mshv_vtl_return_hypercall
include/linux/compiler_types.h +4 -4
@@ -11,6 +11,10 @@
 #define __has_builtin(x) (0)
 #endif
 
+/* Indirect macros required for expanded argument pasting, eg. __LINE__. */
+#define ___PASTE(a,b) a##b
+#define __PASTE(a,b) ___PASTE(a,b)
+
 #ifndef __ASSEMBLY__
 
 /*
@@ -79,10 +83,6 @@ static inline void __chk_io_ptr(const vo
 # define __builtin_warning(x, y...) (1)
 #endif /* __CHECKER__ */
 
-/* Indirect macros required for expanded argument pasting, eg. __LINE__. */
-#define ___PASTE(a,b) a##b
-#define __PASTE(a,b) ___PASTE(a,b)
-
 #ifdef __KERNEL__
 
 /* Attributes */
include/linux/static_call_types.h +4 -0
@@ -25,6 +25,8 @@
 #define STATIC_CALL_SITE_INIT 2UL	/* init section */
 #define STATIC_CALL_SITE_FLAGS 3UL
 
+#ifndef __ASSEMBLY__
+
 /*
  * The static call site table needs to be created by external tooling (objtool
  * or a compiler plugin).
@@ -100,4 +102,6 @@ struct static_call_key {
 
 #endif /* CONFIG_HAVE_STATIC_CALL */
 
+#endif /* __ASSEMBLY__ */
+
 #endif /* _STATIC_CALL_TYPES_H */
author
#6 lore raw

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

Quoted 40 lines · Peter Zijlstra
Quoted 8 lines · Naman Jain

This would have been the cleanest approach. We discussed this before and unfortunately it didn't work. Please find the link to this discussion:

https://lore.kernel.org/all/[email protected]/

To summarize above discussion, I see below compilation error with this from objtool. You may have CONFIG_X86_KERNEL_IBT enabled in your workspace, which would have masked this.

IBT isn't the problem, the thing is running objtool on vmlinux.o vs the individual translation units. vmlinux.o will have that symbol, while your .S file doesn't.

Quoted 3 lines

AS arch/x86/hyperv/mshv_vtl_asm.o
arch/x86/hyperv/mshv_vtl_asm.o: error: objtool: static_call: can't find static_call_key symbol: __SCK____mshv_vtl_return_hypercall

Right, and I said you had to do that ADDRESSABLE thing. So I added a DECLARE_STATIC_CALL() and a static_call() in hv.c, compiled it so .s and stole the bits.

And then you get something like the below. See symbol 5, that's the entry we need.

# readelf -sW defconfig-build/arch/x86/hyperv/mshv_vtl_asm.o

Symbol table '.symtab' contains 8 entries:

    Num:    Value          Size Type    Bind   Vis      Ndx Name
      0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
      1: 0000000000000000     8 OBJECT  LOCAL  DEFAULT    6 __UNIQUE_ID_addressable___SCK____mshv_vtl_return_hypercall_662.0
      2: 0000000000000000     0 SECTION LOCAL  DEFAULT    4 .noinstr.text
      3: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND __SCT____mshv_vtl_return_hypercall
      4: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND __x86_return_thunk
      5: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND __SCK____mshv_vtl_return_hypercall
      6: 0000000000000010   179 FUNC    GLOBAL DEFAULT    4 __mshv_vtl_return_call
      7: 0000000000000000    16 FUNC    GLOBAL DEFAULT    4 __pfx___mshv_vtl_return_call
Quoted 1 line

---

Thanks a lot for sharing the changes. I tested this and it works fine. I can create a separate patch for the include/linux/* changes and add it as the first patch in the next version of my patch series.

Please let me know if this is fine and if I can add your Signed-off-by in that patch.

Regards,
Naman

maintainer
#7 lore raw

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

Quoted 46 lines · Naman Jain
Quoted 40 lines · Peter Zijlstra
Quoted 8 lines · Naman Jain

This would have been the cleanest approach. We discussed this before and unfortunately it didn't work. Please find the link to this discussion:

https://lore.kernel.org/all/[email protected]/

To summarize above discussion, I see below compilation error with this from objtool. You may have CONFIG_X86_KERNEL_IBT enabled in your workspace, which would have masked this.

IBT isn't the problem, the thing is running objtool on vmlinux.o vs the individual translation units. vmlinux.o will have that symbol, while your .S file doesn't.

Quoted 3 lines

AS arch/x86/hyperv/mshv_vtl_asm.o
arch/x86/hyperv/mshv_vtl_asm.o: error: objtool: static_call: can't find static_call_key symbol: __SCK____mshv_vtl_return_hypercall

Right, and I said you had to do that ADDRESSABLE thing. So I added a DECLARE_STATIC_CALL() and a static_call() in hv.c, compiled it so .s and stole the bits.

And then you get something like the below. See symbol 5, that's the entry we need.

# readelf -sW defconfig-build/arch/x86/hyperv/mshv_vtl_asm.o

Symbol table '.symtab' contains 8 entries:

    Num:    Value          Size Type    Bind   Vis      Ndx Name
      0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
      1: 0000000000000000     8 OBJECT  LOCAL  DEFAULT    6 __UNIQUE_ID_addressable___SCK____mshv_vtl_return_hypercall_662.0
      2: 0000000000000000     0 SECTION LOCAL  DEFAULT    4 .noinstr.text
      3: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND __SCT____mshv_vtl_return_hypercall
      4: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND __x86_return_thunk
      5: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND __SCK____mshv_vtl_return_hypercall
      6: 0000000000000010   179 FUNC    GLOBAL DEFAULT    4 __mshv_vtl_return_call
      7: 0000000000000000    16 FUNC    GLOBAL DEFAULT    4 __pfx___mshv_vtl_return_call
Quoted 1 line

---

Quoted 6 lines

Thanks a lot for sharing the changes. I tested this and it works fine. I can create a separate patch for the include/linux/* changes and add it as the first patch in the next version of my patch series.

Please let me know if this is fine and if I can add your Signed-off-by in that patch.

Sure, have at. Thanks!

#8 lore raw

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

In reply to Peter Zijlstra

From: Peter Zijlstra <[email protected]> Sent: Tuesday, November 11, 2025 12:14 AM

Quoted 80 lines · Naman Jain

IBT isn't the problem, the thing is running objtool on vmlinux.o vs the individual translation units. vmlinux.o will have that symbol, while your .S file doesn't.

Quoted 3 lines

AS arch/x86/hyperv/mshv_vtl_asm.o
arch/x86/hyperv/mshv_vtl_asm.o: error: objtool: static_call: can't find static_call_key symbol: __SCK____mshv_vtl_return_hypercall

Right, and I said you had to do that ADDRESSABLE thing. So I added a DECLARE_STATIC_CALL() and a static_call() in hv.c, compiled it so .s and stole the bits.

And then you get something like the below. See symbol 5, that's the entry we need.

# readelf -sW defconfig-build/arch/x86/hyperv/mshv_vtl_asm.o

Symbol table '.symtab' contains 8 entries:

   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000000000     8 OBJECT  LOCAL  DEFAULT    6
__UNIQUE_ID_addressable___SCK____mshv_vtl_return_hypercall_662.0
     2: 0000000000000000     0 SECTION LOCAL  DEFAULT    4 .noinstr.text
     3: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND
__SCT____mshv_vtl_return_hypercall
     4: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND __x86_return_thunk
     5: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND
__SCK____mshv_vtl_return_hypercall
     6: 0000000000000010   179 FUNC    GLOBAL DEFAULT    4 __mshv_vtl_return_call
     7: 0000000000000000    16 FUNC    GLOBAL DEFAULT    4

__pfx___mshv_vtl_return_call

---

arch/x86/hyperv/hv_vtl.c +0 -14
@@ -256,20 +256,6 @@ int __init hv_vtl_early_init(void)
 
 DEFINE_STATIC_CALL_NULL(__mshv_vtl_return_hypercall, void (*)(void));
 
-noinstr void mshv_vtl_return_hypercall(void)
-{
-	asm volatile ("call " STATIC_CALL_TRAMP_STR(__mshv_vtl_return_hypercall));
-}
-
-/*
- * ASM_CALL_CONSTRAINT is intentionally not used in above asm block before making a call to
- * __mshv_vtl_return_hypercall, to avoid rbp clobbering before actual VTL return happens.
- * This however leads to objtool complain about "call without frame pointer save/setup".
- * To ignore that warning, and inform objtool about this non-standard function,
- * STACK_FRAME_NON_STANDARD_FP is used.
- */
-STACK_FRAME_NON_STANDARD_FP(mshv_vtl_return_hypercall);
-
 void mshv_vtl_return_call_init(u64 vtl_return_offset)
 {
 	static_call_update(__mshv_vtl_return_hypercall,
arch/x86/hyperv/mshv_vtl_asm.S +2 -1
@@ -9,6 +9,7 @@
  */
 
 #include <linux/linkage.h>
+#include <linux/static_call_types.h>
 #include <asm/asm.h>
 #include <asm/asm-offsets.h>
 #include <asm/frame.h>
@@ -57,7 +58,7 @@ SYM_FUNC_START(__mshv_vtl_return_call)
 	xor %ecx, %ecx
 
 	/* make a hypercall to switch VTL */
-	call mshv_vtl_return_hypercall
+	call STATIC_CALL_TRAMP_STR(__mshv_vtl_return_hypercall)
 
 	/* stash guest registers on stack, restore saved host copies */
 	pushq %rax

arch/x86/hyperv/mshv_vtl_asm.S SYM_FUNC_START(__mshv_vtl_return_call) · line 106

Quoted 10 lines
arch/x86/hyperv/mshv_vtl_asm.S +7 -0
@@ -96,3 +97,10 @@ SYM_FUNC_START(__mshv_vtl_return_call)
 	pop %rbp
 	RET
 SYM_FUNC_END(__mshv_vtl_return_call)
+
+	.section	.discard.addressable,"aw"
+	.align 8
+	.type 	__UNIQUE_ID_addressable___SCK____mshv_vtl_return_hypercall_662.0, @object
+	.size 	__UNIQUE_ID_addressable___SCK____mshv_vtl_return_hypercall_662.0, 8
+__UNIQUE_ID_addressable___SCK____mshv_vtl_return_hypercall_662.0:
+	.quad	__SCK____mshv_vtl_return_hypercall

This is pretty yucky itself. Why is it better than calling out to a C function? Is it because in spite of the annotations, there's no guarantee the C compiler won't generate some code that messes up a register value? Or is there some other reason?

Does the magic "_662.0" have any significance? Or is it just some uniqueness salt on the symbol name?

Michael

#9 lore raw

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

Quoted 7 lines · Michael Kelley

arch/x86/hyperv/mshv_vtl_asm.S SYM_FUNC_START(__mshv_vtl_return_call) · line 106

Quoted 6 lines
arch/x86/hyperv/mshv_vtl_asm.S +6 -0
+     .section        .discard.addressable,"aw"
+     .align 8
+     .type   __UNIQUE_ID_addressable___SCK____mshv_vtl_return_hypercall_662.0, @object
+     .size   __UNIQUE_ID_addressable___SCK____mshv_vtl_return_hypercall_662.0, 8
+__UNIQUE_ID_addressable___SCK____mshv_vtl_return_hypercall_662.0:
+     .quad   __SCK____mshv_vtl_return_hypercall
Quoted 7 lines

This is pretty yucky itself. Why is it better than calling out to a C function? Is it because in spite of the annotations, there's no guarantee the C compiler won't generate some code that messes up a register value? Or is there some other reason?

Does the magic "_662.0" have any significance? Or is it just some uniqueness salt on the symbol name?

It's just a counter coming from include/linux/compiler.h:

#define __UNIQUE_ID(prefix) \
        __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
#define ___ADDRESSABLE(sym, __attrs) \
        static void * __used __attrs \
        __UNIQUE_ID(__PASTE(__addressable_,sym)) = (void *)(uintptr_t)&sym;
#define __ADDRESSABLE(sym) \
        ___ADDRESSABLE(sym, __section(".discard.addressable"))

You can replace the whole ugly symbol with just something like __dummy_SCK____mshv_vtl_return_hypercall if you prefer.

Paolo

Paolo

maintainer
#10 lore raw

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

In reply to Michael Kelley
Quoted 5 lines · Michael Kelley
Quoted 5 lines
arch/x86/hyperv/mshv_vtl_asm.S +1 -0
@@ -96,3 +97,10 @@ SYM_FUNC_START(__mshv_vtl_return_call)
 	pop %rbp
 	RET
 SYM_FUNC_END(__mshv_vtl_return_call)
+
Quoted 8 lines

arch/x86/hyperv/mshv_vtl_asm.S SYM_FUNC_START(__mshv_vtl_return_call) · line 106

Quoted 6 lines
arch/x86/hyperv/mshv_vtl_asm.S +6 -0
+	.section	.discard.addressable,"aw"
+	.align 8
+	.type 	__UNIQUE_ID_addressable___SCK____mshv_vtl_return_hypercall_662.0, @object
+	.size 	__UNIQUE_ID_addressable___SCK____mshv_vtl_return_hypercall_662.0, 8
+__UNIQUE_ID_addressable___SCK____mshv_vtl_return_hypercall_662.0:
+	.quad	__SCK____mshv_vtl_return_hypercall

This is pretty yucky itself.

Definitely doesn't win any prizes, for sure.

Quoted 1 line

Why is it better than calling out to a C function?

It keeps all the code in one place is a strong argument.

Quoted 3 lines

Is it because in spite of the annotations, there's no guarantee the C compiler won't generate some code that messes up a register value? Or is there some other reason?

There is that too, a frame pointer build would be in its right to add a stack frame (although they typically won't in this case). And the C ABI doesn't provide the guarantees your need, so calling out into C is very much you get to keep the pieces.

Quoted 2 lines

Does the magic "_662.0" have any significance? Or is it just some uniqueness salt on the symbol name?

Like Paolo already said, that's just the crazy generated by our __ADRESSABLE() macro, this name is mostly irrelevant, all we really need is a reference to that __SCK____mshv_vtl_return_hypercall symbol so it ends up in the symbol table. (And the final link will then complain if the symbol doesn't end up being resolved)

Keeping the name somewhat in line with __ADDRESSABLE() has the advantage that you can clearly see where it comes from, but yeah, we can strip of the number if you like.

#11 lore raw

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

From: Peter Zijlstra <[email protected]> Sent: Wednesday, November 12, 2025 1:37 AM

Quoted 41 lines · Michael Kelley
Quoted 14 lines

arch/x86/hyperv/mshv_vtl_asm.S SYM_FUNC_START(__mshv_vtl_return_call) · line 106

Quoted 12 lines
arch/x86/hyperv/mshv_vtl_asm.S +7 -0
@@ -96,3 +97,10 @@ SYM_FUNC_START(__mshv_vtl_return_call)
 	pop %rbp
 	RET
 SYM_FUNC_END(__mshv_vtl_return_call)
+
+	.section	.discard.addressable,"aw"
+	.align 8
+	.type 	__UNIQUE_ID_addressable___SCK____mshv_vtl_return_hypercall_662.0, @object
+	.size 	__UNIQUE_ID_addressable___SCK____mshv_vtl_return_hypercall_662.0, 8
+__UNIQUE_ID_addressable___SCK____mshv_vtl_return_hypercall_662.0:
+	.quad	__SCK____mshv_vtl_return_hypercall

This is pretty yucky itself.

Definitely doesn't win any prizes, for sure.

Quoted 1 line

Why is it better than calling out to a C function?

It keeps all the code in one place is a strong argument.

Quoted 3 lines

Is it because in spite of the annotations, there's no guarantee the C compiler won't generate some code that messes up a register value? Or is there some other reason?

There is that too, a frame pointer build would be in its right to add a stack frame (although they typically won't in this case). And the C ABI doesn't provide the guarantees your need, so calling out into C is very much you get to keep the pieces.

Quoted 2 lines

Does the magic "_662.0" have any significance? Or is it just some uniqueness salt on the symbol name?

Like Paolo already said, that's just the crazy generated by our __ADRESSABLE() macro, this name is mostly irrelevant, all we really need is a reference to that __SCK____mshv_vtl_return_hypercall symbol so it ends up in the symbol table. (And the final link will then complain if the symbol doesn't end up being resolved)

Quoted 3 lines

Keeping the name somewhat in line with __ADDRESSABLE() has the advantage that you can clearly see where it comes from, but yeah, we can strip of the number if you like.

Thanks. If that symbol is referenced only by these few lines, I'd go with something even shorter and simpler. Perhaps:

.section .discard.addressable,"aw"
.align 8
.type vtl_return_sym, @object
.size vtl_return_sym, 8
vtl_return_sym:
.quad __SCK____mshv_vtl_return_hypercall

Regardless of the choice of symbol name, add a comment about mimicking __ADDRESSABLE(). That feels less messy to me, but it's Naman's call.

Michael

maintainer
#12 lore raw

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

Quoted 10 lines · Michael Kelley

Thanks. If that symbol is referenced only by these few lines, I'd go with something even shorter and simpler. Perhaps:

.section .discard.addressable,"aw"
.align 8
.type vtl_return_sym, @object
.size vtl_return_sym, 8
vtl_return_sym:
.quad __SCK____mshv_vtl_return_hypercall

Quoted 3 lines

Regardless of the choice of symbol name, add a comment about mimicking __ADDRESSABLE(). That feels less messy to me, but it's Naman's call.

Right, that'll work.

author
#13 lore raw

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

Quoted 12 lines · Peter Zijlstra
Quoted 10 lines · Michael Kelley

Thanks. If that symbol is referenced only by these few lines, I'd go with something even shorter and simpler. Perhaps:

.section .discard.addressable,"aw"
.align 8
.type vtl_return_sym, @object
.size vtl_return_sym, 8
vtl_return_sym:
.quad __SCK____mshv_vtl_return_hypercall

Quoted 5 lines
Quoted 3 lines

Regardless of the choice of symbol name, add a comment about mimicking __ADDRESSABLE(). That feels less messy to me, but it's Naman's call.

Right, that'll work.

Thanks Michael, Peter and Paolo for your inputs. I'll use a simpler name and add a comment to explain it.

Regards,
Naman