]> 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 7b89eff07440281b22e400a1f3dcd05d0d12df7b..e118b2797909aa5c5239534fa3200a908cb63c3f 100644 (file)
@@ -7,4 +7,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 a11e99ae0056fbfce4e16f7d76e01f94a326aa9b..08f91f966ac69a19a2f755d37604099ea3fa45d7 100644 (file)
@@ -7,3 +7,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 a11e99ae0056fbfce4e16f7d76e01f94a326aa9b..08f91f966ac69a19a2f755d37604099ea3fa45d7 100644 (file)
@@ -7,3 +7,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 a11e99ae0056fbfce4e16f7d76e01f94a326aa9b..08f91f966ac69a19a2f755d37604099ea3fa45d7 100644 (file)
@@ -7,3 +7,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 e9be2efe76e0867244de542d06e87f99eeaf125b..158a8ed334b8689f78f98805328c8462371bf471 100644 (file)
@@ -31,3 +31,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 9edd0bde064baab02341ff13ff271d708dab1b7e..b103620e5f3308225ed7920b388e16498a785442 100644 (file)
@@ -1,5 +1,6 @@
 #include "slab.h"
 #include "pmm.h"
+#include "hal/mm.h"
 #include "uart_console.h"
 
 #include <stddef.h>
@@ -35,8 +36,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);