VirtuMMU is a C-based simulation of a memory management unit (MMU), a crucial hardware component in modern computer architecture. This project provides a hands-on implementation of virtual memory concepts, including page tables, virtual-to-physical address translation, and memory protection mechanisms. The codebase is structured for clarity and is accompanied by a suite of unit tests built with the Criterion framework to ensure the correctness of the MMU's behavior.
For more background on Linux environments and code I/O, see: Linux Operating Systems - rsiddiq.com.
- Virtual Address Translation: Translates virtual memory addresses into physical addresses using a single-level page table.
- Memory Protection: Implements page-level permissions for read, write, and execute operations, preventing unauthorized memory access.
- Page Fault Simulation: The system can identify and handle cases where a virtual page is not mapped to a physical frame.
- Dynamic Page Mapping: Provides functions to map virtual pages to free physical frames and set their access permissions.
- Comprehensive Test Suite: Includes a full suite of unit tests built with the Criterion framework to verify the functionality of the MMU.
- Cross-Platform Build: Uses a standard
Makefilefor easy compilation on Linux, macOS, and WSL.
VirtuMMU/
├── Makefile
├── README.md
├── unit_tests.c
├── src/
│ ├── common.h
│ ├── defines.h
│ ├── memory_management_unit.c
│ ├── memory_management_unit.h
│ ├── virtummu.c
│ └── virtummu.h
└── tests/
├── unittests_functions.c
└── unittests_mmu_pagetable.c
- GCC or Clang
makepkg-config(recommended)- Criterion test framework
sudo apt update
sudo apt install -y build-essential pkg-config libcriterion-dev
make # builds the 'unit_tests' binary
make test # runs the Criterion test suite# Install Xcode Command Line Tools
xcode-select --install
# Install dependencies via Homebrew
brew install criterion pkg-config
make
make testThe allocator's public interface is defined in src/virtummu.h.
MMU: Astructrepresenting the Memory Management Unit, containing pointers to the page table, physical memory, and page usage information, along with function pointers for core operations.PageTableEntry: A type definition, typically an unsigned 64-bit integer, that holds information about a page, including the physical frame number (PFN) and permission bits.
bool is_entry_valid(PageTableEntry pte): Checks if the page table entry is valid.bool is_read_enabled(PageTableEntry pte): Checks for read permissions.bool is_write_enabled(PageTableEntry pte): Checks for write permissions.bool is_execute_enabled(PageTableEntry pte): Checks for execute permissions.
PFN convert_PageTableEntry_to_PFN(PageTableEntry pte): Extracts the Physical Frame Number from a page table entry.PFN find_free_page(MMU m): Finds an unused page in physical memory.
PageTableEntry get_pagetableentry__MMU_pagetable(MMU m, VirtualAddress vpn): Retrieves the page table entry for a given virtual page number.void map_page__MMU_pagetable(MMU* m, VPN vpn, bool can_read, bool can_write, bool can_exec): Maps a virtual page to a physical frame with specified permissions.
Page* get_page(MMU m, VirtualAddress a, bool for_read, bool for_write, bool for_execute): Retrieves a page from memory, checking permissions.char read_byte(MMU m, VirtualAddress va): Reads a single byte from a virtual address.void write_byte(MMU m, VirtualAddress va, char val): Writes a single byte to a virtual address.
- The core logic resides in
src/virtummu.candsrc/memory_management_unit.c. - Unit tests are located in the
tests/directory and are run via theunit_tests.centry point. - The
Makefilehandles the compilation of all source files and links against the Criterion library.
To run the tests:
make testTo clean the build artifacts:
make clean