summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-08-12 23:37:01 -0400
committerIan Moffett <ian@osmora.org>2025-08-12 23:37:01 -0400
commita63b1db5237e4b62a2137a7ee32326497aab6e14 (patch)
treedd269ebcc0912fb14ffcfcc42c3c4051d721ce15
parent60af87ead21331501d3eac54648122f5ec6a4082 (diff)
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 <ian@osmora.org>
-rw-r--r--sys/arch/amd64/amd64/tsc.c30
1 files changed, 30 insertions, 0 deletions
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 <sys/errno.h>
#include <sys/types.h>
+#include <sys/param.h>
#include <sys/cdefs.h>
#include <sys/driver.h>
#include <sys/syslog.h>
#include <machine/tsc.h>
#include <machine/asm.h>
+#include <machine/cpuid.h>
/* 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();