Skip to content

Memory Management

kazah-png edited this page Jul 11, 2026 · 3 revisions

Memory Management

NyxOS uses a layered memory strategy: bitmap physical allocator, 4-level paging, kernel heap, and slab caches.

See also: Architecture, Boot Process, Process Management, Security

Physical allocator (memory.c)

Bitmap tracking each 4 KB page, up to 512 MB max RAM. alloc_page() / free_page().

4-level paging (paging.c)

PML4 → PDPT → PD → PT → 4 KB page. Higher-half kernel at PML4[511]. Identity-mapped first 64 MB.

  • Demand paging: PTE bit 9 (PTE_DEMAND) — allocate zeroed page on first touch via #PF handler
  • Copy-on-write: PTE bit 10 (PTE_COW) — private copy on write fault via #PF handler (used by fork(), see Process Management)
  • NX bit: Non-executable user stack/data pages
  • CR0.WP: Supervisor writes to read-only pages fault (required for COW)
  • vm_handle_fault: resolves user page faults (demand, COW, file-backed mmap slice, lazy sbrk heap)
  • vm_protect_range: rewrites present-page flags for mprotect (writable per PROT_WRITE, NX unless PROT_EXEC)

User-space VM

  • mmap (mmap.c): anonymous MAP_PRIVATE with demand-zero pages; file-backed snapshots into per-VMA buffers (copied from file offset)
  • mprotect: change protection of mapped ranges (updates both present PTEs and VMA prot)
  • munmap: free present pages (refcount-aware) and drop VMAs; file buffers freed with the VMA
  • fork COW: clone_page_directory_cow + vm_handle_fault — shared until first write (see Process Management)
  • Lazy sbrk: break moves without allocating; pages materialise on first touch via the #PF handler

Kernel heap (heap.c)

16 MB heap with free-list, header/footer metadata, coalescing. kmalloc/kfree with slab fallback.

Slab allocator (slab.c)

Fixed-size caches: 8, 16, 32, 64, 128, 256, 512, 1024 bytes. kmalloc falls back to heap when no slab fits.

Clone this wiki locally