moonshot

technical

jenna

jenna is moonshot's memory, who owns every physical page, how small allocations are carved out of big ones, and the page tables that decide what the addresses the kernel uses actually mean.

pages

the bottom layer hands out physical memory in 4096-byte pages, from the first usable region past the end of the kernel. a freed page goes onto a free list kept inside the freed pages themselves, and an allocation takes from that list first, so freed memory is reused.

kernel heap

kmalloc and kfree sit on top, for the many small pieces of memory the kernel needs. every block has a small header with its size and whether it is free, allocation takes the first block that fits, and a free merges the block with free neighbours. kfree refuses a pointer it never handed out and a block that is already free, loudly, since a quiet double free corrupts something unrelated much later.

page tables

jenna builds the kernel's page tables at boot from pages it allocated itself, mapping physical memory one to one. it also owns the page fault handler. a fault in the kernel gets a fresh zeroed page and the instruction runs again. a fault in a ring-3 program is a bad address of its own and ends the program.

every ring-3 program runs on page tables of its own, which share the kernel's entries and add a private range for the program's code, stack and heap, so two programs never share a page. when the program is gone, pt_free gives all of it back.