]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
refactor: extract x86 rdtsc from kernel/kaslr.c to HAL layer
authorTulio A M Mendes <[email protected]>
Thu, 12 Feb 2026 06:39:37 +0000 (03:39 -0300)
committerTulio A M Mendes <[email protected]>
Fri, 13 Feb 2026 02:44:55 +0000 (23:44 -0300)
Added hal_cpu_read_timestamp() to the HAL CPU API with x86
implementation using rdtsc and a fallback stub for other arches.

kernel/kaslr.c no longer contains x86 inline assembly — it calls
the generic HAL function for timestamp-based PRNG seeding.

include/hal/cpu.h
src/hal/x86/cpu.c
src/kernel/kaslr.c

index b1ac16c207d62152344fec785e45acb76bcf756d..1a1413acfbd67e1181a1adb90b3130162cd8504b 100644 (file)
@@ -22,4 +22,6 @@ void hal_cpu_enable_interrupts(void);
 void hal_cpu_disable_interrupts(void);
 void hal_cpu_idle(void);
 
+uint64_t hal_cpu_read_timestamp(void);
+
 #endif
index dfe8d216a42da6293dd825d4a6b440fdef718a08..6e801c80287720bf0e8c51e28e6fa290a99dbd61 100644 (file)
@@ -57,6 +57,12 @@ void hal_cpu_idle(void) {
     __asm__ volatile("hlt");
 }
 
+uint64_t hal_cpu_read_timestamp(void) {
+    uint32_t lo, hi;
+    __asm__ volatile("rdtsc" : "=a"(lo), "=d"(hi));
+    return ((uint64_t)hi << 32) | lo;
+}
+
 #else
 
 uintptr_t hal_cpu_get_stack_pointer(void) {
@@ -84,4 +90,8 @@ void hal_cpu_disable_interrupts(void) {
 void hal_cpu_idle(void) {
 }
 
+uint64_t hal_cpu_read_timestamp(void) {
+    return 0;
+}
+
 #endif
index d2926c383ca53f77bf7e1e9f8f77b0d1936f348c..e3404ca2aaf3c65c1376370c99509b428ae9a85c 100644 (file)
@@ -8,18 +8,13 @@
  */
 
 #include "kaslr.h"
+#include "hal/cpu.h"
 #include "uart_console.h"
 
 static uint32_t prng_state;
 
-static inline uint64_t rdtsc(void) {
-    uint32_t lo, hi;
-    __asm__ volatile("rdtsc" : "=a"(lo), "=d"(hi));
-    return ((uint64_t)hi << 32) | lo;
-}
-
 void kaslr_init(void) {
-    uint64_t tsc = rdtsc();
+    uint64_t tsc = hal_cpu_read_timestamp();
     prng_state = (uint32_t)(tsc ^ (tsc >> 32));
     if (prng_state == 0) prng_state = 0xDEADBEEF;
     uart_print("[KASLR] PRNG seeded from TSC\n");