Skip to content

Commit 6c6a7c6

Browse files
sean-jcgregkh
authored andcommitted
KVM: VMX: Extract checking of guest's DEBUGCTL into helper
[ Upstream commit 8a4351a ] Move VMX's logic to check DEBUGCTL values into a standalone helper so that the code can be used by nested VM-Enter to apply the same logic to the value being loaded from vmcs12. KVM needs to explicitly check vmcs12->guest_ia32_debugctl on nested VM-Enter, as hardware may support features that KVM does not, i.e. relying on hardware to detect invalid guest state will result in false negatives. Unfortunately, that means applying KVM's funky suppression of BTF and LBR to vmcs12 so as not to break existing guests. No functional change intended. Reviewed-by: Dapeng Mi <dapeng1.mi@linux.intel.com> Link: https://lore.kernel.org/r/20250610232010.162191-6-seanjc@google.com Stable-dep-of: 7d0cce6 ("KVM: VMX: Wrap all accesses to IA32_DEBUGCTL with getter/setter APIs") Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 56eb5c5 commit 6c6a7c6

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

arch/x86/kvm/vmx/vmx.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,6 +2192,19 @@ static u64 vmx_get_supported_debugctl(struct kvm_vcpu *vcpu, bool host_initiated
21922192
return debugctl;
21932193
}
21942194

2195+
static bool vmx_is_valid_debugctl(struct kvm_vcpu *vcpu, u64 data,
2196+
bool host_initiated)
2197+
{
2198+
u64 invalid;
2199+
2200+
invalid = data & ~vmx_get_supported_debugctl(vcpu, host_initiated);
2201+
if (invalid & (DEBUGCTLMSR_BTF | DEBUGCTLMSR_LBR)) {
2202+
kvm_pr_unimpl_wrmsr(vcpu, MSR_IA32_DEBUGCTLMSR, data);
2203+
invalid &= ~(DEBUGCTLMSR_BTF | DEBUGCTLMSR_LBR);
2204+
}
2205+
return !invalid;
2206+
}
2207+
21952208
/*
21962209
* Writes msr value into the appropriate "register".
21972210
* Returns 0 on success, non-0 otherwise.
@@ -2260,19 +2273,12 @@ int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
22602273
}
22612274
vmcs_writel(GUEST_SYSENTER_ESP, data);
22622275
break;
2263-
case MSR_IA32_DEBUGCTLMSR: {
2264-
u64 invalid;
2265-
2266-
invalid = data & ~vmx_get_supported_debugctl(vcpu, msr_info->host_initiated);
2267-
if (invalid & (DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR)) {
2268-
kvm_pr_unimpl_wrmsr(vcpu, msr_index, data);
2269-
data &= ~(DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR);
2270-
invalid &= ~(DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR);
2271-
}
2272-
2273-
if (invalid)
2276+
case MSR_IA32_DEBUGCTLMSR:
2277+
if (!vmx_is_valid_debugctl(vcpu, data, msr_info->host_initiated))
22742278
return 1;
22752279

2280+
data &= vmx_get_supported_debugctl(vcpu, msr_info->host_initiated);
2281+
22762282
if (is_guest_mode(vcpu) && get_vmcs12(vcpu)->vm_exit_controls &
22772283
VM_EXIT_SAVE_DEBUG_CONTROLS)
22782284
get_vmcs12(vcpu)->guest_ia32_debugctl = data;
@@ -2282,7 +2288,6 @@ int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
22822288
(data & DEBUGCTLMSR_LBR))
22832289
intel_pmu_create_guest_lbr_event(vcpu);
22842290
return 0;
2285-
}
22862291
case MSR_IA32_BNDCFGS:
22872292
if (!kvm_mpx_supported() ||
22882293
(!msr_info->host_initiated &&

0 commit comments

Comments
 (0)