Memory management (kernel/mm/)¶
Source: kernel/mm/
Three layers that turn "the RAM the firmware reports" into "memory a kernel object can use": a physical memory manager, a virtual memory manager and a heap. Everything else in the kernel allocates through them.
kmalloc / kfree / new / delete heap.cpp, common/heap/ small objects, any size
AddressSpace::allocatePages vmm.cpp 2 MB virtual pages
PhysicalMemoryManager pmm.cpp 2 MB physical blocks, 4 KB frames
What is it for?¶
A CPU in long mode never uses physical addresses directly: every access goes through the page tables, which translate virtual addresses to physical ones. So there are two separate questions:
- Which physical memory is free? The firmware hands over a memory map (via multiboot2): "these ranges are RAM, these are reserved". Some of the RAM already holds the kernel. The physical memory manager (PMM) keeps track of what is free and hands out pieces of it.
- Where does that memory appear in the address space? The virtual memory manager (VMM) writes page table entries so that a range of virtual addresses points at the physical memory the PMM gave out.
On top, kernel code wants new Thread(...) of 200 bytes, not "a 2 MB page".
The heap cuts pages into small allocations.
How could it be designed?¶
Physical memory¶
| Approach | How | Plus | Minus | Used by |
|---|---|---|---|---|
| Bitmap | one bit per page/block, scan for a 0 | tiny, simple, easy to reserve ranges | allocation scans; slow for huge RAM | many hobby OSes, early Linux |
| Free list | free pages linked through themselves | O(1) alloc/free | needs the memory mapped to write the links; contiguous runs are hard | xv6 (kalloc) |
| Stack of free frames | array of free addresses | O(1) | memory for the stack, no contiguity | some hobby OSes |
| Buddy allocator | power-of-two blocks, split and merge | fast, contiguous ranges, little fragmentation | more code | Linux (page_alloc) |
Page size and table access¶
| Question | Options |
|---|---|
| Page size | 4 KB (fine grained, 4 table levels) · 2 MB (3 levels, fewer tables) · 1 GB |
| How the kernel edits page tables | identity map them · recursive mapping (a PML4 entry points at the PML4 itself) · temporary window (map one table at a time) · physmap: map all physical memory at a fixed offset (Linux, FreeBSD) |
| Page size for user programs | 2 MB like the kernel (simple, few tables; coarse protection) · 4 KB (fine grained, needed for copy-on-write, fork, mmap) |
Heap¶
How a heap cuts pages into blocks (free list, size classes, buddy, slab) has a README of its own: common/heap/README.md. The kernel and user programs share that code.
How atlOS does it¶
PMM: bitmap of 2 MB blocks (pmm.cpp). The simplest structure that can
also reserve arbitrary ranges (the kernel image, the multiboot info). One
bit per 2 MB block; 16 KB of bitmap covers 256 GB. Allocation is first fit:
scan for n zero bits in a row. The bitmap storage is a constructor argument,
so the self tests build managers over a fake 128 MB "machine".
-
Everything starts in use:
addRegion(base, size)frees the whole, aligned blocks inside a range,reserveRegion(base, size)marks every block it touches as used again. -
Memory::populateFromMultiboot(memory.cpp) frees theAVAILABLEregions, then reserves0 .. kernel_physical_end(the firmware reports the kernel's memory as available too; without the reservation the PMM would hand out the kernel's own pages). - Block 0 is never handed out, its address would read as
nullptr. freeBlocksrejects unaligned addresses and double frees;freeBlockCount()lets the self tests detect leaks.- Page tables are 4 KB, a whole 2 MB block for each would waste 2044 KB. So the PMM also hands out 4 KB frames, cut from 2 MB blocks ("frame pools", up to 16; a pool that becomes empty gives its block back).
VMM: 2 MB pages + a physmap (vmm.cpp). With 2 MB pages there are only
three table levels (PML4 → PDPT → PD) and far fewer tables to manage; user
programs use 2 MB pages too.
To edit a table the kernel needs a virtual address for it, but entries hold
physical addresses. The physmap solves that once: all physical memory
(up to 64 GB) is mapped at physmapBase (0xFFFF800000000000, PML4[256]),
so the table at physical p is at physmapBase + p, whichever address space
it belongs to and whether it is loaded or not. Its own 64 page directories
live in the kernel image (.bss, 256 KB): they have to exist before the
physmap does.
Every address space has the same layout:
PML4[0] identity map of the first megabytes (boot) kernel only, shared
PML4[1..255] user programs: userBase 0x0000008000000000 per address space
PML4[256] physmap: physical memory at 0xFFFF800000000000 shared
PML4[511] kernel image, heap, thread stacks (-2 GB) shared
A new address space copies the kernel's entries 0 and 256..511, so they point at the same PDPTs: a kernel page mapped later shows up everywhere. That is also why the kernel half can not get a new PDPT later (it would exist in one address space only; a mapping that needs one panics); the two it needs are there from boot.
Memory::AddressSpace holds just the physical address of its PML4:
AddressSpace(pmm)is the kernel's: it takes over boot.S's tables and builds the physmap.AddressSpace(pmm, kernel)is a new, empty one for a user program;activate()loads it into CR3.- Kernel pages:
allocatePages(n)findsnfree virtual pages above the kernel image and backs each with a PMM block;mapPhysicalmaps a given physical page without the PMM;mapDevicemaps the page around a device's registers at a free address, uncached (cache disable and write through bits), for MMIO such as the APICs: the physmap is cached, and a device must see every read and write. - User pages:
allocateAt(virt, n, flags)maps zeroed blocks in the user range,WritableandUser(ring 3) as asked; the tables above get the user bit, the PD entry decides.isUserAccessible(virt, size, write)is what system calls check user pointers with. - An unused bit of the entry (bit 9) marks pages whose block the address
space owns: unmapping or destroying it gives those back, never a
mapPhysicalpage. The destructor also frees its PDs and PDPTs. translate(virt, phys)walks the tables without faulting andforEachMappingreports every mapped page;walk(virt, steps)returns every entry it reads on the way (the shell'spt); the shell'smd,mwandvmmapuse them.
Physmap or recursive mapping¶
A common alternative is a recursive slot: one PML4 entry points back at the
PML4 itself, so addresses through that slot land on the tables (with slot
510, [510][510][i][j] is the PD for (i, j)). It needs no memory and no
mapping of physical memory, but it only shows the tables loaded in CR3: to
build a second address space for a program the kernel would switch CR3 to it
(and back) for every edit, or point a second recursive slot at the other
PML4. The physmap costs 256 KB of tables and makes every table, of every
address space, one addition away; atlOS needs exactly that for user
programs.
Heap: a free list on the address space's pages (heap.cpp).
Common::Heap (common/heap/) gets its pages
from a Memory::AddressSpacePages, which asks AddressSpace::allocatePages
for 2 MB pages and gives them back when a whole chunk is free again.
kmalloc and operator new can not be handed an object, so kernelMain
constructs both and binds them once (Memory::setKernelHeap, which also
binds the kernel address space for Thread(entry, arg)); a kmalloc before that
panics. A boundary global, see docs/style.md. kfree
of a pointer the heap did not hand out, or freed twice, panics. No lock: the
scheduler is cooperative and interrupt handlers must not allocate.
Deliberately not done¶
- No 4 KB pages: user programs use 2 MB pages as well, so even a tiny program
takes a few MB (code, data and stack need separate pages: protection is per
page). 4 KB come with copy-on-write,
forkormmap. - The physmap covers 64 GB; the PMM manages no more than that.
- No NX (no-execute) bit yet; no
PCID, so a CR3 switch flushes the TLB. - No buddy allocator, no NUMA, no swapping, no copy-on-write.
- The PMM scan is linear; with 256 blocks (512 MB) nobody notices.
Flow: new Thread(...) down to a page table entry¶
sequenceDiagram
participant C as Caller
participant H as Heap (kmalloc)
participant V as AddressSpace
participant P as PhysicalMemoryManager
participant T as Page tables
C->>H: kmalloc(200)
alt a free block is large enough
H-->>C: pointer inside it (the rest of the block stays free)
else needs more memory
H->>V: AddressSpacePages::allocatePages(1) -> allocatePages(1)
V->>V: find a free 2 MB virtual page
V->>P: allocateFrame() (only if a new PD is needed)
V->>P: allocateBlocks(1)
P-->>V: physical address of a 2 MB block
V->>T: write the PD entry (present, writable, 2 MB)
V-->>H: virtual address
H-->>C: pointer inside the new chunk
end
Boot: from the memory map to a working heap¶
flowchart TD
A[GRUB: multiboot2 memory map] --> B[PhysicalMemoryManager: everything used]
B --> C[addRegion for each AVAILABLE range]
C --> D[reserveRegion: kernel image, multiboot info]
D --> E[AddressSpace: physmap in PML4 256]
E --> F[Heap on AddressSpacePages, setKernelHeap: kmalloc can now get pages]
Tests¶
kernel/tests/selftest.cpp:
- PMM blocks / frames (isolated): a 64 block manager: regions, reservations, exhaustion, double frees, first fit, frame pools and their limit.
- PMM from a multiboot memory map: a hand built multiboot2 structure.
- VMM: allocate, write, free, translate, map the same physical page twice.
- VMM translate and mappings: the physmap, physical 0 vs unmapped, the mapping list.
- Address spaces: a second one shares the kernel half and the identity
map, user pages only in the user range, zeroed, built while inactive and
seen after
activate(), flags, everything given back by the destructor. - Heap: sizes from 1 byte to 3 MB,
krealloc,kcallocon the kernel heap; the free list itself is tested on its own (see common/heap/README.md).
References¶
- OSDev Wiki: Paging, Page Frame Allocation, Detecting Memory (x86), Higher Half Kernel
- Intel SDM Vol. 3A, chapter "Paging" (4-level paging, PS bit for 2 MB pages)
- Operating Systems: Three Easy Pieces: "Paging: Introduction", "Free-Space Management"
- xv6 book (MIT 6.1810): chapter "Page tables"