Kernel/Memory/ and provides the foundation for process isolation and memory protection.
Core Components
MemoryManager
TheMemoryManager is the central singleton responsible for all memory operations in the kernel. Located in Kernel/Memory/MemoryManager.h, it provides:
- Physical page allocation and deallocation
- Virtual memory region management
- Page fault handling
- Kernel memory allocation
- DMA buffer management
The MemoryManager is initialized before global constructors run, so it cannot use the standard Singleton pattern.
Key Responsibilities
Physical Page Management The memory manager tracks physical RAM pages through thePhysicalRAMPage class. Each page is reference-counted and can be:
- Allocated: Active page in use by a process or kernel
- Free: Available for allocation
- Shared Zero Page: Special read-only page filled with zeros
- Lazy Committed: Page reserved but not yet backed by physical memory
allocate_kernel_region(): General kernel memory allocationallocate_contiguous_kernel_region(): Physically contiguous memory (for DMA)allocate_dma_buffer_pages(): Device DMA buffers with specific memory typesallocate_mmio_kernel_region(): Memory-mapped I/O regions
AddressSpace
Each process has its ownAddressSpace (Kernel/Memory/AddressSpace.h) which encapsulates:
- A
PageDirectoryfor page table management - A
RegionTreecontaining all virtual memory regions - Memory allocation and deallocation for the process
Address Space Statistics
Address Space Statistics
The AddressSpace class provides methods to query memory usage:
amount_resident(): Pages currently in physical memoryamount_virtual(): Total virtual address space usedamount_shared(): Shared memory pagesamount_dirty_private(): Modified private pagesamount_purgeable_volatile(): Volatile purgeable memoryamount_clean_inode(): Clean file-backed pages
Region
ARegion (Kernel/Memory/Region.h) represents a contiguous virtual memory mapping with specific properties:
- Virtual address range (
VirtualRange) - Access permissions (read/write/execute)
- Backing VMObject
- Memory type (Normal, IO, NonCacheable)
- Special flags (stack, mmap, shared, immutable)
VMObject
Virtual Memory Objects (Kernel/Memory/VMObject.h) represent the backing store for memory regions. Several types exist:
AnonymousVMObject (AnonymousVMObject.h)
- Anonymous memory (not backed by files)
- Used for heap, stack, and anonymous mmap
- Supports copy-on-write
InodeVMObject.h)
- File-backed memory
- Subtypes:
SharedInodeVMObject: Shared file mappingsPrivateInodeVMObject: Private file mappings with COW
MMIOVMObject.h)
- Memory-mapped I/O regions
- Direct physical memory access for devices
Memory Allocation Strategies
The kernel supports different allocation strategies defined inAllocationStrategy.h:
- Reserve: Reserve virtual address space without committing physical pages
- AllocateNow: Immediately allocate and map physical pages
- None: Don’t allocate anything (for special cases)
Reserve strategy enables lazy allocation where physical pages are only allocated on first access (demand paging).
Page Fault Handling
The MemoryManager handles page faults throughhandle_page_fault(). Page faults occur when:
- Lazy Allocation: Accessing reserved but uncommitted memory
- Copy-on-Write: Writing to a shared page
- File-backed Pages: Accessing unmapped file data
- Invalid Access: Permission violations or unmapped memory
PageFaultResponse.h):
Continue: Fault handled successfullyShouldCrash: Invalid memory accessOutOfMemory: Physical memory exhausted
Physical Memory Management
Physical Regions
Physical memory is divided into regions tracked byPhysicalRegion and organized into PhysicalZone buckets. The system maintains:
- Available physical pages
- Used memory ranges (kernel, boot modules, SMBIOS, etc.)
- Reserved memory ranges
Committed Pages
The kernel uses a commit/uncommit model for memory:Memory Types
Different memory regions require different caching and access characteristics (MemoryType.h):
- Normal: Standard cacheable memory
- IO: Memory-mapped I/O (uncacheable)
- NonCacheable: Non-cached memory (for DMA buffers)
Quickmap
For temporary kernel access to arbitrary physical pages, the MemoryManager provides a “quickmap” facility:Key Operations
Allocating Kernel Memory
Creating User Space Regions
Mapping Physical Memory
Related Files
Kernel/Memory/MemoryManager.{h,cpp}- Core memory managerKernel/Memory/AddressSpace.{h,cpp}- Per-process address spacesKernel/Memory/Region.{h,cpp}- Virtual memory regionsKernel/Memory/VMObject.{h,cpp}- Memory object abstractionKernel/Memory/PhysicalRAMPage.{h,cpp}- Physical page trackingKernel/Arch/PageDirectory.h- Architecture-specific page tables
