Skip to content
r-siddiqPublic

About

VirtuMMU is a C-based simulation of a memory management unit. It demonstrates virtual-to-physical address translation, page mapping, and memory protection with read, write, and execute permissions.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

VirtuMMU: A Virtual Memory Management Unit in C

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.

Features

  • 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 Makefile for easy compilation on Linux, macOS, and WSL.

Project Layout

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

Build Requirements

  • GCC or Clang
  • make
  • pkg-config (recommended)
  • Criterion test framework

Linux / WSL (Ubuntu/Debian)

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

macOS

# Install Xcode Command Line Tools
xcode-select --install
# Install dependencies via Homebrew
brew install criterion pkg-config
make
make test

API and Usage

The allocator's public interface is defined in src/virtummu.h.

Key Data Structures

  • MMU: A struct representing 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.

Core Functions

Page Helpers

  • 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.

MMU Helpers

  • 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.

Page Table Functions

  • 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.

End-to-End Functions

  • 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.

Development and Testing

  • The core logic resides in src/virtummu.c and src/memory_management_unit.c.
  • Unit tests are located in the tests/ directory and are run via the unit_tests.c entry point.
  • The Makefile handles the compilation of all source files and links against the Criterion library.

To run the tests:

make test

To clean the build artifacts:

make clean

About

VirtuMMU is a C-based simulation of a memory management unit. It demonstrates virtual-to-physical address translation, page mapping, and memory protection with read, write, and execute permissions.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages