Patchwork [0.14+master,2/3] qemu-timer: Introduce warp callback

login
register
mail settings
Submitter Jan Kiszka
Date Feb. 2, 2011, 9:38 a.m.
Message ID <fdf6ae78ca269d6b6cafba268e833df45104b63f.1296639517.git.jan.kiszka@siemens.com>
Download mbox | patch
Permalink /patch/81493/
State New
Headers show

Comments

Jan Kiszka - Feb. 2, 2011, 9:38 a.m.
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.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
---
 qemu-timer.c |   52 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 qemu-timer.h |    5 +++++
 2 files changed, 56 insertions(+), 1 deletions(-)

Patch

diff --git a/qemu-timer.c b/qemu-timer.c
index 94c1073..2b98c8a 100644
--- a/qemu-timer.c
+++ b/qemu-timer.c
@@ -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);
diff --git a/qemu-timer.h b/qemu-timer.h
index 8cd8f83..a7b37da 100644
--- a/qemu-timer.h
+++ b/qemu-timer.h
@@ -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);