notes

Physical memory

The bottom of the memory hierarchy. Page tables (milestone 4), the kernel heap, DMA buffers (milestone 8), and user process pages (milestone 7) all ask this for memory, and there is nothing underneath it to ask.

The allocator itself is the page_frames crate, and its nifeos/nife:crates/page_frames/README.md states the bitmap, the all-used start and the rounding for a reader of the crate alone. This note keeps the kernel's side: where the RAM comes from, where the bitmap goes, and the order the boot declares regions in.

Two questions

  1. Where is the RAM? Answered by the device tree, which the machine hands us in x0 (boot-protocol.md).
  2. Which parts of it are ours to give away? That's this note.

Bitmap, not free list

The classic hobby-OS answer is a free list: link each free page to the next by writing a pointer into the free page itself. Zero metadata overhead, O(1) alloc and free, genuinely elegant. It's what xv6 does.

We use a bitmap (one bit per frame, 1 = used). Two reasons:

Contiguity. A free list cannot answer "give me 8 physically adjacent frames." A device doing DMA reads physical addresses directly, with no MMU in the way to hide a scattered buffer, so virtio at milestone 8 needs genuinely contiguous memory. Retrofitting that means throwing the free list away.

Testability. A free list stores its metadata inside the memory it manages, so testing it means handing it real memory and doing unsafe pointer writes. A bitmap's logic is pure: given a bitmap and a request, which frame? We test it exhaustively on the host with no memory at all, in milliseconds, with no emulator (DECISIONS §7).

The cost is 1 bit per frame: 32 KiB of bitmap per GiB of RAM. Or, for QEMU's 128 MiB: 4 KiB. One frame of overhead to manage 32768 frames. Cheap.

The bootstrap problem

The allocator needs memory for its bitmap. There is no allocator.

The way out: carve the bitmap, by hand, out of the very memory it is about to manage, and then have the allocator reserve those frames from itself. place_bitmap scans RAM in order for the first frame-aligned run that clears every forbidden region, jumping past the end of whatever a candidate hits. It does not simply take the memory after __image_end: image_size in the arm64 Image header stops at __stack_top, so nothing past the image is memory we told the bootloader we wanted. QEMU happens to place the device tree and the initial ramdisk 64 MiB higher, and other firmware need not.

The allocator's first act is to allocate itself.

Guilty until proven innocent

PageFrameAllocator::new marks every frame used. Nothing is handed out until someone explicitly says "this address range is real RAM."

This ordering is not fussiness. If the bitmap defaulted to free, the allocator would cheerfully hand out the MMIO hole at 0x0900_0000 and give you the UART's control registers as scratch space. Default-used means an unknown address is never allocated, and the worst case is wasted memory rather than a machine that writes garbage to a device.

So memory::bring_up_page_frames goes:

1. everything used
2. mark_free()  each RAM region the machine description reports
3. mark_used()  each forbidden region: the kernel image, the device tree blob (it sits in
                the RAM just freed), the initial ramdisk, every firmware reservation
4. mark_used()  the bitmap itself

Free first, reserve second. Reserving has to win.

The rounding asymmetry, which is the whole ballgame

mark_used rounds the start down and the end up. It claims every frame the region so much as touches. mark_free does the opposite: it rounds the start up and the end down, and frees only the frames lying wholly inside the region, so a region holding no whole frame frees nothing.

Consider: our image ends at 0x4009_7010. That is not frame-aligned. The frame at 0x4009_7000 holds the last 0x10 bytes of the kernel, and 4080 bytes of nothing.

Round that end down and the frame is declared free. The allocator hands it out. Something writes to it. The tail of the kernel is silently overwritten, and the crash lands somewhere else entirely, much later, in code that did nothing wrong.

Freeing runs the same argument the other way. Nothing obliges the x86_64 firmware memory map to report usable regions on frame boundaries, and a frame at an unaligned end of one also holds bytes nobody said were RAM. Free that frame and the allocator hands those bytes out.

Over-reserving or under-freeing wastes at most 4 KiB at each end of a region. Under-reserving or over-freeing corrupts whatever owns the other bytes. The asymmetry in the cost is why the asymmetry in the rounding.

There is a host test for exactly this shape (mark_used_claims_partially_covered_frames) and a kernel test that states the invariant directly (every_frame_of_the_kernel_image_is_reserved, which walks every frame of the image and asserts each is marked used). On the freeing side, a host test covers each unaligned shape (mark_free_of_an_unaligned_start_keeps_the_frame_it_starts_in and its neighbors). A Kani harness (mark_free_frees_only_whole_frames_inside_the_range) proves that every frame mark_free frees lies inside its range.

