[RFC PATCH v2 02/31] KVM: x86: hyper-v: Bank GUEST_OS_ID and HYPERCALL MSRs per plane

From: David Woodhouse

Date: Fri Nov 13 2026 - 14:17:56 EST


From: David Woodhouse <dwmw@xxxxxxxxxxxx>

The TLFS makes HV_X64_MSR_GUEST_OS_ID and HV_X64_MSR_HYPERCALL
separate instances per VTL; KVM kept a single partition-wide copy, so
the Windows secure kernel enabling its own hypercall page from VTL1
clobbered VTL0's.

Index both MSRs by the vCPU's plane. The hypercall page contents are
still written into guest RAM on enable, now once per plane's page.

Signed-off-by: David Woodhouse <dwmw@xxxxxxxxxxxx>
---
arch/x86/include/asm/kvm_host.h | 9 +++++++--
arch/x86/kvm/hyperv.c | 19 ++++++++++---------
arch/x86/kvm/hyperv.h | 3 ++-
3 files changed, 19 insertions(+), 12 deletions(-)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 69c32ce22b56..c642a20f3b5f 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1089,10 +1089,15 @@ enum hv_tsc_page_status {
/* Hyper-V emulation context */
struct kvm_hv {
struct mutex hv_lock;
- u64 hv_guest_os_id;
- u64 hv_hypercall;
/* KVM_HYPERV_HCALL_* features enabled by KVM_CAP_HYPERV_FORWARD_HCALL */
u64 hcall_features;
+ /*
+ * GUEST_OS_ID and the hypercall page are per-VTL instances in the
+ * TLFS; bank them per plane so a higher VTL enabling its own
+ * hypercall page does not clobber the lower VTL's.
+ */
+ u64 hv_guest_os_id[KVM_MAX_PLANES];
+ u64 hv_hypercall[KVM_MAX_PLANES];
u64 hv_tsc_page;
enum hv_tsc_page_status hv_tsc_page_status;

diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c
index 9ebcad807c69..387fff64597d 100644
--- a/arch/x86/kvm/hyperv.c
+++ b/arch/x86/kvm/hyperv.c
@@ -1371,7 +1371,7 @@ static void __kvm_hv_xsaves_xsavec_maybe_warn(struct kvm_vcpu *vcpu)
if (hv->xsaves_xsavec_checked)
return;

- if ((hv->hv_guest_os_id & KVM_HV_WIN2016_GUEST_ID_MASK) !=
+ if ((hv->hv_guest_os_id[0] & KVM_HV_WIN2016_GUEST_ID_MASK) !=
KVM_HV_WIN2016_GUEST_ID)
return;

@@ -1413,10 +1413,11 @@ static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,

switch (msr) {
case HV_X64_MSR_GUEST_OS_ID:
- hv->hv_guest_os_id = data;
+ hv->hv_guest_os_id[vcpu->plane_level] = data;
/* setting guest os id to zero disables hypercall page */
- if (!hv->hv_guest_os_id)
- hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
+ if (!hv->hv_guest_os_id[vcpu->plane_level])
+ hv->hv_hypercall[vcpu->plane_level] &=
+ ~HV_X64_MSR_HYPERCALL_ENABLE;
break;
case HV_X64_MSR_HYPERCALL: {
u8 instructions[9];
@@ -1424,10 +1425,10 @@ static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,
u64 addr;

/* if guest os id is not set hypercall should remain disabled */
- if (!hv->hv_guest_os_id)
+ if (!hv->hv_guest_os_id[vcpu->plane_level])
break;
if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
- hv->hv_hypercall = data;
+ hv->hv_hypercall[vcpu->plane_level] = data;
break;
}

@@ -1456,7 +1457,7 @@ static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,
addr = data & HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_MASK;
if (kvm_vcpu_write_guest(vcpu, addr, instructions, i))
return 1;
- hv->hv_hypercall = data;
+ hv->hv_hypercall[vcpu->plane_level] = data;
break;
}
case HV_X64_MSR_REFERENCE_TSC:
@@ -1669,10 +1670,10 @@ static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata,

switch (msr) {
case HV_X64_MSR_GUEST_OS_ID:
- data = hv->hv_guest_os_id;
+ data = hv->hv_guest_os_id[vcpu->plane_level];
break;
case HV_X64_MSR_HYPERCALL:
- data = hv->hv_hypercall;
+ data = hv->hv_hypercall[vcpu->plane_level];
break;
case HV_X64_MSR_TIME_REF_COUNT:
data = get_time_ref_counter(kvm);
diff --git a/arch/x86/kvm/hyperv.h b/arch/x86/kvm/hyperv.h
index 7c6a21f02d71..2617ee1e57b2 100644
--- a/arch/x86/kvm/hyperv.h
+++ b/arch/x86/kvm/hyperv.h
@@ -202,7 +202,8 @@ int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)

static inline bool kvm_hv_hypercall_enabled(struct kvm_vcpu *vcpu)
{
- return vcpu->arch.hyperv_enabled && to_kvm_hv(vcpu->kvm)->hv_guest_os_id;
+ return vcpu->arch.hyperv_enabled &&
+ to_kvm_hv(vcpu->kvm)->hv_guest_os_id[vcpu->plane_level];
}

/* Features reported and accepted by KVM_CAP_HYPERV_FORWARD_HCALL. */
--
2.50.1