From: Tulio A M Mendes Date: Tue, 10 Feb 2026 04:04:05 +0000 (-0300) Subject: fix: replace hardcoded 0xC0000000 in slab.c with hal_mm_phys_to_virt() X-Git-Url: https://projects.tadryanom.me/sitemap.xml?a=commitdiff_plain;h=8300757e23b49af40aa2b01616f412d5220cb5f2;p=AdrOS.git fix: replace hardcoded 0xC0000000 in slab.c with hal_mm_phys_to_virt() 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. --- diff --git a/include/hal/mm.h b/include/hal/mm.h index 7b89eff..e118b27 100644 --- a/include/hal/mm.h +++ b/include/hal/mm.h @@ -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 diff --git a/src/hal/arm/mm.c b/src/hal/arm/mm.c index a11e99a..08f91f9 100644 --- a/src/hal/arm/mm.c +++ b/src/hal/arm/mm.c @@ -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; +} diff --git a/src/hal/mips/mm.c b/src/hal/mips/mm.c index a11e99a..08f91f9 100644 --- a/src/hal/mips/mm.c +++ b/src/hal/mips/mm.c @@ -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; +} diff --git a/src/hal/riscv/mm.c b/src/hal/riscv/mm.c index a11e99a..08f91f9 100644 --- a/src/hal/riscv/mm.c +++ b/src/hal/riscv/mm.c @@ -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; +} diff --git a/src/hal/x86/mm.c b/src/hal/x86/mm.c index e9be2ef..158a8ed 100644 --- a/src/hal/x86/mm.c +++ b/src/hal/x86/mm.c @@ -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; +} diff --git a/src/mm/slab.c b/src/mm/slab.c index 9edd0bd..b103620 100644 --- a/src/mm/slab.c +++ b/src/mm/slab.c @@ -1,5 +1,6 @@ #include "slab.h" #include "pmm.h" +#include "hal/mm.h" #include "uart_console.h" #include @@ -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);