From d6b81de2a1377858929c79f6be84108b023b5b00 Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Wed, 13 May 2026 15:56:08 +0800 Subject: feat: update Virtual Memory Manager to use page-based allocation and introduce VmFaultCause enum --- na/include/lume/vmm.h | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) (limited to 'na/include') diff --git a/na/include/lume/vmm.h b/na/include/lume/vmm.h index ec26d6a..85988e4 100644 --- a/na/include/lume/vmm.h +++ b/na/include/lume/vmm.h @@ -26,6 +26,12 @@ inline constexpr uint64 VM_WRITE = (1ULL << 1); inline constexpr uint64 VM_EXEC = (1ULL << 2); inline constexpr uint64 VM_USER = (1ULL << 3); +enum class VmFaultCause : uint8 { + Read = 0, + Write = 1, + Exec = 2, +}; + /* Forward declarations */ struct VmSpace; struct VmAreaStruct; @@ -48,13 +54,16 @@ struct VmAreaStruct { * * Lock contract: * Holds: lock (internal spinlock) - * May acquire: PMM global lock (via pmm_alloc_frame inside page fault) + * May acquire: PMM global lock (via pmm_alloc_page inside page fault) * Callers must NOT hold: PMM lock (lock ordering: VmSpace -> PMM) */ struct VmSpace { uint64 root_pa; // SV39 root page table physical address struct rb_root vma_tree; // Red-black tree of VmAreaStructs Spinlock lock; // Protects vma_tree mutations + + uint64 brk_start; // Start of heap (page-aligned) + uint64 brk_end; // Current heap end (page-aligned) // Create a brand new VmSpace (allocates root pagetable) static VmSpace* create(); @@ -72,18 +81,23 @@ void vmm_init_ap(); /* Map MMIO device region to the active kernel page table */ void vmm_map_kernel_mmio(uint64 pa, uint64 size); -/* Query frame_map allocation (set by vmm_init, read by pmm_init). - * Returns the PA, byte size, and frame count of the vmemmap region. */ -uint64 vmm_get_frame_map_pa(); -uint64 vmm_get_frame_map_size(); -uint64 vmm_get_num_frames(); +/* Query page_map allocation (set by vmm_init, read by pmm_init). + * Returns the PA, byte size, and page count of the vmemmap region. */ +uint64 vmm_get_page_map_pa(); +uint64 vmm_get_page_map_size(); +uint64 vmm_get_num_pages(); /* User space memory management (VMA & Demand Paging) */ int vmm_map_user(VmSpace* space, uint64 va, uint64 len, uint64 perm); int vmm_unmap_user(VmSpace* space, uint64 va, uint64 len); +int vmm_protect_user(VmSpace* space, uint64 va, uint64 len, uint64 perm); + +/* VMA helpers for mmap/mprotect implementations */ +uint64 mm_find_free_va(VmSpace* space, uint64 length, uint64 align); +bool vma_range_covered(VmSpace* space, uint64 start, uint64 end); -/* Handle user page fault (lookup VMA -> allocate frame -> map) */ -int vmm_handle_page_fault(VmSpace* space, uint64 fault_addr, uint64 cause); +/* Handle user page fault (lookup VMA -> allocate page -> map) */ +int vmm_handle_page_fault(VmSpace* space, uint64 fault_addr, VmFaultCause cause); /* Clone an address space for fork() */ VmSpace* vmm_clone(VmSpace* parent_space); -- cgit v1.2.3