From a63b1db5237e4b62a2137a7ee32326497aab6e14 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Tue, 12 Aug 2025 23:37:01 -0400 Subject: kernel/amd64: tsc: Bail if the TSC isn't supported Some older CPUs may not even support the TSC so it would be a good idea to check before we start assuming its existence. Signed-off-by: Ian Moffett --- sys/arch/amd64/amd64/tsc.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/sys/arch/amd64/amd64/tsc.c b/sys/arch/amd64/amd64/tsc.c index 1880821..2111cd0 100644 --- a/sys/arch/amd64/amd64/tsc.c +++ b/sys/arch/amd64/amd64/tsc.c @@ -27,12 +27,15 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#include #include +#include #include #include #include #include #include +#include /* See kconf(9) */ #if defined(__USER_TSC) @@ -52,10 +55,37 @@ rdtsc_rel(void) return rdtsc() - tsc_i; } +/* + * Check if the TSC and RDTSC instruction is + * supported on the current CPU. + * + * Returns zero if supported, otherwise a less + * than zero value is returned. + */ +static int +tsc_check(void) +{ + uint32_t edx, unused; + + CPUID(1, unused, unused, unused, edx); + if (ISSET(edx, BIT(4))) { + return 0; + } + + return -ENOTSUP; +} + static int tsc_init(void) { uint64_t cr4; + int error; + + /* Is the TSC even supported? */ + if ((error = tsc_check()) != 0) { + pr_error("TSC not supported by machine\n"); + return error; + } cr4 = amd64_read_cr4(); tsc_i = rdtsc(); -- cgit v1.2.3