[RFC PATCH 4/9] KVM: SVM: Add Alternate Injection path
From: Melody Wang
Date: Tue Nov 10 2026 - 03:36:09 EST
Now that everything is in place, add the Alternate Injection path to
KVM's interrupt handling flow.
Signed-off-by: Melody Wang <huibo.wang@xxxxxxx>
---
arch/x86/include/asm/kvm-x86-ops.h | 1 +
arch/x86/include/asm/kvm_host.h | 1 +
arch/x86/kvm/irq.c | 3 +++
arch/x86/kvm/svm/sev.c | 30 +++++++++++++++++++++++++-----
arch/x86/kvm/svm/svm.c | 17 ++++++++++++++++-
5 files changed, 46 insertions(+), 6 deletions(-)
diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h
index 4f96090c04c9..16da6d1678e5 100644
--- a/arch/x86/include/asm/kvm-x86-ops.h
+++ b/arch/x86/include/asm/kvm-x86-ops.h
@@ -95,6 +95,7 @@ KVM_X86_OP_OPTIONAL(sync_pir_to_irr)
KVM_X86_OP_OPTIONAL_RET0(set_tss_addr)
KVM_X86_OP_OPTIONAL_RET0(set_identity_map_addr)
KVM_X86_OP_OPTIONAL_RET0(get_mt_mask)
+KVM_X86_OP_OPTIONAL_RET0(has_interrupt)
KVM_X86_OP(load_mmu_pgd)
KVM_X86_OP_OPTIONAL(link_external_spt)
KVM_X86_OP_OPTIONAL(set_external_spte)
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index bfa0188d372f..985d7f4d7140 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1873,6 +1873,7 @@ struct kvm_x86_ops {
u32 (*get_interrupt_shadow)(struct kvm_vcpu *vcpu);
void (*patch_hypercall)(struct kvm_vcpu *vcpu,
unsigned char *hypercall_addr);
+ int (*has_interrupt)(struct kvm_vcpu *vcpu);
void (*inject_irq)(struct kvm_vcpu *vcpu, bool reinjected);
void (*inject_nmi)(struct kvm_vcpu *vcpu);
void (*inject_exception)(struct kvm_vcpu *vcpu);
diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c
index 5f7e5f3e5528..c781ce916fbd 100644
--- a/arch/x86/kvm/irq.c
+++ b/arch/x86/kvm/irq.c
@@ -111,6 +111,9 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_cpu_has_injectable_intr);
*/
int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
{
+ if (kvm_x86_call(has_interrupt)(v))
+ return 1;
+
if (kvm_cpu_has_extint(v))
return 1;
diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index 78b8487627ed..543851bfb94d 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -5958,7 +5958,7 @@ bool sev_snp_ai_queue_exception(struct kvm_vcpu *vcpu)
return true;
}
-static void __maybe_unused sev_snp_ai_inject_irq(struct kvm_vcpu *vcpu)
+static void sev_snp_ai_inject_irq(struct kvm_vcpu *vcpu)
{
struct kvm_lapic *apic;
int irq, trig_mode;
@@ -5981,12 +5981,32 @@ static void __maybe_unused sev_snp_ai_inject_irq(struct kvm_vcpu *vcpu)
bool sev_snp_inject(enum inject_type type, struct kvm_vcpu *vcpu)
{
- if (!sev_snp_is_rinj_active(vcpu))
- return false;
+ struct vcpu_svm *svm = to_svm(vcpu);
- __sev_snp_inject(type, vcpu);
+ if (!vcpu->plane_level) {
+ if (!sev_snp_is_rinj_active(vcpu)) {
+ return false;
+ } else {
+ __sev_snp_inject(type, vcpu);
+ return true;
+ }
+ } else {
+ if (sev_snp_is_ai_active(vcpu)) {
+ if (svm->sev_es.cancel_flag) {
+ svm->sev_es.cancel_flag = false;
+ return sev_snp_ai_inject(vcpu);
+ }
+ if (type == INJECT_NMI)
+ sev_snp_ai_update_doorbell(INJECT_NMI, vcpu, 0, 0);
+ if (type == INJECT_MCE)
+ sev_snp_ai_update_doorbell(INJECT_MCE, vcpu, 0, 0);
+ if (type == INJECT_IRQ)
+ sev_snp_ai_inject_irq(vcpu);
+ return sev_snp_ai_inject(vcpu);
+ }
+ }
- return true;
+ return false;
}
void sev_snp_cancel_injection(struct kvm_vcpu *vcpu)
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 96d31fabb292..7c4b3d05ae88 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -395,6 +395,9 @@ static void svm_inject_exception(struct kvm_vcpu *vcpu)
if (sev_snp_queue_exception(vcpu))
return;
+ if (sev_snp_ai_queue_exception(vcpu))
+ return;
+
svm->vmcb->control.event_inj = ex->vector
| SVM_EVTINJ_VALID
| (ex->has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
@@ -3976,6 +3979,9 @@ bool svm_nmi_blocked(struct kvm_vcpu *vcpu)
if (sev_snp_is_rinj_active(vcpu))
return sev_snp_blocked(INJECT_NMI, vcpu);
+ if (sev_snp_is_ai_active(vcpu))
+ return sev_snp_ai_blocked(INJECT_NMI, vcpu);
+
if (is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
return false;
@@ -4011,6 +4017,9 @@ bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)
if (sev_snp_is_rinj_active(vcpu))
return sev_snp_blocked(INJECT_IRQ, vcpu);
+ if (sev_snp_is_ai_active(vcpu))
+ return sev_snp_ai_blocked(INJECT_IRQ, vcpu);
+
if (is_guest_mode(vcpu)) {
/* As long as interrupts are being delivered... */
if ((svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK)
@@ -4054,6 +4063,9 @@ bool svm_mce_blocked(struct kvm_vcpu *vcpu)
if (sev_snp_is_rinj_active(vcpu))
return sev_snp_blocked(INJECT_MCE, vcpu);
+ if (sev_snp_is_ai_active(vcpu))
+ return sev_snp_ai_blocked(INJECT_MCE, vcpu);
+
return false;
}
@@ -4065,7 +4077,7 @@ static int svm_mce_allowed(struct kvm_vcpu *vcpu)
return 1;
}
-static int __maybe_unused svm_has_interrupt(struct kvm_vcpu *vcpu)
+static int svm_has_interrupt(struct kvm_vcpu *vcpu)
{
int ret = 0;
unsigned int state;
@@ -4413,6 +4425,8 @@ static void svm_cancel_injection(struct kvm_vcpu *vcpu)
sev_snp_cancel_injection(vcpu);
+ sev_snp_cancel_ai_injection(vcpu);
+
control->exit_int_info = control->event_inj;
control->exit_int_info_err = control->event_inj_err;
control->event_inj = 0;
@@ -5431,6 +5445,7 @@ struct kvm_x86_ops svm_x86_ops __initdata = {
.set_interrupt_shadow = svm_set_interrupt_shadow,
.get_interrupt_shadow = svm_get_interrupt_shadow,
.patch_hypercall = svm_patch_hypercall,
+ .has_interrupt = svm_has_interrupt,
.inject_irq = svm_inject_irq,
.inject_nmi = svm_inject_nmi,
.is_vnmi_pending = svm_is_vnmi_pending,
--
2.43.0