Skip to contentSkip to conversation

Let RTC follow backward jumps of host clock immediately

PATCH Superseded Jan Kiszka 1 participant 4 messages Feb 2, 2011

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

cover Let RTC follow backward jumps of host clock immediately

By default, we base the mc146818 RTC on the host clock (CLOCK_REALTIME). This works fine if only the frequency of the host clock is tuned (e.g. by NTP) or if it is set to a future time. However, if the host is tuned backward, e.g. because NTP obtained the correct time after the guest was already started or the admin decided to tune the local time, we see an unpleasant effect in the guest: The RTC will stall for the period the host clock is set back. We identified that one prominent guest affected by this is Windows which relies on the periodic RTC interrupt for time keeping.

This series address the issue by detecting those warps and providing a callback mechanism to device models. The RTC is enabled to update its timers and register content immediately. Tested successfully both with hwclock in a Linux guest and by monitoring the Windows clock while fiddling with the host time.

Note that if this kind of RTC adjustment is not wanted, the user is still free to decouple the RTC from the host clock and base it on the VM clock - just like before.

Jan Kiszka (3):
qemu-timer: Consolidate qemu_get_clock and qemu_get_clock_ns
qemu-timer: Introduce warp callback
mc146818rtc: Handle host clock warps

 hw/mc146818rtc.c |   17 ++++++++++++
 qemu-timer.c     |   77 ++++++++++++++++++++++++++++++++++++++++++++----------
 qemu-timer.h     |    5 +++
 3 files changed, 85 insertions(+), 14 deletions(-)
author
#1 lore raw

PATCH 1/3 qemu-timer: Consolidate qemu_get_clock and qemu_get_clock_ns

Both functions have a lot in common, push those bits into a shared helper.

---
 qemu-timer.c |   27 +++++++++++++--------------
 1 files changed, 13 insertions(+), 14 deletions(-)
qemu-timer.c +13 -14
index db1ec49..94c1073 100644
@@ -504,11 +504,9 @@ static void qemu_run_timers(QEMUClock *clock)
     }
 }
 
-int64_t qemu_get_clock(QEMUClock *clock)
+static int64_t get_clock_common(QEMUClock *clock)
 {
     switch(clock->type) {
-    case QEMU_CLOCK_REALTIME:
-        return get_clock() / 1000000;
     default:
     case QEMU_CLOCK_VIRTUAL:
         if (use_icount) {
@@ -521,20 +519,21 @@ int64_t qemu_get_clock(QEMUClock *clock)
     }
 }
 
+int64_t qemu_get_clock(QEMUClock *clock)
+{
+    if (clock->type == QEMU_CLOCK_REALTIME) {
+        return get_clock() / 1000000;
+    } else {
+        return get_clock_common(clock);
+    }
+}
+
 int64_t qemu_get_clock_ns(QEMUClock *clock)
 {
-    switch(clock->type) {
-    case QEMU_CLOCK_REALTIME:
+    if (clock->type == QEMU_CLOCK_REALTIME) {
         return get_clock();
-    default:
-    case QEMU_CLOCK_VIRTUAL:
-        if (use_icount) {
-            return cpu_get_icount();
-        } else {
-            return cpu_get_clock();
-        }
-    case QEMU_CLOCK_HOST:
-        return get_clock_realtime();
+    } else {
+        return get_clock_common(clock);
     }
 }
 
Signature · 1 line
1.7.1
author
#2 lore raw

PATCH 2/3 qemu-timer: Introduce warp callback

In reply to Jan Kiszka

QEMU_CLOCK_HOST is based on the system time which may jump backward in case the admin or NTP adjusts it. RTC emulations and other device models can suffer in this case as timers will stall for the period the clock was tuned back.

This adds a detection mechanism that checks on every host clock readout if the new time is before the last result. In that case callbacks are fired that any interested device model can register with the clock.

---
 qemu-timer.c |   52 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 qemu-timer.h |    5 +++++
 2 files changed, 56 insertions(+), 1 deletions(-)
qemu-timer.c +51 -1
index 94c1073..2b98c8a 100644
@@ -152,9 +152,17 @@ void cpu_disable_ticks(void)
 #define QEMU_CLOCK_VIRTUAL  1
 #define QEMU_CLOCK_HOST     2
 
+struct QEMUClockWarpListener {
+    QEMUClockWarpCB cb;
+    void *opaque;
+    QTAILQ_ENTRY(QEMUClockWarpListener) entry;
+};
+
 struct QEMUClock {
     int type;
     int enabled;
+    QTAILQ_HEAD(warp_listeners, QEMUClockWarpListener) warp_listeners;
+    int64_t last;
     /* XXX: add frequency */
 };
 
@@ -382,9 +390,15 @@ static QEMUTimer *active_timers[QEMU_NUM_CLOCKS];
 static QEMUClock *qemu_new_clock(int type)
 {
     QEMUClock *clock;
+
     clock = qemu_mallocz(sizeof(QEMUClock));
     clock->type = type;
     clock->enabled = 1;
+    QTAILQ_INIT(&clock->warp_listeners);
+    /* required to detect & report backward jumps */
+    if (type == QEMU_CLOCK_HOST) {
+        clock->last = get_clock_realtime();
+    }
     return clock;
 }
 
@@ -506,6 +520,9 @@ static void qemu_run_timers(QEMUClock *clock)
 
 static int64_t get_clock_common(QEMUClock *clock)
 {
+    struct QEMUClockWarpListener *listener;
+    int64_t now, last;
+
     switch(clock->type) {
     default:
     case QEMU_CLOCK_VIRTUAL:
@@ -515,7 +532,15 @@ static int64_t get_clock_common(QEMUClock *clock)
             return cpu_get_clock();
         }
     case QEMU_CLOCK_HOST:
-        return get_clock_realtime();
+        now = get_clock_realtime();
+        last = clock->last;
+        clock->last = now;
+        if (now < last) {
+            QTAILQ_FOREACH(listener, &clock->warp_listeners, entry) {
+                listener->cb(clock, now, listener->opaque);
+            }
+        }
+        return now;
     }
 }
 
@@ -537,6 +562,31 @@ int64_t qemu_get_clock_ns(QEMUClock *clock)
     }
 }
 
