@@ -1803,6 +1803,8 @@ void tsc_set_info(struct domain *d,
uint32_t tsc_mode, uint64_t elapsed_nsec,
uint32_t gtsc_khz, uint32_t incarnation)
{
+ bool_t enable_tsc_scaling;
+
if ( is_idle_domain(d) || is_hardware_domain(d) )
{
d->arch.vtsc = 0;
@@ -1864,7 +1866,9 @@ void tsc_set_info(struct domain *d,
case TSC_MODE_PVRDTSCP:
d->arch.vtsc = !boot_cpu_has(X86_FEATURE_RDTSCP) ||
!host_tsc_is_safe();
- d->arch.tsc_khz = cpu_khz;
+ enable_tsc_scaling = has_hvm_container_domain(d) &&
+ cpu_has_tsc_ratio && !d->arch.vtsc;
+ d->arch.tsc_khz = (enable_tsc_scaling && gtsc_khz) ? gtsc_khz : cpu_khz;
set_time_scale(&d->arch.vtsc_to_ns, d->arch.tsc_khz * 1000 );
d->arch.ns_to_vtsc = scale_reciprocal(d->arch.vtsc_to_ns);
if ( d->arch.vtsc )
@@ -1872,7 +1876,8 @@ void tsc_set_info(struct domain *d,
else {
/* when using native TSC, offset is nsec relative to power-on
* of physical machine */
- d->arch.vtsc_offset = scale_delta(rdtsc(), &d->arch.vtsc_to_ns) -
+ d->arch.vtsc_offset = scale_delta(rdtsc(),
+ &this_cpu(cpu_time).tsc_scale) -
elapsed_nsec;
}
break;
@@ -347,9 +347,12 @@ struct arch_domain
s_time_t vtsc_last; /* previous TSC value (guarantee monotonicity) */
spinlock_t vtsc_lock;
uint64_t vtsc_offset; /* adjustment for save/restore/migrate */
- uint32_t tsc_khz; /* cached khz for certain emulated cases */
- struct time_scale vtsc_to_ns; /* scaling for certain emulated cases */
- struct time_scale ns_to_vtsc; /* scaling for certain emulated cases */
+ uint32_t tsc_khz; /* cached guest khz for certain emulated or
+ hardware TSC scaling cases */
+ struct time_scale vtsc_to_ns; /* scaling for certain emulated or
+ hardware TSC scaling cases */
+ struct time_scale ns_to_vtsc; /* scaling for certain emulated or
+ hardware TSC scaling cases */
uint32_t incarnation; /* incremented every restore or live migrate
(possibly other cases in the future */
#if !defined(NDEBUG) || defined(PERF_COUNTERS)
When TSC_MODE_PVRDTSCP is used for a HVM container and TSC scaling is available, use the non-zero value of argument gtsc_khz of tsc_set_info() as the guest TSC frequency rather than using the host TSC frequency. Otherwise, TSC scaling will not be able get the correct ratio between the host and guest TSC frequencies. Signed-off-by: Haozhong Zhang <haozhong.zhang@intel.com> --- Changes in v3: (addressing Boris Ostrovsky's comments) * Add the missing ! before d->arch.vtsc when initializing enable_tsc_scaling. * Use this_cpu(cpu_time).tsc_scale for both scaling and non-scaling cases. * Adapt comments for some TSC-related fields in struct arch_domain. xen/arch/x86/time.c | 9 +++++++-- xen/include/asm-x86/domain.h | 9 ++++++--- 2 files changed, 13 insertions(+), 5 deletions(-)