From: Tulio A M Mendes Date: Tue, 10 Feb 2026 05:01:18 +0000 (-0300) Subject: refactor: replace hardcoded 0xC0000000 in elf.c and uaccess.c with hal_mm_kernel_virt... X-Git-Url: https://projects.tadryanom.me/sitemap.xml?a=commitdiff_plain;h=5e872beaea8db7533780c95766dfd8b24f821a7c;p=AdrOS.git refactor: replace hardcoded 0xC0000000 in elf.c and uaccess.c with hal_mm_kernel_virt_base() Both elf.c and uaccess.c had X86_KERNEL_VIRT_BASE defined as 0xC0000000U inside #if x86 blocks. While technically correct (only compiled on x86), this is inconsistent with the rest of the codebase which now uses hal_mm_kernel_virt_base() everywhere. Replace with the HAL abstraction for consistency and to ensure the kernel virtual base is defined in exactly one place per arch. Passes: make, cppcheck, QEMU smoke test. --- diff --git a/src/kernel/elf.c b/src/kernel/elf.c index f76c005..379c38a 100644 --- a/src/kernel/elf.c +++ b/src/kernel/elf.c @@ -10,11 +10,11 @@ #include "errno.h" #include "hal/cpu.h" +#include "hal/mm.h" #include #if defined(__i386__) -#define X86_KERNEL_VIRT_BASE 0xC0000000U static void* pmm_alloc_page_low_16mb(void) { for (int tries = 0; tries < 4096; tries++) { @@ -52,7 +52,7 @@ static int elf32_validate(const elf32_ehdr_t* eh, size_t file_len) { if (ph_end > file_len) return -EINVAL; if (eh->e_entry == 0) return -EINVAL; - if (eh->e_entry >= X86_KERNEL_VIRT_BASE) return -EINVAL; + if (eh->e_entry >= hal_mm_kernel_virt_base()) return -EINVAL; return 0; } @@ -60,11 +60,11 @@ static int elf32_validate(const elf32_ehdr_t* eh, size_t file_len) { static int elf32_map_user_range(uintptr_t as, uintptr_t vaddr, size_t len, uint32_t flags) { if (len == 0) return 0; if (vaddr == 0) return -EINVAL; - if (vaddr >= X86_KERNEL_VIRT_BASE) return -EINVAL; + if (vaddr >= hal_mm_kernel_virt_base()) return -EINVAL; uintptr_t end = vaddr + len - 1; if (end < vaddr) return -EINVAL; - if (end >= X86_KERNEL_VIRT_BASE) return -EINVAL; + if (end >= hal_mm_kernel_virt_base()) return -EINVAL; uintptr_t start_page = vaddr & ~(uintptr_t)0xFFF; uintptr_t end_page = end & ~(uintptr_t)0xFFF; @@ -168,7 +168,7 @@ int elf32_load_user_from_initrd(const char* filename, uintptr_t* entry_out, uint vmm_as_destroy(new_as); return -EINVAL; } - if (ph[i].p_vaddr >= X86_KERNEL_VIRT_BASE) { + if (ph[i].p_vaddr >= hal_mm_kernel_virt_base()) { uart_print("[ELF] PT_LOAD in kernel range rejected\n"); kfree(file); vmm_as_activate(old_as); @@ -183,7 +183,7 @@ int elf32_load_user_from_initrd(const char* filename, uintptr_t* entry_out, uint vmm_as_destroy(new_as); return -EINVAL; } - if (seg_end >= X86_KERNEL_VIRT_BASE) { + if (seg_end >= hal_mm_kernel_virt_base()) { kfree(file); vmm_as_activate(old_as); vmm_as_destroy(new_as); diff --git a/src/kernel/uaccess.c b/src/kernel/uaccess.c index b47e57d..84e3267 100644 --- a/src/kernel/uaccess.c +++ b/src/kernel/uaccess.c @@ -2,19 +2,19 @@ #include "errno.h" #include "interrupts.h" +#include "hal/mm.h" #include #if defined(__i386__) -#define X86_KERNEL_VIRT_BASE 0xC0000000U static int x86_user_range_basic_ok(uintptr_t uaddr, size_t len) { if (len == 0) return 1; if (uaddr == 0) return 0; - if (uaddr >= X86_KERNEL_VIRT_BASE) return 0; + if (uaddr >= hal_mm_kernel_virt_base()) return 0; uintptr_t end = uaddr + len - 1; if (end < uaddr) return 0; - if (end >= X86_KERNEL_VIRT_BASE) return 0; + if (end >= hal_mm_kernel_virt_base()) return 0; return 1; } @@ -31,7 +31,7 @@ int uaccess_try_recover(uintptr_t fault_addr, struct registers* regs) { if (g_uaccess_recover_eip == 0) return 0; // Only recover faults on user addresses; kernel faults should still panic. - if (fault_addr >= X86_KERNEL_VIRT_BASE) return 0; + if (fault_addr >= hal_mm_kernel_virt_base()) return 0; g_uaccess_faulted = 1; regs->eip = (uint32_t)g_uaccess_recover_eip;