+void qemu_register_clock_warp(QEMUClock *clock, QEMUClockWarpCB cb,
+                              void *opaque)
+{
+    struct QEMUClockWarpListener *listener =
+        qemu_malloc(sizeof(struct QEMUClockWarpListener));
+
+    listener->cb = cb;
+    listener->opaque = opaque;
+    QTAILQ_INSERT_TAIL(&clock->warp_listeners, listener, entry);
+}
+
+void qemu_unregister_clock_warp(QEMUClock *clock, QEMUClockWarpCB cb,
+                                void *opaque)
+{
+    struct QEMUClockWarpListener *listener;
+
+    QTAILQ_FOREACH(listener, &clock->warp_listeners, entry) {
+        if (listener->cb == cb && listener->opaque == opaque) {
+            QTAILQ_REMOVE(&clock->warp_listeners, listener, entry);
+            qemu_free(listener);
+            break;
+        }
+    }
+}
+
 void init_clocks(void)
 {
     rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
qemu-timer.h +5 -0
index 8cd8f83..a7b37da 100644
@@ -13,6 +13,7 @@
 /* timers */
 
 typedef struct QEMUClock QEMUClock;
+typedef void (*QEMUClockWarpCB)(QEMUClock *clock, int64_t now, void *opaque);
 typedef void QEMUTimerCB(void *opaque);
 
 /* The real time clock should be used only for stuff which does not
@@ -36,6 +37,10 @@ extern QEMUClock *host_clock;
 int64_t qemu_get_clock(QEMUClock *clock);
 int64_t qemu_get_clock_ns(QEMUClock *clock);
 void qemu_clock_enable(QEMUClock *clock, int enabled);
+void qemu_register_clock_warp(QEMUClock *clock, QEMUClockWarpCB cb,
+                              void *opaque);
+void qemu_unregister_clock_warp(QEMUClock *clock, QEMUClockWarpCB cb,
+                                void *opaque);
 
 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque);
 void qemu_free_timer(QEMUTimer *ts);
Signature · 1 line
1.7.1
author
#3 lore raw

PATCH 3/3 mc146818rtc: Handle host clock warps

In reply to Jan Kiszka

Make use of the new warp notifier to update the RTC whenever rtc_clock is the host clock and that happens to jump backward. This avoids that the RTC stalls for the period the host clock was set back.

---
 hw/mc146818rtc.c |   17 +++++++++++++++++
 1 files changed, 17 insertions(+), 0 deletions(-)
hw/mc146818rtc.c +17 -0
index a1b0e31..04da794 100644
@@ -572,6 +572,21 @@ static const VMStateDescription vmstate_rtc = {
     }
 };
 
+static void rtc_clock_warp(QEMUClock *clock, int64_t now, void *opaque)
+{
+    RTCState *s = opaque;
+
+    rtc_set_date_from_host(&s->dev);
+    s->next_second_time = now + (get_ticks_per_sec() * 99) / 100;
+    qemu_mod_timer(s->second_timer2, s->next_second_time);
+    rtc_timer_update(s, now);
+#ifdef TARGET_I386
+    if (rtc_td_hack) {
+        rtc_coalesced_timer_update(s);
+    }
+#endif
+}
+
 static void rtc_reset(void *opaque)
 {
     RTCState *s = opaque;
@@ -608,6 +623,8 @@ static int rtc_initfn(ISADevice *dev)
     s->second_timer = qemu_new_timer(rtc_clock, rtc_update_second, s);
     s->second_timer2 = qemu_new_timer(rtc_clock, rtc_update_second2, s);
 
+    qemu_register_clock_warp(rtc_clock, rtc_clock_warp, s);
+
     s->next_second_time =
         qemu_get_clock(rtc_clock) + (get_ticks_per_sec() * 99) / 100;
     qemu_mod_timer(s->second_timer2, s->next_second_time);
Signature · 1 line
1.7.1