[PATCH 09/16] KVM: TDX: Complete interrupts after TD exit
From: Binbin Wu
Date: Sun Dec 08 2024 - 20:09:44 EST
From: Isaku Yamahata <isaku.yamahata@xxxxxxxxx>
Complete NMI injection by updating the status of NMI injection for TDX.
Because TDX virtualizes vAPIC, and non-NMI interrupts are delivered
using posted-interrupt mechanism, KVM only needs to care about NMI
injection.
For VMX, KVM injects an NMI by setting VM_ENTRY_INTR_INFO_FIELD via
vector-event injection mechanism. For TDX, KVM needs to request TDX
module to inject an NMI into a guest TD vCPU when the vCPU is not
active by setting PEND_NMI field within the TDX vCPU scope metadata
(Trust Domain Virtual Processor State (TDVPS)). TDX module will attempt
to inject an NMI as soon as possible on TD entry. KVM can read PEND_NMI
to get the status of NMI injection. A value of 0 indicates the NMI has
been injected into the guest TD vCPU.
Update KVM's NMI status on TD exit by checking whether a requested NMI
has been injected into the TD. Reading the PEND_NMI field via SEAMCALL
is expensive so only perform the check if an NMI was requested to inject.
If the read back value is 0, the NMI has been injected, update the NMI
status. If the read back value is 1, no action needed since the PEND_NMI
is still set.
Signed-off-by: Isaku Yamahata <isaku.yamahata@xxxxxxxxx>
Signed-off-by: Binbin Wu <binbin.wu@xxxxxxxxxxxxxxx>
Reviewed-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
---
TDX interrupts breakout:
- Shortlog "tdexit" -> "TD exit" (Reinette)
- Update change log as following suggested by Reinette with a little
supplement.
https://lore.kernel.org/lkml/fe9cec78-36ee-4a20-81df-ec837a45f69f@xxxxxxxxxxxxxxx/
- Fix comment, "nmi" -> "NMI" and add a missing period. (Reinette)
- Add a comment to explain why no need to request KVM_REQ_EVENT.
v19:
- move tdvps_management_check() to this patch
- typo: complete -> Complete in short log
---
arch/x86/kvm/vmx/tdx.c | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index 65fe7ba8a6c6..b0f525069ebd 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -853,6 +853,21 @@ int tdx_vcpu_pre_run(struct kvm_vcpu *vcpu)
return 1;
}
+static void tdx_complete_interrupts(struct kvm_vcpu *vcpu)
+{
+ /* Avoid costly SEAMCALL if no NMI was injected. */
+ if (vcpu->arch.nmi_injected) {
+ /*
+ * No need to request KVM_REQ_EVENT because PEND_NMI is still
+ * set if NMI re-injection needed. No other event types need
+ * to be handled because TDX doesn't support injection of
+ * exception, SMI or interrupt (via event injection).
+ */
+ vcpu->arch.nmi_injected = td_management_read8(to_tdx(vcpu),
+ TD_VCPU_PEND_NMI);
+ }
+}
+
struct tdx_uret_msr {
u32 msr;
unsigned int slot;
@@ -1004,6 +1019,8 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu, bool force_immediate_exit)
if (unlikely(tdx_has_exit_reason(vcpu) && tdexit_exit_reason(vcpu).failed_vmentry))
return EXIT_FASTPATH_NONE;
+ tdx_complete_interrupts(vcpu);
+
return tdx_exit_handlers_fastpath(vcpu);
}
--
2.46.0