Skip to content

Commit a8db759

Browse files
Maxim Levitskygregkh
authored andcommitted
KVM: VMX: Wrap all accesses to IA32_DEBUGCTL with getter/setter APIs
[ Upstream commit 7d0cce6 ] Introduce vmx_guest_debugctl_{read,write}() to handle all accesses to vmcs.GUEST_IA32_DEBUGCTL. This will allow stuffing FREEZE_IN_SMM into GUEST_IA32_DEBUGCTL based on the host setting without bleeding the state into the guest, and without needing to copy+paste the FREEZE_IN_SMM logic into every patch that accesses GUEST_IA32_DEBUGCTL. No functional change intended. Cc: stable@vger.kernel.org Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com> [sean: massage changelog, make inline, use in all prepare_vmcs02() cases] Reviewed-by: Dapeng Mi <dapeng1.mi@linux.intel.com> Link: https://lore.kernel.org/r/20250610232010.162191-8-seanjc@google.com 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 73a8e77 commit a8db759

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

arch/x86/kvm/vmx/nested.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2653,11 +2653,11 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
26532653
if (vmx->nested.nested_run_pending &&
26542654
(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
26552655
kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
2656-
vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl &
2657-
vmx_get_supported_debugctl(vcpu, false));
2656+
vmx_guest_debugctl_write(vcpu, vmcs12->guest_ia32_debugctl &
2657+
vmx_get_supported_debugctl(vcpu, false));
26582658
} else {
26592659
kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
2660-
vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.pre_vmenter_debugctl);
2660+
vmx_guest_debugctl_write(vcpu, vmx->nested.pre_vmenter_debugctl);
26612661
}
26622662
if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending ||
26632663
!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)))
@@ -3527,7 +3527,7 @@ enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
35273527

35283528
if (!vmx->nested.nested_run_pending ||
35293529
!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
3530-
vmx->nested.pre_vmenter_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
3530+
vmx->nested.pre_vmenter_debugctl = vmx_guest_debugctl_read();
35313531
if (kvm_mpx_supported() &&
35323532
(!vmx->nested.nested_run_pending ||
35333533
!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)))
@@ -4774,7 +4774,7 @@ static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
47744774
__vmx_set_segment(vcpu, &seg, VCPU_SREG_LDTR);
47754775

47764776
kvm_set_dr(vcpu, 7, 0x400);
4777-
vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4777+
vmx_guest_debugctl_write(vcpu, 0);
47784778

47794779
if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
47804780
vmcs12->vm_exit_msr_load_count))

arch/x86/kvm/vmx/pmu_intel.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -605,11 +605,11 @@ static void intel_pmu_reset(struct kvm_vcpu *vcpu)
605605
*/
606606
static void intel_pmu_legacy_freezing_lbrs_on_pmi(struct kvm_vcpu *vcpu)
607607
{
608-
u64 data = vmcs_read64(GUEST_IA32_DEBUGCTL);
608+
u64 data = vmx_guest_debugctl_read();
609609

610610
if (data & DEBUGCTLMSR_FREEZE_LBRS_ON_PMI) {
611611
data &= ~DEBUGCTLMSR_LBR;
612-
vmcs_write64(GUEST_IA32_DEBUGCTL, data);
612+
vmx_guest_debugctl_write(vcpu, data);
613613
}
614614
}
615615

@@ -679,7 +679,7 @@ void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu)
679679

680680
if (!lbr_desc->event) {
681681
vmx_disable_lbr_msrs_passthrough(vcpu);
682-
if (vmcs_read64(GUEST_IA32_DEBUGCTL) & DEBUGCTLMSR_LBR)
682+
if (vmx_guest_debugctl_read() & DEBUGCTLMSR_LBR)
683683
goto warn;
684684
if (test_bit(INTEL_PMC_IDX_FIXED_VLBR, pmu->pmc_in_use))
685685
goto warn;
@@ -701,7 +701,7 @@ void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu)
701701

702702
static void intel_pmu_cleanup(struct kvm_vcpu *vcpu)
703703
{
704-
if (!(vmcs_read64(GUEST_IA32_DEBUGCTL) & DEBUGCTLMSR_LBR))
704+
if (!(vmx_guest_debugctl_read() & DEBUGCTLMSR_LBR))
705705
intel_pmu_release_guest_lbr_event(vcpu);
706706
}
707707

arch/x86/kvm/vmx/vmx.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,7 +2148,7 @@ int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
21482148
msr_info->data = vmx->pt_desc.guest.addr_a[index / 2];
21492149
break;
21502150
case MSR_IA32_DEBUGCTLMSR:
2151-
msr_info->data = vmcs_read64(GUEST_IA32_DEBUGCTL);
2151+
msr_info->data = vmx_guest_debugctl_read();
21522152
break;
21532153
default:
21542154
find_uret_msr:
@@ -2282,7 +2282,8 @@ int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
22822282
VM_EXIT_SAVE_DEBUG_CONTROLS)
22832283
get_vmcs12(vcpu)->guest_ia32_debugctl = data;
22842284

2285-
vmcs_write64(GUEST_IA32_DEBUGCTL, data);
2285+
vmx_guest_debugctl_write(vcpu, data);
2286+
22862287
if (intel_pmu_lbr_is_enabled(vcpu) && !to_vmx(vcpu)->lbr_desc.event &&
22872288
(data & DEBUGCTLMSR_LBR))
22882289
intel_pmu_create_guest_lbr_event(vcpu);
@@ -4831,7 +4832,8 @@ static void init_vmcs(struct vcpu_vmx *vmx)
48314832
vmcs_write32(GUEST_SYSENTER_CS, 0);
48324833
vmcs_writel(GUEST_SYSENTER_ESP, 0);
48334834
vmcs_writel(GUEST_SYSENTER_EIP, 0);
4834-
vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4835+
4836+
vmx_guest_debugctl_write(&vmx->vcpu, 0);
48354837

48364838
if (cpu_has_vmx_tpr_shadow()) {
48374839
vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);

arch/x86/kvm/vmx/vmx.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,16 @@ void vmx_update_cpu_dirty_logging(struct kvm_vcpu *vcpu);
438438
u64 vmx_get_supported_debugctl(struct kvm_vcpu *vcpu, bool host_initiated);
439439
bool vmx_is_valid_debugctl(struct kvm_vcpu *vcpu, u64 data, bool host_initiated);
440440

441+
static inline void vmx_guest_debugctl_write(struct kvm_vcpu *vcpu, u64 val)
442+
{
443+
vmcs_write64(GUEST_IA32_DEBUGCTL, val);
444+
}
445+
446+
static inline u64 vmx_guest_debugctl_read(void)
447+
{
448+
return vmcs_read64(GUEST_IA32_DEBUGCTL);
449+
}
450+
441451
/*
442452
* Note, early Intel manuals have the write-low and read-high bitmap offsets
443453
* the wrong way round. The bitmaps control MSRs 0x00000000-0x00001fff and

0 commit comments

Comments
 (0)