Detailed analysis of the specific implementation of the MMU in the linux system

first look at a picture

picture

​First: MMU memory management

 

MMU (Memory Management Unit, memory management unit) is a hardware module used to implement virtual memory management between the CPU and memory.

Its main function is to convert virtual addresses into physical addresses, and at the same time provide functions such as access control and cache management. MMU is an important part of modern computer operating system, which can improve the stability and security of the system.

In terms of memory management, the MMU can implement virtual memory management through the Page Table. The page table is a data structure that records the mapping relationship between each virtual page and its corresponding physical page.

When the CPU issues a virtual address, the MMU looks it up through the page tables and translates it to the corresponding physical address.

In addition, the MMU can also implement functions such as memory protection and sharing through the page table, thereby improving the security and efficiency of the system.

In short, MMU is an important hardware component in memory management, which can realize functions such as virtual memory management, memory protection, sharing and caching, and provide support for the stability and security of modern computer operating systems.

​Second: give an example

Suppose we have a program that needs to access two memory areas: one is a read-only code area, and the other is a readable and writable data area.

We now want to run this program on a system without an MMU. If there is no MMU, the code area and data area can only be mapped to two fixed physical addresses. This means that if a program tries to access an incorrect address, it may crash the system.

Now, if we run this program on a system with an MMU, things will be different. The MMU can map the addresses that the program tries to access to different physical addresses, so that the code area and the data area are no longer fixed locations in the physical memory.

This means that if a program tries to access an incorrect address, the MMU can protect the system from crashing by remapping.

MMU can also map multiple virtual addresses to the same physical address, which is called page sharing, which can reduce the use of physical memory.

If there is no MMU, the program can only use the physical address when accessing the memory, and the physical address is directly mapped to the address on the memory chip, and the program can access any physical address at will.

In this case, if the program accesses a wrong address or attempts to access an unauthorized address, an access error or illegal access will occur, which may lead to problems such as system crash and data loss.

With the MMU, the program accesses the virtual address, and the MMU is responsible for mapping the virtual address to the physical address, so that the program cannot directly access the physical address.

At the same time, the MMU can restrict the program's access to the memory according to the memory access authority to ensure the security and stability of the system.

Therefore, when there is no MMU, the program may access other addresses, but with the MMU, the program can only access the addresses that are allowed to be accessed, which can effectively avoid the problem of illegal access.

picture

​Third: Why does the same virtual address space not conflict at the physical address?

The same virtual address space may be mapped to different physical addresses in different processes, and this mapping process is completed by the MMU. In the operating system, each process has an independent virtual address space, and these virtual address spaces do not interfere with each other.

The MMU will map the virtual address of each process to the corresponding physical address, so that memory accesses between different processes will not interfere with each other. At the same time, the MMU will also provide some security mechanisms, such as page protection, to prevent the process from accessing memory or accessing the memory of other processes.

Therefore, the MMU plays a role in protecting the memory between processes from interfering with each other, and is also an important part of the modern operating system.

​Fourth: What is a page table?

A page table is a data structure used to store the mapping relationship between virtual memory addresses and physical memory addresses. In systems that use virtual memory, each process has its own virtual address space, and these virtual address spaces are divided into many pages (usually 4KB or larger in size), rather than a single continuous block of memory.

Therefore, when a process needs to access a certain virtual address, it needs to be translated into the corresponding physical address. This translation process is done through the page table.

The basic principle of the page table is to divide the virtual address into a page number and an offset.

The page number is used to look up the corresponding physical page frame number in the page table, and the offset is used to calculate the offset of the virtual address in the physical page frame. In this way, the virtual address can be mapped to the physical address, so that the process can access the corresponding memory area.

The page table is generally maintained by the operating system, because the operating system needs to master the mapping relationship between virtual addresses and physical addresses.

In a hardware-supported system using MMU (Memory Management Unit), when a process accesses a virtual address, the MMU converts the virtual address into a physical address through the page table, and directs the access to the correct physical address. This way, a process can access memory without knowing its own real physical address.

​Fifth: Why can't a Linux system run without an MMU?

This is because the Linux kernel divides the virtual address space into pages and maps these pages onto the physical address space for functions such as memory isolation, protection, and virtual memory.

Without the MMU, this mapping would not be possible to run a Linux system.

​Sixth:​Why some simpler SOCs may not have an MMU, but they can still run some embedded operating systems or bare-metal programs?

RTOSs can run on systems without an MMU because RTOSs usually don't need advanced features like memory protection and virtual address mapping.

In contrast, RTOSs are designed with a focus on real-time and low latency, so typically only simple memory management and task scheduling are required.

This allows RTOSs to run on many embedded systems, including some without an MMU.

Guess you like

Origin blog.csdn.net/weixin_41114301/article/details/132047970