Skip to content

Commit 8a4351a

Browse files
committed
KVM: VMX: Extract checking of guest's DEBUGCTL into helper
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 Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 17ec2f9 commit 8a4351a

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
@@ -2193,6 +2193,19 @@ static u64 vmx_get_supported_debugctl(struct kvm_vcpu *vcpu, bool host_initiated
21932193
return debugctl;
21942194
}
21952195

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

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

0 commit comments

Comments
 (0)