]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
fix: replace hardcoded 0xC0000000 in slab.c with hal_mm_phys_to_virt()
authorTulio A M Mendes <[email protected]>
Tue, 10 Feb 2026 04:04:05 +0000 (01:04 -0300)
committerTulio A M Mendes <[email protected]>
Tue, 10 Feb 2026 04:04:05 +0000 (01:04 -0300)
slab.c used 'base + 0xC0000000U' to convert physical to virtual
addresses, which is x86 higher-half specific. This breaks on ARM,
RISC-V, and MIPS where the kernel virtual base differs.

Add hal_mm_phys_to_virt(), hal_mm_virt_to_phys(), and
hal_mm_kernel_virt_base() to the HAL mm interface with proper
implementations for x86 (0xC0000000 offset) and identity-mapped
stubs for ARM, RISC-V, MIPS.

Passes: make, cppcheck, QEMU smoke test.

include/hal/mm.h
src/hal/arm/mm.c
src/hal/mips/mm.c
src/hal/riscv/mm.c
src/hal/x86/mm.c
src/mm/slab.c

index e58131ff59354f53f43f67e715ab9066c24f46a0..ff14f79e2005cbef0fb41f8556000625b350b02a 100644 (file)
@@ -16,4 +16,8 @@
 
 int hal_mm_map_physical_range(uintptr_t phys_start, uintptr_t phys_end, uint32_t flags, uintptr_t* out_virt);
 
+uintptr_t hal_mm_phys_to_virt(uintptr_t phys);
+uintptr_t hal_mm_virt_to_phys(uintptr_t virt);
+uintptr_t hal_mm_kernel_virt_base(void);
+
 #endif
index 440738625c8b49e3c66c21e550e719d70415f47e..9d40d007ffe0f024ddc46233a40389663afb4c1d 100644 (file)
@@ -16,3 +16,15 @@ int hal_mm_map_physical_range(uintptr_t phys_start, uintptr_t phys_end, uint32_t
     (void)out_virt;
     return -1;
 }
+
+uintptr_t hal_mm_phys_to_virt(uintptr_t phys) {
+    return phys;
+}
+
+uintptr_t hal_mm_virt_to_phys(uintptr_t virt) {
+    return virt;
+}
+
+uintptr_t hal_mm_kernel_virt_base(void) {
+    return 0;
+}
index 440738625c8b49e3c66c21e550e719d70415f47e..9d40d007ffe0f024ddc46233a40389663afb4c1d 100644 (file)
@@ -16,3 +16,15 @@ int hal_mm_map_physical_range(uintptr_t phys_start, uintptr_t phys_end, uint32_t
     (void)out_virt;
     return -1;
 }
+
+uintptr_t hal_mm_phys_to_virt(uintptr_t phys) {
+    return phys;
+}
+
+uintptr_t hal_mm_virt_to_phys(uintptr_t virt) {
+    return virt;
+}
+
+uintptr_t hal_mm_kernel_virt_base(void) {
+    return 0;
+}
index 440738625c8b49e3c66c21e550e719d70415f47e..9d40d007ffe0f024ddc46233a40389663afb4c1d 100644 (file)
@@ -16,3 +16,15 @@ int hal_mm_map_physical_range(uintptr_t phys_start, uintptr_t phys_end, uint32_t
     (void)out_virt;
     return -1;
 }
+
+uintptr_t hal_mm_phys_to_virt(uintptr_t phys) {
+    return phys;
+}
+
+uintptr_t hal_mm_virt_to_phys(uintptr_t virt) {
+    return virt;
+}
+
+uintptr_t hal_mm_kernel_virt_base(void) {
+    return 0;
+}
index 3a9d9575ba09f95a7f4bddcd78a8d8f34a52dc4e..566bc5b40f9750c76dab57ff43e4863c916ff8f4 100644 (file)
@@ -40,3 +40,17 @@ int hal_mm_map_physical_range(uintptr_t phys_start, uintptr_t phys_end, uint32_t
     *out_virt = virt_base + (phys_start - phys_start_aligned);
     return 0;
 }
+
+#define X86_KERNEL_VIRT_BASE 0xC0000000U
+
+uintptr_t hal_mm_phys_to_virt(uintptr_t phys) {
+    return phys + X86_KERNEL_VIRT_BASE;
+}
+
+uintptr_t hal_mm_virt_to_phys(uintptr_t virt) {
+    return virt - X86_KERNEL_VIRT_BASE;
+}
+
+uintptr_t hal_mm_kernel_virt_base(void) {
+    return X86_KERNEL_VIRT_BASE;
+}
index caf1bf90486056adc112924169b11262c809c613..439277b686f377d6f28ed0f2e0b880bc99bab47e 100644 (file)
@@ -9,6 +9,7 @@
 
 #include "slab.h"
 #include "pmm.h"
+#include "hal/mm.h"
 #include "uart_console.h"
 
 #include <stddef.h>
@@ -44,8 +45,7 @@ static int slab_grow(slab_cache_t* cache) {
      * For now, slab pages come from pmm_alloc_page which returns
      * physical addresses. We need to convert to virtual. */
 
-    /* Use kernel virtual = phys + 0xC0000000 for higher-half */
-    uint8_t* vbase = base + 0xC0000000U;
+    uint8_t* vbase = (uint8_t*)hal_mm_phys_to_virt((uintptr_t)base);
 
     for (uint32_t i = 0; i < cache->objs_per_slab; i++) {
         struct slab_free_node* node = (struct slab_free_node*)(vbase + i * cache->obj_size);