Span, not sum

We track every frame between the lowest RAM address and the highest, including any holes.

A board with RAM at 0x4000_0000 and again at 0x8_0000_0000 would give us a bitmap covering the enormous gap between them, and we simply never free those frames. A bit of wasted bitmap buys a much simpler index calculation: frame_index = (addr - base) / 4096, with no region table to search.

If that ever becomes expensive, the fix is a per-region allocator. It isn't expensive yet.

The span is RAM's widened by whatever the forbidden list reaches past it. On x86_64, QEMU's PVH loader places the initial ramdisk at a fixed offset below the top of guest memory. A boot whose ACPI tables grow (VT-d plus NVMe) moves e820's usable-RAM boundary down past the ramdisk's last few hundred bytes. The x86 front end still claims the whole ramdisk as forbidden, so the bitmap has to reach it, or is_used on those frames answers None (out of range) instead of Some(true), and the claim is silently clamped away. The extra frames start used and are never freed, because they are not in RAM. The top is rounded up to a whole frame, since a forbidden region need not be frame-aligned and page_frames_in truncates. A forbidden region inside RAM, the case on the other two architectures, leaves the span unchanged.

The seam with the machine description

memory::initialize is a front end per description: a device tree on aarch64 and riscv64, PVH and ACPI on x86_64 (arch::discovery::initialize). Both end in bring_up_page_frames(ram, forbidden), which does not care where the description came from. What crosses the seam is RAM and reservations. The device windows the device-tree front end also discovers (the interrupt controller, the RTC, the UART's interrupt line, the PCIe ECAM range) are stored in memory's statics. They stay None on a machine with no tree. x86_64 records its own through record_* calls: the UART's interrupt line, the PCIe windows, each VT-d or AMD-Vi unit and the framebuffer; it records no RTC and no GIC or PLIC (see x86-port/acpi-and-pci.md). The record_* calls for facts the front end also reserves (the initial ramdisk, the boot file) store and do not reserve. The caller is already building the forbidden list, and reserving in both places would double-reserve or let each side assume the other did it.

The low-water ledger

memory::allocation_pressure reports the fewest frames that were ever free at once on a boot, how many allocations were refused and the largest refused request. The frame ledger in testing.rs reads the allocator before the first test and after the last; what a boot runs out of is the middle, and an exhaustion shows up far from the tests that spent the memory. Measured 2026-09-26 at 484f3ebe plus #1347, from the closing frames: lines:

free before the first test lowest free free after the last refused
aarch64 57033 23654 34790 0
riscv64 57882 24682 35780 0
x86_64 58841 35719 46813 0

Every low sits about 11,100 frames under its own end-of-suite figure, during a progenitor test (the_report_child_builder_parses_an_elf_and_builds_a_running_child, or on riscv64 the_least_authority_demo_builder_passes_its_demo_an_argument). Both carve a 12,288-page building budget (memory_region::create(12288) in kernel/src/user.rs) and give it back. So the ledger at the low-water is frames.md's held list plus that one reservation: the held list growing moves the low, and the refusal count catches a load that no longer fits. Contiguity is not tracked per test: largest_free_run never moved from the first test's reading, and a bitmap scan per test overran the x86_64 17 GiB leg's time bound. A refused alloc_contiguous is the contiguity failure itself, and it is counted. The figure reports and does not gate, for sched::MAXIMUM_THREADS's reason: the held list grows on purpose, and the ledger's two end-of-suite gates hold the permanent part to account.

No heap, and an interrupt-safe lock

The kernel has no heap: no Vec and no Box (kmem is a fixed carve, see kernel-budget.md). The regions coming out of the device tree go into fixed-size arrays of MAXIMUM_REGIONS entries, because a fixed-size array is what you use when there is no allocator.

ALLOCATOR is an InterruptSafeMutualExclusionLock (DECISIONS §9 (locking: IrqSafeMutex, plus a discipline)), not a bare spinlock. An interrupt handler that tried to allocate while the interrupted code held a bare spinlock would spin forever for a lock only the code it interrupted can release. The discipline that goes with it: interrupt handlers do not allocate. They record what happened and defer the work.

BUGS


Add to this file as new memory concepts come up.