Skip to contentSkip to conversation

tsc: use kvmclock for calibration

PATCH In review Gerd Hoffmann 5 participants 23 messages Aug 9, 2012 – Aug 13, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tsc: use kvmclock for calibration

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 ...

---
 src/clock.c    |    9 +++++
 src/paravirt.c |   90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/paravirt.h |    1 +
 3 files changed, 100 insertions(+), 0 deletions(-)
src/clock.c +9 -0
index 69e9f17..5883b1a 100644
@@ -13,6 +13,7 @@
 #include "bregs.h" // struct bregs
 #include "biosvar.h" // GET_GLOBAL
 #include "usb-hid.h" // usb_check_event
+#include "paravirt.h" // kvm clock
 
 // RTC register flags
 #define RTC_A_UIP 0x80
@@ -80,6 +81,14 @@ calibrate_tsc(void)
         return;
     }
 
+    if (kvm_para_available()) {
+        u32 khz = kvm_tsc_khz();
+        if (khz != 0) {
+            SET_GLOBAL(cpu_khz, khz);
+            return;
+        }
+    }
+
     // Setup "timer2"
     u8 orig = inb(PORT_PS2_CTRLB);
     outb((orig & ~PPCB_SPKR) | PPCB_T2GATE, PORT_PS2_CTRLB);
src/paravirt.c +90 -0

99 lines not on this page · raw message

src/paravirt.h +1 -0
index a284c41..eedfcc3 100644
@@ -27,6 +27,7 @@ static inline int kvm_para_available(void)
 
     return 0;
 }
+extern u64 kvm_tsc_khz(void);
 
 #define QEMU_CFG_SIGNATURE              0x00
 #define QEMU_CFG_ID                     0x01
Signature · 1 line
1.7.1
#1 lore raw
Quoted 30 lines · Gerd Hoffmann

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 ...

src/paravirt.c +23 -0
+
+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) {

src/paravirt.c void qemu_cfg_romfile_setup(void) · line 428

Quoted 10 lines
src/paravirt.c +10 -0
+        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.

src/paravirt.c void qemu_cfg_romfile_setup(void) · line 437

Quoted 9 lines
src/paravirt.c +9 -0
+    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.

Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw

It should be kHz not khz.

- msr, (u32)khz / 1000);
+ msr, (u32)kHz / 1000);

Quoted 67 lines · Avi Kivity
Quoted 30 lines · Gerd Hoffmann

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 ...

src/paravirt.c +23 -0
+
+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) {

src/paravirt.c void qemu_cfg_romfile_setup(void) · line 428

Quoted 10 lines
src/paravirt.c +10 -0
+        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.

src/paravirt.c void qemu_cfg_romfile_setup(void) · line 437

Quoted 9 lines
src/paravirt.c +9 -0
+    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

_______________________________________________
SeaBIOS mailing list
[email protected]
http://www.seabios.org/mailman/listinfo/seabios

#3 lore raw
In reply to Avi Kivity

Hi,

Quoted 14 lines
Quoted 14 lines
src/paravirt.c +14 -0
+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 {
Quoted 8 lines

src/paravirt.c void qemu_cfg_romfile_setup(void) · line 428

Quoted 6 lines
src/paravirt.c +6 -0
+        return 0;
+    }
+
+    /* ask kvm hypervisor to fill struct */
+    memset(&time, 0, sizeof(time));
+    wrmsr(msr, addr | 1);

How can this work?

It did in my testing, although maybe by pure luck ...

Quoted 1 line

There is a 64-byte alignment requirement.

64 bytes? Sure? The whole struct is only 32 bytes in size ...

Easily fixable though, just need to grab some memory with memalign instead of using the stack.

Quoted 10 lines

src/paravirt.c void qemu_cfg_romfile_setup(void) · line 437

Quoted 9 lines
src/paravirt.c +9 -0
+    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;
Quoted 4 lines

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.

seabios uses the tsc for timeout calculations only, so it doesn't need to be 100% accurate. The order of magnitude should be correct though. The Linux kernel uses the value for delay loops too, so using it for the given purpose can't be *that* horrible after all ...

It is certainly an improvement over the current code which tries to calibrate the tsc and gets totally broken results in case the busy host happens to schedule the guest in the middle of calibration.

So what do you suggest? The options I see are:

(1) Use this patch (with alignment issue fixed of course).
(2) Do a full kvmclock implementation. Feels a bit like overkill.
(3) SeaBIOS can fallback to the PIT for timing on machines which have no TSC. We could do that too in case we detect kvm ...

cheers,
Gerd

#5 lore raw
In reply to Avi Kivity
Quoted 28 lines · Avi Kivity
Quoted 27 lines · Gerd Hoffmann

Hi,

Quoted 14 lines
Quoted 14 lines
src/paravirt.c +14 -0
+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 {
Quoted 8 lines

src/paravirt.c void qemu_cfg_romfile_setup(void) · line 428

Quoted 6 lines
src/paravirt.c +6 -0
+        return 0;
+    }
+
+    /* ask kvm hypervisor to fill struct */
+    memset(&time, 0, sizeof(time));
+    wrmsr(msr, addr | 1);

How can this work?

It did in my testing, although maybe by pure luck ...

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

There is a 64-byte alignment requirement.

64 bytes? Sure? The whole struct is only 32 bytes in size ...

er, the documentation says 4 bytes (so stack alignment works). I distinctly remember having a large alignment requirement so we don't cross a page or slot boundary... something's wrong here.

	case MSR_KVM_SYSTEM_TIME: {
		kvmclock_reset(vcpu);

		vcpu->arch.time = data;
		kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);

		/* we verify if the enable bit is set... */
		if (!(data & 1))
			break;

		/* ...but clean it before doing the actual write */
		vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);

		vcpu->arch.time_page =
				gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);

		if (is_error_page(vcpu->arch.time_page))
			vcpu->arch.time_page = NULL;

		break;

