Skip to content

Commit 0977cfa

Browse files
sashukla1sean-jc
authored andcommitted
KVM: nSVM: Implement support for nested VNMI
Allow L1 to use vNMI to accelerate its injection of NMI to L2 by propagating vNMI int_ctl bits from/to vmcb12 to/from vmcb02. To handle both the case where vNMI is enabled for L1 and L2, and where vNMI is enabled for L1 but _not_ L2, move pending L1 vNMIs to nmi_pending on nested VM-Entry and raise KVM_REQ_EVENT, i.e. rely on existing code to route the NMI to the correct domain. On nested VM-Exit, reverse the process and set/clear V_NMI_PENDING for L1 based one whether nmi_pending is zero or non-zero. There is no need to consider vmcb02 in this case, as V_NMI_PENDING can be set in vmcb02 if vNMI is disabled for L2, and if vNMI is enabled for L2, then L1 and L2 have different NMI contexts. Co-developed-by: Maxim Levitsky <mlevitsk@redhat.com> Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com> Signed-off-by: Santosh Shukla <santosh.shukla@amd.com> Link: https://lore.kernel.org/r/20230227084016.3368-12-santosh.shukla@amd.com [sean: massage changelog to match the code] Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent fa4c027 commit 0977cfa

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

arch/x86/kvm/svm/nested.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,11 @@ static bool __nested_vmcb_check_controls(struct kvm_vcpu *vcpu,
281281
if (CC(!nested_svm_check_tlb_ctl(vcpu, control->tlb_ctl)))
282282
return false;
283283

284+
if (CC((control->int_ctl & V_NMI_ENABLE_MASK) &&
285+
!vmcb12_is_intercept(control, INTERCEPT_NMI))) {
286+
return false;
287+
}
288+
284289
return true;
285290
}
286291

@@ -436,6 +441,9 @@ void nested_sync_control_from_vmcb02(struct vcpu_svm *svm)
436441
if (nested_vgif_enabled(svm))
437442
mask |= V_GIF_MASK;
438443

444+
if (nested_vnmi_enabled(svm))
445+
mask |= V_NMI_BLOCKING_MASK | V_NMI_PENDING_MASK;
446+
439447
svm->nested.ctl.int_ctl &= ~mask;
440448
svm->nested.ctl.int_ctl |= svm->vmcb->control.int_ctl & mask;
441449
}
@@ -655,6 +663,17 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm,
655663
else
656664
int_ctl_vmcb01_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK);
657665

666+
if (vnmi) {
667+
if (vmcb01->control.int_ctl & V_NMI_PENDING_MASK) {
668+
svm->vcpu.arch.nmi_pending++;
669+
kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
670+
}
671+
if (nested_vnmi_enabled(svm))
672+
int_ctl_vmcb12_bits |= (V_NMI_PENDING_MASK |
673+
V_NMI_ENABLE_MASK |
674+
V_NMI_BLOCKING_MASK);
675+
}
676+
658677
/* Copied from vmcb01. msrpm_base can be overwritten later. */
659678
vmcb02->control.nested_ctl = vmcb01->control.nested_ctl;
660679
vmcb02->control.iopm_base_pa = vmcb01->control.iopm_base_pa;
@@ -1055,6 +1074,20 @@ int nested_svm_vmexit(struct vcpu_svm *svm)
10551074
svm_update_lbrv(vcpu);
10561075
}
10571076

1077+
if (vnmi) {
1078+
if (vmcb02->control.int_ctl & V_NMI_BLOCKING_MASK)
1079+
vmcb01->control.int_ctl |= V_NMI_BLOCKING_MASK;
1080+
else
1081+
vmcb01->control.int_ctl &= ~V_NMI_BLOCKING_MASK;
1082+
1083+
if (vcpu->arch.nmi_pending) {
1084+
vcpu->arch.nmi_pending--;
1085+
vmcb01->control.int_ctl |= V_NMI_PENDING_MASK;
1086+
} else {
1087+
vmcb01->control.int_ctl &= ~V_NMI_PENDING_MASK;
1088+
}
1089+
}
1090+
10581091
/*
10591092
* On vmexit the GIF is set to false and
10601093
* no event can be injected in L1.

arch/x86/kvm/svm/svm.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4246,6 +4246,8 @@ static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
42464246

42474247
svm->vgif_enabled = vgif && guest_cpuid_has(vcpu, X86_FEATURE_VGIF);
42484248

4249+
svm->vnmi_enabled = vnmi && guest_cpuid_has(vcpu, X86_FEATURE_VNMI);
4250+
42494251
svm_recalc_instruction_intercepts(vcpu, svm);
42504252

42514253
/* For sev guests, the memory encryption bit is not reserved in CR3. */
@@ -5001,6 +5003,9 @@ static __init void svm_set_cpu_caps(void)
50015003
if (vgif)
50025004
kvm_cpu_cap_set(X86_FEATURE_VGIF);
50035005

5006+
if (vnmi)
5007+
kvm_cpu_cap_set(X86_FEATURE_VNMI);
5008+
50045009
/* Nested VM can receive #VMEXIT instead of triggering #GP */
50055010
kvm_cpu_cap_set(X86_FEATURE_SVME_ADDR_CHK);
50065011
}

arch/x86/kvm/svm/svm.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ struct vcpu_svm {
266266
bool pause_filter_enabled : 1;
267267
bool pause_threshold_enabled : 1;
268268
bool vgif_enabled : 1;
269+
bool vnmi_enabled : 1;
269270

270271
u32 ldr_reg;
271272
u32 dfr_reg;
@@ -540,6 +541,12 @@ static inline bool nested_npt_enabled(struct vcpu_svm *svm)
540541
return svm->nested.ctl.nested_ctl & SVM_NESTED_CTL_NP_ENABLE;
541542
}
542543

544+
static inline bool nested_vnmi_enabled(struct vcpu_svm *svm)
545+
{
546+
return svm->vnmi_enabled &&
547+
(svm->nested.ctl.int_ctl & V_NMI_ENABLE_MASK);
548+
}
549+
543550
static inline bool is_x2apic_msrpm_offset(u32 offset)
544551
{
545552
/* 4 msrs per u8, and 4 u8 in u32 */

0 commit comments

Comments
 (0)