The main difference between an MMU (Memory Management Unit) and an MPU (Memory Protection Unit) lies in their capabilities for managing memory access. An MMU provides full virtual memory support with address translation and page-level protection. An MPU offers simpler memory protection without address translation.
An MMU is a hardware unit that maps virtual addresses used by the CPU to physical addresses in main memory. This provides an abstraction layer that allows the CPU to operate in a virtual address space that is isolated from the physical memory layout. The key functions of an MMU are:
- Address translation – Mapping virtual addresses to physical addresses using mechanisms like page tables.
- Memory protection – Preventing access to restricted memory regions by using access permission checks.
- Caching – Improving performance by using high-speed TLB caches to store popular address translations.
- Address spaces – Separating processes into distinct virtual address spaces for isolation and security.
The MMU allows the CPU to use contiguous virtual addresses while actual physical memory may be non-contiguous. It handles mapping between the two address spaces automatically. Virtual memory provided by the MMU has significant advantages:
- Processes can use large virtual address spaces beyond the available physical memory.
- Page-level memory protection prevents process interference and restricts bugs.
- Easy sharing of memory pages between processes via page tables.
- Process address spaces can be swapped in/out of memory by the OS.
The MMU is a complex piece of hardware typically found in general-purpose OS-based systems like desktops/servers. It requires significant hardware resources and OS support for handling page tables, TLB coherence, etc. The MMU provides full virtual memory capabilities essential for modern multi-tasking OS environments.
An MPU is a simpler form of memory protection mechanism without full virtual memory support. The key characteristics of an MPU are:
- Access control – Prevent unauthorized access to memory regions.
- No address translation – MPU works with physical addresses only.
- Static memory regions – MPU rules are defined at configuration time.
- Limited number of regions – Typical MPU supports 4-16 regions.
The MPU divides the physical address space into configurable protection regions with defined access permissions. Memory accesses are monitored by hardware and subjected to access permission checks. Any access violating the permissions will lead to a fault/exception. This allows simple access control protections to be implemented without a full MMU.
MPUs are commonly found in embedded systems like microcontrollers. They have relatively small hardware overhead and do not require complex OS page table management. The limitations are lack of virtual memory and fixed number of regions. MPUs work well for simpler embedded applications that do not need full MMU capabilities.
Key Differences Between MMU and MPU
The main differences between MMU and MPU are:
- Address translation – MMU does virtual to physical address translation, MPU works with physical addresses only.
- Page tables – MMU uses page tables managed by the OS, MPU has static regions configured at setup.
- Memory abstraction – MMU provides full virtual memory abstraction, MPU just partitions physical memory.
- Hardware overhead – MMU requires TLBs and other hardware, MPU has simpler hardware.
- OS involvement – MMU requires extensive OS support, MPU can work with simpler OS or bare-metal.
- Use cases – MMU used in desktop/server OS environments, MPU commonly used in embedded systems.
MMU Capabilities and Operation
The key capabilities provided by the MMU are:
- Address translation – The MMU maps virtual addresses to physical addresses using page tables. The CPU generates virtual addresses which are translated by the MMU hardware to get the actual physical memory location.
- Page-level protection – Page table entries include access permission bits that are checked on each memory access to enforce protection.
- Caching with TLBs – The TLB is a hardware cache that stores popular translations to avoid page table lookups. The OS must ensure coherence.
- Isolated address spaces – The MMU provides each process its own virtual address space for isolation and security.
The OS manages the page tables that contain the virtual to physical mappings. On a TLB miss for a virtual address, the MMU does a page table walk to retrieve the mapping and caches it in the TLB. The OS must ensure that page tables are coherent across all TLBs and caches in a multiprocessor system.
The MMU allows processes to access non-contiguous physical memory through contiguous virtual addresses. The OS handles actually allocating physical pages and wiring them into the virtual address space. Swapping out inactive pages to disk is also managed by the OS.
MPU Access Control Operation
The MPU performs memory access control checks as follows:
- The MPU is configured with a set of memory regions and associated access permission rules.
- On each memory access, the physical address is compared to the MPU regions.
- If the address matches a region, the access permissions are checked.
- If the access is permitted, it proceeds. Otherwise, an exception/fault occurs.
The MPU does not perform any address translation – it works directly with physical addresses already generated by the CPU. The limited number of MPU regions make it suitable only for simple access controls, not full OS virtual memory management.
MPU region permissions are typically configured during system initialization and remain static at runtime. The OS or applications can modify permissions by reconfiguring the MPU as needed. Some MPUs may also support dynamic reconfiguration at runtime.
Use Cases and Examples
Some common use cases for MMUs and MPUs are:
- General-purpose OS – The MMU is essential for mapping virtual memory, isolating processes, and efficient multi-tasking in desktop/server OS like Linux, Windows, etc.
- Real-time OS – An MMU provides virtual memory while an MPU gives static partitioning of physical memory suitable for real-time systems.
- Embedded OS – A simple MPU allows partitioning memory between trusted and untrusted code in embedded OS environments.
- Bare-metal/RTOS – MPU access permissions can protect critical code/data regions from corruption in bare-metal/RTOS applications.
Example MMU usage:
- Linux page tables manage complex virtual memory mappings and protections for processes.
- TLBs in x86 processors cache virtual to physical translations.
- ARM MMUs are configured by page tables set up by the OS software.
Example MPU usage:
- Cortex-M MPUs protect flash and SRAM memory regions from corruption.
- Memory protection units in microcontrollers safeguard critical system code/data.
- Autosar OS uses MPU to partition memory for automotive applications.
MMU vs MPU Memory Architecture
The MMU and MPU take different approaches to memory management:
- MMU – Provides a layer of abstraction between virtual and physical memory. The OS manages virtual memory which is mapped to physical memory using page tables. Memory protection works at the granularity of pages.
- MPU – Partitions the physical memory address space directly with access permission rules. The MPU controls access at the region granularity defined in its configuration.
With the MMU, the OS and software operate in virtual address spaces backed by physical memory that may be non-contiguous. The MPU directly controls access to physical memory regions without any address translation.
The MMU approach provides greater flexibility, security isolation, and ease of use through virtual memory abstractions. But comes at a higher hardware cost. The MPU approach is lightweight but lacks virtual memory capabilities.
The MMU requires significant hardware resources:
- Page table walk hardware – to traverse page tables for translations.
- TLBs – to cache translations for fast lookup.
- Extra chip area – for above hardware components.
The MMU also needs OS support for managing page tables and handling TLB coherence issues in multi-processor systems. This adds OS software overhead.
In contrast, the simpler MPU can be implemented with just a register set and comparators to check addresses against regions. This takes less hardware, making MPUs suitable for embedded devices.
When to use MMU vs MPU
Guidelines on when to use each technology:
- Use MMU for full virtual memory support on desktop/server OS environments.
- Use MMU for isolation and security between processes and kernel space.
- Choose MPU for simpler embedded systems where virtual memory is not needed.
- Prefer MPU for real-time applications which require static memory partitioning.
- MMUs have higher hardware cost, select MPU for lower-cost embedded devices.
Modern desktop/server processors have large TLBs and efficient MMUs tuned over many generations. For these systems, an MMU is most suitable. Embedded devices under resource constraints can benefit from a simpler MPU.
The MMU provides full-featured virtual memory via address translation, memory isolation and paging to disk. The MPU is a simple access controller for partitioning physical memory without translation. MMUs excel at managing memory in complex OS environments while MPUs are sufficient for simpler embedded systems.
Understanding the differences between MMU and MPU architectures enables selecting the right technology for a given application based on requirements for performance, flexibility, security and hardware cost.