Skip to content

Commit 07acbd8

Browse files
committed
FIXME: use hyper-v time counter (rather than i8254) to calibrate TSC
The i8254 PIT counter emulated by Hyper-V is not reliable. In probe_tsc_freq() -> DELAY -> init_ops.early_delay() -> i8254_delay(), getit() can return these values when it is invoked 10 times: 1 pit count0 = 228 2 pit count0 = 131 3 pit count0 = 34 4 pit count0 = 65473 <-- this is a normal wrap-around. 5 pit count0 = 65375 6 pit count0 = 65278 7 pit count0 = 65180 8 pit count0 = 65388 <-- this is bad! 9 pit count0 = 65290 10 pit count0 = 65193 For the 8th time, the 'delta' in i8254_delay() is < 0 while it shouldn't, so the later "delta += i8254_max_count;" and "ticks_left -= delta" will cause i8254_delay() to wait shorter than expected, and finally probe_tsc_freq() get a smaller 'tsc_freq'. A smaller 'tsc_freq' can cause time inaccuracy in dtrace. It can also cause warnings like calcru: runtime went backwards from 50 usec to 25 usec for pid 0 (kernel) calcru: runtime went backwards from 1471 usec to 743 usec for pid 0 (kernel) calcru: runtime went backwards from 40 usec to 20 usec for pid 0 (kernel) calcru: runtime went backwards from 18 usec to 9 usec for pid 0 (kernel) calcru: runtime went backwards from 46204978 usec to 23362331 usec for pid 0 (kernel) We use Hyper-V time counter, which is much more reliable than i8254, to calibrate TSC. Signed-off-by: Dexuan Cui <decui@microsoft.com>
1 parent 7c29ef1 commit 07acbd8

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

sys/x86/x86/delay.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,23 @@ delay_tc(int n)
9595
return (1);
9696
}
9797

98+
#define HV_X64_MSR_TIME_REF_COUNT 0x40000020
9899
void
99100
DELAY(int n)
100101
{
102+
uint64_t now, end;
101103

102104
if (delay_tc(n))
103105
return;
106+
//FIXME: XXX: need to find a way to do this cleanly...
107+
#if 1
108+
now = rdmsr(HV_X64_MSR_TIME_REF_COUNT);
109+
end = now + n * 10;
110+
111+
while (now < end)
112+
now = rdmsr(HV_X64_MSR_TIME_REF_COUNT);
113+
#else
104114

105115
init_ops.early_delay(n);
116+
#endif
106117
}

0 commit comments

Comments
 (0)