So your tests worked by pure luck, but the bug is in kvm. We need to grab two pages here.

Signature · 1 line
error compiling committee.c: too many arguments to function
#6 lore raw

Hi,

Quoted 5 lines
Quoted 3 lines

er, the documentation says 4 bytes (so stack alignment works). I distinctly remember having a large alignment requirement so we don't cross a page or slot boundary... something's wrong here.

	case MSR_KVM_SYSTEM_TIME: {

[ ... ]

Quoted 2 lines

So your tests worked by pure luck, but the bug is in kvm. We need to grab two pages here.

Ok, so better use memalign(32,32) to make sure the struct doesn't cross a page border ...

cheers,
Gerd

#7 lore raw
Quoted 6 lines · Gerd Hoffmann

Hi,

Quoted 4 lines
Quoted 3 lines

er, the documentation says 4 bytes (so stack alignment works). I distinctly remember having a large alignment requirement so we don't cross a page or slot boundary... something's wrong here.

Quoted 8 lines
Quoted 1 line
	case MSR_KVM_SYSTEM_TIME: {

[ ... ]

Quoted 2 lines

So your tests worked by pure luck, but the bug is in kvm. We need to grab two pages here.

Ok, so better use memalign(32,32) to make sure the struct doesn't cross a page border ...

No, we need to fix kvm, no need to complicate the guest for that.

Signature · 1 line
error compiling committee.c: too many arguments to function
#8 lore raw
In reply to Avi Kivity

Hi,

Quoted 8 lines
Quoted 6 lines

So what do you suggest? The options I see are:

(1) Use this patch (with alignment issue fixed of course).
(2) Do a full kvmclock implementation. Feels a bit like overkill.
(3) SeaBIOS can fallback to the PIT for timing on machines which have no TSC. We could do that too in case we detect kvm ...

What sort of timeouts are these? If seconds, maybe the rtc would be best.

All sorts of timeouts, from a few miliseconds to seconds.

The problematic ones are the longer timeouts, which wait for I/O stuff like disk reads complete. The stuff with smaller timeouts (like waiting for AHCI link become ready) tend to finish instantly in kvm.

cheers,
Gerd

#9 lore raw
Quoted 9 lines · Gerd Hoffmann

Hi,

Quoted 7 lines
Quoted 6 lines

So what do you suggest? The options I see are:

(1) Use this patch (with alignment issue fixed of course).
(2) Do a full kvmclock implementation. Feels a bit like overkill.
(3) SeaBIOS can fallback to the PIT for timing on machines which have no TSC. We could do that too in case we detect kvm ...

Quoted 7 lines
Quoted 1 line

What sort of timeouts are these? If seconds, maybe the rtc would be best.

All sorts of timeouts, from a few miliseconds to seconds.

The problematic ones are the longer timeouts, which wait for I/O stuff like disk reads complete. The stuff with smaller timeouts (like waiting for AHCI link become ready) tend to finish instantly in kvm.

That's not guaranteed. The AHCI adapter might be real hardware. Or the emulation may change.

What's wrong with having a full kvmclock implementation? Instead of issuing rdtsc call a function pointer.

Signature · 1 line
error compiling committee.c: too many arguments to function
#4 lore raw
In reply to Gerd Hoffmann
Quoted 25 lines · Gerd Hoffmann

Hi,

Quoted 14 lines
Quoted 14 lines
src/paravirt.c +14 -0
+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 {
Quoted 8 lines

src/paravirt.c void qemu_cfg_romfile_setup(void) · line 428

Quoted 6 lines
src/paravirt.c +6 -0
+        return 0;
+    }
+
+    /* ask kvm hypervisor to fill struct */
+    memset(&time, 0, sizeof(time));
+    wrmsr(msr, addr | 1);

How can this work?

Quoted 5 lines

It did in my testing, although maybe by pure luck ...

Quoted 1 line

There is a 64-byte alignment requirement.

64 bytes? Sure? The whole struct is only 32 bytes in size ...

er, the documentation says 4 bytes (so stack alignment works). I distinctly remember having a large alignment requirement so we don't cross a page or slot boundary... something's wrong here.

Quoted 29 lines

Easily fixable though, just need to grab some memory with memalign instead of using the stack.

Quoted 10 lines

src/paravirt.c void qemu_cfg_romfile_setup(void) · line 437

Quoted 9 lines
src/paravirt.c +9 -0
+    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;
Quoted 4 lines

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.

seabios uses the tsc for timeout calculations only, so it doesn't need to be 100% accurate. The order of magnitude should be correct though. The Linux kernel uses the value for delay loops too, so using it for the given purpose can't be *that* horrible after all ...

It is certainly an improvement over the current code which tries to calibrate the tsc and gets totally broken results in case the busy host happens to schedule the guest in the middle of calibration.

Quoted 6 lines

So what do you suggest? The options I see are:

(1) Use this patch (with alignment issue fixed of course).
(2) Do a full kvmclock implementation. Feels a bit like overkill.
(3) SeaBIOS can fallback to the PIT for timing on machines which have no TSC. We could do that too in case we detect kvm ...

What sort of timeouts are these? If seconds, maybe the rtc would be best.

Signature · 1 line
error compiling committee.c: too many arguments to function
#21 lore raw
In reply to Avi Kivity
Quoted 54 lines · Avi Kivity
Quoted 30 lines · Gerd Hoffmann

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 ...

src/paravirt.c +23 -0
+
+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) {

src/paravirt.c void qemu_cfg_romfile_setup(void) · line 428

Quoted 10 lines
src/paravirt.c +10 -0
+        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.

src/paravirt.c void qemu_cfg_romfile_setup(void) · line 437

Quoted 9 lines
src/paravirt.c +9 -0
+    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;
Quoted 2 lines

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

Thats why there exists hardware tsc frequency scaling and the software equivalent for that on kvm.

Quoted 9 lines

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

Signature · 5 lines
error compiling committee.c: too many arguments to function
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#10 lore raw
In reply to Avi Kivity
Quoted 18 lines · Avi Kivity
Quoted 9 lines · Gerd Hoffmann

Hi,

Quoted 7 lines
Quoted 6 lines

So what do you suggest? The options I see are:

(1) Use this patch (with alignment issue fixed of course).
(2) Do a full kvmclock implementation. Feels a bit like overkill.
(3) SeaBIOS can fallback to the PIT for timing on machines which have no TSC. We could do that too in case we detect kvm ...

Quoted 7 lines
Quoted 1 line

What sort of timeouts are these? If seconds, maybe the rtc would be best.

All sorts of timeouts, from a few miliseconds to seconds.

The problematic ones are the longer timeouts, which wait for I/O stuff like disk reads complete. The stuff with smaller timeouts (like waiting for AHCI link become ready) tend to finish instantly in kvm.

Quoted 5 lines

That's not guaranteed. The AHCI adapter might be real hardware. Or the emulation may change.

What's wrong with having a full kvmclock implementation? Instead of issuing rdtsc call a function pointer.

Its not necessary (someone is going to maintain the kvmclock frequency retrieve, which patch is already here, versus maintainance of full kvmclock).

Frequency scaling (or the software equivalent: TSC trapping) are required for other reasons anyway.

#12 lore raw
In reply to Avi Kivity
Quoted 65 lines · Avi Kivity
Quoted 25 lines · Gerd Hoffmann

Hi,

Quoted 14 lines
Quoted 14 lines
src/paravirt.c +14 -0
+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 {
Quoted 8 lines

src/paravirt.c void qemu_cfg_romfile_setup(void) · line 428

Quoted 6 lines
src/paravirt.c +6 -0
+        return 0;
+    }
+
+    /* ask kvm hypervisor to fill struct */
+    memset(&time, 0, sizeof(time));
+    wrmsr(msr, addr | 1);

How can this work?

Quoted 5 lines

It did in my testing, although maybe by pure luck ...

Quoted 1 line

There is a 64-byte alignment requirement.

64 bytes? Sure? The whole struct is only 32 bytes in size ...

er, the documentation says 4 bytes (so stack alignment works). I distinctly remember having a large alignment requirement so we don't cross a page or slot boundary... something's wrong here.

Quoted 29 lines

Easily fixable though, just need to grab some memory with memalign instead of using the stack.

Quoted 10 lines

src/paravirt.c void qemu_cfg_romfile_setup(void) · line 437

Quoted 9 lines
src/paravirt.c +9 -0
+    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;
Quoted 4 lines

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.

seabios uses the tsc for timeout calculations only, so it doesn't need to be 100% accurate. The order of magnitude should be correct though. The Linux kernel uses the value for delay loops too, so using it for the given purpose can't be *that* horrible after all ...

It is certainly an improvement over the current code which tries to calibrate the tsc and gets totally broken results in case the busy host happens to schedule the guest in the middle of calibration.

Quoted 8 lines
Quoted 6 lines

So what do you suggest? The options I see are:

(1) Use this patch (with alignment issue fixed of course).
(2) Do a full kvmclock implementation. Feels a bit like overkill.
(3) SeaBIOS can fallback to the PIT for timing on machines which have no TSC. We could do that too in case we detect kvm ...

What sort of timeouts are these? If seconds, maybe the rtc would be best.

I vote for 3 so nobody has to maintain kvmclock code in SeaBIOS and Gerd can fix the in-kernel PIT issues with GRUB (see Michaels message) while testing.

#13 lore raw
Quoted 68 lines · Marcelo Tosatti
Quoted 67 lines · Avi Kivity
Quoted 25 lines · Gerd Hoffmann

Hi,

Quoted 14 lines
Quoted 14 lines
src/paravirt.c +14 -0
+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 {
Quoted 8 lines

src/paravirt.c void qemu_cfg_romfile_setup(void) · line 428

Quoted 6 lines
src/paravirt.c +6 -0
+        return 0;
+    }
+
+    /* ask kvm hypervisor to fill struct */
+    memset(&time, 0, sizeof(time));
+    wrmsr(msr, addr | 1);

How can this work?

Quoted 5 lines

It did in my testing, although maybe by pure luck ...

Quoted 1 line

There is a 64-byte alignment requirement.

64 bytes? Sure? The whole struct is only 32 bytes in size ...

er, the documentation says 4 bytes (so stack alignment works). I distinctly remember having a large alignment requirement so we don't cross a page or slot boundary... something's wrong here.

Quoted 31 lines

Easily fixable though, just need to grab some memory with memalign instead of using the stack.

Quoted 10 lines

src/paravirt.c void qemu_cfg_romfile_setup(void) · line 437

Quoted 9 lines
src/paravirt.c +9 -0
+    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;
Quoted 4 lines

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.

seabios uses the tsc for timeout calculations only, so it doesn't need to be 100% accurate. The order of magnitude should be correct though. The Linux kernel uses the value for delay loops too, so using it for the given purpose can't be *that* horrible after all ...

It is certainly an improvement over the current code which tries to calibrate the tsc and gets totally broken results in case the busy host happens to schedule the guest in the middle of calibration.

So what do you suggest? The options I see are:

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

(1) Use this patch (with alignment issue fixed of course).
(2) Do a full kvmclock implementation. Feels a bit like overkill.
(3) SeaBIOS can fallback to the PIT for timing on machines which have no TSC. We could do that too in case we detect kvm ...

What sort of timeouts are these? If seconds, maybe the rtc would be best.

I vote for 3 so nobody has to maintain kvmclock code in SeaBIOS and Gerd

That or pm timer.

Quoted 2 lines

can fix the in-kernel PIT issues with GRUB (see Michaels message) while testing.

What message exactly?

--
Gleb.

#14 lore raw
Quoted 4 lines · Gleb Natapov
Quoted 2 lines

can fix the in-kernel PIT issues with GRUB (see Michaels message) while testing.

What message exactly?

found it.

--
Gleb.

#15 lore raw
In reply to Marcelo Tosatti

Hi,

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

(1) Use this patch (with alignment issue fixed of course).
(2) Do a full kvmclock implementation. Feels a bit like overkill.
(3) SeaBIOS can fallback to the PIT for timing on machines which have no TSC. We could do that too in case we detect kvm ...

What sort of timeouts are these? If seconds, maybe the rtc would be best.

I vote for 3 so nobody has to maintain kvmclock code in SeaBIOS and Gerd can fix the in-kernel PIT issues with GRUB (see Michaels message) while testing.

(2) turned out to be not too bad when taking a shortcut: Go through an enable/disable cycle each time we read the clock, then just grab system_time. Not that efficient, but should be ok for seabios. Usually it checks the clock when sitting around idle, waiting for something to happen. And it simplifies the implementation alot as we can just skip all the tsc frequency & delta calculations.

Draft patch attached. Comments?

cheers,
Gerd

Quoted 1 line

From e42d62e90ae4b8a00413a0665d4022069154a516 Mon Sep 17 00:00:00 2001

From: Gerd Hoffmann <[email protected]>
Date: Thu, 9 Aug 2012 13:26:18 +0200
Subject: [PATCH] kvmclock clocksource

---
 Makefile       |    4 +-
 src/clock.c    |   13 +++++++++++
 src/paravirt.c |   65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/paravirt.h |    3 ++
 4 files changed, 83 insertions(+), 2 deletions(-)
Makefile +2 -2
index 72ee152..b692a96 100644
@@ -13,11 +13,11 @@ SRCBOTH=misc.c stacks.c pmm.c output.c util.c block.c floppy.c ata.c mouse.c \
     pnpbios.c pirtable.c vgahooks.c ramdisk.c pcibios.c blockcmd.c \
     usb.c usb-uhci.c usb-ohci.c usb-ehci.c usb-hid.c usb-msc.c \
     virtio-ring.c virtio-pci.c virtio-blk.c virtio-scsi.c apm.c ahci.c \
-    usb-uas.c lsi-scsi.c esp-scsi.c
+    usb-uas.c lsi-scsi.c esp-scsi.c paravirt.c
 SRC16=$(SRCBOTH) system.c disk.c font.c
 SRC32FLAT=$(SRCBOTH) post.c shadow.c memmap.c coreboot.c boot.c \
     acpi.c smm.c mptable.c smbios.c pciinit.c optionroms.c mtrr.c \
-    lzmadecode.c bootsplash.c jpeg.c usb-hub.c paravirt.c \
+    lzmadecode.c bootsplash.c jpeg.c usb-hub.c \
     biostables.c xen.c bmp.c romfile.c
 SRC32SEG=util.c output.c pci.c pcibios.c apm.c stacks.c
 
src/clock.c +13 -0
index 69e9f17..15921fa 100644
@@ -13,6 +13,7 @@
 #include "bregs.h" // struct bregs
 #include "biosvar.h" // GET_GLOBAL
 #include "usb-hid.h" // usb_check_event
+#include "paravirt.h" // kvm clock
 
 // RTC register flags
 #define RTC_A_UIP 0x80
@@ -64,6 +65,7 @@
 
 u32 cpu_khz VAR16VISIBLE;
 u8 no_tsc VAR16VISIBLE;
+u8 use_kvmclock VAR16VISIBLE;
 
 static void
 calibrate_tsc(void)
@@ -80,6 +82,15 @@ calibrate_tsc(void)
         return;
     }
 
+    if (kvm_para_available()) {
+        u32 hz = kvmclock_init();
+        if (hz != 0) {
+            SET_GLOBAL(use_kvmclock, 1);
+            SET_GLOBAL(cpu_khz, hz / 1000);
+            return;
+        }
+    }
+
     // Setup "timer2"
     u8 orig = inb(PORT_PS2_CTRLB);
     outb((orig & ~PPCB_SPKR) | PPCB_T2GATE, PORT_PS2_CTRLB);
@@ -134,6 +145,8 @@ get_tsc(void)
 {
     if (unlikely(GET_GLOBAL(no_tsc)))
         return emulate_tsc();
+    if (unlikely(GET_GLOBAL(use_kvmclock)))
+        return kvmclock_get();
     return rdtscll();
 }
 
src/paravirt.c +65 -0

74 lines not on this page · raw message

src/paravirt.h +3 -0
index a284c41..64ed3d8 100644
@@ -28,6 +28,9 @@ static inline int kvm_para_available(void)
     return 0;
 }
 
