From: Tulio A M Mendes Date: Thu, 12 Feb 2026 06:39:37 +0000 (-0300) Subject: refactor: extract x86 rdtsc from kernel/kaslr.c to HAL layer X-Git-Url: https://projects.tadryanom.me/docs/static/gitweb.js?a=commitdiff_plain;h=5d477dd5c9c9b88854867c2c30d176ad17c2cec6;p=AdrOS.git refactor: extract x86 rdtsc from kernel/kaslr.c to HAL layer 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. --- diff --git a/include/hal/cpu.h b/include/hal/cpu.h index 3fee40c..dcb196d 100644 --- a/include/hal/cpu.h +++ b/include/hal/cpu.h @@ -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 diff --git a/src/hal/x86/cpu.c b/src/hal/x86/cpu.c index 718fbfc..1adc72e 100644 --- a/src/hal/x86/cpu.c +++ b/src/hal/x86/cpu.c @@ -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 diff --git a/src/kernel/kaslr.c b/src/kernel/kaslr.c index 86f65fd..0415356 100644 --- a/src/kernel/kaslr.c +++ b/src/kernel/kaslr.c @@ -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");