From 99606bed001df903d86e278dc4fa21038d67289f Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 1 Nov 2022 01:54:15 +0200 Subject: [PATCH] Added Counter() function to CPU --- Core/CPU.cpp | 18 ++++++++++++++++++ include/cpu.hpp | 3 +++ 2 files changed, 21 insertions(+) diff --git a/Core/CPU.cpp b/Core/CPU.cpp index a477f98d..7ff1616d 100644 --- a/Core/CPU.cpp +++ b/Core/CPU.cpp @@ -240,6 +240,24 @@ namespace CPU trace("Features for BSP initialized."); #elif defined(__i386__) #elif defined(__aarch64__) +#endif + } + + uint64_t Counter() + { + // TODO: Get the counter from the x2APIC or any other timer that is available. (TSC is not available on all CPUs) +#if defined(__amd64__) + uint64_t counter; + asmv("rdtsc" + : "=A"(counter)); + return counter; +#elif defined(__i386__) + return 0; +#elif defined(__aarch64__) + uint64_t counter; + asmv("mrs %0, cntvct_el0" + : "=r"(counter)); + return counter; #endif } } diff --git a/include/cpu.hpp b/include/cpu.hpp index f064a529..2b343026 100644 --- a/include/cpu.hpp +++ b/include/cpu.hpp @@ -203,6 +203,9 @@ namespace CPU /** @brief To be used only once. */ void InitializeFeatures(); + /** @brief Get CPU counter value. */ + uint64_t Counter(); + namespace MemBar { __attribute__((no_stack_protector)) static inline void Barrier()