refactor(memory): complete PMM/VMM/LMM rewrite - bitmap allocators, bug fixes, hardening #40

Merged
t3hn3rd merged 1 commits from feature/memory-allocation-optimization into develop 2026-03-04 19:34:46 +00:00
Owner

PMM Rewrite:

  • Replace ~8KB record array with compact bitmaps
    (PhysPresent/PhysAlloc/PhysScanned, 128B each)
  • Add PhysOwner[0..1023] for per-block ownership tracking
  • new_block: O(1) amortised via NextFreeHint + dword-level BSF scan,
    returns 0 as OOM sentinel
  • free_block: O(1) direct bit clear with double-free detection
  • alloc_block: validates PhysPresent and PhysAlloc bits before allocation
  • Add pmm_free_blocks/pmm_total_blocks stats functions

VMM Correctness Fixes:

  • Fix free_page block extraction (Address SHR 10, was using
    raw Address field)
  • Fix invlpg operand (use virtual address, was passing PDE index)
  • Clear PDE entry on free_page before returning block to PMM
  • Remove map_page double-write (delegate to map_page_ex only)
  • Add OOM propagation (new_page returns false if PMM returns 0)
  • Fix missing pop_trace in new_page error path

LMM Rewrite:

  • Replace per-entry record array with bitmap allocator
    (93% vs 43% page efficiency)
  • Layout: Header(24B) + Bitmap(64512B) + Padding + Data at 0x10000
  • SIZE_PREFIX=16 for 16-byte aligned returns (SSE MOVAPS requirement)
  • Next-fit bitmap scan with roving hint (NextFree) and dword-level
    fast skip
  • Large alloc sentinel (LARGE_ALLOC_MAGIC=$FFFFFFFE) so kfree detects
    klalloc'd memory
  • Implement klfree (was previously a no-op - pages could never
    be returned)
  • Implement try_release_page (return fully-free heap pages to VMM/PMM)
  • Fix interrupt safety: stack-balanced pushfd/cli/pop +
    restore_if (sti only)
    Fixes Bad TSS caused by popfd restoring dangerous EFLAGS bits (NT, IOPL)
  • Zero memory via rep stosd instead of byte-by-byte loop

Hardening:

  • kalloc OOM now triggers BSOD instead of returning nil
    (90% of callers don't check)
  • Double-free detection in kfree (verify bitmap bits set before clearing)
  • Guard bytes ({$IFDEF DEBUG_LMM}): $DEADBEEF sentinel for buffer overrun detection
  • kpalloc: bounds check (block >= 1024 -> GPF) + syslog MMIO mapping audit log

Observability:

  • Add MEMINFO command as src/prog/meminfo.pas (moved from inline kernel.pas)
  • Displays multiboot memory, PMM block stats, LMM heap stats
  • Register via progmanager.pas following standard prog pattern

Files changed:

  • src/pmemorymanager.pas
  • src/vmemorymanager.pas
  • src/lmemorymanager.pas
  • src/kernel.pas
  • src/progmanager.pas
  • src/prog/meminfo.pas
PMM Rewrite: - Replace ~8KB record array with compact bitmaps (PhysPresent/PhysAlloc/PhysScanned, 128B each) - Add PhysOwner[0..1023] for per-block ownership tracking - new_block: O(1) amortised via NextFreeHint + dword-level BSF scan, returns 0 as OOM sentinel - free_block: O(1) direct bit clear with double-free detection - alloc_block: validates PhysPresent and PhysAlloc bits before allocation - Add pmm_free_blocks/pmm_total_blocks stats functions VMM Correctness Fixes: - Fix free_page block extraction (Address SHR 10, was using raw Address field) - Fix invlpg operand (use virtual address, was passing PDE index) - Clear PDE entry on free_page before returning block to PMM - Remove map_page double-write (delegate to map_page_ex only) - Add OOM propagation (new_page returns false if PMM returns 0) - Fix missing pop_trace in new_page error path LMM Rewrite: - Replace per-entry record array with bitmap allocator (93% vs 43% page efficiency) - Layout: Header(24B) + Bitmap(64512B) + Padding + Data at 0x10000 - SIZE_PREFIX=16 for 16-byte aligned returns (SSE MOVAPS requirement) - Next-fit bitmap scan with roving hint (NextFree) and dword-level fast skip - Large alloc sentinel (LARGE_ALLOC_MAGIC=$FFFFFFFE) so kfree detects klalloc'd memory - Implement klfree (was previously a no-op - pages could never be returned) - Implement try_release_page (return fully-free heap pages to VMM/PMM) - Fix interrupt safety: stack-balanced pushfd/cli/pop + restore_if (sti only) Fixes Bad TSS caused by popfd restoring dangerous EFLAGS bits (NT, IOPL) - Zero memory via rep stosd instead of byte-by-byte loop Hardening: - kalloc OOM now triggers BSOD instead of returning nil (90% of callers don't check) - Double-free detection in kfree (verify bitmap bits set before clearing) - Guard bytes ({$IFDEF DEBUG_LMM}): $DEADBEEF sentinel for buffer overrun detection - kpalloc: bounds check (block >= 1024 -> GPF) + syslog MMIO mapping audit log Observability: - Add MEMINFO command as src/prog/meminfo.pas (moved from inline kernel.pas) - Displays multiboot memory, PMM block stats, LMM heap stats - Register via progmanager.pas following standard prog pattern Files changed: - src/pmemorymanager.pas - src/vmemorymanager.pas - src/lmemorymanager.pas - src/kernel.pas - src/progmanager.pas - src/prog/meminfo.pas
t3hn3rd added the ImprovementBug labels 2026-03-04 10:56:09 +00:00
t3hn3rd self-assigned this 2026-03-04 10:56:09 +00:00
t3hn3rd added 1 commit 2026-03-04 10:56:10 +00:00
refactor(memory): complete PMM/VMM/LMM rewrite - bitmap allocators, bug fixes, hardening
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
8b4335768b
PMM Rewrite:
- Replace ~8KB record array with compact bitmaps
  (PhysPresent/PhysAlloc/PhysScanned, 128B each)
