hvm/hpet: Detect comparator values in the past
authorDon Slutz <dslutz@verizon.com>
Fri, 2 May 2014 20:18:08 +0000 (16:18 -0400)
committerTim Deegan <tim@xen.org>
Thu, 8 May 2014 11:03:53 +0000 (12:03 +0100)
This statement only works using 64-bit arithmetic for the main
                                     63
counter never changing by more then 2  .  (Which is a boundary
case that should not happen in my life time.)

Signed-off-by: Don Slutz <dslutz@verizon.com>
Acked-by: Tim Deegan <tim@xen.org>
xen/arch/x86/hvm/hpet.c

index 2a99fed8c6ed6a11776ddd9bdbc506407797d127..167994ed334b39de022538379652efed2033437a 100644 (file)
@@ -98,10 +98,12 @@ static uint64_t hpet_get_comparator(HPETState *h, unsigned int tn,
         uint64_t period = h->hpet.period[tn];
         if (period)
         {
-            elapsed = hpet_read_maincounter(h, guest_time) +
-                period - comparator;
-            comparator += (elapsed / period) * period;
-            h->hpet.comparator64[tn] = comparator;
+            elapsed = hpet_read_maincounter(h, guest_time) - comparator;
+            if ( (int64_t)elapsed >= 0 )
+            {
+                comparator += ((elapsed + period) / period) * period;
+                h->hpet.comparator64[tn] = comparator;
+            }
         }
     }