mc146818rtc: fix timer interrupt reinjection

commit 369b41359a 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.

V2: reorganize code (Paolo Bonzini)

Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>

Message-Id: <20191010123008.GA19158@amt.cnet>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Marcelo Tosatti 2019-10-10 09:30:08 -03:00 committed by Paolo Bonzini
parent 73284563dc
commit b429de7301

View file

@ -203,24 +203,28 @@ periodic_timer_update(RTCState *s, int64_t current_time, uint32_t old_period)
period = rtc_periodic_clock_ticks(s); period = rtc_periodic_clock_ticks(s);
if (period) { if (!period) {
/* compute 32 khz clock */ s->irq_coalesced = 0;
cur_clock = timer_del(s->periodic_timer);
muldiv64(current_time, RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND); return;
}
/* /* compute 32 khz clock */
* if the periodic timer's update is due to period re-configuration, cur_clock =
* we should count the clock since last interrupt. muldiv64(current_time, RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND);
*/
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); * if the periodic timer's update is due to period re-configuration,
last_periodic_clock = next_periodic_clock - old_period; * we should count the clock since last interrupt.
lost_clock = cur_clock - last_periodic_clock; */
assert(lost_clock >= 0); 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: * s->irq_coalesced can change for two reasons:
@ -251,22 +255,19 @@ periodic_timer_update(RTCState *s, int64_t current_time, uint32_t old_period)
rtc_coalesced_timer_update(s); rtc_coalesced_timer_update(s);
} }
} else { } else {
/* /*
* no way to compensate the interrupt if LOST_TICK_POLICY_SLEW * no way to compensate the interrupt if LOST_TICK_POLICY_SLEW
* is not used, we should make the time progress anyway. * is not used, we should make the time progress anyway.
*/ */
lost_clock = MIN(lost_clock, 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);
} }
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);
} }
static void rtc_periodic_timer(void *opaque) static void rtc_periodic_timer(void *opaque)