In This Article
What is MMU (Memory Management Unit)?
MMU, a short for Memory Management Unit, refers to that particular hardware component in a computer which handles the virtual memory. It is usually found in the Central Processing Unit or the CPU of the computer system.
The MMU finds out the location of the information when it receives a request for data. This location can be on a permanent storage device or in the Random Access Memory or RAM.
It typically uses a table to match the physical memory addresses with the virtual ones.
- The Memory Management Unit usually divides the virtual address space into pages and therefore is called the Paged Memory Management Unit as well.
- Memory management in the earlier microprocessor designs was done by a separate IC or Integrated Circuit but later on a separate MMU was placed in the CPU.
- The MMU alleviates the issues of memory fragmentation or discontinuity after the blocks are freed and allocated, making the largest continuous unit smaller than the entire amount.
- The memory holds a table called the Translation Look-aside Buffer or TLB to match and coordinate virtual addresses to physical addresses.
- The MMU helps the system run several tasks as separate programs in their own virtual memory space without needing to know the actual address used by it or other programs that are executed at the same time.
Understanding Memory Management Unit
Memory Management Unit is a particular hardware unit that manages virtual memory.
Since the MMU divides this memory into pages, it is also called a Paged Memory Management Unit or PMMU as well.
Ideally, all memory references pass through the MMU which is responsible mainly for translating virtual memory addresses into physical addresses.
In addition to managing virtual memory, the MMU also helps with several other things at the same time, such as:
- Memory protection
- Bus administration and
- Cache control.
In the simpler architectures, especially in the 8-bit systems, it also helps in bank switching.
The range of addresses or the virtual address space used by the CPU is divided by the MMU into different pages.
Each page has a size expressed as a power of 2 and is often a few kilobytes long, though a few may be longer.
The offset within the page or the bottom bits of the address are left unchanged and the upper bits of the address represent the virtual page numbers.
The memory protection ability of the MMU can protect against a software bug or page fault.
Even the operating system can use it to disallow access to the memory by a specific errant program.
Ideally, that is the main reason that the operating system allocates virtual address space to each program.
The same virtual memory space can be used for each program due to the MMU, which also allows working with an adjoining virtual memory map even when the physical memory is split.
Since this virtual address space is different from the real physical map of the system memory, it allows writing, compiling and even linking applications so that these can be run within the virtual memory space.
The hardware in a virtual memory system needs to translate the address to do this. This is issued by the CPU to a physical address in the system memory.
The most important bits of the virtual address are used by the MMU for indexing entries in the translation table and for finding out which particular block address is being accessed.
It also decodes the data of the virtual addresses into the physical addresses in the actual system. All these processes are visible to the application and are carried out automatically in the hardware.
Apart from translating addresses, the MMU is also responsible for managing permissions for memory access and ordering as well as caching policies for every area of the memory.
The working principle of MMU involves handling all data request inputs that pass through it and determining whether the data has to be retrieved from the RAM or the ROM.
It also involves translating virtual memory addresses into physical addresses by matching them in the table or TLB.
Ideally, the working of the MMU hardware involves three basic functions such as:
- Hardware memory management – This process regulates and monitors the use of RAM and cache memory by the CPU.
- Application memory management – This process involves allocating the necessary memory to each program and then recycling the memory space freed-up when the operation is completed.
- Operating system memory management – This process ensures that each of the running programs has a sufficient amount of memory for the objects and data structures.
Typically, it is responsible for matching virtual and physical addresses by looking them up in the TLB. The working process is completed in the following steps:
- Addresses are issued by the running CPU
- Data and instructions are obtained or put
- The addresses are intercepted by the MMU
- It is matched in the TLB with the physical address and
- The addresses taken from the list are replaced.
However, if the address does not match, it signals an interrupt to the processor. It then switches tables to continue the search.
Therefore, the basic function of the MMU is to keep all functions related to the memory operating most efficiently.
For this, the working process includes different functions, apart from the basics mentioned above, such as:
- Memory distribution – This is done before and after the system processes are executed by moving the resources to where they will meet the system requirements in the best possible way.
- Monitoring – This involves tracking all memory resources.
- Efficiency – This involves conserving main memory as well as other memory resources.
- System integrity – This is done by reducing the possibility of memory fragmentation and poor memory allotment.
- Data integrity – This ensures that the data is reliable by maintaining its integrity.
- Data risk – This eliminates the chances of the data getting corrupted.
- Cost control – This keeps the cost of all memory-related operations down.
MMU in OS
MMU in Operating systems helps to control and manage the functions of the primary memory of the system. It is used to ensure better system performance, more concurrency, and proper utilization of memory.
It helps in moving processes from primary to secondary memory and vice versa, and also helps in tracking the allocated and unallocated memory.
There are a few specific techniques followed by the MMU in OS for better management. However, the three most significant processes are as follows:
In this method, the primary memory of the system is divided into several blocks, called segments. Each of these segments has a fixed length.
The length of each segment decides the available free memory amount in it.
There is also a starting address in each segment which is called the base address.
The distance from this base address to the actual position of the data helps in determining the location of the data item stored in the segment. This distance is called the displacement value or an offset.
When data needs to be acquired from the segment, the actual address of it is calculated by adding the base address and the offset value, both of which are defined in the program instruction itself.
This is the process that plays a vital role in ensuring the proper implementation and utilization of virtual memory by the computer system.
This is very important since it allows using more memory than what is available in the computer hardware.
In paging, the process address space is divided into blocks of the same size. These blocks are referred to as pages.
The size of each page is a power of 2, and the value ranges between 512 bytes and 8192 bytes. The number of pages denotes the actual size of the process.
Any smaller division of the memory is also divided into blocks of equal size and equivalent to the pages. These are called frames.
Paging offers several advantages such as:
- It reduces external fragmentation
- It increases efficiency of the memory
- It makes swapping easy because the size of the frames and pages is the same
- It ensures faster access to data.
- Paging is useful for accessing data quickly.
In this technique, the operating system further ensures better management of the memory of the computer system. It includes two specific tasks, namely, swapping in and swapping out.
- Swapping in – This is the process by which the pages or blocks of data are moved from the secondary memory to the primary memory.
- Swapping out – This process refers to the removal of blocks or pages of data from the primary memory of the system to the ROM.
Both of these processes are valuable when a large program is executed or any operation that needs to be carried out on a large file.
How Does MMU Translate?
Translation of virtual addresses to physical addresses in the Memory Management Unit is typically done by software.
The operating system programs the MMU for the translation process and the hardware in a virtual memory system provides address translation.
Ideally, in an MMU, there are two components that help in the translation process. These are:
- The TLBs or Translation Lookaside Buffers that are responsible for caching the translations used recently
- The table walk unit with the logic which reads the translation tables from the memory.
During the translation process, all the memory addresses issued pass through the MMU. This then checks the TLBs for any translation that has been used recently.
If there is no translation cached recently, the table walk unit will read the correct table entry or entries from the memory.
Next, in order to make the table entry, the translation table provides one entry per block in the table. This is usually done after splitting the virtual address space into blocks of equal size. Each block contains two parts such as:
- The upper-order bits – This is the physical address for the virtual address and indicates the particular block entry to look at in the table and are also used as an index.
- The lower-order bits – This is the offset within the block and is not altered during the translation process.
When the translation happens, a table lookup is performed. For example, when the table entry is 0, the mapping for block 0 will be provided in the table, entry 1 for block 1, and so on.
This means that there will be an address of the matching block of physical memory in each entry along with the attributes to use for accessing that particular address.
MMU can also do multilevel translation by creating a multilevel table by dividing the virtual address space into big blocks.
An entry in the table may point to a block of equal size in the physical memory or to another table that has the block further subdivided into smaller ones.
This specific approach makes it easy to describe both larger and smaller blocks that differ in efficiency and levels of reads.
The operating system balances this by using the MMU and large mappings instead of smaller mappings for greater flexibility and optimum performance.
What is the Use of MMU?
One of the most significant tasks of the Memory Management Unit is to allow the system to multitask by running them as independent programs in their own individual virtual memory space.
The MMU also helps in modifying the physical address of the memory decoded by hard-wired logic so that the logical address avowed by the software is different from the physical address.
Generally, using the MMU turns out to be helpful in a number of contexts as follows:
- It helps to follow a specific process model so that the tasks in the operating system are organized in a better way. Therefore, it is used by Linux and most major desktop operating systems.
- It helps each task or process to use the memory from address zero upwards just like it would run on the CPU by itself.
- It also helps in mapping the addresses onto a particular area of the actual physical memory, and this mapping is changed every time there is a context switch or the control is taken by a different process.
- Since it does not allow a process to access data that belongs to another process, it also helps in simplifying the development of a process and maintaining a high level of security for it.
- It prevents the failure of allocations or the need for changing each pointer into a handle that may be moved by a compactor by ensuring that there is no free memory in a large allocation in small non-conjoint pieces.
- It offers independent linear memory space to a process based on the actual size or location of the physical memory of the machine. This also eliminates the need to relocate or recompile a program but run it along with others and coded in a standard way.
- Allowing the operating system to configure it, the MMU allows isolation among programs and over-committing memory.
- Using some of the disk space as a swap file, it allows the larger programs than the physical memory to be executed.
Does the GPU Have MMU?
Yes, in the GpuMmu model, The GPU or the Graphics Processing Unit comes with its own Memory Management Unit.
This performs the same task that is to translate the virtual addresses to physical addresses but for the GPU exclusively.
The GPU MMU uses the hardware layout of the page tables. This is not known to the video memory manager and the Device Driver Interface or DDI abstracts it.
This abstraction supports translation at multiple levels, which includes a resizable root page table and a preset size page table.
How Does an MMU Provide Memory Protection?
The MMU provides protection to the memory by translating the linear or virtual address of a process to physical address.
There are significant functions performed by the MMU to provide protection to the memory. These are:
- All the entries in the Memory Management Unit offer privilege checking to ensure that the CPU has the appropriate privilege level to access a specific memory region.
- It also provides read and write protection to the memory.
- It offers specific security features for memory protection such as marking only a few parts of it as executable.
In addition to that, the MMU also provides the necessary data structures to the kernel for virtual memory and for implementing process swapping. This prevents the memory pages belonging to one particular process from being seen by another process but both can be seen by the dependent kernel.
How Does the MMU Use Page Tables?
The MMU uses the page tables or the table of items in the memory for translating the virtual addresses to physical addresses for the operating system.
However, the MMU does not have its own page tables and therefore has to share them with the operating system.
Ideally, these page tables come with at least one Page Table Entry or PTE on each page. These PTEs help in mapping the virtual page numbers to the physical page numbers in the primary memory in specific ways as follows:
- There is an allied cache for these PTEs which is called the Translation Lookaside Buffer. The MMU stores a cache of mappings used recently from the page table of the operating system. This eliminates the need to access the main memory every time a virtual address is required to be mapped.
- The page table entries may be held in the registers of the MMU or in the private group of memory that some MMUs have.
- The page offset and the physical page number are combined to provide the complete physical address.
Moreover, the PTE may also contain some other important information that helps in using the page tables by the MMU. For example, it may contain information about:
- Dirty bit, or whether the page has been written to
- Accessed bit, or when was page last used for a recently used page replacement algorithm
- Type of processes such as supervisor mode or user mode that can read or write in it and
- Whether or not it should be cached.
Ideally, when the TLB is searched by the MMU during translation, if there is a match found, called a TLB hit, the physical address is sent back and accessing the memory continues.
If there is a TLB miss, or no match is found, the MMU or the TLB miss handler of the operating system will look up in the page table for address mapping to find whether or not it exists. This process is known as a page walk.
It is written back in the TLB if it exists. This is important because the memory is accessed by the hardware via the TLB in a virtual memory system.
The faulting instruction may be resumed in parallel as well. The following translation will cause a TLB hit, and the memory access continues.
When the PTE denies access to a virtual page due to unallocated RAM for it, the MMU signals a page fault to the processor.
Such situations are handled by the operating system. It tries to find an unused RAM frame and set up a new PTE so that it can be mapped to the virtual address requested.
If there is no free RAM, then it will choose an existing or a ‘victim’ page with the help of a replacement algorithm and save it to the disk.
This process is called ‘paging.’ If there is a shortage of PTEs in the MMU, then the operating system must free one for new mapping.
Is MMU a Part of the CPU?
Yes, the Memory Management Unit of a computer is typically located in the Central Processing Unit or CPU.
However, there may be times when it may also operate as a separate Integrated Circuit or IC.
In fact, all x86-architecture CPUs from Intel and AMD, including the 64-bit variants, have this unit.
However, there are a few processors designed for embedded systems that may not have an MMU.
How to Setup MMU?
You can set up the MMU very easily in three simple steps that involve building the translation table that includes cache-able and buffer-able flags, virtual to physical address translation, access permissions and the domain number.
Then you have to store the location of the table created in a register and enable the caches and MMU by writing to the register.
Does MMU Have Its Own Memory?
No, the MMU usually does not have its own memory. Therefore, in order to operate, it has to rely on the data available in the main memory of the system.
This data is typically stored in the data structures such as the page tables and specifies which particular ranges of physical addresses match with the corresponding linear address ranges.
Which Address is Generated by the MMU?
The Memory Management Unit generates and computes the physical address and the logical address is typically generated by the CPU.
The Memory Management Unit ensures efficient functioning and allocation of memory to the processes which allows faster and better functioning of the processor and the computer system as a whole.
There are different aspects of it which are entailed in this article for your easy and better understanding.