[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v2] x86/vLAPIC: only accept interrupts on enabled LAPICs


  • To: "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Jan Beulich <jbeulich@xxxxxxxx>
  • Date: Tue, 1 Dec 2026 10:02:46 +0100
  • Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=google header.d=suse.com header.i="@suse.com" header.h="Content-Transfer-Encoding:Content-Type:Autocrypt:Content-Language:Cc:To:Subject:From:User-Agent:MIME-Version:Date:Message-ID"
  • Autocrypt: addr=jbeulich@xxxxxxxx; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcnHAUSuCXBPy0jOlBhxPqRWv6ND4c9PH1xjQ3NP nxJuMBS8rnNg22uyfAgmBKNLpLgAGVRMZGaGoJObGf72s6TeIqKJo/LtggAS9qAUiuKVnygo 3wjfkS9A3DRO+SpU7JqWdsveeIQyeyEJ/8PTowmSQLakF+3fote9ybzd880fSmFuIEJldWxp Y2ggPGpiZXVsaWNoQHN1c2UuY29tPsJgBBMRAgAgBQJZN5xEAhsDBgsJCAcDAgQVAggDBBYC AwECHgECF4AACgkQoDSui/t3IH4J+wCfQ5jHdEjCRHj23O/5ttg9r9OIruwAn3103WUITZee e7Sbg12UgcQ5lv7SzsFNBFk3nEQQCACCuTjCjFOUdi5Nm244F+78kLghRcin/awv+IrTcIWF hUpSs1Y91iQQ7KItirz5uwCPlwejSJDQJLIS+QtJHaXDXeV6NI0Uef1hP20+y8qydDiVkv6l IreXjTb7DvksRgJNvCkWtYnlS3mYvQ9NzS9PhyALWbXnH6sIJd2O9lKS1Mrfq+y0IXCP10eS FFGg+Av3IQeFatkJAyju0PPthyTqxSI4lZYuJVPknzgaeuJv/2NccrPvmeDg6Coe7ZIeQ8Yj t0ARxu2xytAkkLCel1Lz1WLmwLstV30g80nkgZf/wr+/BXJW/oIvRlonUkxv+IbBM3dX2OV8 AmRv1ySWPTP7AAMFB/9PQK/VtlNUJvg8GXj9ootzrteGfVZVVT4XBJkfwBcpC/XcPzldjv+3 HYudvpdNK3lLujXeA5fLOH+Z/G9WBc5pFVSMocI71I8bT8lIAzreg0WvkWg5V2WZsUMlnDL9 mpwIGFhlbM3gfDMs7MPMu8YQRFVdUvtSpaAs8OFfGQ0ia3LGZcjA6Ik2+xcqscEJzNH+qh8V m5jjp28yZgaqTaRbg3M/+MTbMpicpZuqF4rnB0AQD12/3BNWDR6bmh+EkYSMcEIpQmBM51qM EKYTQGybRCjpnKHGOxG0rfFY1085mBDZCH5Kx0cl0HVJuQKC+dV2ZY5AqjcKwAxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL
  • Cc: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Teddy Astie <teddy.astie@xxxxxxxxxx>, Roger Pau Monné <roger@xxxxxxxxxxxxxx>, Stefano Stabellini <sstabellini@xxxxxxxxxx>
  • Delivery-date: Tue, 01 Dec 2026 09:02:51 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

In hardware-disabled mode the APIC behaves as if absent altogether. In
software-disabled mode it only accepts INIT, NMI, SMI, and SIPI.

Integrate the enabled checking into vlapic_match_dest(), requiring the
delivery mode to be passed in. This way (for vlapic_lowest_prio() and
vioapic_deliver()) the cheaper check also is done first.

Modernize a few involved types at the same time.

