Overview
Aurora OS memory management consists of three layers:- Physical Memory Allocator - Bitmap-based page frame allocator
- Paging (Virtual Memory) - 4-level page tables for x86_64
- Kernel Heap - kmalloc/kfree with slab caches
Aurora uses 4 KiB pages and supports up to 4 GiB of RAM with the bitmap allocator.
Physical Memory Allocator
Bitmap-based allocator managing 4 KiB page frames. Source:kernel/src/mm/phys.c
Architecture
- Page Size: 4096 bytes (4 KiB)
- Max Pages: 1,048,576 (4 GiB total)
- Bitmap Size: 128 KiB (1 bit per page)
- Allocation Strategy: O(1) amortized with free hint
Memory Map Types
Usable RAM - available for allocation.
Reserved by firmware/BIOS.
ACPI tables - reclaimable after parsing.
Kernel ELF and loaded modules.
GOP framebuffer memory.
kernel/src/mm/phys.h:15-23
phys_init_mmap
Initialize physical memory allocator with memory map from bootloader (Limine/Multiboot2).Array of memory regions from bootloader.
Number of memory map entries.
- Marks all pages as allocated (safe default)
- Frees pages in regions with
type == PHYS_MMAP_USABLE - Reserves first 1 MiB (BIOS/bootloader area)
- Page-aligns region boundaries
kernel/src/mm/phys.c:50-87
phys_alloc
Allocate a single 4 KiB physical page frame.Physical address of allocated page, or
NULL if out of memory.- Uses free hint for O(1) amortized allocation
- Scans bitmap starting from
next_free_hint - Updates hint after allocation
kernel/src/mm/phys.c:89-100
phys_free
Free a physical page frame.Physical address (must be page-aligned).
- Clears bitmap bit for the page
- Updates free hint if freed page is before current hint (optimization)
kernel/src/mm/phys.c:102-109
phys_reserve_range
Reserve a specific physical address range (for kernel, modules, framebuffer).Starting physical address.
Size in bytes.
kernel/src/mm/phys.c:119-127
Virtual Memory (Paging)
4-level page tables for x86_64 with higher-half kernel mapping. Source:kernel/src/mm/paging.c
Architecture
- Levels: PML4 → PDPT → PD → PT → Page (4 levels)
- Kernel Virtual Address:
0xFFFFFFFF80000000(higher-half) - Identity Map: First 2 MiB for boot transition
- Page Size: 4 KiB (also supports 2 MiB large pages)
Aurora uses higher-half kernel design: kernel mapped at high virtual addresses, user space at low addresses.
Page Table Entry Flags
kernel/src/mm/paging.h:12-16
paging_init
Initialize kernel page tables with higher-half mapping.- Allocates new PML4 table
- Identity maps first 2 MiB (for boot):
0x0 → 0x0 - Higher-half maps first 2 MiB:
0xFFFFFFFF80000000 → 0x0 - Uses 2 MiB large pages for efficiency
- Loads new page table into CR3
kernel/src/mm/paging.c:92-116
paging_map
Map a virtual page to a physical page in the current address space.Virtual address (page-aligned).
Physical address (page-aligned).
Combination of
PAGE_* flags.- Walks 4-level page table hierarchy
- Allocates missing intermediate tables on-demand
- Sets final page table entry:
pt[idx] = phys | flags | PAGE_PRESENT - Invalidates TLB entry with
invlpg
kernel/src/mm/paging.c:118-138
paging_unmap
Unmap a virtual page.Virtual address to unmap.
- Clears page table entry
- Flushes TLB with
invlpg - Does not free physical page (caller must call
phys_free)
kernel/src/mm/paging.c:140-155
paging_clone_kernel
Create a new page table for a process by cloning kernel mappings.Physical address of new PML4 table, or
0 on failure.- Allocates new PML4 table
- Copies kernel half (PML4 entries 256–511) by reference
- Leaves user half (entries 0–255) empty
- Used by
proc_create()
kernel/src/mm/paging.c:161-175
paging_clone_address_space
Fork a complete address space (forfork() syscall).
Physical address of source PML4 (parent process).
Physical address of cloned PML4, or
0 on failure.- Clones kernel half (entries 256–511) by reference
- Deep-copies user half (entries 0–255):
- Allocates new physical pages
- Copies page contents with
page_copy_4k()
- Preserves page flags (writable, user, NX, COW)
Copy-on-Write (COW) optimization planned but not yet implemented. Current fork does eager copy.
kernel/src/mm/paging.c:177-198
paging_map_in
Map a page in a specific PML4 (not current). Used for loading ELF into new process.Target PML4 physical address.
Virtual address to map.
Physical address.
Page flags.
kernel/src/mm/paging.c:200-221
paging_virt_to_phys
Translate virtual address to physical address.Physical address, or
0 if not mapped.- Walks page table hierarchy
- Handles 2 MiB large pages
- Returns 0 if any level is not present
kernel/src/mm/paging.c:227-242
Kernel Heap (kmalloc)
Dynamic memory allocator for kernel with slab caches for small objects. Source:kernel/src/mm/heap.c
Architecture
- Small Allocations (≤2048 bytes): Slab caches
- Large Allocations: Free-list heap with coalescing
- Alignment: 16-byte aligned
- Thread Safety: Spinlock protected
heap_init
Initialize kernel heap allocator.Starting virtual address of heap region.
Total heap size in bytes.
- Initializes free-list with single large block
- Creates 8 slab caches for sizes 16–2048 bytes
- Each slab cache has 32 KiB backing memory
kernel/src/mm/heap.c:40-50
kmalloc
Allocate kernel memory.Number of bytes to allocate.
Pointer to allocated memory, or
NULL if out of memory.- Round size up to 16-byte boundary
- Try slab cache if size ≤ 2048
- Fall back to free-list heap
- Splits blocks if remaining space > 16 bytes
- Thread-safe with spinlock
kernel/src/mm/heap.c:52-87
kfree
Free kernel memory.Pointer returned by
kmalloc().- Automatically detects slab vs. free-list allocation
- Coalesces adjacent free blocks
- Thread-safe with spinlock
kernel/src/mm/heap.c:89-120
Memory Layout
Performance Characteristics
Physical Allocation
Time: O(1) amortizedFree hint avoids full bitmap scan.
Page Mapping
Time: O(1)4-level lookup, allocates missing tables.
kmalloc (small)
Time: O(1)Slab allocator for ≤2048 bytes.
kmalloc (large)
Time: O(n)Free-list scan, n = number of blocks.
Security Features
- KASLR: Kernel Address Space Layout Randomization
- ASLR: User-space address randomization (per-process
mmap_next) - NX Pages: No-execute protection via
PAGE_NXflag - COW: Copy-on-write for fork (software flag defined)
- ASLR:
kernel/src/proc/process.c:72-74 - KASLR:
STATUS.md:20
Related APIs
- Scheduler - Uses page tables for context switch
- VFS - Uses kmalloc for file descriptors
- Process Management - Uses
paging_clone_*for fork