Skip to contentSkip to conversation

mc146818rtc: fix timer interrupt reinjection

PATCH Superseded Marcelo Tosatti 2 participants 3 messages Oct 9, 2019 – Oct 10, 2019

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mc146818rtc: fix timer interrupt reinjection

commit 369b41359af46bded5799c9ef8be2b641d92e043 broke timer interrupt reinjection when there is no period change by the guest.

In that case, old_period is 0, which ends up zeroing irq_coalesced (counter of reinjected interrupts).

The consequence is Windows 7 is unable to synchronize time via NTP. Easily reproducible by playing a fullscreen video with cirrus and VNC.

Fix by not updating s->irq_coalesced when old_period is 0.

hw/timer/mc146818rtc.c +4 -1
index 6cb378751b..aabffa8c67 100644
@@ -241,7 +241,10 @@ periodic_timer_update(RTCState *s, int64_t current_time, uint32_t old_period)
 
             s->period = period;
             lost_clock += old_irq_coalesced * old_period;
-            s->irq_coalesced = lost_clock / s->period;
+            if (old_period) {
+                s->irq_coalesced = lost_clock / s->period;
+            }
+
             lost_clock %= s->period;
             if (old_irq_coalesced != s->irq_coalesced ||
                 old_period != s->period) {
#1 lore raw

Review

Quoted 10 lines · Marcelo Tosatti
             s->period = period;
             lost_clock += old_irq_coalesced * old_period;
(quoted diff) +4 -1
-            s->irq_coalesced = lost_clock / s->period;
+            if (old_period) {
+                s->irq_coalesced = lost_clock / s->period;
+            }
+
             lost_clock %= s->period;
             if (old_irq_coalesced != s->irq_coalesced ||
                 old_period != s->period) {

I think none of the code in the "if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {" matters if old_period == 0 (and lost_clock will always be 0). So perhaps we should place all that big "if" under the existing "if (old_period)"?

Or even something like:

hw/timer/mc146818rtc.c +33 -32
index 6cb378751b..3337a8da98 100644
@@ -202,25 +202,33 @@ periodic_timer_update(RTCState *s, int64_t current_time, uint32_t old_period)
     int64_t cur_clock, next_irq_clock, lost_clock = 0;
 
     period = rtc_periodic_clock_ticks(s);
+    if (old_period && old_period == period) {
+        return;
+    }
 
-    if (period) {
-        /* compute 32 khz clock */
-        cur_clock =
-            muldiv64(current_time, RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND);
+    if (!period) {
+        s->irq_coalesced = 0;
+        timer_del(s->periodic_timer);
+        return;
 
-        /*
-        * if the periodic timer's update is due to period re-configuration,
-        * we should count the clock since last interrupt.
-        */
-        if (old_period) {
-            int64_t last_periodic_clock, next_periodic_clock;
-
-            next_periodic_clock = muldiv64(s->next_periodic_time,
-                                    RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND);
-            last_periodic_clock = next_periodic_clock - old_period;
-            lost_clock = cur_clock - last_periodic_clock;
-            assert(lost_clock >= 0);
-        }
+    }
+
+    /* compute 32 khz clock */
+    cur_clock =
+        muldiv64(current_time, RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND);
+
+    /*
+     * if the periodic timer's update is due to period re-configuration,
+     * we should count the clock since last interrupt.
+     */
+    if (old_period) {
+        int64_t last_periodic_clock, next_periodic_clock;
+
+        next_periodic_clock = muldiv64(s->next_periodic_time,
+                                       RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND);
+        last_periodic_clock = next_periodic_clock - old_period;
+        lost_clock = cur_clock - last_periodic_clock;
+        assert(lost_clock >= 0);
 
         /*
          * s->irq_coalesced can change for two reasons:
@@ -243,13 +251,10 @@ periodic_timer_update(RTCState *s, int64_t current_time, uint32_t old_period)
             lost_clock += old_irq_coalesced * old_period;
             s->irq_coalesced = lost_clock / s->period;
             lost_clock %= s->period;
-            if (old_irq_coalesced != s->irq_coalesced ||
-                old_period != s->period) {
-                DPRINTF_C("cmos: coalesced irqs scaled from %d to %d, "
-                          "period scaled from %d to %d\n", old_irq_coalesced,
-                          s->irq_coalesced, old_period, s->period);
-                rtc_coalesced_timer_update(s);
-            }
+            DPRINTF_C("cmos: coalesced irqs scaled from %d to %d, "
+                      "period scaled from %d to %d\n", old_irq_coalesced,
+                      s->irq_coalesced, old_period, s->period);
+            rtc_coalesced_timer_update(s);
         } else {
            /*
              * no way to compensate the interrupt if LOST_TICK_POLICY_SLEW
@@ -257,16 +262,12 @@ periodic_timer_update(RTCState *s, int64_t current_time, uint32_t old_period)
              */
             lost_clock = MIN(lost_clock, period);
         }
-
         assert(lost_clock >= 0 && lost_clock <= period);
-
-        next_irq_clock = cur_clock + period - lost_clock;
-        s->next_periodic_time = periodic_clock_to_ns(next_irq_clock) + 1;
-        timer_mod(s->periodic_timer, s->next_periodic_time);
-    } else {
-        s->irq_coalesced = 0;
-        timer_del(s->periodic_timer);
     }
+
+    next_irq_clock = cur_clock + period - lost_clock;
+    s->next_periodic_time = periodic_clock_to_ns(next_irq_clock) + 1;
+    timer_mod(s->periodic_timer, s->next_periodic_time);
 }
 
 static void rtc_periodic_timer(void *opaque)

Best read with "diff -b".

Paolo

#2 lore raw
Quoted 12 lines · Paolo Bonzini

Review

Quoted 10 lines · Marcelo Tosatti
             s->period = period;
             lost_clock += old_irq_coalesced * old_period;
(quoted diff) +4 -1
-            s->irq_coalesced = lost_clock / s->period;
+            if (old_period) {
+                s->irq_coalesced = lost_clock / s->period;
+            }
+
             lost_clock %= s->period;
             if (old_irq_coalesced != s->irq_coalesced ||
                 old_period != s->period) {
Quoted 6 lines

I think none of the code in the "if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {" matters if old_period == 0 (and lost_clock will always be 0). So perhaps we should place all that big "if" under the existing "if (old_period)"?

Or even something like:

This skips reprogramming the timer if the period is equal.

Sending v2 based on your suggestions and patch below.

Quoted 100 lines
hw/timer/mc146818rtc.c +33 -32
index 6cb378751b..3337a8da98 100644
@@ -202,25 +202,33 @@ periodic_timer_update(RTCState *s, int64_t current_time, uint32_t old_period)
     int64_t cur_clock, next_irq_clock, lost_clock = 0;
 
     period = rtc_periodic_clock_ticks(s);
+    if (old_period && old_period == period) {
+        return;
+    }
 
-    if (period) {
-        /* compute 32 khz clock */
-        cur_clock =
-            muldiv64(current_time, RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND);
+    if (!period) {
+        s->irq_coalesced = 0;
+        timer_del(s->periodic_timer);
+        return;
 
-        /*
-        * if the periodic timer's update is due to period re-configuration,
-        * we should count the clock since last interrupt.
-        */
-        if (old_period) {
-            int64_t last_periodic_clock, next_periodic_clock;
-
-            next_periodic_clock = muldiv64(s->next_periodic_time,
-                                    RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND);
-            last_periodic_clock = next_periodic_clock - old_period;
-            lost_clock = cur_clock - last_periodic_clock;
-            assert(lost_clock >= 0);
-        }
+    }
+
+    /* compute 32 khz clock */
+    cur_clock =
+        muldiv64(current_time, RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND);
+
+    /*
+     * if the periodic timer's update is due to period re-configuration,
+     * we should count the clock since last interrupt.
+     */
+    if (old_period) {
+        int64_t last_periodic_clock, next_periodic_clock;
+
+        next_periodic_clock = muldiv64(s->next_periodic_time,
+                                       RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND);
+        last_periodic_clock = next_periodic_clock - old_period;
+        lost_clock = cur_clock - last_periodic_clock;
+        assert(lost_clock >= 0);
 
         /*
          * s->irq_coalesced can change for two reasons:
@@ -243,13 +251,10 @@ periodic_timer_update(RTCState *s, int64_t current_time, uint32_t old_period)
             lost_clock += old_irq_coalesced * old_period;
             s->irq_coalesced = lost_clock / s->period;
             lost_clock %= s->period;
-            if (old_irq_coalesced != s->irq_coalesced ||
-                old_period != s->period) {
-                DPRINTF_C("cmos: coalesced irqs scaled from %d to %d, "
-                          "period scaled from %d to %d\n", old_irq_coalesced,
-                          s->irq_coalesced, old_period, s->period);
-                rtc_coalesced_timer_update(s);
-            }
+            DPRINTF_C("cmos: coalesced irqs scaled from %d to %d, "
+                      "period scaled from %d to %d\n", old_irq_coalesced,
+                      s->irq_coalesced, old_period, s->period);
+            rtc_coalesced_timer_update(s);
         } else {
            /*
              * no way to compensate the interrupt if LOST_TICK_POLICY_SLEW
@@ -257,16 +262,12 @@ periodic_timer_update(RTCState *s, int64_t current_time, uint32_t old_period)
              */
             lost_clock = MIN(lost_clock, period);
         }
-
         assert(lost_clock >= 0 && lost_clock <= period);
-
-        next_irq_clock = cur_clock + period - lost_clock;
-        s->next_periodic_time = periodic_clock_to_ns(next_irq_clock) + 1;
-        timer_mod(s->periodic_timer, s->next_periodic_time);
-    } else {
-        s->irq_coalesced = 0;
-        timer_del(s->periodic_timer);
     }
+
+    next_irq_clock = cur_clock + period - lost_clock;
+    s->next_periodic_time = periodic_clock_to_ns(next_irq_clock) + 1;
+    timer_mod(s->periodic_timer, s->next_periodic_time);
 }
 
 static void rtc_periodic_timer(void *opaque)

Best read with "diff -b".

Paolo