[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] [xen-3.1-testing] hvm: hpet: Fix overflow when converting to nanoseconds.



# HG changeset patch
# User Keir Fraser <keir.fraser@xxxxxxxxxx>
# Date 1199965163 0
# Node ID 05e63f05ece02a4dcd0377b2229fbc76fb14e786
# Parent  26226f0e593c1d0547f6f932770aabc52f663bf1
hvm: hpet: Fix overflow when converting to nanoseconds.

Currently in hpet_tick_to_ns, the approach is multiplying first, which
easily causes overflow when tick is quite large. The patch cannot
handle arbitratry large ticks duo to the precision requirement and
64bit's value range. But by optimize the equation, a larger ticks than
current code can be supported. Also an overflow check is added before
the calculation.

From: Haitao Shan <haitao.shan@xxxxxxxxx>
Signed-off-by: Keir Fraser <keir.fraser@xxxxxxxxxx>
xen-unstable changeset:   16693:9ff64d045e61e7db6844b3db473e660e8c7e0551
xen-unstable date:        Tue Jan 08 16:20:04 2008 +0000
---
 xen/arch/x86/hvm/hpet.c       |    8 ++++++--
 xen/include/asm-x86/hvm/vpt.h |    2 ++
 2 files changed, 8 insertions(+), 2 deletions(-)

diff -r 26226f0e593c -r 05e63f05ece0 xen/arch/x86/hvm/hpet.c
--- a/xen/arch/x86/hvm/hpet.c   Thu Jan 10 11:38:10 2008 +0000
+++ b/xen/arch/x86/hvm/hpet.c   Thu Jan 10 11:39:23 2008 +0000
@@ -71,8 +71,9 @@
 #define HPET_TN_INT_ROUTE_CAP_MASK (0xffffffffULL \
                     << HPET_TN_INT_ROUTE_CAP_SHIFT)
 
-#define hpet_tick_to_ns(h, tick) ((s_time_t)(tick)* \
-                                  (S_TO_NS*TSC_PER_HPET_TICK)/h->tsc_freq)
+#define hpet_tick_to_ns(h, tick)                        \
+    ((s_time_t)((((tick) > (h)->hpet_to_ns_limit) ?     \
+        ~0ULL : (tick) * (h)->hpet_to_ns_scale) >> 10))
 
 #define timer_config(h, n)       (h->hpet.timers[n].config)
 #define timer_is_periodic(h, n)  (timer_config(h, n) & HPET_TN_PERIODIC)
@@ -465,6 +466,9 @@ void hpet_init(struct vcpu *v)
     h->vcpu = v;
     h->tsc_freq = ticks_per_sec(v);
 
+    h->hpet_to_ns_scale = ((S_TO_NS * TSC_PER_HPET_TICK) << 10) / h->tsc_freq;
+    h->hpet_to_ns_limit = (~0ULL >> 1) / h->hpet_to_ns_scale;
+
     /* 64-bit main counter; 3 timers supported; LegacyReplacementRoute. */
     h->hpet.capability = 0x8086A201ULL;
 
diff -r 26226f0e593c -r 05e63f05ece0 xen/include/asm-x86/hvm/vpt.h
--- a/xen/include/asm-x86/hvm/vpt.h     Thu Jan 10 11:38:10 2008 +0000
+++ b/xen/include/asm-x86/hvm/vpt.h     Thu Jan 10 11:39:23 2008 +0000
@@ -41,6 +41,8 @@ typedef struct HPETState {
     struct hvm_hw_hpet hpet;
     struct vcpu *vcpu;
     uint64_t tsc_freq;
+    uint64_t hpet_to_ns_scale; /* hpet ticks to ns (multiplied by 2^10) */
+    uint64_t hpet_to_ns_limit; /* max hpet ticks convertable to ns      */
     uint64_t mc_offset;
     struct timer timers[HPET_TIMER_NUM];
     struct HPET_timer_fn_info timer_fn_info[HPET_TIMER_NUM]; 

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.