Skip to contentSkip to conversation

x86/vLAPIC: only accept interrupts on enabled LAPICs

PATCH In review Jan Beulich 3 participants 5 messages Nov 19 – Nov 23

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/vLAPIC: only accept interrupts on enabled LAPICs

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")
xen/arch/x86/hvm/vioapic.c +6 -7
@@ -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;
xen/arch/x86/hvm/vlapic.c +25 -7
@@ -238,12 +238,28 @@ 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_enabled(target) )
+            return false;
+
+    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 )
xen/arch/x86/hvm/vmsi.c +6 -4
@@ -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;
xen/arch/x86/include/asm/hvm/hvm.h +2 -1
@@ -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);
xen/arch/x86/include/asm/hvm/vlapic.h +1 -1
@@ -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__ */
xen/drivers/passthrough/x86/hvm.c +6 -3
@@ -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;
#1 lore raw

xen/arch/x86/include/asm/hvm/vlapic.h struct vlapic *vlapic_lowest_prio( · line 140

Quoted 9 lines · Jan Beulich
xen/arch/x86/include/asm/hvm/vlapic.h +1 -1
@@ -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);
 

Even before this change, I was thinking that 3 fragments of ICR were too many parameters to be passing, especially as some are bits as per their ICR position, but dest_mode is shifted down to bit 0.

I think passing a real ICR pair will be an improvement both for legibility and for code generation.

~Andrew

#2 lore raw
Quoted 15 lines · Andrew Cooper

xen/arch/x86/include/asm/hvm/vlapic.h struct vlapic *vlapic_lowest_prio( · line 140

Quoted 9 lines · Jan Beulich
xen/arch/x86/include/asm/hvm/vlapic.h +1 -1
@@ -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);
 

Even before this change, I was thinking that 3 fragments of ICR were too many parameters to be passing, especially as some are bits as per their ICR position, but dest_mode is shifted down to bit 0.

Quoted 2 lines

I think passing a real ICR pair will be an improvement both for legibility and for code generation.

Are you saying this with just two of the three uses in vlapic.c in mind, or did you mean to suggest to have all the other call sites end up more complicated to achieve this goal?

Jan

#3 lore raw
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Andrew Cooper

xen/arch/x86/include/asm/hvm/vlapic.h struct vlapic *vlapic_lowest_prio( · line 140

Quoted 9 lines · Jan Beulich
xen/arch/x86/include/asm/hvm/vlapic.h +1 -1
@@ -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);
 

Even before this change, I was thinking that 3 fragments of ICR were too many parameters to be passing, especially as some are bits as per their ICR position, but dest_mode is shifted down to bit 0.

Quoted 6 lines
Quoted 2 lines

I think passing a real ICR pair will be an improvement both for legibility and for code generation.

Are you saying this with just two of the three uses in vlapic.c in mind, or did you mean to suggest to have all the other call sites end up more complicated to achieve this goal?

Only the IPI paths have an ICR, and even there the destination has to be decoded in the sender's mode first. The vIO-APIC, MSI and passthrough callers would have to build a fake ICR.

On the patch itself:

default: needs fallthrough;

In _hvm_dpci_msi_eoi() if the guest EOIs after disabling its LAPIC in software, the match now fails and the physical EOI is left to eoi_timer. This is a change compared to existing behavior, was this intended?

This patch is better than my "[PATCH 2/2] x86/passthrough: skip disabled vLAPICs in vector_hashing_dest()". I'll drop it.

#4 lore raw
Quoted 24 lines · Stefano Stabellini
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Andrew Cooper

xen/arch/x86/include/asm/hvm/vlapic.h struct vlapic *vlapic_lowest_prio( · line 140

Quoted 9 lines · Jan Beulich
xen/arch/x86/include/asm/hvm/vlapic.h +1 -1
@@ -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);
 

Even before this change, I was thinking that 3 fragments of ICR were too many parameters to be passing, especially as some are bits as per their ICR position, but dest_mode is shifted down to bit 0.

Quoted 6 lines
Quoted 2 lines

I think passing a real ICR pair will be an improvement both for legibility and for code generation.

Are you saying this with just two of the three uses in vlapic.c in mind, or did you mean to suggest to have all the other call sites end up more complicated to achieve this goal?

Quoted 8 lines

Only the IPI paths have an ICR, and even there the destination has to be decoded in the sender's mode first. The vIO-APIC, MSI and passthrough callers would have to build a fake ICR.

On the patch itself:

default: needs fallthrough;

Oh, yes, just that it wants to be break.

Quoted 3 lines

In _hvm_dpci_msi_eoi() if the guest EOIs after disabling its LAPIC in software, the match now fails and the physical EOI is left to eoi_timer. This is a change compared to existing behavior, was this intended?

Yes, it was intended, yet I wasn't quite sure of the (supposed) behavior there. I should have added a remark, and I've now done so:

"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?"

Jan