The helix-memory crate provides the entire memory stack — physical frame allocation, virtual address mapping, heap allocators, and address space management. Every layer is trait-based, so you can swap out any allocator without touching the rest of the kernel.
Memory is the first thing initialized after boot (stages 3-6), and every other subsystem depends on it. Understanding this crate is fundamental to working with Helix.
The physical allocator manages raw hardware memory frames. Helix provides four implementations with different tradeoffs — you pick the right one in your profile's helix.toml.
The virtual mapper creates the mapping between virtual addresses (what the CPU sees) and physical frames (actual RAM). It works hand-in-hand with the HAL's page table trait.
subsystems/memory/src/virtual_memory/mod.rs
1116rust
1
bitflags!{
4 refs
pubstructPageFlags:u64{
3
constPRESENT=1<<0;
4
constWRITABLE=1<<1;
5
constUSER=1<<2;
6
constWRITE_THROUGH=1<<3;
7
constNO_CACHE=1<<4;
8
constACCESSED=1<<5;
9
constDIRTY=1<<6;
10
constHUGE=1<<7;
11
constGLOBAL=1<<8;
12
constNO_EXECUTE=1<<63;
13
}
14
}
15
4 refs
implPageFlags{
17
pubconstKERNEL_READ:Self;// PRESENT
18
pubconstKERNEL_WRITE:Self;// PRESENT | WRITABLE
19
pubconstKERNEL_CODE:Self;// PRESENT
20
pubconstUSER_READ:Self;// PRESENT | USER
21
pubconstUSER_WRITE:Self;// PRESENT | USER | WRITABLE
Each process gets its own AddressSpace that tracks all virtual memory regions. The address space provides POSIX-compatible memory operations — mmap, munmap, mprotect, and brk.
The kernel heap is what alloc::vec!, Box::new(), and all dynamic allocations use. The GlobalHeap struct wraps a dyn HeapAllocator and registers itself as Rust's #[global_allocator].
Every memory operation returns MemResult<T>. These errors are descriptive enough for the self-healing system to decide on recovery strategies.
Error
Description
Recovery
OutOfMemory
No frames or pages available
Trigger memory pressure event
InvalidAddress
Address outside valid range
Return error to caller
NotMapped
Virtual address has no mapping
Page fault handler
AlreadyMapped
Page already has a mapping
Unmap first, then remap
InvalidAlignment
Address not aligned to page boundary
Caller bug — align up
PermissionDenied
Insufficient capability rights
Check capability token
During early boot (stages 1-3), the BumpAllocator provides a 4MB static heap. It's blazing fast (O(1) allocation) but never frees — perfect for the temporary allocations needed to get the full memory subsystem running. Once the buddy allocator is ready, the bump allocator is retired.