From 09614b35c01702b73c757d9ec374bf381d51b008 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Wed, 12 Apr 2023 23:33:29 +0200 Subject: boot: Use compiler intrinsic for TSC --- src/boot/efi/ticks.c | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) (limited to 'src/boot') diff --git a/src/boot/efi/ticks.c b/src/boot/efi/ticks.c index 13972528cd..84dfdc4542 100644 --- a/src/boot/efi/ticks.c +++ b/src/boot/efi/ticks.c @@ -4,28 +4,15 @@ #include "util.h" #include "vmm.h" -#ifdef __x86_64__ +#if defined(__i386__) || defined(__x86_64__) static uint64_t ticks_read(void) { - uint64_t a, d; - /* The TSC might or might not be virtualized in VMs (and thus might not be accurate or start at zero * at boot), depending on hypervisor and CPU functionality. If it's not virtualized it's not useful * for keeping time, hence don't attempt to use it. */ if (in_hypervisor()) return 0; - __asm__ volatile ("rdtsc" : "=a" (a), "=d" (d)); - return (d << 32) | a; -} -#elif defined(__i386__) -static uint64_t ticks_read(void) { - uint64_t val; - - if (in_hypervisor()) - return 0; - - __asm__ volatile ("rdtsc" : "=A" (val)); - return val; + return __builtin_ia32_rdtsc(); } #elif defined(__aarch64__) static uint64_t ticks_read(void) { -- cgit v1.2.1 From 706fd67e4a61e615e9d05427e00e7e676973d8f2 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Wed, 12 Apr 2023 23:45:04 +0200 Subject: boot: Rework timer frquency reading This is in preparation for the next commit. --- src/boot/efi/ticks.c | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) (limited to 'src/boot') diff --git a/src/boot/efi/ticks.c b/src/boot/efi/ticks.c index 84dfdc4542..7dd808ae19 100644 --- a/src/boot/efi/ticks.c +++ b/src/boot/efi/ticks.c @@ -5,7 +5,8 @@ #include "vmm.h" #if defined(__i386__) || defined(__x86_64__) -static uint64_t ticks_read(void) { + +static uint64_t ticks_read_arch(void) { /* The TSC might or might not be virtualized in VMs (and thus might not be accurate or start at zero * at boot), depending on hypervisor and CPU functionality. If it's not virtualized it's not useful * for keeping time, hence don't attempt to use it. */ @@ -14,36 +15,51 @@ static uint64_t ticks_read(void) { return __builtin_ia32_rdtsc(); } + +static uint64_t ticks_freq_arch(void) { + return 0; +} + #elif defined(__aarch64__) -static uint64_t ticks_read(void) { + +static uint64_t ticks_read_arch(void) { uint64_t val; asm volatile("mrs %0, cntvct_el0" : "=r"(val)); return val; } -#else -static uint64_t ticks_read(void) { - return 0; -} -#endif -#if defined(__aarch64__) -static uint64_t ticks_freq(void) { +static uint64_t ticks_freq_arch(void) { uint64_t freq; asm volatile("mrs %0, cntfrq_el0" : "=r"(freq)); return freq; } + #else -/* count TSC ticks during a millisecond delay */ + +static uint64_t ticks_read_arch(void) { + return 0; +} + +static uint64_t ticks_freq_arch(void) { + return 0; +} + +#endif + static uint64_t ticks_freq(void) { - uint64_t ticks_start, ticks_end; static uint64_t cache = 0; if (cache != 0) return cache; - ticks_start = ticks_read(); + cache = ticks_freq_arch(); + if (cache != 0) + return cache; + + /* As a fallback, count ticks during a millisecond delay. */ + uint64_t ticks_start = ticks_read_arch(); BS->Stall(1000); - ticks_end = ticks_read(); + uint64_t ticks_end = ticks_read_arch(); if (ticks_end < ticks_start) /* Check for an overflow (which is not that unlikely, given on some * archs the value is 32bit) */ @@ -52,16 +68,13 @@ static uint64_t ticks_freq(void) { cache = (ticks_end - ticks_start) * 1000UL; return cache; } -#endif uint64_t time_usec(void) { - uint64_t ticks, freq; - - ticks = ticks_read(); + uint64_t ticks = ticks_read_arch(); if (ticks == 0) return 0; - freq = ticks_freq(); + uint64_t freq = ticks_freq(); if (freq == 0) return 0; -- cgit v1.2.1 From 2a3ae5fae09a48387d40768faf0ba5a837dc0513 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Thu, 13 Apr 2023 15:31:36 +0200 Subject: boot: Use CPUID to detect TSC frequency Aside from being more accurate on CPUs that report the information this is also orders of magnitude faster than sleeping for 1ms. --- src/boot/efi/ticks.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'src/boot') diff --git a/src/boot/efi/ticks.c b/src/boot/efi/ticks.c index 7dd808ae19..f902b83a99 100644 --- a/src/boot/efi/ticks.c +++ b/src/boot/efi/ticks.c @@ -5,6 +5,7 @@ #include "vmm.h" #if defined(__i386__) || defined(__x86_64__) +# include static uint64_t ticks_read_arch(void) { /* The TSC might or might not be virtualized in VMs (and thus might not be accurate or start at zero @@ -17,7 +18,35 @@ static uint64_t ticks_read_arch(void) { } static uint64_t ticks_freq_arch(void) { - return 0; + /* Detect TSC frequency from CPUID information if available. */ + + unsigned max_leaf, ebx, ecx, edx; + if (__get_cpuid(0, &max_leaf, &ebx, &ecx, &edx) == 0) + return 0; + + /* Leaf 0x15 is Intel only. */ + if (max_leaf < 0x15 || ebx != signature_INTEL_ebx || ecx != signature_INTEL_ecx || + edx != signature_INTEL_edx) + return 0; + + unsigned denominator, numerator, crystal_hz; + __cpuid(0x15, denominator, numerator, crystal_hz, edx); + if (denominator == 0 || numerator == 0) + return 0; + + uint64_t freq = crystal_hz; + if (crystal_hz == 0) { + /* If the crystal frquency is not available, try to deduce it from + * the processor frequency leaf if available. */ + if (max_leaf < 0x16) + return 0; + + unsigned core_mhz; + __cpuid(0x16, core_mhz, ebx, ecx, edx); + freq = core_mhz * 1000ULL * 1000ULL * denominator / numerator; + } + + return freq * numerator / denominator; } #elif defined(__aarch64__) -- cgit v1.2.1