menios icon indicating copy to clipboard operation
menios copied to clipboard

Design formal per-process virtual memory layout system

Open pbalduino opened this issue 4 months ago • 0 comments

Goal

Lock down a consistent user address-space layout so every process sees the same virtual windows and guard gaps. Currently the kernel picks ad-hoc bases and lacks canonical descriptors.

Current State

The system currently has fragmented address space management:

  • Process helpers still derive addresses from PID-strided helpers (src/kernel/proc/proc.c:800-819)
  • Stack registration happens manually during proc_create_user/proc_exec_image (src/kernel/proc/proc.c:1982-2059, src/kernel/proc/proc.c:1406-1496)
  • ELF loader maps PT_LOAD segments exactly where the binary requests (src/kernel/user/elf_loader.c:68-122)
  • Heap growth: A userland brk/sbrk shim now exists (#423) backed by mmap(MAP_ANONYMOUS). Native kernel heap regions could still be added for tighter integration.
  • Anonymous mmap walks a hard-coded [0x40000000, 0x80000000) window tracked by mmap_base/mmap_next (src/kernel/mem/kmmap.c:73-138)

Definition of Done

1. Create Formal Layout Definition

  • Add shared vm_layout.h describing text/rodata/data/heap/stack/mmap windows
  • Insert guard pages between regions
  • Document in docs/architecture/per_process_vm.md:15-18 and docs/architecture/mem.md:55-58

2. Rewrite Process Creation Flow

  • Replace PID-strided address derivation in src/kernel/proc/proc.c:800-819
  • Update proc_create_user/proc_exec_image to consult shared layout (src/kernel/proc/proc.c:1982-2059, src/kernel/proc/proc.c:1406-1496)
  • Pre-reserve each window and keep guard gaps intact

3. Fix ELF Loader

  • Predefine code/data slots in src/kernel/user/elf_loader.c:68-122
  • Validate incoming PT_LOAD segments against canonical layout
  • Ensure vm_region_add leaves surrounding guard pages unmapped

4. (Optional) Native Kernel Heap Region

  • Note: brk/sbrk now works via userland shim (#423)
  • If desired, carve out a kernel-managed grow-up heap region in the layout
  • Initialize proc->heap/brk pointers for kernel-side management
  • Let existing grow-up fault path populate pages on demand (src/kernel/user/vm_region.c:128-195)

5. Formalize mmap Range

  • Expose [0x40000000, 0x80000000) range formally in layout
  • Enforce guard padding when vm_map inserts regions (src/kernel/user/vm.c:59-111)
  • Keep mmap cursor within bounds

6. Update Teardown and Cloning

  • Extend vm_region_t to represent reserved vs committed spans (for guard enforcement)
  • Ensure proc_release_user_memory disposes whole windows cleanly (src/kernel/proc/proc.c:256-275)
  • Ensure vm_clone duplicates windows properly (src/kernel/user/vm.c:300-362)

Next Steps After Layout Lands

  1. Update userland/libc expectations (heap/brk already functional via #423)
  2. Add regression tests that probe guard pages
  3. Document the finalized map for future features (SMP, demand paging)

Dependencies

  • Issue #28: Per-process virtual memory management
  • Existing virtual memory infrastructure

Related Issues

  • #423: brk/sbrk compatibility shim (CLOSED - provides userland heap)
  • This is part of the per-process virtual memory management overhaul (issue #28).

pbalduino avatar Sep 26 '25 18:09 pbalduino