From mboxrd@z Thu Jan 1 00:00:00 1970 From: Avi Kivity Subject: Re: [PATCH] tsc: use kvmclock for calibration Date: Thu, 09 Aug 2012 15:53:24 +0300 Message-ID: <5023B2C4.90302@redhat.com> References: <1344513463-7329-1-git-send-email-kraxel@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Cc: seabios@seabios.org, kvm@vger.kernel.org To: Gerd Hoffmann Return-path: In-Reply-To: <1344513463-7329-1-git-send-email-kraxel@redhat.com> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: seabios-bounces@seabios.org Sender: seabios-bounces@seabios.org List-Id: kvm.vger.kernel.org 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