+extern u64 kvmclock_get(void);
+extern u32 kvmclock_init(void);
+
 #define QEMU_CFG_SIGNATURE              0x00
 #define QEMU_CFG_ID                     0x01
 #define QEMU_CFG_UUID                   0x02
Signature · 1 line
1.7.1

_______________________________________________
SeaBIOS mailing list
[email protected]
http://www.seabios.org/mailman/listinfo/seabios

#16 lore raw
Quoted 12 lines · Gerd Hoffmann

Hi,

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

(1) Use this patch (with alignment issue fixed of course).
(2) Do a full kvmclock implementation. Feels a bit like overkill.
(3) SeaBIOS can fallback to the PIT for timing on machines which have no TSC. We could do that too in case we detect kvm ...

What sort of timeouts are these? If seconds, maybe the rtc would be best.

I vote for 3 so nobody has to maintain kvmclock code in SeaBIOS and Gerd can fix the in-kernel PIT issues with GRUB (see Michaels message) while testing.

Quoted 8 lines

(2) turned out to be not too bad when taking a shortcut: Go through an enable/disable cycle each time we read the clock, then just grab system_time. Not that efficient, but should be ok for seabios. Usually it checks the clock when sitting around idle, waiting for something to happen. And it simplifies the implementation alot as we can just skip all the tsc frequency & delta calculations.