- Add PhysOwner[0..1023] for per-block ownership tracking
- new_block: O(1) amortised via NextFreeHint + dword-level BSF scan,
  returns 0 as OOM sentinel
- free_block: O(1) direct bit clear with double-free detection
- alloc_block: validates PhysPresent and PhysAlloc bits before allocation
- Add pmm_free_blocks/pmm_total_blocks stats functions

VMM Correctness Fixes:
- Fix free_page block extraction (Address SHR 10, was using
  raw Address field)
- Fix invlpg operand (use virtual address, was passing PDE index)
- Clear PDE entry on free_page before returning block to PMM
- Remove map_page double-write (delegate to map_page_ex only)
- Add OOM propagation (new_page returns false if PMM returns 0)
- Fix missing pop_trace in new_page error path

LMM Rewrite:
- Replace per-entry record array with bitmap allocator
  (93% vs 43% page efficiency)
- Layout: Header(24B) + Bitmap(64512B) + Padding + Data at 0x10000
- SIZE_PREFIX=16 for 16-byte aligned returns (SSE MOVAPS requirement)
- Next-fit bitmap scan with roving hint (NextFree) and dword-level
  fast skip
- Large alloc sentinel (LARGE_ALLOC_MAGIC=$FFFFFFFE) so kfree detects
  klalloc'd memory
- Implement klfree (was previously a no-op - pages could never
  be returned)
- Implement try_release_page (return fully-free heap pages to VMM/PMM)
- Fix interrupt safety: stack-balanced pushfd/cli/pop +
  restore_if (sti only)
  Fixes Bad TSS caused by popfd restoring dangerous EFLAGS bits (NT, IOPL)
- Zero memory via rep stosd instead of byte-by-byte loop

Hardening:
- kalloc OOM now triggers BSOD instead of returning nil
  (90% of callers don't check)
- Double-free detection in kfree (verify bitmap bits set before clearing)
- Guard bytes ({$IFDEF DEBUG_LMM}): $DEADBEEF sentinel for buffer overrun detection
- kpalloc: bounds check (block >= 1024 -> GPF) + syslog MMIO mapping audit log

Observability:
- Add MEMINFO command as src/prog/meminfo.pas (moved from inline kernel.pas)
- Displays multiboot memory, PMM block stats, LMM heap stats
- Register via progmanager.pas following standard prog pattern

Files changed:
- src/pmemorymanager.pas
- src/vmemorymanager.pas
- src/lmemorymanager.pas
- src/kernel.pas
- src/progmanager.pas
- src/prog/meminfo.pas
t3hn3rd requested review from Aaron 2026-03-04 10:56:11 +00:00
Aaron approved these changes 2026-03-04 19:23:11 +00:00
t3hn3rd force-pushed feature/memory-allocation-optimization from 8b4335768b to f427b7bcc7 2026-03-04 19:23:53 +00:00 Compare
t3hn3rd scheduled this pull request to auto merge when all checks succeed 2026-03-04 19:25:59 +00:00
t3hn3rd merged commit aa374b6816 into develop 2026-03-04 19:34:46 +00:00
Sign in to join this conversation.