| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "ticks.h" |
| 4 |
#include "vmm.h" |
| 5 |
|
| 6 |
#if defined(__i386__) || defined(__x86_64__) |
| 7 |
# include <cpuid.h> |
| 8 |
|
| 9 |
static uint64_t ticks_read_arch(void) { |
| 10 |
/* The TSC might or might not be virtualized in VMs (and thus might not be accurate or start at zero |
| 11 |
* at boot), depending on hypervisor and CPU functionality. If it's not virtualized it's not useful |
| 12 |
* for keeping time, hence don't attempt to use it. */ |
| 13 |
if (in_hypervisor()) |
| 14 |
return 0; |
| 15 |
|
| 16 |
return __builtin_ia32_rdtsc(); |
| 17 |
} |
| 18 |
|
| 19 |
static uint64_t ticks_freq_arch(void) { |
| 20 |
/* Detect TSC frequency from CPUID information if available. */ |
| 21 |
|
| 22 |
unsigned max_leaf, ebx, ecx, edx; |
| 23 |
if (__get_cpuid(0, &max_leaf, &ebx, &ecx, &edx) == 0) |
| 24 |
return 0; |
| 25 |
|
| 26 |
/* Leaf 0x15 is Intel only. */ |
| 27 |
if (max_leaf < 0x15 || ebx != signature_INTEL_ebx || ecx != signature_INTEL_ecx || |
| 28 |
edx != signature_INTEL_edx) |
| 29 |
return 0; |
| 30 |
|
| 31 |
unsigned denominator, numerator, crystal_hz; |
| 32 |
__cpuid(0x15, denominator, numerator, crystal_hz, edx); |
| 33 |
if (denominator == 0 || numerator == 0) |
| 34 |
return 0; |
| 35 |
|
| 36 |
uint64_t freq = crystal_hz; |
| 37 |
if (crystal_hz == 0) { |
| 38 |
/* If the crystal frequency is not available, try to deduce it from |
| 39 |
* the processor frequency leaf if available. */ |
| 40 |
if (max_leaf < 0x16) |
| 41 |
return 0; |
| 42 |
|
| 43 |
unsigned core_mhz; |
| 44 |
__cpuid(0x16, core_mhz, ebx, ecx, edx); |
| 45 |
freq = core_mhz * 1000ULL * 1000ULL * denominator / numerator; |
| 46 |
} |
| 47 |
|
| 48 |
return freq * numerator / denominator; |
| 49 |
} |
| 50 |
|
| 51 |
#elif defined(__aarch64__) |
| 52 |
|
| 53 |
static uint64_t ticks_read_arch(void) { |
| 54 |
uint64_t val; |
| 55 |
asm volatile("mrs %0, cntvct_el0" : "=r"(val)); |
| 56 |
return val; |
| 57 |
} |
| 58 |
|
| 59 |
static uint64_t ticks_freq_arch(void) { |
| 60 |
uint64_t freq; |
| 61 |
asm volatile("mrs %0, cntfrq_el0" : "=r"(freq)); |
| 62 |
return freq; |
| 63 |
} |
| 64 |
|
| 65 |
#else |
| 66 |
|
| 67 |
static uint64_t ticks_read_arch(void) { |
| 68 |
return 0; |
| 69 |
} |
| 70 |
|
| 71 |
static uint64_t ticks_freq_arch(void) { |
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
#endif |
| 76 |
|
| 77 |
static uint64_t ticks_freq(void) { |
| 78 |
static uint64_t cache = 0; |
| 79 |
|
| 80 |
if (cache != 0) |
| 81 |
return cache; |
| 82 |
|
| 83 |
cache = ticks_freq_arch(); |
| 84 |
if (cache != 0) |
| 85 |
return cache; |
| 86 |
|
| 87 |
/* As a fallback, count ticks during a millisecond delay. */ |
| 88 |
uint64_t ticks_start = ticks_read_arch(); |
| 89 |
BS->Stall(1000); |
| 90 |
uint64_t ticks_end = ticks_read_arch(); |
| 91 |
|
| 92 |
if (ticks_end < ticks_start) /* Check for an overflow (which is not that unlikely, given on some |
| 93 |
* archs the value is 32-bit) */ |
| 94 |
return 0; |
| 95 |
|
| 96 |
cache = (ticks_end - ticks_start) * 1000UL; |
| 97 |
return cache; |
| 98 |
} |
| 99 |
|
| 100 |
uint64_t time_usec(void) { |
| 101 |
uint64_t ticks = ticks_read_arch(); |
| 102 |
if (ticks == 0) |
| 103 |
return 0; |
| 104 |
|
| 105 |
uint64_t freq = ticks_freq(); |
| 106 |
if (freq == 0) |
| 107 |
return 0; |
| 108 |
|
| 109 |
return 1000UL * 1000UL * ticks / freq; |
| 110 |
} |