debuggers.hg

changeset 20638:cf4f3e2f425c

Make tsc_mode=3 (pvrdtscp) work correctly.

Initial tsc_mode patch contained a rough cut at pvrdtscp mode. This
patch gets it working correctly. For the record, pvrdtscp mode allows
an application to obtain information from Xen to descale/de-offset
a physical tsc value to obtain "nsec since VM start". Though the
raw tsc value may change across migration due to different Hz rates
and different start times of different physical machines, applying
the pvrdtscp algorithm to a raw tsc value guarantees that the result
will always be both a fixed known rate (nanoseconds) and monotonically
increasing. BUT, pvrdtscp will only be fast on physical machines that
support the rdtscp instruction AND on which tsc is "safe"; on other
machines both the rdtsc and rdtscp instructions will be emulated.
Also note that when tsc_mode=3 is enabled, tsc-sensitive applications
that do NOT implement the pvrdtscp algorithm will behave incorrectly.
So, tsc_mode=3 should only be used when all apps are either
tsc-resilient
or pvrdtscp-modified, and only has a performance advantage on very
recent generation processors.

Signed-off-by: Dan Magenheiemer <dan.magenheimer@oracle.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Dec 08 07:48:45 2009 +0000 (2009-12-08)
parents 91555131e235
children ab0d71f7f596
files xen/arch/x86/time.c xen/arch/x86/traps.c
line diff
     1.1 --- a/xen/arch/x86/time.c	Tue Dec 08 07:47:52 2009 +0000
     1.2 +++ b/xen/arch/x86/time.c	Tue Dec 08 07:48:45 2009 +0000
     1.3 @@ -832,13 +832,9 @@ static void __update_vcpu_system_time(st
     1.4      else
     1.5          tsc_stamp = t->local_tsc_stamp;
     1.6  
     1.7 -    if ( boot_cpu_has(X86_FEATURE_RDTSCP) )
     1.8 -    {
     1.9 -        if ( d->arch.tsc_mode ==  TSC_MODE_PVRDTSCP )
    1.10 -            write_rdtscp_aux(d->arch.incarnation);
    1.11 -        else
    1.12 -            write_rdtscp_aux(0);
    1.13 -    }
    1.14 +    if ( (d->arch.tsc_mode ==  TSC_MODE_PVRDTSCP) &&
    1.15 +         boot_cpu_has(X86_FEATURE_RDTSCP) )
    1.16 +        write_rdtscp_aux(d->arch.incarnation);
    1.17  
    1.18      /* Don't bother unless timestamps have changed or we are forced. */
    1.19      if ( !force && (u->tsc_timestamp == tsc_stamp) )
    1.20 @@ -1644,9 +1640,8 @@ void cpuid_time_leaf(uint32_t sub_idx, u
    1.21                        uint32_t *ecx, uint32_t *edx)
    1.22  {
    1.23      struct domain *d = current->domain;
    1.24 -    struct cpu_time *t;
    1.25 +    uint64_t offset;
    1.26  
    1.27 -    t = &this_cpu(cpu_time);
    1.28      switch ( sub_idx )
    1.29      {
    1.30      case 0: /* features */
    1.31 @@ -1658,22 +1653,23 @@ void cpuid_time_leaf(uint32_t sub_idx, u
    1.32          *ecx = d->arch.tsc_khz;
    1.33          *edx = d->arch.incarnation;
    1.34          break;
    1.35 -    case 1: /* pvclock group1 */ /* FIXME are these right? */
    1.36 -        *eax = (uint32_t)t->local_tsc_stamp;
    1.37 -        *ebx = (uint32_t)(t->local_tsc_stamp >> 32);
    1.38 -        *ecx = t->tsc_scale.mul_frac;
    1.39 -        *edx = d->arch.incarnation;
    1.40 +    case 1: /* scale and offset */
    1.41 +        if ( !d->arch.vtsc )
    1.42 +            offset = d->arch.vtsc_offset;
    1.43 +        else
    1.44 +            /* offset already applied to value returned by virtual rdtscp */
    1.45 +            offset = 0;
    1.46 +        *eax = (uint32_t)offset;
    1.47 +        *ebx = (uint32_t)(offset >> 32);
    1.48 +        *ecx = d->arch.vtsc_to_ns.mul_frac;
    1.49 +        *edx = (s8)d->arch.vtsc_to_ns.shift;
    1.50          break;
    1.51 -    case 2: /* pvclock scaling values */ /* FIXME  are these right? */
    1.52 -        *eax = (uint32_t)t->stime_local_stamp;
    1.53 -        *ebx = (uint32_t)(t->stime_local_stamp >> 32);
    1.54 -        *ecx = t->tsc_scale.shift;
    1.55 -        *edx = d->arch.incarnation;
    1.56 -    case 3: /* physical cpu_khz */
    1.57 +    case 2: /* physical cpu_khz */
    1.58          *eax = cpu_khz;
    1.59 -        *ebx = *ecx = 0;
    1.60 -        *edx = d->arch.incarnation;
    1.61 +        *ebx = *ecx = *edx = 0;
    1.62          break;
    1.63 +    default:
    1.64 +        *eax = *ebx = *ecx = *edx = 0;
    1.65      }
    1.66  }
    1.67  
    1.68 @@ -1708,8 +1704,17 @@ void tsc_get_info(struct domain *d, uint
    1.69          }
    1.70          break;
    1.71      case TSC_MODE_PVRDTSCP:
    1.72 -        *elapsed_nsec = get_s_time() - d->arch.vtsc_offset; /* FIXME scale? */
    1.73 -        *gtsc_khz =  d->arch.tsc_khz;
    1.74 +        if ( d->arch.vtsc )
    1.75 +        {
    1.76 +            *elapsed_nsec = get_s_time() - d->arch.vtsc_offset;
    1.77 +            *gtsc_khz =  cpu_khz;
    1.78 +        } else {
    1.79 +            uint64_t tsc = 0;
    1.80 +            rdtscll(tsc);
    1.81 +            *elapsed_nsec = scale_delta(tsc,&d->arch.vtsc_to_ns) -
    1.82 +                            d->arch.vtsc_offset;
    1.83 +            *gtsc_khz = 0; /* ignored by tsc_set_info */
    1.84 +        }
    1.85          break;
    1.86      }
    1.87  }
    1.88 @@ -1755,12 +1760,20 @@ void tsc_set_info(struct domain *d,
    1.89              d->arch.ns_to_vtsc = scale_reciprocal(d->arch.vtsc_to_ns);
    1.90          break;
    1.91      case TSC_MODE_PVRDTSCP:
    1.92 -        if ( boot_cpu_has(X86_FEATURE_RDTSCP) && gtsc_khz != 0 ) {
    1.93 -            d->arch.vtsc = 0;
    1.94 -            set_time_scale(&d->arch.vtsc_to_ns, gtsc_khz * 1000 );
    1.95 -        } else {
    1.96 -            d->arch.vtsc = 1;
    1.97 +        d->arch.vtsc =  boot_cpu_has(X86_FEATURE_RDTSCP) &&
    1.98 +                        host_tsc_is_safe() ?  0 : 1;
    1.99 +        d->arch.tsc_khz = cpu_khz;
   1.100 +        set_time_scale(&d->arch.vtsc_to_ns, d->arch.tsc_khz * 1000 );
   1.101 +        d->arch.ns_to_vtsc = scale_reciprocal(d->arch.vtsc_to_ns);
   1.102 +        if ( d->arch.vtsc )
   1.103              d->arch.vtsc_offset = get_s_time() - elapsed_nsec;
   1.104 +        else {
   1.105 +            /* when using native TSC, offset is nsec relative to power-on
   1.106 +             * of physical machine */
   1.107 +            uint64_t tsc = 0;
   1.108 +            rdtscll(tsc);
   1.109 +            d->arch.vtsc_offset = scale_delta(tsc,&d->arch.vtsc_to_ns) -
   1.110 +                                  elapsed_nsec;
   1.111          }
   1.112          break;
   1.113      }
     2.1 --- a/xen/arch/x86/traps.c	Tue Dec 08 07:47:52 2009 +0000
     2.2 +++ b/xen/arch/x86/traps.c	Tue Dec 08 07:48:45 2009 +0000
     2.3 @@ -688,7 +688,7 @@ int cpuid_hypervisor_leaves( uint32_t id
     2.4      uint32_t base = is_viridian_domain(d) ? 0x40000100 : 0x40000000;
     2.5  
     2.6      idx -= base;
     2.7 -    if ( idx > 2 ) 
     2.8 +    if ( idx > 3 ) 
     2.9          return 0;
    2.10  
    2.11      switch ( idx )