milo

03 — Memory Management

Milo manages physical memory, kernel heap objects, and isolated user virtual address spaces using freestanding C++20 components (kernel/mm/pfa.{hpp,cpp}, kernel/mm/heap.{hpp,cpp}, kernel/mm/AddressSpace.{hpp,cpp}) paired with AArch64 translation table and fault handling (arch/aarch64-linux-gnu/mmu_aarch64.cpp, arch/aarch64-linux-gnu/trap_aarch64.cpp, arch/aarch64-linux-gnu/cpu.cpp, arch/aarch64-linux-gnu/linker.ld) and host HAL mocks (tests/host/mocks/host_hal.{hpp,cpp}).

Physical & Virtual Memory Map

Address Constants & Layout (arch/aarch64-linux-gnu/memory_config.hpp, arch/aarch64-linux-gnu/cpu.cpp, arch/aarch64-linux-gnu/linker.ld, kernel/syscalls/SyscallInternal.hpp)

flowchart LR
    subgraph LOW_VA["Lower Virtual Space (TTBR0_EL1)"]
        MMIO_DEV["Device MMIO<br/>GICv2 0x08000000 / 0x08010000<br/>UART0 0x09000000, fw_cfg 0x09020000<br/>VirtIO-MMIO 0x0A000000+"]
        U_CODE_BRK["User ELF Segments & brk Heap<br/>(0x400000+ / brk_start..brk_current)"]
        MMAP_LOW["Lower mmap Arena<br/>(0x20000000..0x3FFE0000)"]
        USTACK["User Stack (64 KiB default)<br/>(0x3FFE0000..0x3FFF0000)"]
        K_IMG_LOW["Kernel ELF Image (.text RX, .rodata R, .data/.bss RW)<br/>at 0x40080000 + Hybrid 2 MiB / 4 KiB RAM"]
        MMAP_HIGH["High mmap Search Window<br/>(0x80000000..0x100000000)"]
    end

    subgraph HIGH_VA["Higher-Half Kernel Space (TTBR1_EL1, +0xFFFF800000000000)"]
        HH_DEV["Higher-Half Device Mappings<br/>(0xFFFF800008000000 / 0x08010000 / 0x09000000)"]
        HH_KIMG["Higher-Half Kernel ELF Segments<br/>(0xFFFF800040080000+, W^X)"]
        HH_RAM["Higher-Half RAM Alias<br/>(2 MiB blocks & 4 KiB pages for PFA, Heap & DMA)"]
    end

    MMIO_DEV -.->|"Same PA"| HH_DEV
    K_IMG_LOW -.->|"Same PA"| HH_KIMG
    K_IMG_LOW -.->|"Same PA"| HH_RAM

Page Frame Allocator & Heap

Page Frame Allocator (kernel/mm/pfa.hpp, kernel/mm/pfa.cpp)

PageFrameAllocator is a singleton (Kernel::MM::PageFrameAllocator::instance()) with inline wrappers pfa_init, pfa_on_mmu_enabled, alloc_frame, alloc_frames, free_frame, free_frames, page_get, page_put, page_ref_count, page_from_paddr, page_to_paddr, pfa_is_page_allocated, pfa_allocated_pages_count, and pfa_free_pages_count:

Kernel Heap (kernel/mm/heap.hpp, kernel/mm/heap.cpp, kernel/runtime/cxx_abi.cpp)

KernelHeap (KernelHeap::instance(), wrapped by heap_init, kmalloc, kzalloc, krealloc, kfree, and heap_get_stats) provides dynamic kernel memory above the PFA:

Address Spaces & Paging

AArch64 MMU & Fault Resolution (arch/aarch64-linux-gnu/mmu_aarch64.cpp, arch/aarch64-linux-gnu/trap_aarch64.cpp, kernel/hal/mmu.hpp)

VMA Tracking & User Copy (kernel/mm/AddressSpace.hpp, kernel/mm/AddressSpace.cpp)

AddressSpace owns m_root_paddr and a page-backed sorted array of up to MAX_VMAS = 64U Vma descriptors (start_vaddr, end_vaddr, page_flags, prot, map_flags, inode, file_offset), retaining and releasing Inode references on add_vma, unmap_vma_range, protect_vma_range, clone, and clear_vmas. copy_from_user and copy_to_user validate user_addr_space_limit(), resolve demand-zero/file-backed/CoW pages across page boundaries, and return -14 (-EFAULT) on invalid accesses.