]> 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 3fee40cca015b489cddefd3ec4635ec10be81dfa..dcb196d91112e7c82a502d23e36d253b056d0d2b 100644 (file)
@@ -13,4 +13,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 718fbfcc64d4f8f16a6b67490de1a234f9a3cb2b..1adc72ecf093d0da8c6a4fdd5c7a402dace14f33 100644 (file)
@@ -48,6 +48,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) {
@@ -75,4 +81,8 @@ void hal_cpu_disable_interrupts(void) {
 void hal_cpu_idle(void) {
 }
 
+uint64_t hal_cpu_read_timestamp(void) {
+    return 0;
+}
+
 #endif
index 86f65fd47f60c46bf24152e52d06e82418053b8a..04153568e0286319c26ce04ac6c9e6cdbd57267c 100644 (file)
@@ -1,16 +1,11 @@
 #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");