Fixes: 7429bfd50dd7 ("This patch provide local APIC support for vmx guest")
Fixes: e03ad58f0b8c ("x86 hvm: Clean up vlapic/vioapic/vmsi delivery")
Fixes: d75c52c50c4b ("iommu: Fix pirq conflict issue when guest adopts per-cpu 
vector")
Fixes: 023e3bc72dc3 ("x86: Make assigned devices' interrupts delivery to right 
vcpu")
Fixes: 7242e0dc2c6c ("hvm: Improve APIC INIT/SIPI emulation, fixing it for call 
paths other than x86_emulate()")
Fixes: 35a1caf8b6b5 ("pass-through: update IRTE according to guest interrupt 
config changes")
Reported-by: Roger Pau Monné <roger.pau@xxxxxxx>
Signed-off-by: Jan Beulich <jbeulich@xxxxxxxx>
---
What correct (or maybe merely "best") behavior is in _hvm_dpci_msi_eoi()
isn't quite clear to me: SDM section "Local APIC State After It Has Been
Software Disabled" doesn't cover the EOI register's behavior in that mode.
Nor does APM's "Local APIC Enable" section. Do we perhaps need a fake
APIC_DM_EOI mode which would then also only check HW-disabled?
---
v2: Add missing "break" in vlapic_match_dest() and use vlapic_disabled()
    there. Re-base.

--- a/xen/arch/x86/hvm/vioapic.c
+++ b/xen/arch/x86/hvm/vioapic.c
@@ -438,9 +438,8 @@ static void vioapic_deliver(struct hvm_v
 
         /* Force to pick vCPU 0 if part of the destination list */
         if ( (irq == hvm_isa_irq_to_gsi(0)) && pt_active(&d->arch.vpit.pt0) &&
-             vlapic_match_dest(lapic0, NULL, 0, dest, dest_mode) &&
-             /* Mimic the vlapic_enabled check found in vlapic_lowest_prio. */
-             vlapic_enabled(lapic0) )
+             vlapic_match_dest(lapic0, NULL, 0, dest, dest_mode,
+                               APIC_DM_LOWEST) )
             target = lapic0;
         else
 #endif
@@ -463,8 +462,8 @@ static void vioapic_deliver(struct hvm_v
         {
             struct vlapic *vlapic = vcpu_vlapic(v);
 
-            if ( vlapic_enabled(vlapic) &&
-                 vlapic_match_dest(vlapic, NULL, 0, dest, dest_mode) )
+            if ( vlapic_match_dest(vlapic, NULL, 0, dest, dest_mode,
+                                   APIC_DM_FIXED) )
                 ioapic_inj_irq(vioapic, vlapic, vector, trig_mode,
                                delivery_mode);
         }
@@ -473,8 +472,8 @@ static void vioapic_deliver(struct hvm_v
     case dest_NMI:
     {
         for_each_vcpu ( d, v )
-            if ( vlapic_match_dest(vcpu_vlapic(v), NULL,
-                                   0, dest, dest_mode) &&
+            if ( vlapic_match_dest(vcpu_vlapic(v), NULL, 0, dest, dest_mode,
+                                   APIC_DM_NMI) &&
                  !test_and_set_bool(v->arch.nmi_state.pending) )
                 vcpu_kick(v);
         break;
--- a/xen/arch/x86/hvm/vlapic.c
+++ b/xen/arch/x86/hvm/vlapic.c
@@ -238,12 +238,29 @@ static bool vlapic_match_logical_addr(co
 
 bool vlapic_match_dest(
     const struct vlapic *target, const struct vlapic *source,
-    int short_hand, uint32_t dest, bool dest_mode)
+    unsigned int short_hand, uint32_t dest, bool dest_mode, unsigned int dm)
 {
     HVM_DBG_LOG(DBG_LEVEL_VLAPIC, "target %p, source %p, dest %#x, "
                 "dest_mode %#x, short_hand %#x",
                 target, source, dest, dest_mode, short_hand);
 
+    switch ( dm )
+    {
+    default:
+        if ( vlapic_disabled(target) )
+            return false;
+        break;
+
+    case APIC_DM_SMI:
+    case APIC_DM_REMRD:
+    case APIC_DM_NMI:
+    case APIC_DM_INIT:
+    case APIC_DM_STARTUP:
+        if ( vlapic_hw_disabled(target) )
+            return false;
+        break;
+    }
+
     switch ( short_hand )
     {
     case APIC_DEST_NOSHORT:
@@ -317,7 +333,8 @@ static void cf_check vlapic_init_sipi_ac
     struct vcpu *origin = data;
     uint32_t icr = vcpu_vlapic(origin)->init_sipi.icr;
     uint32_t dest = vcpu_vlapic(origin)->init_sipi.dest;
-    uint32_t short_hand = icr & APIC_SHORT_MASK;
+    unsigned int short_hand = icr & APIC_SHORT_MASK;
+    unsigned int dm = icr & APIC_DM_MASK;
     bool dest_mode = icr & APIC_DEST_MASK;
     struct vcpu *v;
 
@@ -327,7 +344,7 @@ static void cf_check vlapic_init_sipi_ac
     for_each_vcpu ( origin->domain, v )
     {
         if ( vlapic_match_dest(vcpu_vlapic(v), vcpu_vlapic(origin),
-                               short_hand, dest, dest_mode) )
+                               short_hand, dest, dest_mode, dm) )
             vlapic_init_sipi_one(v, icr);
     }
 
@@ -399,8 +416,8 @@ struct vlapic *vlapic_lowest_prio(
     do {
         v = v->next_in_list ? : d->vcpu[0];
         vlapic = vcpu_vlapic(v);
-        if ( vlapic_match_dest(vlapic, source, short_hand, dest, dest_mode) &&
-             vlapic_enabled(vlapic) &&
+        if ( vlapic_match_dest(vlapic, source, short_hand, dest, dest_mode,
+                               APIC_DM_LOWEST) &&
              ((ppr = vlapic_get_ppr(vlapic)) < target_ppr) )
         {
             target = vlapic;
@@ -498,13 +515,14 @@ void vlapic_ipi(
 {
     unsigned int dest;
     unsigned int short_hand = icr_low & APIC_SHORT_MASK;
+    unsigned int dm = icr_low & APIC_DM_MASK;
     bool dest_mode = icr_low & APIC_DEST_MASK;
 
     HVM_DBG_LOG(DBG_LEVEL_VLAPIC, "icr = 0x%08x:%08x", icr_high, icr_low);
 
     dest = _VLAPIC_ID(vlapic, icr_high);
 
-    switch ( icr_low & APIC_DM_MASK )
+    switch ( dm )
     {
     case APIC_DM_INIT:
     case APIC_DM_STARTUP:
@@ -546,7 +564,7 @@ void vlapic_ipi(
         for_each_vcpu ( vlapic_domain(vlapic), v )
         {
             if ( vlapic_match_dest(vcpu_vlapic(v), vlapic,
-                                   short_hand, dest, dest_mode) )
+                                   short_hand, dest, dest_mode, dm) )
                 vlapic_accept_irq(v, icr_low);
         }
         if ( batch )
--- a/xen/arch/x86/hvm/vmsi.c
+++ b/xen/arch/x86/hvm/vmsi.c
@@ -89,8 +89,8 @@ int vmsi_deliver(
         for_each_vcpu ( d, v )
         {
             target = vcpu_vlapic(v);
-            if ( vlapic_enabled(target) &&
-                 vlapic_match_dest(target, NULL, 0, dest, dest_mode) )
+            if ( vlapic_match_dest(target, NULL, 0, dest, dest_mode,
+                                   MASK_INSR(delivery_mode, APIC_DM_MASK)) )
                 vmsi_inj_irq(target, vector, trig_mode, delivery_mode);
         }
         break;
@@ -125,7 +125,8 @@ void vmsi_deliver_pirq(struct domain *d,
 }
 
 /* Return value, -1 : multi-dests, non-negative value: dest_vcpu_id */
-int hvm_girq_dest_2_vcpu_id(struct domain *d, uint8_t dest, uint8_t dest_mode)
+int hvm_girq_dest_2_vcpu_id(struct domain *d, unsigned int dest, bool 
dest_mode,
+                            unsigned int delivery_mode)
 {
     int dest_vcpu_id = -1, w = 0;
     struct vcpu *v;
@@ -135,7 +136,8 @@ int hvm_girq_dest_2_vcpu_id(struct domai
  
     for_each_vcpu ( d, v )
     {
-        if ( vlapic_match_dest(vcpu_vlapic(v), NULL, 0, dest, dest_mode) ) 
+        if ( vlapic_match_dest(vcpu_vlapic(v), NULL, 0, dest, dest_mode,
+                               MASK_INSR(delivery_mode, APIC_DM_MASK)) )
         {
             w++;
             dest_vcpu_id = v->vcpu_id;
--- a/xen/arch/x86/include/asm/hvm/hvm.h
+++ b/xen/arch/x86/include/asm/hvm/hvm.h
@@ -299,7 +299,8 @@ int vmsi_deliver(
     uint8_t delivery_mode, uint8_t trig_mode);
 struct hvm_pirq_dpci;
 void vmsi_deliver_pirq(struct domain *d, const struct hvm_pirq_dpci 
*pirq_dpci);
-int hvm_girq_dest_2_vcpu_id(struct domain *d, uint8_t dest, uint8_t dest_mode);
+int hvm_girq_dest_2_vcpu_id(struct domain *d, unsigned int dest, bool 
dest_mode,
+                            unsigned int delivery_mode);
 
 enum hvm_intblk
 hvm_interrupt_blocked(struct vcpu *v, struct hvm_intack intack);
--- a/xen/arch/x86/include/asm/hvm/vlapic.h
+++ b/xen/arch/x86/include/asm/hvm/vlapic.h
@@ -136,6 +136,6 @@ struct vlapic *vlapic_lowest_prio(
 
 bool vlapic_match_dest(
     const struct vlapic *target, const struct vlapic *source,
-    int short_hand, uint32_t dest, bool dest_mode);
+    unsigned int short_hand, uint32_t dest, bool dest_mode, unsigned int dm);
 
 #endif /* __ASM_X86_HVM_VLAPIC_H__ */
--- a/xen/drivers/passthrough/x86/hvm.c
+++ b/xen/drivers/passthrough/x86/hvm.c
@@ -191,7 +191,7 @@ static struct vcpu *vector_hashing_dest(
     for_each_vcpu ( d, v )
     {
         if ( !vlapic_match_dest(vcpu_vlapic(v), NULL, APIC_DEST_NOSHORT,
-                                dest_id, dest_mode) )
+                                dest_id, dest_mode, APIC_DM_LOWEST) )
             continue;
 
         __set_bit(v->vcpu_id, dest_vcpu_bitmap);
@@ -371,7 +371,8 @@ int pt_irq_create_bind(
         delivery_mode = MASK_EXTR(pirq_dpci->gmsi.gflags,
                                   XEN_DOMCTL_VMSI_X86_DELIV_MASK);
 
-        dest_vcpu_id = hvm_girq_dest_2_vcpu_id(d, dest, dest_mode);
+        dest_vcpu_id = hvm_girq_dest_2_vcpu_id(d, dest, dest_mode,
+                                               delivery_mode);
         pirq_dpci->gmsi.dest_vcpu_id = dest_vcpu_id;
 
         pirq_dpci->gmsi.posted = false;
@@ -819,9 +820,11 @@ static int cf_check _hvm_dpci_msi_eoi(
         unsigned int dest = MASK_EXTR(pirq_dpci->gmsi.gflags,
                                       XEN_DOMCTL_VMSI_X86_DEST_ID_MASK);
         bool dest_mode = pirq_dpci->gmsi.gflags & XEN_DOMCTL_VMSI_X86_DM_MASK;
+        unsigned int dm = MASK_EXTR(pirq_dpci->gmsi.gflags,
+                                    XEN_DOMCTL_VMSI_X86_DELIV_MASK);
 
         if ( vlapic_match_dest(vcpu_vlapic(current), NULL, 0, dest,
-                               dest_mode) )
+                               dest_mode, MASK_INSR(dm, APIC_DM_MASK)) )
         {
             __msi_pirq_eoi(pirq_dpci);
             return 1;



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.