Memory & paging

A fixed ladder and a 4 KB frame allocator.

NebOS identity-maps physical RAM with 4 MB pages and hands out 4 KB frames from a bitmap. A small set of low-memory addresses is fixed by the boot path and the ABI; everything else — process stacks, window canvases, the ring-3 heap and the rootfs copy — is dynamic.

128 MB physical ceiling 4 KB frames 4 MB PSE pages identity map

Physical memory

The real-mode stage probes INT 15h E820 and writes the map to 0x1000; the UEFI loader synthesises the same structure from GetMemoryMap. mm.c starts with every page marked used, frees the E820 “usable RAM” ranges, then reserves the regions the kernel already owns.

Physical memory composition Of the 128 MB addressable window, the low 1 MB and an 8 MB VGA shadow reserve are unavailable, leaving about 119 MB for the frame allocator. 0x00000000 0x08000000 · 128 MB low 1 MB VGA shadow · 8 MB frame allocator · ~119 MB boot structures · kernel image · BIOS/UMA · DMA buffers The 8 MB reserve protects the shadow buffer; the allocator starts at 0x900000.
128 MB is a hard ceiling. The frame bitmap covers [0, 0x08000000); addresses above it are not mapped by the kernel page tables. The VGA shadow reserve is conservative: the actual buffer is smaller, but the gap keeps a future resolution bump from colliding with allocated pages.

Address landmarks

These addresses are part of the boot contract or the kernel ABI. Changing one requires updating the assembly, linker scripts, headers and this table together.

AddressPurposeWritten by / notes
0x500Boot info, 12 dwordsVESA stage, UEFI loader or kexec; bootinfo.h
0x600Boot framebuffer infoVESA stage or UEFI loader; vga.h
0x7C00Boot-sector load address / UEFI handoff stack topBIOS and UEFI trampoline
0x7E00Kernel load address and VESA-stage entrykernel.ld
0x800kexec and UEFI trampolineRelocated at runtime; below the kernel
0x900kexec/UEFI flat GDT and GDTRBuilt at runtime; below the kernel
0x1000E820 memory mapVESA stage, UEFI loader or kexec; parsed by mm.c
0x3000UHCI frame list1024 × 4 bytes, 4 KB aligned
0x4000ISA DMA bufferOne sector, used by the floppy driver
0x6000AP real/protected-mode trampolineSIPI vector 0x06; image ends below 0x6C00
0x6C00AP trampoline real-mode stack topGrows down
0x6F00AP startup info blockCR3, stack top, C entry, CPU index
0x60000Former rootfs stagingNo longer used by the loader; kernel BSS/stack may use it
_endKernel endkernel.ld asserts _end ≤ 0x90000
0x900000Boot pack (written directly by the legacy stage); dynamic stacks and kexec stagingFreed after ramfs_init takes its copy
0x100000VGA shadow buffer baseVGA_SHADOW_BASE; 8 MB reserved
0x40000000Ring-3 ELF link/load addressUSER_BASE, 2 MB window up to 0x40200000
0x50000000Ring-3 window canvasUSER_WIN_BASE, 4 MB slot
0x58000000WM shadow mappingWM_SHADOW_VA; ring-3 WM only
0x59000000WM content slotsWM_CONTENT_VA_BASE + index × 2 MB (WM_CONTENT_SLOT)
0x60000000Ring-3 anonymous heapUSER_HEAP_BASE, up to 32 MB

Paging & caching

paging.c builds an identity map of RAM with 4 MB pages through CR4.PSE, adds the framebuffer's LFB range, and enables CR0.PG and CR0.WP. Before switching, it clears translation controls a BIOS protected-mode call can leave behind: CR4.PAE/PGE/LA57/PCIDE/SMEP/SMAP and EFER.LME.

There is no cross-CPU TLB shootdown. MMIO mappings for another CPU's running process rely on not-present TLB entries not being cached; normal process teardown is unaffected because no two processes share an address space.

NVIDIA ION special case. On the ION the framebuffer is stolen DRAM and the VBE LFB is a BAR3 window into it. The BIOS leaves a 512 MB uncacheable MTRR over the PCIe apertures, so nvidia.c locates the framebuffer in the stolen VRAM and writes through that alias once a WC MTRR covers the stolen range. Everything else keeps the uncacheable LFB.

Frame allocator

mm.c keeps one bit per 4 KB page for the whole 128 MB window. The bitmap is not IRQ-safe; callers save and disable interrupts around allocations. The allocator returns:

At boot, mm_init reserves the low 1 MB, the 8 MB VGA shadow region, any LFB that happens to live below 128 MB, and the boot-time rootfs pack. The pack reservation is released by ramfs_init after it copies/inflates the pack into fresh pages.

Fresh pages are not zeroed. Structures whose fields are read before being written must be cleared explicitly; clear_proc() in process.c exists because a stale cr3 faulted on real hardware while QEMU's cleared RAM hid the bug.

kmalloc

kmalloc.c is a power-of-two heap allocator on top of the frame allocator. It is intentionally simple: allocation rounds to a size class, free lists are per class, and large objects become contiguous page runs. PCBs, command context and sys_fs.c file loads use it.

The kernel heap is separate from userspace. Ring-3 processes get one anonymous arena through SYS_MEM_ALLOC, SYS_MEM_GROW and SYS_MEM_FREE; commands manage it with malloc/free from libc/malloc.c. The syscalls are the backend in libc/ucmd_mem.c and are not called by application code.

Ring-3 memory

A ring-3 ELF is linked at 0x40000000. The image and its 8-page user stack live in a 2 MB window ending at 0x40200000. The kernel window server maps a window's RGB565 back buffer at 0x50000000, and the anonymous heap starts at 0x60000000.

Three separate rings. The image/stack window is fixed at link time; the canvas slot is one 4 MB mapping; the heap is a growable arena. Bar widths are relative to the 32 MB heap cap.
RangeSizeContents
0x40000000–0x402000002 MBELF segments, user stack top at 0x40200000 (8 pages, grows down)
0x50000000–0x504000004 MBOne window canvas mapping (RW, per window)
0x60000000–0x62000000up to 32 MBAnonymous heap; initial allocation zeroed, growth pages are not

Ring-3 commands treat that arena as a real heap: libc/malloc.c maps 64 KB on first use, grows in page-rounded steps with 64 KB slack, and uses boundary-tag blocks with a first-fit free list and immediate coalescing. heap_reset() discards everything and unmaps the arena; the browser calls it on navigation, so page-scoped allocations (body, rules, image bitmaps and decode arenas) go back to the frame allocator. A single allocation near 32 MB fails because block headers count against the same cap.

Kernel stacks are 4 KB pages from the frame allocator, with an 8-stack static fallback pool in BSS. The idle/bootstrap context uses the in-image 8 KB .stack section. The user stack is separate: 8 pages just below 0x40200000.

Rootfs pack lifetime

  1. The boot path places the gzip pack in RAM: the legacy stage copies it directly to 0x900000 with INT 15h AH=87h, UEFI at the top of RAM below 128 MB, kexec in its staging buffer.
  2. mm_init reserves [pack_addr, pack_addr + pack_size) so the frame allocator cannot reuse it.
  3. ramfs_init takes a private page-allocated copy, validates the NBROOT1 header and inflates the pack into it.
  4. mm_free_range releases the boot region when it is above the VGA shadow reserve. The private copy lives until ramfs_replace or reboot.

The ramfs holds at most 128 files and 8 MB of decompressed pack. A larger pack fails to load rather than partially mounting.

Limits & leaks

Source map