Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit b7e4be0

Browse files
Ravi Bangoriabonzini
authored andcommitted
KVM: SEV-ES: Delegate LBR virtualization to the processor
As documented in APM[1], LBR Virtualization must be enabled for SEV-ES guests. Although KVM currently enforces LBRV for SEV-ES guests, there are multiple issues with it: o MSR_IA32_DEBUGCTLMSR is still intercepted. Since MSR_IA32_DEBUGCTLMSR interception is used to dynamically toggle LBRV for performance reasons, this can be fatal for SEV-ES guests. For ex SEV-ES guest on Zen3: [guest ~]# wrmsr 0x1d9 0x4 KVM: entry failed, hardware error 0xffffffff EAX=00000004 EBX=00000000 ECX=000001d9 EDX=00000000 Fix this by never intercepting MSR_IA32_DEBUGCTLMSR for SEV-ES guests. No additional save/restore logic is required since MSR_IA32_DEBUGCTLMSR is of swap type A. o KVM will disable LBRV if userspace sets MSR_IA32_DEBUGCTLMSR before the VMSA is encrypted. Fix this by moving LBRV enablement code post VMSA encryption. [1]: AMD64 Architecture Programmer's Manual Pub. 40332, Rev. 4.07 - June 2023, Vol 2, 15.35.2 Enabling SEV-ES. https://bugzilla.kernel.org/attachment.cgi?id=304653 Fixes: 376c6d2 ("KVM: SVM: Provide support for SEV-ES vCPU creation/loading") Co-developed-by: Nikunj A Dadhania <nikunj@amd.com> Signed-off-by: Nikunj A Dadhania <nikunj@amd.com> Signed-off-by: Ravi Bangoria <ravi.bangoria@amd.com> Message-ID: <20240531044644.768-4-ravi.bangoria@amd.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent d922056 commit b7e4be0

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

arch/x86/kvm/svm/sev.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,14 @@ static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu,
779779
*/
780780
fpstate_set_confidential(&vcpu->arch.guest_fpu);
781781
vcpu->arch.guest_state_protected = true;
782+
783+
/*
784+
* SEV-ES guest mandates LBR Virtualization to be _always_ ON. Enable it
785+
* only after setting guest_state_protected because KVM_SET_MSRS allows
786+
* dynamic toggling of LBRV (for performance reason) on write access to
787+
* MSR_IA32_DEBUGCTLMSR when guest_state_protected is not set.
788+
*/
789+
svm_enable_lbrv(vcpu);
782790
return 0;
783791
}
784792

@@ -3222,7 +3230,6 @@ static void sev_es_init_vmcb(struct vcpu_svm *svm)
32223230
struct kvm_vcpu *vcpu = &svm->vcpu;
32233231

32243232
svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ES_ENABLE;
3225-
svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
32263233

32273234
/*
32283235
* An SEV-ES guest requires a VMSA area that is a separate from the
@@ -3274,10 +3281,6 @@ static void sev_es_init_vmcb(struct vcpu_svm *svm)
32743281
/* Clear intercepts on selected MSRs */
32753282
set_msr_interception(vcpu, svm->msrpm, MSR_EFER, 1, 1);
32763283
set_msr_interception(vcpu, svm->msrpm, MSR_IA32_CR_PAT, 1, 1);
3277-
set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
3278-
set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
3279-
set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
3280-
set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
32813284
}
32823285

32833286
void sev_init_vmcb(struct vcpu_svm *svm)

arch/x86/kvm/svm/svm.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ static const struct svm_direct_access_msrs {
9999
{ .index = MSR_IA32_SPEC_CTRL, .always = false },
100100
{ .index = MSR_IA32_PRED_CMD, .always = false },
101101
{ .index = MSR_IA32_FLUSH_CMD, .always = false },
102+
{ .index = MSR_IA32_DEBUGCTLMSR, .always = false },
102103
{ .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
103104
{ .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
104105
{ .index = MSR_IA32_LASTINTFROMIP, .always = false },
@@ -990,7 +991,7 @@ void svm_copy_lbrs(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
990991
vmcb_mark_dirty(to_vmcb, VMCB_LBR);
991992
}
992993

993-
static void svm_enable_lbrv(struct kvm_vcpu *vcpu)
994+
void svm_enable_lbrv(struct kvm_vcpu *vcpu)
994995
{
995996
struct vcpu_svm *svm = to_svm(vcpu);
996997

@@ -1000,6 +1001,9 @@ static void svm_enable_lbrv(struct kvm_vcpu *vcpu)
10001001
set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
10011002
set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
10021003

1004+
if (sev_es_guest(vcpu->kvm))
1005+
set_msr_interception(vcpu, svm->msrpm, MSR_IA32_DEBUGCTLMSR, 1, 1);
1006+
10031007
/* Move the LBR msrs to the vmcb02 so that the guest can see them. */
10041008
if (is_guest_mode(vcpu))
10051009
svm_copy_lbrs(svm->vmcb, svm->vmcb01.ptr);
@@ -1009,6 +1013,8 @@ static void svm_disable_lbrv(struct kvm_vcpu *vcpu)
10091013
{
10101014
struct vcpu_svm *svm = to_svm(vcpu);
10111015

1016+
KVM_BUG_ON(sev_es_guest(vcpu->kvm), vcpu->kvm);
1017+
10121018
svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
10131019
set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
10141020
set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);

arch/x86/kvm/svm/svm.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#define IOPM_SIZE PAGE_SIZE * 3
3131
#define MSRPM_SIZE PAGE_SIZE * 2
3232

33-
#define MAX_DIRECT_ACCESS_MSRS 47
33+
#define MAX_DIRECT_ACCESS_MSRS 48
3434
#define MSRPM_OFFSETS 32
3535
extern u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
3636
extern bool npt_enabled;
@@ -553,6 +553,7 @@ u32 *svm_vcpu_alloc_msrpm(void);
553553
void svm_vcpu_init_msrpm(struct kvm_vcpu *vcpu, u32 *msrpm);
554554
void svm_vcpu_free_msrpm(u32 *msrpm);
555555
void svm_copy_lbrs(struct vmcb *to_vmcb, struct vmcb *from_vmcb);
556+
void svm_enable_lbrv(struct kvm_vcpu *vcpu);
556557
void svm_update_lbrv(struct kvm_vcpu *vcpu);
557558

558559
int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer);

0 commit comments

Comments
 (0)