KVM: x86: Bus Lock Detect fixes and SVM support
Sean Christopherson
maintainerv6 cover KVM: x86: Bus Lock Detect fixes and SVM support
Fix KVM's handling of DR6.BLD, and add support for virtualizing BLD on SVM. KUT tests: https://lore.kernel.org/all/[email protected]
v6:
- Throw in the towel, at least for now, on WARNing if KVM sees a DR6 without all of the fixed-1 bits set. [Sashiko]
v5:
- https://lore.kernel.org/all/[email protected]
- Fix the DR6.BLD clobbering bug Sashiko pointed out. [Sashiko]
- Force-set fixed-1 bits after synchronizing DR6 from the guest (because the guest could set bits that are supposed to be fixed-1 due to virtualization holes).
- Open code nested_vmcb12_has_lbrv().
- Fix the Author for Ravi's patch.
- Add svm_need_lbr_virtualization() to preserve short-circuit logic.
- Rename kvm_dr6_fixed() to kvm_get_dr6_fixed_1().
- Invert svm_get_debugctl_reserved_bits() into svm_get_supported_debugctl() to match VMX, and KVM's preferred style.
v4:
* Rename nested_vmcb12_has_lbrv() to nested_lbrv_enabled() (Yosry).
* Drop the redundant !lbrv check and the extra comment (Yosry).
* Spell out in the changelog what breaks when DR6_BUS_LOCK is forced to 1 (Nikunj).
* Make it a pure NFC macro-to-helper conversion, and move the BUS_LOCK_DETECT gating to patch 5 (Nikunj).
* Fold in the DEBUGCTLMSR_BUS_LOCK_DETECT gating moved from patch 4.
* Collect Reviewed-by tags.
v3:
* https://lore.kernel.org/kvm/[email protected]
* Reworked the single v2 patch into a prep series (patches 1-4) plus the feature.
* Rewrite svm_update_lbrv() as 'if' statements so the BLD LBRV dependency can be added cleanly (Yosry Ahmed).
* Sanitize V_LBR in the nested control cache like NP/GMET and drop the redundant X86_FEATURE_LBRV checks (Yosry Ahmed).
* Use kvm_dr6_fixed() for nested DR6 instead of DR6_FIXED_1 | DR6_RTM.
* Compute DEBUGCTL reserved bits per-vCPU, gating BUS_LOCK_DETECT on guest CPUID rather than a static macro.
v2:
* https://lore.kernel.org/kvm/[email protected]
* Rebased on kvm-x86-next-2026.06.24.
* Used guest_cpu_cap_has() instead of guest_cpuid_has().
v1: https://lore.kernel.org/all/[email protected]
Ravi Bangoria (1):
KVM: SVM: Add support for virtualizating Bus Lock Detect
Sean Christopherson (8):
KVM: x86: Preserve DR6.BLD (Bus Lock Detect) when delivering #DB
payload
KVM: x86: Rename kvm_dr6_fixed() => kvm_get_dr6_fixed_1()
KVM: Return an "unsigned long", not "u64" for the fixed-1 DR6 bits
KVM: x86: Force fixed-1 bits in DR6 after synchronizing with hardware
KVM: x86: Set guest's fixed-1 DR6 bits when delivering #DB payload
KVM: x86: Kill off DR6_FIXED_1 to prevent future misuse
KVM: SVM: Add helper to query if LBR virtualization needs to be
enabled
KVM: nSVM: Open code check on LBR virtualization being enabled in
vmcb12
Shivansh Dhiman (3):
KVM: nSVM: Disable LBRV in nested control cache when unsupported
KVM: nSVM: Don't assume all active-low bits DR6 are fixed-1
KVM: SVM: Add a vCPU-aware helper to get supported DEBUGCTL bits
arch/x86/kvm/regs.c | 8 +++--- arch/x86/kvm/regs.h | 11 ++++---- arch/x86/kvm/svm/nested.c | 27 +++++++++----------- arch/x86/kvm/svm/svm.c | 31 +++++++++++++++++------ arch/x86/kvm/svm/svm.h | 12 +++++++-- arch/x86/kvm/x86.c | 53 +++++++++++++++++++++++---------------- 6 files changed, 87 insertions(+), 55 deletions(-)
base-commit: 7f734412a73e910658dc81ba74f76af789202f58
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Sean Christopherson
maintainerPATCH v6 1/12 KVM: x86: Preserve DR6.BLD (Bus Lock Detect) when delivering #DB payload
Preserve Bus Lock Detect if it isn't present in the payload. Unlike RTM, which is explicitly modified on every #DB, DR6.BLD is modified if and only if the #DB is due to a (coincident) Bus Lock Detect.
Per Intel's SDM:
Other debug exceptions do not modify this bit.
And AMD's APM
All other #DB exceptions leave DR6[BLD] unmodified,
Restore the explicit RTM handling that was clobbered by commit 9a3ecd5e2aa1 ("KVM: X86: Rename DR6_INIT to DR6_ACTIVE_LOW"), as the semantics of any given bit are feature specific, i.e. are obviously not consistent for active-high versus active-low. In other words, while the bug was technically introduced when support for Bus Lock Detect was added, it's really that attempt to genericize handling of active-low bits that set KVM up to fail.
--- arch/x86/kvm/x86.c | 52 ++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 22 deletions(-)
arch/x86/kvm/x86.c +30 -22
@@ -401,30 +401,37 @@ void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu,switch (ex->vector) {case DB_VECTOR :/* -* "Certain debug exceptions may clear bit 0-3. The -* remaining contents of the DR6 register are never -* cleared by the processor". +* DR6 is a mess. Reserved/unused bits are fixed-to-1, and so +* to maintain backwards compatibility with existing software, +* features that use previously-reserved bits have active-low +* semantics, i.e. clear the bit when the feature is present in +* the payload. +* +* Further complicating matters, some DR6 bits are preserved by +* hardware, while others are explicitly modified on every #DB. +* The trap bits are always set based on the payload, as is the +* RTM flag (but it's active low). All other bits are modified +* if and only if a relevant debug exception occurs, e.g. BD, +* BS, and BT are never cleared by hardware, and BLD is never +* set by hardware (when supported, excepting RESET). +* +* Lastly, the payload does NOT have active-low semantics, e.g. +* so that it's compatible VMX's pending debug exceptions and +* qualification fields, and to avoid bleeding the DR6 madness +* into other KVM code. +* +* To compute DR6: +* +* 1. "Reset" the bits that are modified on all #DBs +* 2. Clear active-low bits that are present in the payload. +* 3. Set active-high bits that are present in the payload. +* 4. Clear fixed-0 bits. +* 5. Set fixed-1 bits. */ vcpu->arch.dr6 &= ~DR_TRAP_BITS ; -/* -* In order to reflect the #DB exception payload in guest -* dr6, three components need to be considered: active low -* bit, FIXED_1 bits and active high bits (e.g. DR6_BD, -* DR6_BS and DR6_BT) -* DR6_ACTIVE_LOW contains the FIXED_1 and active low bits. -* In the target guest dr6: -* FIXED_1 bits should always be set. -* Active low bits should be cleared if 1-setting in payload. -* Active high bits should be set if 1-setting in payload. -* -* Note, the payload is compatible with the pending debug -* exceptions/exit qualification under VMX, that active_low bits -* are active high in payload. -* So they need to be flipped for DR6. -*/ - vcpu->arch.dr6 |=DR6_ACTIVE_LOW ; - vcpu->arch.dr6 |= ex->payload; - vcpu->arch.dr6 ^= ex->payload &DR6_ACTIVE_LOW ; + vcpu->arch.dr6 |=DR6_RTM ; + vcpu->arch.dr6 &= ~(ex->payload &DR6_ACTIVE_LOW ); + vcpu->arch.dr6 |= (ex->payload & ~DR6_ACTIVE_LOW );/* * The #DB payload is defined as compatible with the 'pending
@@ -433,6 +440,7 @@ void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu,* breakpoint), it is reserved and must be zero in DR6. */ vcpu->arch.dr6 &= ~BIT (12 ); + vcpu->arch.dr6 |=DR6_FIXED_1 ;break ;case PF_VECTOR : vcpu->arch.cr2 = ex->payload;
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Re: [PATCH v6 01/12] KVM: x86: Preserve DR6.BLD (Bus Lock Detect) when delivering #DB payload
Quoted 20 lines · Sean Christopherson
Preserve Bus Lock Detect if it isn't present in the payload. Unlike RTM, which is explicitly modified on every #DB, DR6.BLD is modified if and only if the #DB is due to a (coincident) Bus Lock Detect.
Per Intel's SDM:
Other debug exceptions do not modify this bit.
And AMD's APM
All other #DB exceptions leave DR6[BLD] unmodified,
Restore the explicit RTM handling that was clobbered by commit
9a3ecd5e2aa1("KVM: X86: RenameDR6_INITtoDR6_ACTIVE_LOW"), as the semantics of any given bit are feature specific, i.e. are obviously not consistent for active-high versus active-low. In other words, while the bug was technically introduced when support for Bus Lock Detect was added, it's really that attempt to genericize handling of active-low bits that set KVM up to fail.
Quoted 3 lines
Fixes e8ea85fb280e ("KVM: X86: Add support for the emulation of DR6_BUS_LOCK bit")Signed-off-by Sean Christopherson <[email protected]>
Tested on Turin with Bus Lock Detect enabled in both L1 and L2 guests. The expected bus lock warnings show up in dmesg. Also, all the KUT tests pass with Sean's KUT patches applied.
Sean Christopherson
maintainerPATCH v6 2/12 KVM: x86: Rename kvm_dr6_fixed() => kvm_get_dr6_fixed_1()
In reply to Sean ChristophersonRename kvm_dr6_fixed() to kvm_get_dr6_fixed_1() to make it more clear that the helper retrieves the fixed-1 DR6 bits for a given vCPU.
No functional change intended.
--- arch/x86/kvm/regs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/regs.c +2 -2
@@ -772,7 +772,7 @@ void kvm_update_dr7(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_update_dr7); -static u64 kvm_dr6_fixed (struct kvm_vcpu *vcpu) +static u64 kvm_get_dr6_fixed_1 (struct kvm_vcpu *vcpu) {u64 fixed =DR6_FIXED_1 ;
@@ -798,7 +798,7 @@ int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)case 6 :if (!kvm_dr6_valid (val))return 1 ;/* #GP */ - vcpu->arch.dr6 = (val &DR6_VOLATILE ) |kvm_dr6_fixed (vcpu); + vcpu->arch.dr6 = (val &DR6_VOLATILE ) |kvm_get_dr6_fixed_1 (vcpu);break ;case 5 :default :/* 7 */
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Sean Christopherson
maintainerPATCH v6 3/12 KVM: Return an "unsigned long", not "u64" for the fixed-1 DR6 bits
In reply to Sean ChristophersonReturn an "unsigned long" instead of a "u64" from the helper to get the fixed-1 DR6 bits, as debug registers are natural width registers, not 64-bit registers.
No functional change intended (the fixed-1 bits only set bits in 31:0, and the sole caller truncates the value to an "unsigned long").
--- arch/x86/kvm/regs.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/regs.c +3 -2
@@ -772,15 +772,16 @@ void kvm_update_dr7(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_update_dr7); -static u64 kvm_get_dr6_fixed_1 (struct kvm_vcpu *vcpu) +static unsigned long kvm_get_dr6_fixed_1 (struct kvm_vcpu *vcpu) { -u64 fixed =DR6_FIXED_1 ; +unsigned long fixed =DR6_FIXED_1 ;if (!guest_cpu_cap_has (vcpu,X86_FEATURE_RTM )) fixed |=DR6_RTM ;if (!guest_cpu_cap_has (vcpu,X86_FEATURE_BUS_LOCK_DETECT )) fixed |=DR6_BUS_LOCK ; +return fixed; }
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Sean Christopherson
maintainerPATCH v6 4/12 KVM: x86: Force fixed-1 bits in DR6 after synchronizing with hardware
In reply to Sean ChristophersonSet all fixed-1 bits in KVM's version of DR6 after synchronizing with hardware to paper over as much of the virtualization hole as possible. Because KVM dynamically disables DR interception, a guest can write any DR6 bit that isn't fixed in bare metal, even if the bit is supposed to be fixed from the guest's perspective.
In addition to providing some amount of consistency in KVM, this will allow adding sanity checks that KVM never ends up with fixed-1 bits clear in its version of DR6.
--- arch/x86/kvm/regs.c | 2 +- arch/x86/kvm/regs.h | 1 + arch/x86/kvm/x86.c | 1 + 3 files changed, 3 insertions(+), 1 deletion(-)
arch/x86/kvm/regs.c +1 -1
@@ -772,7 +772,7 @@ void kvm_update_dr7(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_update_dr7); -static unsigned long kvm_get_dr6_fixed_1 (struct kvm_vcpu *vcpu) +unsigned long kvm_get_dr6_fixed_1 (struct kvm_vcpu *vcpu) {unsigned long fixed =DR6_FIXED_1 ;
arch/x86/kvm/regs.h +1 -0
@@ -62,6 +62,7 @@ int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);int kvm_set_cr8 (struct kvm_vcpu *vcpu,unsigned long cr8);int kvm_set_dr (struct kvm_vcpu *vcpu,int dr,unsigned long val);unsigned long kvm_get_dr (struct kvm_vcpu *vcpu,int dr); +unsigned long kvm_get_dr6_fixed_1 (struct kvm_vcpu *vcpu);unsigned long kvm_get_cr8 (struct kvm_vcpu *vcpu);void kvm_lmsw (struct kvm_vcpu *vcpu,unsigned long msw);int load_pdptrs (struct kvm_vcpu *vcpu,unsigned long cr3);
arch/x86/kvm/x86.c +1 -0
@@ -8417,6 +8417,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)WARN_ON (vcpu->arch.switch_db_regs &KVM_DEBUGREG_AUTO_SWITCH );kvm_x86_call (sync_dirty_debug_regs)(vcpu);kvm_update_dr0123 (vcpu); + vcpu->arch.dr6 |=kvm_get_dr6_fixed_1 (vcpu);kvm_update_dr7 (vcpu); }
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Sean Christopherson
maintainerPATCH v6 5/12 KVM: x86: Set guest's fixed-1 DR6 bits when delivering #DB payload
In reply to Sean ChristophersonWhen delivering a #DB payload, set all DR6 bits that are fixed-1 from the guest's perspective, not just the host's perspective. Don't WARN on a "bad" payload (or existing DR6) even though KVM itself shouldn't set DR6 or synthesize a payload without the fixed-1 bits set, as userspace can provide a completely arbitrary payload via KVM_SET_VCPU_EVENTS (and "hardening" that flow is not worth the effort as there's no benefit in practice).
--- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/x86.c +1 -1
@@ -440,7 +440,7 @@ void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu,* breakpoint), it is reserved and must be zero in DR6. */ vcpu->arch.dr6 &= ~BIT (12 ); - vcpu->arch.dr6 |=DR6_FIXED_1 ; + vcpu->arch.dr6 |=kvm_get_dr6_fixed_1 (vcpu);break ;case PF_VECTOR : vcpu->arch.cr2 = ex->payload;
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Sean Christopherson
maintainerPATCH v6 6/12 KVM: x86: Kill off DR6_FIXED_1 to prevent future misuse
In reply to Sean ChristophersonNow that the only user of DR6_FIXED_1 is kvm_get_dr6_fixed_1(), drop the hardcoded macro to force all references to fixed-1 bits to use the vCPU- aware helper.
Opportunistically reword the comment about DR6_ACTIVE_LOW to explain why treating fixed-1 bits as active-low is correct (barring a *very* egregious architectural goof).
No functional change intended.
--- arch/x86/kvm/regs.c | 2 +- arch/x86/kvm/regs.h | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-)
arch/x86/kvm/regs.c +1 -1
@@ -774,7 +774,7 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_update_dr7);unsigned long kvm_get_dr6_fixed_1 (struct kvm_vcpu *vcpu) { -unsigned long fixed =DR6_FIXED_1 ; +unsigned long fixed =DR6_ACTIVE_LOW & ~DR6_VOLATILE ;if (!guest_cpu_cap_has (vcpu,X86_FEATURE_RTM )) fixed |=DR6_RTM ;
arch/x86/kvm/regs.h +4 -6
@@ -38,16 +38,14 @@ static_assert(!(KVM_POSSIBLE_CR0_GUEST_BITS & X86_CR0_PDPTR_BITS));#define DR6_BT (1 << 15)#define DR6_RTM (1 << 16)/* -* DR6_ACTIVE_LOW combines fixed-1 and active-low bits. -* We can regard all the bits in DR6_FIXED_1 as active_low bits; -* they will never be 0 for now, but when they are defined -* in the future it will require no code change. +* DR6_ACTIVE_LOW combines fixed-1 and active-low bits (bits that are currently +* fixed-1 are guaranteed to have active-low semantics if they are ever used to +* support a new feature). * -* DR6_ACTIVE_LOW is also used as the init/reset value for DR6. +* Note, DR6_ACTIVE_LOW is also the INIT/RESET value for DR6. */ #define DR6_ACTIVE_LOW 0xffff0ff0#define DR6_VOLATILE 0x0001e80f -#define DR6_FIXED_1 (DR6_ACTIVE_LOW & ~DR6_VOLATILE)#define DR7_BP_EN_MASK 0x000000ff#define DR7_GE (1 << 9)
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Sean Christopherson
maintainerPATCH v6 7/12 KVM: SVM: Add helper to query if LBR virtualization needs to be enabled
In reply to Sean ChristophersonExtract the logic for determining whether or not LBR virtualization needs to be enabled into a helper in anticipation of growing the list of features that depend on LBR virtualization.
No functional change intended.
--- arch/x86/kvm/svm/svm.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/svm.c +12 -3
@@ -875,13 +875,22 @@ static void __svm_disable_lbrv(struct kvm_vcpu *vcpu)to_svm (vcpu)->vmcb->control.misc_ctl2 &= ~SVM_MISC2_ENABLE_V_LBR ; } +static bool svm_need_lbr_virtualization (struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); + +if (svm->vmcb->save.dbgctl &DEBUGCTLMSR_LBR ) +return true; + +return is_guest_mode (vcpu) &&guest_cpu_cap_has (vcpu,X86_FEATURE_LBRV ) && + (svm->nested.ctl.misc_ctl2 &SVM_MISC2_ENABLE_V_LBR ); +} +void svm_update_lbrv (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu);bool current_enable_lbrv = svm->vmcb->control.misc_ctl2 &SVM_MISC2_ENABLE_V_LBR ; -bool enable_lbrv = (svm->vmcb->save.dbgctl &DEBUGCTLMSR_LBR ) || - (is_guest_mode (vcpu) &&guest_cpu_cap_has (vcpu,X86_FEATURE_LBRV ) && - (svm->nested.ctl.misc_ctl2 &SVM_MISC2_ENABLE_V_LBR )); +bool enable_lbrv =svm_need_lbr_virtualization (vcpu);if (enable_lbrv && !current_enable_lbrv)__svm_enable_lbrv (vcpu);
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Sean Christopherson
maintainerPATCH v6 8/12 KVM: nSVM: Disable LBRV in nested control cache when unsupported
In reply to Sean ChristophersonFrom: Shivansh Dhiman <[email protected]>
Clear SVM_MISC2_ENABLE_V_LBR in __nested_copy_vmcb_control_to_cache() when the vCPU does not support LBR Virtualization. This lets the cached value be consumed directly instead of re-checking X86_FEATURE_LBRV on every access.
--- arch/x86/kvm/svm/nested.c | 8 +++++--- arch/x86/kvm/svm/svm.c | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/nested.c +5 -3
@@ -527,12 +527,16 @@ void __nested_copy_vmcb_control_to_cache(struct kvm_vcpu *vcpu,/* Always clear misc_ctl bits that the guest cannot use */ to->misc_ctl = from->misc_ctl; + to->misc_ctl2 = from->misc_ctl2;if (!guest_cpu_cap_has (vcpu,X86_FEATURE_NPT )) to->misc_ctl &= ~SVM_MISC_ENABLE_NP ;if (!gmet_enabled || !guest_cpu_cap_has (vcpu,X86_FEATURE_GMET )) to->misc_ctl &= ~SVM_MISC_ENABLE_GMET ; +if (!guest_cpu_cap_has (vcpu,X86_FEATURE_LBRV )) + to->misc_ctl2 &= ~SVM_MISC2_ENABLE_V_LBR ; + to->iopm_base_pa = from->iopm_base_pa &PAGE_MASK ; to->msrpm_base_pa = from->msrpm_base_pa &PAGE_MASK ; to->tsc_offset = from->tsc_offset;
@@ -550,7 +554,6 @@ void __nested_copy_vmcb_control_to_cache(struct kvm_vcpu *vcpu, to->event_inj_err = from->event_inj_err; to->next_rip = from->next_rip; to->nested_cr3 = from->nested_cr3; - to->misc_ctl2 = from->misc_ctl2; to->pause_filter_count = from->pause_filter_count; to->pause_filter_thresh = from->pause_filter_thresh;
@@ -737,8 +740,7 @@ static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,static bool nested_vmcb12_has_lbrv (struct kvm_vcpu *vcpu) { -return guest_cpu_cap_has (vcpu,X86_FEATURE_LBRV ) && - (to_svm (vcpu)->nested.ctl.misc_ctl2 &SVM_MISC2_ENABLE_V_LBR ); +return to_svm (vcpu)->nested.ctl.misc_ctl2 &SVM_MISC2_ENABLE_V_LBR ; }static void nested_vmcb02_prepare_save (struct vcpu_svm *svm)
arch/x86/kvm/svm/svm.c +1 -1
@@ -882,7 +882,7 @@ static bool svm_need_lbr_virtualization(struct kvm_vcpu *vcpu)if (svm->vmcb->save.dbgctl &DEBUGCTLMSR_LBR )return true; -return is_guest_mode (vcpu) &&guest_cpu_cap_has (vcpu,X86_FEATURE_LBRV ) && +return is_guest_mode (vcpu) && (svm->nested.ctl.misc_ctl2 &SVM_MISC2_ENABLE_V_LBR ); }
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Sean Christopherson
maintainerPATCH v6 9/12 KVM: nSVM: Open code check on LBR virtualization being enabled in vmcb12
In reply to Sean ChristophersonNow that KVM clears ENABLE_V_LBR if LBR virtualization isn't supported when caching vmcb12 control fields, i.e. now that the logic is a simple one-liner, open the check and drop the now-superfluous wrapper.
No functional change intended.
--- arch/x86/kvm/svm/nested.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-)
arch/x86/kvm/svm/nested.c +3 -8
@@ -738,11 +738,6 @@ static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,return 0 ; } -static bool nested_vmcb12_has_lbrv (struct kvm_vcpu *vcpu) -{ -return to_svm (vcpu)->nested.ctl.misc_ctl2 &SVM_MISC2_ENABLE_V_LBR ; -} -static void nested_vmcb02_prepare_save (struct vcpu_svm *svm) {struct vmcb_ctrl_area_cached *control = &svm->nested.ctl;
@@ -817,7 +812,7 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm)vmcb_mark_dirty (vmcb02,VMCB_DR ); } -if (nested_vmcb12_has_lbrv (vcpu)) { +if (control->misc_ctl2 &SVM_MISC2_ENABLE_V_LBR ) {/* * Reserved bits of DEBUGCTL are ignored. Be consistent with * svm_set_msr's definition of reserved bits.
@@ -1310,7 +1305,7 @@ static int nested_svm_vmexit_update_vmcb12(struct kvm_vcpu *vcpu)if (guest_cpu_cap_has (vcpu,X86_FEATURE_NRIPS )) vmcb12->control.next_rip = vmcb02->control.next_rip; -if (nested_vmcb12_has_lbrv (vcpu)) +if (svm->nested.ctl.misc_ctl2 &SVM_MISC2_ENABLE_V_LBR )svm_copy_lbrs (&vmcb12->save, &vmcb02->save); vmcb12->control.event_inj =0 ;
@@ -1387,7 +1382,7 @@ void nested_svm_vmexit(struct vcpu_svm *svm)if (!nested_exit_on_intr (svm))kvm_make_request (KVM_REQ_EVENT , &svm->vcpu); -if (!nested_vmcb12_has_lbrv (vcpu)) { +if (!(svm->nested.ctl.misc_ctl2 &SVM_MISC2_ENABLE_V_LBR )) {svm_copy_lbrs (&vmcb01->save, &vmcb02->save);vmcb_mark_dirty (vmcb01,VMCB_LBR ); }
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Sean Christopherson
maintainerPATCH v6 10/12 KVM: nSVM: Don't assume all active-low bits DR6 are fixed-1
In reply to Sean ChristophersonFrom: Shivansh Dhiman <[email protected]>
When preparing vmcb02 for nested VMRUN, force only the actual fixed-1 bits instead of setting all active-low bits. The flaw is currently benign, as the only active-low bits supported by KVM are RTM (Restricted Transactional Memory) and BLD (Bus Lock Detect), neither of which is currently supported on SVM, but that's about to change.
--- arch/x86/kvm/regs.c | 1 + arch/x86/kvm/svm/nested.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/regs.c +1 -0
@@ -784,6 +784,7 @@ unsigned long kvm_get_dr6_fixed_1(struct kvm_vcpu *vcpu)return fixed; } +EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_get_dr6_fixed_1);int kvm_set_dr (struct kvm_vcpu *vcpu,int dr,unsigned long val) {
arch/x86/kvm/svm/nested.c +1 -1
@@ -808,7 +808,7 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm)if (unlikely (new_vmcb12 ||vmcb12_is_dirty (control,VMCB_DR ))) { vmcb02->save.dr7 = svm->nested.save.dr7 |DR7_FIXED_1 ; - svm->vcpu.arch.dr6 = svm->nested.save.dr6 |DR6_ACTIVE_LOW ; + svm->vcpu.arch.dr6 = svm->nested.save.dr6 |kvm_get_dr6_fixed_1 (vcpu);vmcb_mark_dirty (vmcb02,VMCB_DR ); }
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Sean Christopherson
maintainerPATCH v6 11/12 KVM: SVM: Add a vCPU-aware helper to get supported DEBUGCTL bits
In reply to Sean ChristophersonFrom: Shivansh Dhiman <[email protected]>
Replace DEBUGCTL_RESERVED_BITS with a vCPU-aware helper that provides the set of supported bits so that KVM can handle bits that are conditionally supported based on the vCPU model, e.g. for features like Bus Lock Detect.
No functional change intended.
--- arch/x86/kvm/svm/nested.c | 8 ++++---- arch/x86/kvm/svm/svm.c | 6 +++--- arch/x86/kvm/svm/svm.h | 7 +++++-- 3 files changed, 12 insertions(+), 9 deletions(-)
arch/x86/kvm/svm/nested.c +4 -4
@@ -818,7 +818,7 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm)* svm_set_msr's definition of reserved bits. */ svm_copy_lbrs (&vmcb02->save, save); - vmcb02->save.dbgctl &= ~DEBUGCTL_RESERVED_BITS ; + vmcb02->save.dbgctl &=svm_get_supported_debugctl (vcpu); }else {svm_copy_lbrs (&vmcb02->save, &vmcb01->save); }
@@ -1207,7 +1207,7 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu) }/* Copy state save area fields which are handled by VMRUN */ -void svm_copy_vmrun_state (struct vmcb_save_area *to_save, +void svm_copy_vmrun_state (struct kvm_vcpu *vcpu,struct vmcb_save_area *to_save,struct vmcb_save_area *from_save) { to_save->es = from_save->es;
@@ -1234,7 +1234,7 @@ void svm_copy_vmrun_state(struct vmcb_save_area *to_save,if (kvm_cpu_cap_has (X86_FEATURE_LBRV )) {svm_copy_lbrs (to_save, from_save); - to_save->dbgctl &= ~DEBUGCTL_RESERVED_BITS ; + to_save->dbgctl &=svm_get_supported_debugctl (vcpu); } }
@@ -2081,7 +2081,7 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu, svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa; -svm_copy_vmrun_state (&svm->vmcb01.ptr->save, save); +svm_copy_vmrun_state (vcpu, &svm->vmcb01.ptr->save, save);nested_copy_vmcb_control_to_cache (svm, ctl);svm_switch_vmcb (svm, &svm->nested.vmcb02);
arch/x86/kvm/svm/svm.c +3 -3
@@ -3210,7 +3210,7 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) data &= ~DEBUGCTLMSR_BTF ; } -if (data &DEBUGCTL_RESERVED_BITS ) +if (data & ~svm_get_supported_debugctl (vcpu))return 1 ;if (svm->vmcb->save.dbgctl == data)
@@ -5164,7 +5164,7 @@ static int svm_enter_smm(struct kvm_vcpu *vcpu, union kvm_smram *smram)BUILD_BUG_ON (offsetof(struct vmcb , save) !=0x400 ); -svm_copy_vmrun_state (m_save.map.hva +0x400 , &svm->vmcb01.ptr->save); +svm_copy_vmrun_state (vcpu, m_save.map.hva +0x400 , &svm->vmcb01.ptr->save);return 0 ; }
@@ -5204,7 +5204,7 @@ static int svm_leave_smm(struct kvm_vcpu *vcpu, const union kvm_smram *smram)* used during SMM (see svm_enter_smm()) */ -svm_copy_vmrun_state (&svm->vmcb01.ptr->save, m_save.map.hva +0x400 ); +svm_copy_vmrun_state (vcpu, &svm->vmcb01.ptr->save, m_save.map.hva +0x400 );/* * Enter the nested guest now
arch/x86/kvm/svm/svm.h +5 -2
@@ -787,7 +787,10 @@ BUILD_SVM_MSR_BITMAP_HELPERS(bool, test, test) BUILD_SVM_MSR_BITMAP_HELPERS(void, clear, __clear) BUILD_SVM_MSR_BITMAP_HELPERS(void, set, __set) -#define DEBUGCTL_RESERVED_BITS (~DEBUGCTLMSR_LBR) +static inline u64 svm_get_supported_debugctl (struct kvm_vcpu *vcpu) +{ +return DEBUGCTLMSR_LBR ; +}/* svm.c */ extern bool dump_invalid_vmcb;
@@ -877,7 +880,7 @@ void svm_leave_nested(struct kvm_vcpu *vcpu);void svm_free_nested (struct vcpu_svm *svm);int svm_allocate_nested (struct vcpu_svm *svm);int nested_svm_vmrun (struct kvm_vcpu *vcpu); -void svm_copy_vmrun_state (struct vmcb_save_area *to_save, +void svm_copy_vmrun_state (struct kvm_vcpu *vcpu,struct vmcb_save_area *to_save,struct vmcb_save_area *from_save);void svm_copy_vmloadsave_state (struct vmcb *to_vmcb,struct vmcb *from_vmcb);void nested_svm_vmexit (struct vcpu_svm *svm);
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Sean Christopherson
maintainerPATCH v6 12/12 KVM: SVM: Add support for virtualizating Bus Lock Detect
In reply to Sean ChristophersonFrom: Ravi Bangoria <[email protected]>
Advertise support for virtualizing Bus Lock Detect and allow the guest to enable BUS_LOCK_DETECT if LBR Virtualization is enabled (Bus Lock Detect is enabled through MSR_IA32_DEBUGCTLMSR, which context-switched if and only if LBR Virtualization is enabled).
--- arch/x86/kvm/svm/svm.c | 12 ++++++++++-- arch/x86/kvm/svm/svm.h | 7 ++++++- 2 files changed, 16 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/svm.c +10 -2
@@ -879,7 +879,7 @@ static bool svm_need_lbr_virtualization(struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); -if (svm->vmcb->save.dbgctl &DEBUGCTLMSR_LBR ) +if (svm->vmcb->save.dbgctl & (DEBUGCTLMSR_LBR |DEBUGCTLMSR_BUS_LOCK_DETECT ))return true; return is_guest_mode(vcpu) &&
@@ -5712,9 +5712,17 @@ static __init void svm_set_cpu_caps(void)* Clear capabilities that are automatically configured by common code, * but that require explicit SVM support (that isn't yet implemented). */ -kvm_cpu_cap_clear (X86_FEATURE_BUS_LOCK_DETECT );kvm_cpu_cap_clear (X86_FEATURE_MSR_IMM ); +/* +* LBR Virtualization must be enabled to support BusLockTrap inside the +* guest, since BusLockTrap is enabled through MSR_IA32_DEBUGCTLMSR and +* MSR_IA32_DEBUGCTLMSR is virtualized only if LBR Virtualization is +* enabled. +*/ +if (!lbrv) +kvm_cpu_cap_clear (X86_FEATURE_BUS_LOCK_DETECT ); +kvm_setup_xss_caps ();kvm_finalize_cpu_caps (); }
arch/x86/kvm/svm/svm.h +6 -1
@@ -789,7 +789,12 @@ BUILD_SVM_MSR_BITMAP_HELPERS(void, set, __set)static inline u64 svm_get_supported_debugctl (struct kvm_vcpu *vcpu) { -return DEBUGCTLMSR_LBR ; +u64 debugctl =DEBUGCTLMSR_LBR ; + +if (guest_cpu_cap_has (vcpu,X86_FEATURE_BUS_LOCK_DETECT )) + debugctl |=DEBUGCTLMSR_BUS_LOCK_DETECT ; + +return debugctl; }/* svm.c */
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
[email protected]
AI reviewRe: [PATCH v6 12/12] KVM: SVM: Add support for virtualizating Bus Lock Detect
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] kvm: svm: missing host_initiated check in MSR_IA32_DEBUGCTLMSR restoration --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] kvm: svm: missing host_initiated check in MSR_IA32_DEBUGCTLMSR restoration --
commit 6a801e9cb8e116e2aead8b9c8e78497f605d7a2c
Author: Ravi Bangoria <[email protected]>
KVM: SVM: Add support for virtualizating Bus Lock Detect
This commit advertises support for virtualizing Bus Lock Detect on SVM. It allows the guest to enable BUS_LOCK_DETECT via MSR_IA32_DEBUGCTLMSR if LBR Virtualization is enabled.
Quoted 4 lines
arch/x86/kvm/svm/svm.h +0
-0
[ ... ]
arch/x86/kvm/svm/svm.h BUILD_SVM_MSR_BITMAP_HELPERS(void, set, __set) · line 796
Quoted 10 lines
arch/x86/kvm/svm/svm.h +5
-1@@ -789,7 +789,12 @@ BUILD_SVM_MSR_BITMAP_HELPERS(void, set, __set)static inline u64 svm_get_supported_debugctl (struct kvm_vcpu *vcpu) { -return DEBUGCTLMSR_LBR ; +u64 debugctl =DEBUGCTLMSR_LBR ; + +if (guest_cpu_cap_has (vcpu,X86_FEATURE_BUS_LOCK_DETECT )) + debugctl |=DEBUGCTLMSR_BUS_LOCK_DETECT ; +
[Severity: Medium]
Does this restrict host-initiated MSR restoration? By making the DEBUGCTLMSR_BUS_LOCK_DETECT bit strictly dependent on guest_cpu_cap_has(), this might reject host-initiated writes to MSR_IA32_DEBUGCTLMSR if the VMM restores MSRs before initializing the guest CPUID.
In svm_set_msr(), the rejection happens unconditionally:
svm_set_msr()
if (data & ~svm_get_supported_debugctl(vcpu))
return 1;
Since svm_set_msr() does not appear to check msr->host_initiated before enforcing this capability limit, could this regression break live migration when userspace sends KVM_SET_MSRS before the CPUID is fully configured?
Quoted 2 lines
arch/x86/kvm/svm/svm.h +1
-0+return debugctl; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
Sean Christopherson
maintainerRe: [PATCH v6 12/12] KVM: SVM: Add support for virtualizating Bus Lock Detect
Quoted 24 lines · On Wed, Sep 30, 2026, [email protected] wrote:
Quoted 4 lines
arch/x86/kvm/svm/svm.h +0
-0[ ... ]
arch/x86/kvm/svm/svm.h BUILD_SVM_MSR_BITMAP_HELPERS(void, set, __set) · line 796
Quoted 10 lines
arch/x86/kvm/svm/svm.h +5
-1@@ -789,7 +789,12 @@ BUILD_SVM_MSR_BITMAP_HELPERS(void, set, __set)static inline u64 svm_get_supported_debugctl (struct kvm_vcpu *vcpu) { -return DEBUGCTLMSR_LBR ; +u64 debugctl =DEBUGCTLMSR_LBR ; + +if (guest_cpu_cap_has (vcpu,X86_FEATURE_BUS_LOCK_DETECT )) + debugctl |=DEBUGCTLMSR_BUS_LOCK_DETECT ; +[Severity: Medium]
Does this restrict host-initiated MSR restoration? By making theDEBUGCTLMSR_BUS_LOCK_DETECTbit strictly dependent onguest_cpu_cap_has(), this might reject host-initiated writes toMSR_IA32_DEBUGCTLMSRif the VMM restores MSRs before initializing the guest CPUID.In
svm_set_msr(), the rejection happens unconditionally:
Quoted 8 lines
svm_set_msr()
if (data & ~svm_get_supported_debugctl(vcpu))
return 1;Since
svm_set_msr()does not appear to checkmsr->host_initiatedbefore enforcing this capability limit, could this regression break live migration when userspace sendsKVM_SET_MSRSbefore the CPUID is fully configured?
By design. The msr->host_initiated exemption is "legacy" KVM behavior that we're actively trying to remove.
Quoted 8 lines · Sean Christopherson
Fix KVM's handling of DR6.BLD, and add support for virtualizing BLD on SVM. KUT tests: https://lore.kernel.org/all/[email protected]
v6:
- Throw in the towel, at least for now, on WARNing if KVM sees a DR6 without all of the fixed-1 bits set. [Sashiko][...]
Applied to kvm-x86 misc, thanks!
[01/12] KVM: x86: Preserve DR6.BLD (Bus Lock Detect) when delivering #DB payload
https://github.com/kvm-x86/linux/commit/719a1d58bf43 [02/12] KVM: x86: Rename kvm_dr6_fixed() => kvm_get_dr6_fixed_1() https://github.com/kvm-x86/linux/commit/cc551e8d4f77 [03/12] KVM: Return an "unsigned long", not "u64" for the fixed-1 DR6 bits https://github.com/kvm-x86/linux/commit/28bb033664ee [04/12] KVM: x86: Force fixed-1 bits in DR6 after synchronizing with hardware https://github.com/kvm-x86/linux/commit/7b88577a0983 [05/12] KVM: x86: Set guest's fixed-1 DR6 bits when delivering #DB payload https://github.com/kvm-x86/linux/commit/c62ffee78db8 [06/12] KVM: x86: Kill off DR6_FIXED_1 to prevent future misuse https://github.com/kvm-x86/linux/commit/0e2342b02380 [07/12] KVM: SVM: Add helper to query if LBR virtualization needs to be enabled https://github.com/kvm-x86/linux/commit/7faa73133e40 [08/12] KVM: nSVM: Disable LBRV in nested control cache when unsupported https://github.com/kvm-x86/linux/commit/5cb0577b7497 [09/12] KVM: nSVM: Open code check on LBR virtualization being enabled in vmcb12 https://github.com/kvm-x86/linux/commit/cab4449bddbd [10/12] KVM: nSVM: Don't assume all active-low bits DR6 are fixed-1 https://github.com/kvm-x86/linux/commit/39080c3ca8b1 [11/12] KVM: SVM: Add a vCPU-aware helper to get supported DEBUGCTL bits https://github.com/kvm-x86/linux/commit/cd0e6f4d7cec [12/12] KVM: SVM: Add support for virtualizating Bus Lock Detect https://github.com/kvm-x86/linux/commit/62ad461079e7
Quoted 11 lines · Sean Christopherson
Quoted 8 lines · Sean Christopherson
Fix KVM's handling of DR6.BLD, and add support for virtualizing BLD on SVM. KUT tests: https://lore.kernel.org/all/[email protected]
v6:
- Throw in the towel, at least for now, on WARNing if KVM sees a DR6 without all of the fixed-1 bits set. [Sashiko][...]
Applied to kvm-x86 misc, thanks!
Thanks a lot, Sean, for taking this over, merging it, and adding the KUT coverage along the way. Thanks also to Ravi for the original patch, and to Yosry, Nikunj and Tom for the reviews.
-Shivansh