[SeaBIOS] [PATCH] tsc: use kvmclock for calibration

Avi Kivity avi at redhat.com
Thu Aug 9 14:53:24 CEST 2012


On 08/09/2012 02:57 PM, Gerd Hoffmann wrote:
> Use kvmclock for tsc calibration when running on kvm.  Without this the
> tsc frequency calibrated by seabios can be *way* off in case the virtual
> machine is booted on a loaded host.  I've seen seabios calibrating 27
> instead of ca. 2800 MHz, resulting in timeouts being to short by factor
> 100.  Which in turn leads to disk I/O errors due to timeouts, especially
> as I/O requests tend to take a bit longer than usual on a loaded box ...

> +
> +struct pvclock_vcpu_time_info {
> +	u32   version;
> +	u32   pad0;
> +	u64   tsc_timestamp;
> +	u64   system_time;
> +	u32   tsc_to_system_mul;
> +	s8    tsc_shift;
> +	u8    flags;
> +	u8    pad[2];
> +} PACKED;
> +
> +
> +u64 kvm_tsc_khz(void)
> +{
> +    u32 eax, ebx, ecx, edx, msr;
> +    struct pvclock_vcpu_time_info time;
> +    u32 addr = (u32)(&time);
> +    u64 khz;
> +
> +    /* check presence and figure msr number */
> +    cpuid(KVM_CPUID_FEATURES, &eax, &ebx, &ecx, &edx);
> +    if (eax & KVM_FEATURE_CLOCKSOURCE2) {
> +        msr = MSR_KVM_SYSTEM_TIME_NEW;
> +    } else if (eax & KVM_FEATURE_CLOCKSOURCE) {
> +        msr = MSR_KVM_SYSTEM_TIME;
> +    } else {
> +        return 0;
> +    }
> +
> +    /* ask kvm hypervisor to fill struct */
> +    memset(&time, 0, sizeof(time));
> +    wrmsr(msr, addr | 1);

How can this work?  There is a 64-byte alignment requirement.

> +    wrmsr(msr, 0);
> +    if (time.version < 2 || time.tsc_to_system_mul == 0)
> +        return 0;
> +
> +    /* go figure tsc frequency */
> +    khz = pvclock_tsc_khz(&time);
> +    dprintf(1, "Using kvmclock, msr 0x%x, tsc %d MHz\n",
> +            msr, (u32)khz / 1000);
> +    return khz;

That's a meaningless number.  You can be migrated to a cpu or a machine
with very different tsc.

You want accurate time on kvm, don't use the tsc.


-- 
error compiling committee.c: too many arguments to function



More information about the SeaBIOS mailing list