Definition
A memory management unit (MMU) is a hardware block that translates each address a program uses, called a virtual address, into a physical address, the actual location in memory, and checks the access rights recorded for that location. The operating system keeps the translations and rights in tables in memory, called page tables. The tables cover memory in fixed-size blocks called pages, typically 4 KiB each. Each process, meaning each running program, can have its own tables, so a process can reach only the memory that its tables map.
The idea
A general-purpose computer runs many programs at once, written by different people. Each program is built as if it owned memory: two programs may both keep data at address 4096. A bug in one program must not damage another program or the kernel, the core of the operating system that controls the machine. If all programs shared one set of addresses, each would need its own agreed place in memory, and a stray pointer in any program could write anywhere.
An MMU gives every process a private map. Think of a library in which each reader has a personal catalog. A reader asks for page 1 of their notebook, and the reader’s catalog says on which shelf that page is stored; two readers who both ask for page 1 are sent to different shelves. A shelf that is not in a reader’s catalog does not exist for that reader, so the reader cannot even name another reader’s shelves. The picture stops at two points. Only the kernel may write the catalogs, and the hardware must stop programs from doing so. And a catalog lists whole pages, so it cannot protect part of a page.
Most microcontrollers §6.1 have no MMU. Translation needs chip area and energy; page tables need memory that a small chip cannot spare, as the worked example computes; and translation makes the time of a memory access vary, which is harder to bound in a real-time system §6.34. On such a chip all code shares one address space, and protection comes from a memory protection unit §6.8 (MPU), which checks addresses without translating them, from software, or from nothing.
How it works
Let the page size be bytes; for 4 KiB pages, .
Split the address. The low bits of a virtual address are the offset inside the page. The remaining high bits are the page number.
Look in the TLB. The MMU first checks the translation lookaside buffer (TLB), a small, fast store of recently used translations. On a hit, translation adds little or no time.
Walk the page tables. On a miss, the MMU (on most processors, the hardware itself) reads the current process’s page tables, starting at the address held in a page-table base register:
CR3on x86 processors,TTBR0on Arm application processors, the kind used in phones. The tables have several levels, and each level is indexed by one group of bits of the page number.Read the entry. A page table entry holds a valid bit, the number of a physical frame (a block of physical memory one page long), and the rights: writable or not, usable by programs or by the kernel only, executable or not.
Check the rights. If the entry is valid and allows this kind of access at the current privilege level §6.10, the physical address is the frame’s start address plus the offset, and the access goes ahead.
Fault. Otherwise the MMU raises a page fault, an exception (a forced transfer of control to a handler) that runs the kernel. The kernel either repairs the cause and runs the instruction again, for example after loading the page from disk, or ends the process. On Unix-like systems the second case is the familiar segmentation fault, delivered to the process as the signal
SIGSEGV.Switch processes. When the kernel switches to another process, it loads that process’s table address into the base register. It either clears the TLB or relies on a tag in each TLB entry, called an address space identifier (ASID), that keeps the translations of different processes apart.
The mechanism protects itself through the privilege level, the hardware mode that decides what the running code may do. The page tables lie in frames that no program-accessible entry maps, and only code at the kernel’s privilege level can write the base register. A process therefore cannot change its own map.
Worked example
A toy system uses pages of bytes. A program in process A reads virtual address 4100. The page number is , where means rounded down, and the offset is . In hexadecimal (marked by the prefix 0x) the split is visible: 4100 is 0x1004, page 0x1, offset 0x004.
Process A’s table maps page 1 to frame 2, which starts at physical address . The physical address is (0x2004). Process B also uses virtual address 4100, and B’s table maps page 1 to frame 3, so B reaches (0x3004). The same virtual address names different memory in the two processes. If A’s entry for page 1 is marked read-only, a write by A to address 4100 raises a page fault, while a read succeeds. If A’s table has no valid entry for page 5, which covers virtual addresses to 24575, any access by A in that range faults. Frame 3, B’s memory, appears nowhere in A’s table, so A has no address that reaches it.
The size of the tables explains why microcontrollers do without them. A 32-bit address space with 4 KiB pages has pages. A flat table with one 4-byte entry per page needs bytes, which is 4 MiB per process: sixteen times the whole RAM of a microcontroller with 256 KiB. Tables with two levels avoid most of this. On 32-bit x86, a top-level table of 1024 entries (4 KiB) points to second-level tables of 1024 entries each, and each second-level table maps KiB, which is 4 MiB. A process that uses 8 MiB in one block starting on a 4 MiB boundary needs the top-level table and two second-level tables: KiB, which is 12 KiB.
Stated precisely
Let the page size be bytes. For a virtual address , the page number is and the offset is , the remainder of divided by . Let be the page table of the running process ; for some page numbers it holds a valid entry with a frame number and a set of rights. An access of kind (instruction fetch, read or write) to at privilege level :
goes to physical address if has a valid entry for whose rights allow at level ;
raises a page fault otherwise.
Isolation between processes follows: a process can reach physical frame only if some valid entry of maps a page to . The guarantee rests on five assumptions.
Only code at the kernel’s privilege level can change the page tables and the base register, and the tables lie in frames that no program-accessible entry maps.
The kernel maps one frame into two processes only when it means them to share it.
The kernel removes stale TLB entries whenever it changes or removes a table entry, so no old translation stays usable.
No other device writes the frames on a process’s behalf. A device that uses direct memory access §4.36 (DMA), copying data without the processor, uses physical addresses and bypasses the MMU, unless a separate IOMMU (input-output memory management unit) translates and checks device accesses.
The kernel code that maintains all of this is correct.
Limits and common mistakes
An MMU separates processes, not the parts of one process. A buffer overflow §4.4 inside a process can overwrite anything that process may write, and the smallest protected unit is a page, typically 4 KiB.
The kernel is trusted completely. Code running at the kernel’s privilege level can change any mapping, so a kernel bug undoes the isolation. The kernel is part of the trusted computing base §1.4 (TCB), the code on whose correctness the protection depends.
Devices bypass the MMU, as assumption 4 states.
Translation does not stop side channels §1.29, in which a program learns secrets from timing or other effects rather than from reading memory directly. The Meltdown attack, published in 2018 [396], let programs read kernel memory on affected processors: the processor carried out a forbidden load speculatively before the permission check took effect, and the loaded value left a trace in the cache that the program could measure.
Timing varies. A TLB miss costs extra memory reads for the table walk, which makes the worst case of a memory access harder to bound for real-time tasks.
No MMU does not mean no protection: an MPU, privilege levels and software sandboxing §2.30 remain available. The reverse holds too. An MMU that the system does not configure, or a system that runs all its code at the kernel’s privilege level, protects nothing.
Questions answered
Why is software sandboxing valuable on systems without an MMU?
Software sandboxing is valuable on a system without a memory management unit (MMU) because it is often the only way there to confine code to its own memory, and the way that gives the finest control. Without an MMU there are no per-process address spaces: every instruction of every component can name every byte. A memory protection unit (MPU), if the chip has one, can fence only a few regions, typically 8 or 16, whose sizes and positions follow fixed rules, and only if the untrusted code runs unprivileged and the kernel reprograms the regions at every thread switch.
A software sandbox confines code without that hardware. Either an interpreter §2.13 or virtual machine §2.3 (VM) runs the untrusted code one instruction at a time, or software fault isolation §2.41 (SFI) rewrites native code, machine code that the processor runs directly, so that every memory access is checked. Each memory access the confined code makes is then checked, to the byte, against bounds the sandbox sets. The sandbox also decides which functions the code may call and can limit how many instructions it runs, and it can validate a program before running it. The same sandbox works on every microcontroller. The costs are slower execution, because of interpretation or added checks, and a larger trusted computing base (TCB): the sandbox’s own code must be correct for the protection to hold. Where an MPU exists, the two combine well: the MPU can fence the sandbox’s memory, so that a bug in the sandbox does less damage.
Related entries: Memory protection unit (MPU) (§6.8), Virtual machine (VM) (§2.3), Trusted computing base (TCB) (§1.4), Privilege level (§6.10), Sandbox (§2.30), Software fault isolation (SFI) (§2.41), Direct memory access (DMA) (§4.36), Side channel (§1.29). Further reading: [396].