The Physical Memory Manager (PMM) is one of the most foundational components is OS development – it manages actual RAM, not virtual abstractions.
What is the Physical Memory Management?
The Physical Memory Manager (PMM) is responsible for:
- Tracking which parts of RAM are free/used
- Allocating and freeing physical memory blocks (frames/pages)
- Providing memory to:
- Kernel
- Virtual Memory Manager (VMM)
- Devices drivers (DMA, buffers)
The PMM doesn't manage individual bytes of memory, rather it keeps track of pages. A page is a fixed size determined by the MMU: in the case of x86 this is 4096 (0x1000) bytes.
Memory Layout at Boot
When the OS boots, RAM is NOT fully free.
Some regions are already occupied:
- Kernel code/data
- Bootloader
- BIOS/UEFI structures
- Reserved hardware regions
Example Memory Map
From BIOS (e.g., via E820):
0x00000000 - 0x0009FC00 → Usable
0x0009FC00 - 0x000A0000 → Reserved
0x00100000 - 0x7FFFFFFF → UsablePMM must:
- Parse this map
- Only manage usable regions
Basic Unit: Frames (Pages)
Physical memory is dividied into fixed-size blocks:
- Usually 4 KB pages
- Called:
- Frames (physical)
- Pages (virtual)
Example:
If RAM = 4 GB:
4GB / 4KB = ~1 million framesData Structures Used
There are different ways on how to handle a PMM.
This is where design choices matter:
1 Bitmap (Most Common)
Idea:
Each frame = 1 bit
This is the simplest approach: one bit per page frame, where 0 = free and 1 = allocated. For a 4GiB machine (1,048,576 pages), this costs just 128 KB of RAM – negligible.
0 → free
1 → used (allocated)Example:
Frame: 0 1 2 3 4 5 6 7
Bitmap: 1 0 0 1 1 0 0 0Allocations:
Allocation is O(n) in the worst case – you must scan until you find a zero bit.
Freeing is O(1): just clear the bit.
- Scan for first
0 - Mark it
1
Pros:
- Very space efficient
- Simple
Cons:
- Slow for large memory (linear scan)
Why Do We Need a Physical Memory Manager?
When your OS boots, memory is:
- Partially used (kernel, BIOS, bootloader)
- Fragmented
- Unstructured
The bootloader gives us a memory map, but that's just information, not a management system.
So, PMM must answer:
- Which pages are free?
- Which pages are reserved?
- How do I allocate contiguous memory?
- How do I avoid overwriting critical regions?
Core Design: Bitmap Allocator
We would use a bitmap-based allocator.
Concept:
Divide memory into fixed-size pages (usually 4 KB):
Physical Memory → [Page0][Page1][Page2]...[PageN]Then track each page with a bit:
Bitmap → 1 0 1 0 0 1 ...
↑ ↑ ↑ ↑ ↑ ↑
Used/Free
Leave a comment
Your email address will not be published. Required fields are marked *