Draft patch attached. Comments?

Given the history of problems with kvmclock, would rather see it not being used for delays, if possible. Your shortcut gets rid of a class of problems, but there might be others (...).

Isnt pmtimer ioport usable? 14MHz.

Error handling in kvmclock_init is awkward.

Thanks

#20 lore raw
In reply to Gerd Hoffmann
Quoted 20 lines · Gerd Hoffmann

Hi,

Quoted 9 lines
Quoted 9 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

(1) Use this patch (with alignment issue fixed of course).
(2) Do a full kvmclock implementation. Feels a bit like overkill.
(3) SeaBIOS can fallback to the PIT for timing on machines which have no TSC. We could do that too in case we detect kvm ...

What sort of timeouts are these? If seconds, maybe the rtc would be best.

I vote for 3 so nobody has to maintain kvmclock code in SeaBIOS and Gerd can fix the in-kernel PIT issues with GRUB (see Michaels message) while testing.

(2) turned out to be not too bad when taking a shortcut: Go through an enable/disable cycle each time we read the clock, then just grab system_time. Not that efficient, but should be ok for seabios. Usually it checks the clock when sitting around idle, waiting for something to happen. And it simplifies the implementation alot as we can just skip all the tsc frequency & delta calculations.

Draft patch attached. Comments?

