Memory Management Unit
Memory Management Unit
(hardware, memory management)The virtual address space (the range of addresses used bythe processor) is divided into pages, whose size is 2^N,usually a few kilobytes. The bottom N bits of the address(the offset within a page) are left unchanged. The upperaddress bits are the (virtual) page number. The MMUcontains a page table which is indexed (possiblyassociatively) by the page number. Each page table entry(PTE) gives the physical page number corresponding to thevirtual one. This is combined with the page offset to givethe complete physical address.
A PTE may also include information about whether the page hasbeen written to, when it was last used (for a least recently used replacement algorithm), what kind of processes (user mode, supervisor mode) may read and write it, and whetherit should be cached.
It is possible that no physical memory (RAM) has beenallocated to a given virtual page, in which case the MMU willsignal a "page fault" to the CPU. The operating systemwill then try to find a spare page of RAM and set up a new PTEto map it to the requested virtual address. If no RAM is freeit may be necessary to choose an existing page, using somereplacement algorithm, and save it to disk (this is known as"paging"). There may also be a shortage of PTEs, in whichcase the OS will have to free one for the new mapping.
In a multitasking system all processes compete for the useof memory and of the MMU. Some memory managementarchitectures allow each process to have its own area orconfiguration of the page table, with a mechanism to switchbetween different mappings on a process switch. This meansthat all processes can have the same virtual address spacerather than require load-time relocation.
An MMU also solves the problem of fragmentation of memory.After blocks of memory have been allocated and freed, the freememory may become fragmented (discontinuous) so that thelargest contiguous block of free memory may be much smallerthan the total amount. With virtual memory, a contiguousrange of virtual addresses can be mapped to severalnon-contiguous blocks of physical memory.
In early designs memory management was performed by a separateintegrated circuit such as the MC 68851 used with theMotorola 68020 CPU in the Macintosh II or the Z8015used with the Zilog Z80 family of processors. Later CPUssuch as the Motorola 68030 and the ZILOG Z280 have MMUs onthe same IC as the CPU.