]> 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 f76c0050a05e460a89f906211e461d5c47a71204..379c38adc981272304b6a67c29ba52c2ab28b357 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++) {
@@ -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);
index b47e57ddd603aa11fb545294caebe8dea7698010..84e326769bee6ddc3a2c44cf79b3fc4165cc4051 100644 (file)
@@ -2,19 +2,19 @@
 
 #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;
 }
 
@@ -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;