Memory Management Unit-MMU

Source: Internet
Author: User

Virtual Memory Management (vmmemory Management) mechanism is widely used in modern operating systems. This requires the support of MMU (Memory Management Unit) in the processor. This section briefly introduces the role of MMU.

First, we introduce two concepts: Virtual Address and physical address. If the processor does not have an MMU, or the MMU is not enabled, the memory address sent by the CPU execution unit is directly transferred to the Chip Pin, which is called the physical memory, the physical address (PA), as shown in.

Figure 17.5. physical address


If MMU is enabled for the processor, the memory address sent by the CPU Execution Unit will be intercepted by MMU. The address from CPU to MMU is called virtual address (VA ), MMU translates this address into another address and sends it to the external address pin of the CPU chip, that is, ing Va to Pa, as shown in.

Figure 17.6. Virtual Address


If it is a 32-bit processor, the inner address bus is 32-bit, connected to the CPU Execution Unit (only four address lines are shown in the figure ), the external address bus after MMU conversion is not necessarily 32-bit. That is to say, the virtual address space is independent from the physical address space. The virtual address space of a 32-bit processor is 4 GB, and the physical address space can be larger than or less than 4 GB.

MMU maps Va to PA in the unit of page. The page size of a 32-bit processor is usually 4 kb. For example, MMU can map a page 0xb7001000 ~ 0xb7001fff ing to one page of PA 0x2000 ~ 0x2fff: If the CPU Execution Unit needs to access the virtual address 0xb7001008, the physical address actually accessed is 0x2008. Pages in the physical memory are called physical pages or page frames ). Which page of the virtual memory is mapped to the physical memory? Which page frame is described through the page table. The page table is saved in the physical memory, MMU searches the page table to determine the PA to which a VA is mapped.

The operating system works with MMU as follows:

  1. When the operating system initializes, allocates, and releases memory, it will execute some commands to fill in the page table in the physical memory, and then use the commands to set the MMU to tell the location of the MMU page table in the physical memory.

  2. After the configuration is complete, the CPU automatically triggers MMU to perform lookup and address translation operations every time it executes the memory access command. The hardware automatically completes the address translation operation and does not need to use commands to control MMU.

The variables and functions we use in the program have their own addresses. After the program is compiled, these addresses become the addresses in the instruction, and the addresses in the instruction are interpreted and executed by the CPU, the memory address issued by the CPU Execution Unit. Therefore, when MMU is enabled, all addresses used in the program are virtual addresses, which will lead to MMU lookup and address conversion operations. So why should we design such a complicated memory management mechanism? What are the benefits of the conversion from VA to Pa? All problems in computer science can be solved by another level of indirection. Do you still remember this sentence? An additional layer is indirectly required to solve the problem. After the necessary preparations are completed, we will discuss the role of the virtual memory management mechanism in section 5th "Virtual Memory Management.

In addition to address translation, MMU also provides memory protection. Various architectures are divided into user mode and privileged mode. The operating system can set access permissions for each memory page in the page table, and some pages cannot be accessed, some pages can be accessed only when the CPU is in privileged mode. Some pages can be accessed in both user mode and privileged mode. The access permissions are divided into three types: readable, writable, and executable. After this setting, when the CPU needs to access a va, MMU checks whether the CPU is in user mode or privileged mode. The purpose of access to the memory is to read data, write data, or retrieve commands, if it is consistent with the page permission set by the operating system, access is allowed and converted to Pa. Otherwise, access is not allowed and an exception is thrown ). The exception handling process is similar to the interrupt. The difference is that the interrupt is generated by an external device and the exception is generated by the CPU. The reason for the interrupt is irrelevant to the instruction currently executed by the CPU, the exception is caused by a problem with the commands currently executed by the CPU. For example, if the command used to access the memory is checked by MMU and the division command has a division of 0, an exception occurs.

Figure 17.7. processor Mode


Generally, the operating system divides the virtual address space into user space and kernel space. For example, the virtual address space in Linux on the X86 platform is 0x00000000 ~ 0 xffffffff, first 3 GB (0x00000000 ~ 0 xbfffffff) is the user space, the last 1 GB (0xc0000000 ~ 0 xffffffff) is the kernel space. The user program is loaded to the user space and executed in user mode. It cannot access data in the kernel or jump to the kernel code for execution. This protects the kernel. If a process accesses an Invalid Address, the process crashes at most without affecting the stability of the kernel and the entire system. When the CPU is interrupted or abnormal, it not only jumps to the interrupted or abnormal service program, but also automatically switches from user mode to privileged mode, therefore, you can jump from an interrupted or abnormal service program to the kernel code for execution. In fact, the entire kernel is composed of various interrupt and exception handling programs. To sum up, the processor normally executes the user program in user mode, and switches the processor to privileged mode to execute the kernel program in case of interruption or exception, after an interruption or exception is processed, return to user mode to continue executing the user program.

We have encountered this error many times. It is generated as follows:

  1. A va to be accessed by the user program, which is not authorized to be accessed by MMU check.

  2. MMU generates an exception. The CPU switches from user mode to privileged mode, and the exception service program is executed in kernel code.

  3. The kernel interprets this exception as a segment error and terminates the process that causes the exception.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.