Skip to contentSkip to conversation

x86_ virtual timer related fixes

PATCH New Roger Pau Monne 1 participant 6 messages Jul 27, 2020

lore
Share & tools
lore
#0 lore raw

v2 cover x86_ virtual timer related fixes

Hello,

This is the first part of the vPT fixes that in the past I've posted as a single series. I don't think it has any controversial patches, and most have already been Acked or RB. I've split them from the series because those can likely go in now, while I work on properly finishing the remaining ones.

Thanks, Roger.

Roger Pau Monne (5):
x86/hvm: fix vIO-APIC build without IRQ0_SPECIAL_ROUTING x86/hvm: don't force vCPU 0 for IRQ 0 when using fixed destination
mode
x86/hvm: fix ISA IRQ 0 handling when set as lowest priority mode in IO
APIC
x86/vpt: only try to resume timers belonging to enabled devices x86/hvm: only translate ISA interrupts to GSIs in virtual timers

 xen/arch/x86/hvm/vioapic.c | 42 +++++++++++---------------------------
 xen/arch/x86/hvm/vpt.c     | 19 ++++++++++-------
 2 files changed, 24 insertions(+), 37 deletions(-)
Signature · 1 line
2.27.0
#1 lore raw

PATCH v2 1/5 x86/hvm: fix vIO-APIC build without IRQ0_SPECIAL_ROUTING

pit_channel0_enabled needs to be guarded with IRQ0_SPECIAL_ROUTING since it's only used when the special handling of ISA IRQ 0 is enabled. However such helper being a single line it's better to just inline it directly in vioapic_deliver where it's used.

No functional change.

---
Changes since v1:
- Remove pit_channel0_enabled altogether.

---
 xen/arch/x86/hvm/vioapic.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
xen/arch/x86/hvm/vioapic.c +2 -7
index b87facb0e0..b00037ea87 100644
@@ -391,11 +391,6 @@ static void ioapic_inj_irq(
     vlapic_set_irq(target, vector, trig_mode);
 }
 
