[PATCH v2 19/36] KVM: nVMX: Add check VMCS index for guest timer virtualization

From: isaku . yamahata

Date: Thu Mar 05 2026 - 12:54:20 EST


From: Isaku Yamahata <isaku.yamahata@xxxxxxxxx>

Make vmread/vmwrite to the VMCS fields an error if the guest
MSR_IA32_VMX_PROCBASED_CTLS3 doesn't advertise APIC timer virtualization.
Without this check, test_vmwrite_vmread of the KVM unit test fails.

Signed-off-by: Isaku Yamahata <isaku.yamahata@xxxxxxxxx>
---
arch/x86/kvm/vmx/nested.c | 14 ++++++++++++++
arch/x86/kvm/vmx/nested.h | 7 +++++++
2 files changed, 21 insertions(+)

diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index 562b5ffc6433..3cd29b005afe 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -5775,6 +5775,14 @@ static bool is_vmcs_field_valid(struct kvm_vcpu *vcpu, unsigned long field)
field == TERTIARY_VM_EXEC_CONTROL_HIGH))
return false;

+ if (!nested_cpu_supports_guest_apic_timer(vcpu) &&
+ (field == GUEST_APIC_TIMER_VECTOR ||
+ field == GUEST_DEADLINE_VIR ||
+ field == GUEST_DEADLINE_VIR_HIGH ||
+ field == GUEST_DEADLINE_PHY ||
+ field == GUEST_DEADLINE_PHY_HIGH))
+ return false;
+
return true;
}

@@ -7190,6 +7198,12 @@ static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
vmcs12->tertiary_vm_exec_control)
goto error_guest_mode;

+ if (!nested_cpu_supports_guest_apic_timer(vcpu) &&
+ (vmcs12->virtual_timer_vector ||
+ vmcs12->guest_deadline ||
+ vmcs12->guest_deadline_shadow))
+ goto error_guest_mode;
+
if (nested_vmx_check_controls(vcpu, vmcs12) ||
nested_vmx_check_host_state(vcpu, vmcs12) ||
nested_vmx_check_guest_state(vcpu, vmcs12, &ignored))
diff --git a/arch/x86/kvm/vmx/nested.h b/arch/x86/kvm/vmx/nested.h
index 1100a8114dd9..d5d624150aca 100644
--- a/arch/x86/kvm/vmx/nested.h
+++ b/arch/x86/kvm/vmx/nested.h
@@ -291,6 +291,13 @@ static inline bool nested_cpu_has_encls_exit(struct vmcs12 *vmcs12)
return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENCLS_EXITING);
}

+static inline bool nested_cpu_supports_guest_apic_timer(struct kvm_vcpu *vcpu)
+{
+ return nested_cpu_supports_tertiary_ctls(vcpu) &&
+ to_vmx(vcpu)->nested.msrs.tertiary_ctls &
+ TERTIARY_EXEC_GUEST_APIC_TIMER;
+}
+
static inline bool nested_cpu_has_guest_apic_timer(struct vmcs12 *vmcs12)
{
return nested_cpu_has3(vmcs12, TERTIARY_EXEC_GUEST_APIC_TIMER);
--
2.45.2