src/paravirt.c void qemu_cfg_romfile_setup(void) · line 380

Quoted 8 lines
src/paravirt.c +8 -0
+
+static void kvmclock_fetch(struct pvclock_vcpu_time_info *time)
+{
+    u32 addr = (u32)MAKE_FLATPTR(GET_SEG(SS), time);
+    u32 msr = GET_GLOBAL(kvm_systime_msr);
+
+    memset(time, 0, sizeof(*time));
+    wrmsr(msr, addr | 1);

I'd put the time calculations in here. We don't specify what happens to the data area after disabling kvmclock; it could be in the middle of an update.

src/paravirt.c void qemu_cfg_romfile_setup(void) · line 389

Quoted 9 lines
src/paravirt.c +9 -0
+    wrmsr(msr, 0);
+}
+
+u64 kvmclock_get(void)
+{
+    struct pvclock_vcpu_time_info time;
+
+    kvmclock_fetch(&time);
+    return time.system_time;

That's just a random number. You have to do the full calculation.

Signature · 1 line
error compiling committee.c: too many arguments to function
#22 lore raw
In reply to Marcelo Tosatti
Quoted 11 lines · Marcelo Tosatti
Quoted 11 lines
Quoted 10 lines
src/paravirt.c +9 -0
+    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;
Quoted 6 lines
Quoted 2 lines

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

Thats why there exists hardware tsc frequency scaling and the software equivalent for that on kvm.

The hardware is only available on a minority of processors in the field.
The software equivalent isn't there.

Signature · 1 line
error compiling committee.c: too many arguments to function
#11 lore raw
In reply to Marcelo Tosatti
Quoted 22 lines · Marcelo Tosatti
Quoted 21 lines · Avi Kivity
Quoted 9 lines · Gerd Hoffmann

Hi,

Quoted 7 lines
Quoted 6 lines

So what do you suggest? The options I see are:

(1) Use this patch (with alignment issue fixed of course).
(2) Do a full kvmclock implementation. Feels a bit like overkill.
(3) SeaBIOS can fallback to the PIT for timing on machines which have no TSC. We could do that too in case we detect kvm ...

Quoted 7 lines
Quoted 1 line

What sort of timeouts are these? If seconds, maybe the rtc would be best.

All sorts of timeouts, from a few miliseconds to seconds.

The problematic ones are the longer timeouts, which wait for I/O stuff like disk reads complete. The stuff with smaller timeouts (like waiting for AHCI link become ready) tend to finish instantly in kvm.

That's not guaranteed. The AHCI adapter might be real hardware. Or the emulation may change.

Quoted 6 lines
Quoted 2 lines

What's wrong with having a full kvmclock implementation? Instead of issuing rdtsc call a function pointer.

Its not necessary (someone is going to maintain the kvmclock frequency retrieve, which patch is already here, versus maintainance of full kvmclock).

The frequency is meaninless.

Quoted 3 lines

Frequency scaling (or the software equivalent: TSC trapping) are required for other reasons anyway.

One thing we can do is enable TSC trapping, then disable it if the guest activates kvmclock. That gives us accurate time either way.

Signature · 1 line
error compiling committee.c: too many arguments to function
#17 lore raw
In reply to Marcelo Tosatti

Hi,

Quoted 1 line

Isnt pmtimer ioport usable? 14MHz.

Can give it a try. 14 MHz looks wrong though, apci.h says:

/* PM Timer ticks per second (HZ) */
#define PM_TIMER_FREQUENCY 3579545

Is this fixed? Or hardware specific?

cheers,
Gerd

#18 lore raw
Quoted 11 lines · Gerd Hoffmann

Hi,

Quoted 1 line

Isnt pmtimer ioport usable? 14MHz.

Can give it a try. 14 MHz looks wrong though, apci.h says:

/* PM Timer ticks per second (HZ) */
#define PM_TIMER_FREQUENCY 3579545

Is this fixed? Or hardware specific?

3.579545 MHz clock required by ACPI spec.

--
Gleb.

#19 lore raw

Add a comment about it in the source code.

-#define PM_TIMER_FREQUENCY 3579545
+#define PM_TIMER_FREQUENCY 3579545 // 3.579545 MHz clock required by ACPI spec.

Quoted 21 lines · Gleb Natapov
Quoted 11 lines · Gerd Hoffmann

Hi,

Quoted 1 line

Isnt pmtimer ioport usable? 14MHz.

Can give it a try. 14 MHz looks wrong though, apci.h says:

/* PM Timer ticks per second (HZ) */
#define PM_TIMER_FREQUENCY 3579545

Is this fixed? Or hardware specific?

3.579545 MHz clock required by ACPI spec.

--
Gleb.

_______________________________________________
SeaBIOS mailing list
[email protected]
http://www.seabios.org/mailman/listinfo/seabios