ia64/xen-unstable

changeset 16693:9ff64d045e61

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@intel.com>
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Jan 08 16:20:04 2008 +0000 (2008-01-08)
parents 9865d5e82802
children 15cfd1f8fa38
files xen/arch/x86/hvm/hpet.c xen/include/asm-x86/hvm/vpt.h
line diff
     1.1 --- a/xen/arch/x86/hvm/hpet.c	Tue Jan 08 15:55:29 2008 +0000
     1.2 +++ b/xen/arch/x86/hvm/hpet.c	Tue Jan 08 16:20:04 2008 +0000
     1.3 @@ -71,8 +71,9 @@
     1.4  #define HPET_TN_INT_ROUTE_CAP_MASK (0xffffffffULL \
     1.5                      << HPET_TN_INT_ROUTE_CAP_SHIFT)
     1.6  
     1.7 -#define hpet_tick_to_ns(h, tick) ((s_time_t)(tick)* \
     1.8 -                                  (S_TO_NS*TSC_PER_HPET_TICK)/h->tsc_freq)
     1.9 +#define hpet_tick_to_ns(h, tick)                        \
    1.10 +    ((s_time_t)((((tick) > (h)->hpet_to_ns_limit) ?     \
    1.11 +        ~0ULL : (tick) * (h)->hpet_to_ns_scale) >> 10))
    1.12  
    1.13  #define timer_config(h, n)       (h->hpet.timers[n].config)
    1.14  #define timer_is_periodic(h, n)  (timer_config(h, n) & HPET_TN_PERIODIC)
    1.15 @@ -537,6 +538,9 @@ void hpet_init(struct vcpu *v)
    1.16      h->vcpu = v;
    1.17      h->tsc_freq = ticks_per_sec(v);
    1.18  
    1.19 +    h->hpet_to_ns_scale = ((S_TO_NS * TSC_PER_HPET_TICK) << 10) / h->tsc_freq;
    1.20 +    h->hpet_to_ns_limit = (~0ULL >> 1) / h->hpet_to_ns_scale;
    1.21 +
    1.22      /* 64-bit main counter; 3 timers supported; LegacyReplacementRoute. */
    1.23      h->hpet.capability = 0x8086A201ULL;
    1.24  
     2.1 --- a/xen/include/asm-x86/hvm/vpt.h	Tue Jan 08 15:55:29 2008 +0000
     2.2 +++ b/xen/include/asm-x86/hvm/vpt.h	Tue Jan 08 16:20:04 2008 +0000
     2.3 @@ -58,6 +58,8 @@ typedef struct HPETState {
     2.4      struct hpet_registers hpet;
     2.5      struct vcpu *vcpu;
     2.6      uint64_t tsc_freq;
     2.7 +    uint64_t hpet_to_ns_scale; /* hpet ticks to ns (multiplied by 2^10) */
     2.8 +    uint64_t hpet_to_ns_limit; /* max hpet ticks convertable to ns      */
     2.9      uint64_t mc_offset;
    2.10      struct timer timers[HPET_TIMER_NUM];
    2.11      struct HPET_timer_fn_info timer_fn_info[HPET_TIMER_NUM];