]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
refactor: replace hardcoded 0xC0000000 in elf.c and uaccess.c with hal_mm_kernel_virt...
authorTulio A M Mendes <[email protected]>
Tue, 10 Feb 2026 05:01:18 +0000 (02:01 -0300)
committerTulio A M Mendes <[email protected]>
Tue, 10 Feb 2026 05:01:18 +0000 (02:01 -0300)
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.

src/kernel/elf.c
src/kernel/uaccess.c

index a8bc1a3e23eab9ad54c78a757d8ab7b56eb4a9e9..2e65e7a35805f0f10353dd59267d09f7abeaf042 100644 (file)
 #include "errno.h"
 
 #include "hal/cpu.h"
+#include "hal/mm.h"
 
 #include <stdint.h>
 
 #if defined(__i386__)
-#define X86_KERNEL_VIRT_BASE 0xC0000000U
 
 static void* pmm_alloc_page_low_16mb(void) {
     for (int tries = 0; tries < 4096; tries++) {
@@ -61,7 +61,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;
 }
@@ -69,11 +69,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;
@@ -177,7 +177,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);
@@ -192,7 +192,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);
index ce7b190222e2ed1a3c82f01ed5b7f13ac8cd5051..9f15093aaa4932ade00119a6edbe47696d821f1d 100644 (file)
 
 #include "errno.h"
 #include "interrupts.h"
+#include "hal/mm.h"
 
 #include <stdint.h>
 
 #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;
 }
 
@@ -40,7 +40,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;