Memory Management - FreeWorld OS
Overview
The FreeWorld Memory Management system provides a complete, enterprise-grade memory management infrastructure. It includes:
- Paging System: 4-level paging, virtual memory, page-level permissions, swap support
- Frame Allocator: Physical page management (allocate/free physical pages)
- Heap Allocator: Kernel heap (kmalloc/kfree/krealloc)
- Memory Initialization: One-call initialization for all memory subsystems
- Advanced Features: Huge pages, overcommit, compression, swap files, memory-mapped files, shared memory, locking, protection, accounting, cgroups, pressure handling, defragmentation
Components
1. Paging System (paging.asm)
Location: kernel/memory/paging.asm
Complete paging system with page table management:
Features
- Page table setup (4KB pages, 4MB page tables)
- Virtual memory mapping
- Page-level permissions (read/write, user/supervisor)
- Identity mapping support
- Page fault handling preparation
Key Functions
| Function | Description | Parameters |
|---|---|---|
init_paging |
Initialize paging system | None |
map_page |
Map virtual page to physical page | Virtual address, Physical address, Flags |
unmap_page |
Unmap virtual page | Virtual address |
2. Frame Allocator (UEFI-Aware)
Location: kernel/memory/frame_allocator_uefi.asm (Primary)
Legacy: kernel/memory/frame_allocator.asm (Deprecated - E820-based)
Physical page frame allocator using UEFI memory map:
Features (UEFI Version)
- UEFI Memory Map - Uses EFI memory map from bootloader (replaces E820)
- Bitmap-based frame tracking
- Allocate/free physical pages
- Proper memory region marking (conventional, reserved, ACPI, etc.)
- Frame alignment (4KB pages)
- Marks special regions (kernel, page tables, framebuffer, memory map itself)
Key Functions
| Function | Description | Parameters | Returns |
|---|---|---|---|
init_frame_allocator |
Initialize frame allocator | Total memory size | None |
alloc_frame |
Allocate a physical page frame | None | Physical address or 0 on failure |
free_frame |
Free a physical page frame | Physical address | None |
3. Heap Allocator (heap.asm)
Location: kernel/memory/heap.asm
Kernel heap allocator with malloc/free interface:
Features
- kmalloc/kfree/krealloc functions
- Free list management
- Memory alignment
- Fragmentation handling
Key Functions
| Function | Description | Parameters | Returns |
|---|---|---|---|
init_heap |
Initialize heap allocator | None | None |
kmalloc |
Allocate memory | Size in bytes | Pointer or NULL |
kfree |
Free memory | Pointer | None |
krealloc |
Reallocate memory | Pointer, New size | Pointer or NULL |
4. Memory Initialization (memory_init.asm)
Location: kernel/memory/memory_init.asm
Centralized memory system initialization:
Initialization Order
- Frame allocator initialization (needs total memory size)
- Paging system initialization
- Heap allocator initialization
- Swap system initialization
- Advanced memory features initialization
Function
memory_system_init:
; Input: EAX = total memory size
; Initializes all memory subsystems in correct order
call init_frame_allocator
call init_paging
call init_heap
call init_swap_system
call huge_page_init
call memory_overcommit_init
call memory_compression_init
ret
Advanced Memory Features
1. Huge Pages Support
Location: kernel/memory/huge_pages.asm
Support for 2MB and 1GB huge pages for improved performance:
- 2MB Huge Pages: Page Directory Entry (PDE) level
- 1GB Huge Pages: Page Directory Pointer Table (PDPT) level
- Benefits: Reduced TLB misses, better performance for large allocations
2. Memory Overcommit
Location: kernel/memory/memory_overcommit.asm
Memory overcommit policies for flexible memory management:
- OVERCOMMIT_NONE: Strict (no overcommit)
- OVERCOMMIT_ALWAYS: Always allow overcommit
- OVERCOMMIT_HEURISTIC: Heuristic-based (default)
3. Memory Compression
Location: kernel/memory/memory_compression.asm
Kernel-level memory compression for better utilization:
- Algorithms: LZ4, ZSTD support
- Benefits: Better memory utilization, reduced swap usage
4. Swap to File
Location: kernel/memory/swap_file.asm
Swap file support in addition to swap partition:
- Multiple Swap Files: Support for multiple swap files
- Dynamic Management: Create/destroy swap files at runtime
5. Memory-Mapped Files
Location: kernel/memory/mmap_file.asm
Complete mmap implementation for file-backed memory:
- File Mapping: Map files to memory
- Synchronization: msync() support
- Shared/Private: MAP_SHARED and MAP_PRIVATE support
6. Shared Memory
Location: kernel/memory/shared_memory.asm
Complete IPC shared memory implementation:
- IPC Keys: Key-based shared memory segments
- Reference Counting: Track shared memory usage
- Permissions: Shared memory permissions
7. Memory Locking
Location: kernel/memory/memory_locking.asm
Memory locking to prevent swapping:
- mlock() - Lock memory region
- munlock() - Unlock memory region
- mlockall() - Lock all process memory
- munlockall() - Unlock all process memory
8. Memory Protection Enhancements
Location: kernel/memory/memory_protection.asm
Enhanced mprotect with additional features:
- Guard Pages: PROT_GUARD for no-access pages
- Copy-on-Write: PROT_COW support
- Fine-grained Protection: Page-level protection
9. Memory Accounting
Location: kernel/memory/memory_accounting.asm
Per-process and per-user memory accounting:
- Per-Process: Total, RSS, shared, text, data, stack, heap, mmap
- Per-User: User-level memory tracking
10. Memory Control Groups (cgroups)
Location: kernel/memory/memory_cgroups.asm
Memory control groups for resource limiting:
- Resource Limiting: Memory and swap limits
- OOM Control: OOM kill disable flag
- Process Tracking: Track processes in each cgroup
11. Memory Pressure Handling
Location: kernel/memory/memory_pressure.asm
Memory pressure detection and handling:
- Pressure Levels: None, Low, Medium, High, Critical
- Callbacks: Automatic pressure handling
- Thresholds: Configurable pressure thresholds
12. Memory Defragmentation
Location: kernel/memory/memory_defrag.asm
Memory defragmentation for better utilization:
- Fragmentation Detection: Monitor fragmentation level
- Defragmentation: Consolidate scattered free pages
Integration
The memory management system is integrated into the kernel via kernel/integration.asm:
; In kernel_subsystems_init: mov eax, [total_memory_size] ; Get memory size from E820 detection call memory_system_init ; Initialize all memory subsystems
Status: ✅ Integrated - Called during kernel initialization
Usage Examples
Allocate Memory
; Allocate 1024 bytes mov eax, 1024 call kmalloc ; EAX now contains pointer to allocated memory
Free Memory
; Free previously allocated memory mov eax, [allocated_ptr] call kfree
Allocate Physical Page
; Allocate a physical page frame call alloc_frame ; EAX now contains physical address (4KB aligned)
Implementation Details
Page Size
FreeWorld uses standard 4KB pages (4096 bytes). Page tables are 4MB each (1024 entries × 4KB).
Frame Alignment
All physical page frames are aligned to 4KB boundaries. The frame allocator ensures proper alignment.
Heap Size
The kernel heap is initialized with a default size (typically 1-4MB). This can be expanded as needed.