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}).
arch/aarch64-linux-gnu/memory_config.hpp, arch/aarch64-linux-gnu/cpu.cpp, arch/aarch64-linux-gnu/linker.ld, kernel/syscalls/SyscallInternal.hpp)arch/aarch64-linux-gnu/memory_config.hpp):
PAGE_SIZE = 4096U (0x1000), PAGE_SHIFT = 12UBLOCK_SIZE_2M = 2UL * 1024UL * 1024UL (0x200000), BLOCK_SHIFT_2M = 21URAM_BASE = 0x40000000U (QEMU virt default RAM base; fallback ram_size = 256 MiB when no DTB memory regions are found, MAX_RAM_SIZE = 64 GiB)arch/aarch64-linux-gnu/cpu.cpp, kernel/syscalls/SyscallInternal.hpp):
higher_half_offset = 0xFFFF800000000000ULL (phys_to_virt(paddr) = paddr + higher_half_offset, virt_to_phys(vaddr) = vaddr - higher_half_offset)user_va_limit = 0x0000000100000000ULL (4 GiB)user_brk_base = 0x0000000010000000ULL (default fallback; Process::attach_elf sets brk_start and brk_current to LoadedElf::max_vaddr)MMAP_ARENA_FLOOR = 0x0000000020000000ULL (512 MiB; sys_mmap also supports high-VA search starting at 0x0000000080000000ULL when the lower arena cannot satisfy a request)user_mmap_base = 0x000000003FFE0000ULL (DEFAULT_USER_STACK_SIZE = 64 KiB, so stack_floor = 0x3FFF0000 - 0x10000 = 0x3FFE0000)user_stack_top = 0x000000003FFF0000ULLarch/aarch64-linux-gnu/linker.ld):
0x40080000 (_text_start), with .text (RX), page-aligned .rodata (R, _rodata_start.._rodata_end), and page-aligned .data (RW, _data_start.._data_end, including __init_array_start..__init_array_end) + .bss (__bss_start..__bss_end).virt (arch/aarch64-linux-gnu/mmu_aarch64.cpp, kernel/hal/targets/qemu_virt/gicv2.hpp, kernel/hal/targets/qemu_virt/fw_cfg.hpp):
0x08000000 (GICD_BASE)0x08010000 (GICC_BASE)0x09000000fw_cfg MMIO: 0x09020000 (FW_CFG_MMIO_BASE, size 0x18)0x0A000000 (VIRTIO_MMIO_BASE) with stride 0x200 (0x0A000000..0x0A003E00)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
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:
pfa_init): Computes m_total_pages = (m_ram_end - m_ram_start) >> page_shift(), requires align_up_page(m_total_pages * sizeof(Page)) bytes for the Page descriptor array, carves out a contiguous physical span in usable RAM clear of [kernel_start, kernel_end), DTB, initrd, and /memreserve/ entries (using static storage in HOST_TEST up to 16 MiB of metadata), defaults all descriptors in [m_ram_start, m_ram_end) to Reserved so gaps between discontiguous memory banks stay non-allocatable, marks usable FDT regions as Free, and re-marks all reserved spans.pfa_on_mmu_enabled): Updates m_pages to phys_to_virt(m_pages_paddr) once paging is active.Page): Each Page tracks m_ref_count, PageFlags (Free, Allocated, Reserved, Slab, Compound), m_order, m_slab_cache, intrusive list pointers (m_next, m_prev), and m_freelist. try_claim_free() atomically transitions Free -> Allocated with m_ref_count = 1; try_retain() (page_get) increments m_ref_count on non-reserved pages; release_ref() (page_put) atomically decrements m_ref_count and transitions back to Free when it reaches 0. alloc_frames(count) scans for contiguous free runs and rolls back partial claims on contention.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:
16 B–2048 B): MIN_SLAB_SIZE = 16U, MAX_SLAB_SIZE = 2048U, NUM_SLAB_CLASSES = 8U (16, 32, 64, 128, 256, 512, 1024, 2048 bytes; MIN_HEAP_ALIGN = 16U). Each SlabCache maintains partial_slabs, full_slabs, and one reserve_slab page per class.2048 B (or with alignment promoted past 2048 B) allocate num_pages contiguous frames via alloc_frames(num_pages) and mark the head Page as PageFlags::Compound with m_ref_count = num_pages.krealloc keeps the allocation in place when alignment is compatible and new_size fits in (old_capacity / 2, old_capacity]; otherwise it allocates, copies, and frees. kernel/runtime/cxx_abi.cpp implements throwing and std::nothrow global operator new / operator new[] / operator delete / operator delete[] over kmalloc and kfree.arch/aarch64-linux-gnu/mmu_aarch64.cpp, arch/aarch64-linux-gnu/trap_aarch64.cpp, kernel/hal/mmu.hpp)init_mmu & AddressSpaceBackend): Configures MAIR_EL1 (0x00 for device attr index 0, 0xFF for normal cacheable attr index 1), TCR_EL1 (T0SZ = 16, T1SZ = 16, 4 KiB granule, inner shareable, IPS = pa_range from ID_AA64MMFR0_EL1), TTBR0_EL1, and TTBR1_EL1. L3 page descriptors encode AF, AP (RO vs RW, user vs privileged), SH_ISH, PXN/UXN (W^X / execute-never), DESC_COW (1ULL << 55), PTE_DEMAND_ZERO (1ULL << 56), and DESC_SHARED (1ULL << 57).trap_aarch64.cpp, translation faults (FSC 0x04..0x07) check for PTE_DEMAND_ZERO descriptors or anonymous/file-backed Vmas in Process::current()->address_space(). Demand-zero faults allocate and zero a PFA frame and map it with map_user_page; file-backed faults call AddressSpace::resolve_file_backed_page (kernel/mm/AddressSpace.cpp), which fetches a page via Kernel::FS::PageCache::instance().get_or_load_page(...) and maps it (using map_cow_user_page for writable MAP_PRIVATE VMAs).DESC_COW): Write permission faults (EC == 0x24, FSC 0x0C..0x0F, WnR == 1) on DESC_COW pages copy the frame into a new PFA frame if page->has_shared_refs(), decrement the old frame via page_put, or upgrade the exclusive frame in place, then invalidate TLB (tlbi vaae1is) and I-cache when executable. AddressSpace::clone() clones user mappings by marking private writable entries AP_RO_BIT | DESC_COW and calling page_get on shared frames while preserving DESC_SHARED pages.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.