From 4a432614f68cf35879dbb4ebef089f5b8db95334 Mon Sep 17 00:00:00 2001 From: Colin Percival Date: Mon, 3 Jan 2022 23:28:36 -0800 Subject: [PATCH] TSC: Use 0x40000010 CPUID leaf for all VM types While this CPUID leaf was originally only used by VMWare, other hypervisors now also use it to announce the TSC frequency to guests. This speeds up the boot process by 100 ms in EC2 and other systems, by allowing the early calibration DELAY to be skipped. Reviewed by: markj Sponsored by: https://www.patreon.com/cperciva --- sys/x86/x86/tsc.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/sys/x86/x86/tsc.c b/sys/x86/x86/tsc.c index 75c452f365cf..317be8979feb 100644 --- a/sys/x86/x86/tsc.c +++ b/sys/x86/x86/tsc.c @@ -121,19 +121,29 @@ static struct timecounter tsc_timecounter = { #endif }; +static int +tsc_freq_cpuid_vm(void) +{ + u_int regs[4]; + + if (vm_guest == VM_GUEST_NO) + return (false); + if (hv_high < 0x40000010) + return (false); + do_cpuid(0x40000010, regs); + tsc_freq = (uint64_t)(regs[0]) * 1000; + tsc_early_calib_exact = 1; + return (true); +} + static void tsc_freq_vmware(void) { u_int regs[4]; - if (hv_high >= 0x40000010) { - do_cpuid(0x40000010, regs); - tsc_freq = regs[0] * 1000; - } else { - vmware_hvcall(VMW_HVCMD_GETHZ, regs); - if (regs[1] != UINT_MAX) - tsc_freq = regs[0] | ((uint64_t)regs[1] << 32); - } + vmware_hvcall(VMW_HVCMD_GETHZ, regs); + if (regs[1] != UINT_MAX) + tsc_freq = regs[0] | ((uint64_t)regs[1] << 32); tsc_early_calib_exact = 1; } @@ -305,6 +315,9 @@ probe_tsc_freq(void) break; } + if (tsc_freq_cpuid_vm()) + return; + if (vm_guest == VM_GUEST_VMWARE) { tsc_freq_vmware(); return;