-static inline int pit_channel0_enabled(void)
-{
-    return pt_active(&current->domain->arch.vpit.pt0);
-}
-
 static void vioapic_deliver(struct hvm_vioapic *vioapic, unsigned int pin)
 {
     uint16_t dest = vioapic->redirtbl[pin].fields.dest_id;
@@ -421,7 +416,7 @@ static void vioapic_deliver(struct hvm_vioapic *vioapic, unsigned int pin)
     {
 #ifdef IRQ0_SPECIAL_ROUTING
         /* Force round-robin to pick VCPU 0 */
-        if ( (irq == hvm_isa_irq_to_gsi(0)) && pit_channel0_enabled() )
+        if ( (irq == hvm_isa_irq_to_gsi(0)) && pt_active(&d->arch.vpit.pt0) )
         {
             v = d->vcpu ? d->vcpu[0] : NULL;
             target = v ? vcpu_vlapic(v) : NULL;
@@ -446,7 +441,7 @@ static void vioapic_deliver(struct hvm_vioapic *vioapic, unsigned int pin)
     {
 #ifdef IRQ0_SPECIAL_ROUTING
         /* Do not deliver timer interrupts to VCPU != 0 */
-        if ( (irq == hvm_isa_irq_to_gsi(0)) && pit_channel0_enabled() )
+        if ( (irq == hvm_isa_irq_to_gsi(0)) && pt_active(&d->arch.vpit.pt0) )
         {
             if ( (v = d->vcpu ? d->vcpu[0] : NULL) != NULL )
                 ioapic_inj_irq(vioapic, vcpu_vlapic(v), vector,
Signature · 1 line
2.27.0
#2 lore raw

PATCH v2 2/5 x86/hvm: don't force vCPU 0 for IRQ 0 when using fixed destination mode

In reply to Roger Pau Monne

When the IO APIC pin mapped to the ISA IRQ 0 has been configured to use fixed delivery mode do not forcefully route interrupts to vCPU 0, as the OS might have setup those interrupts to be injected to a different vCPU, and injecting to vCPU 0 can cause the OS to miss such interrupts or errors to happen due to unexpected vectors being injected on vCPU 0.

In order to fix remove such handling altogether for fixed destination mode pins and just inject them according to the data setup in the IO-APIC entry.

---
 xen/arch/x86/hvm/vioapic.c | 23 ++++-------------------
 1 file changed, 4 insertions(+), 19 deletions(-)
xen/arch/x86/hvm/vioapic.c +4 -19
index b00037ea87..123191db75 100644
@@ -438,26 +438,11 @@ static void vioapic_deliver(struct hvm_vioapic *vioapic, unsigned int pin)
     }
 
     case dest_Fixed:
-    {
-#ifdef IRQ0_SPECIAL_ROUTING
-        /* Do not deliver timer interrupts to VCPU != 0 */
-        if ( (irq == hvm_isa_irq_to_gsi(0)) && pt_active(&d->arch.vpit.pt0) )
-        {
-            if ( (v = d->vcpu ? d->vcpu[0] : NULL) != NULL )
-                ioapic_inj_irq(vioapic, vcpu_vlapic(v), vector,
-                               trig_mode, delivery_mode);
-        }
-        else
-#endif
-        {
-            for_each_vcpu ( d, v )
-                if ( vlapic_match_dest(vcpu_vlapic(v), NULL,
-                                       0, dest, dest_mode) )
-                    ioapic_inj_irq(vioapic, vcpu_vlapic(v), vector,
-                                   trig_mode, delivery_mode);
-        }
+        for_each_vcpu ( d, v )
+            if ( vlapic_match_dest(vcpu_vlapic(v), NULL, 0, dest, dest_mode) )
+                ioapic_inj_irq(vioapic, vcpu_vlapic(v), vector, trig_mode,
+                               delivery_mode);
         break;
-    }
 
     case dest_NMI:
     {
Signature · 1 line
2.27.0
#3 lore raw

PATCH v2 3/5 x86/hvm: fix ISA IRQ 0 handling when set as lowest priority mode in IO APIC

In reply to Roger Pau Monne

Lowest priority destination mode does allow the vIO APIC code to select a vCPU to inject the interrupt to, but the selected vCPU must be part of the possible destinations configured for such IO APIC pin.

Fix the code in order to only force vCPU 0 if it's part of the listed destinations.

---
Changes since v1:
- Add a comment regarding the vlapic_enabled check.
---
NB: I haven't added a fallback to vCPU 0 if no destination is found, as it's not how real hardware behaves. I think we should assume that no user have relied on this bogus Xen behavior for IRQ 0 interrupt injection.

---
 xen/arch/x86/hvm/vioapic.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
xen/arch/x86/hvm/vioapic.c +8 -6
index 123191db75..67d4a6237f 100644
@@ -415,12 +415,14 @@ static void vioapic_deliver(struct hvm_vioapic *vioapic, unsigned int pin)
     case dest_LowestPrio:
     {
 #ifdef IRQ0_SPECIAL_ROUTING
-        /* Force round-robin to pick VCPU 0 */
-        if ( (irq == hvm_isa_irq_to_gsi(0)) && pt_active(&d->arch.vpit.pt0) )
-        {
-            v = d->vcpu ? d->vcpu[0] : NULL;
-            target = v ? vcpu_vlapic(v) : NULL;
-        }
+        struct vlapic *lapic0 = vcpu_vlapic(d->vcpu[0]);
+
+        /* 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) )
+            target = lapic0;
         else
 #endif
             target = vlapic_lowest_prio(d, NULL, 0, dest, dest_mode);
Signature · 1 line
2.27.0
#4 lore raw

PATCH v2 4/5 x86/vpt: only try to resume timers belonging to enabled devices

In reply to Roger Pau Monne

Check whether the emulated device is actually enabled before trying to resume the associated timers.

Thankfully all those structures are zeroed at initialization, and since the devices are not enabled they are never populated, which triggers the pt->vcpu check at the beginning of pt_resume forcing an exit from the function.

While there limit the scope of i and make it unsigned.

---
 xen/arch/x86/hvm/vpt.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
xen/arch/x86/hvm/vpt.c +11 -6
index 47f2c2aa64..62c87867c5 100644
@@ -636,14 +636,19 @@ static void pt_resume(struct periodic_time *pt)
 
 void pt_may_unmask_irq(struct domain *d, struct periodic_time *vlapic_pt)
 {
-    int i;
-
     if ( d )
     {
-        pt_resume(&d->arch.vpit.pt0);
-        pt_resume(&d->arch.hvm.pl_time->vrtc.pt);
-        for ( i = 0; i < HPET_TIMER_NUM; i++ )
-            pt_resume(&d->arch.hvm.pl_time->vhpet.pt[i]);
+        if ( has_vpit(d) )
+            pt_resume(&d->arch.vpit.pt0);
+        if ( has_vrtc(d) )
+            pt_resume(&d->arch.hvm.pl_time->vrtc.pt);
+        if ( has_vhpet(d) )
+        {
+            unsigned int i;
+
+            for ( i = 0; i < HPET_TIMER_NUM; i++ )
+                pt_resume(&d->arch.hvm.pl_time->vhpet.pt[i]);
+        }
     }
 
     if ( vlapic_pt )
Signature · 1 line
2.27.0
#5 lore raw

PATCH v2 5/5 x86/hvm: only translate ISA interrupts to GSIs in virtual timers

In reply to Roger Pau Monne

Only call hvm_isa_irq_to_gsi for ISA interrupts, interrupts originating from an IO APIC pin already use a GSI and don't need to be translated.

I haven't observed any issues from this, but I think it's better to use it correctly.

---
Changes since v1:
- Delay the setting of gsi a bit.

---
 xen/arch/x86/hvm/vpt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/hvm/vpt.c +1 -1
index 62c87867c5..c68bbd1558 100644
@@ -86,13 +86,13 @@ static int pt_irq_vector(struct periodic_time *pt, enum hvm_intsrc src)
         return pt->irq;
 
     isa_irq = pt->irq;
-    gsi = hvm_isa_irq_to_gsi(isa_irq);
 
     if ( src == hvm_intsrc_pic )
         return (v->domain->arch.hvm.vpic[isa_irq >> 3].irq_base
                 + (isa_irq & 7));
 
     ASSERT(src == hvm_intsrc_lapic);
+    gsi = pt->source == PTSRC_isa ? hvm_isa_irq_to_gsi(isa_irq) : pt->irq;
     vector = vioapic_get_vector(v->domain, gsi);
     if ( vector < 0 )
     {
Signature · 1 line
2.27.0