How MMU (TLB) and cache work

Source: Internet
Author: User

Functions of TLB in MMU:

The conversion process from a virtual address to a physical address shows that when a level-1 page table is used for address conversion, the memory needs to be accessed twice each time data is read/written. The first time a level-1 page table is accessed, the physical address is obtained, the second is the real read/write data. When two-level page tables are used, three times of memory needs to be accessed for each read/write data operation, and two page tables (level-1 page tables and level-2 page tables) are accessed) obtain the physical address. The third time is the real read/write data.

The above address translation process reduces the CPU performance. Is there any way to improve it? During program execution, the commands and data addresses are usually concentrated in a very small range. The addresses and data are often used multiple times, which is called the locality of program access. Therefore, a high-speed, relatively small-capacity storage is used to store recently used page table entries (segment/large page/small page/extremely small page descriptor ), in this way, you do not need to go to the primary storage to search for each address translation, which can greatly improve the performance. This memory is used to help quickly perform address translation, called "translation search cache" (TLB ).

When the CPU sends a virtual address, MMU first accesses TLB. If TLB contains a descriptor that can be converted to this virtual address, it is directly used for address conversion and permission check; otherwise, the MMU accesses the page table and finds the descriptor before performing address translation and permission check, and fills in the descriptor in TLB. (If TLB is full, an entry is found using the round-robin algorithm, then overwrite it). The next time you use this virtual address, you can directly use the Address Descriptor in TLB.

Cache and MMU:

According to the working principle, the cache has several working methods: Physical Index physical tagged, virtual index virtual tagged, and physical index virtual tagged.

Physical Index physical tagged is the easiest way to understand. The cache is simple and crude to operate on physical addresses without ambiguity. However, this method has obvious defects. In a multi-process operating system, each process has its own independent address space, and the commands and code both exist in the form of virtual addresses, the memory access commands issued by the CPU are issued in the form of virtual addresses. In this way, for every memory access operation, you must first wait for MMU to translate the virtual address into a physical address, this is a serial method with low efficiency.

Virtual index virtual tagged is purely addressing with virtual addresses, which brings more problems, the process ID must be added to each row of data on the basis of the original tag to distinguish the same address among multiple processes, which is also troublesome for processing shared memory, there are different virtual addresses in different processes in the sharing, and how to synchronize them is a problem.

The virtual index physical tagged method is usually used. The virtual index means that when the CPU sends an address request, low-level address removal matches the index in the cache (because low-level addresses are usually intra-page offset addresses, which does not affect page selection ), physical tagged indicates that the high address of the virtual address matches the page table in MMU to obtain the physical address of the page, which ensures that the cache and MMU work in parallel. Then, the physical page address obtained from MMU is used as the tag (or part of the tag) to match the tag bit of the cache line, in this way, the uniqueness of the same address in the cache is ensured (with an exception, cache alias), MMU and cache can work in parallel, and the efficiency is improved. The only problem caused by this method is cache alias. When the cache size of cache alias is inconsistent with the size of the Memory Page, for example, the size of each cache is 8 K, the size of commonly used page boxes is 4 kb. The two numbers indicate the following problems:
The size of the page box is 4 K, which means that MMU uses 4 K as the granularity when dividing virtual space. Every 4 K alignment memory area serves as the smallest unit of memory allocation, so this result is displayed, the low 12-bit virtual address is the same as the physical address, because the low 12-bit address is used as its offset in the 4 K page, no matter what its virtual address is, the offset in the 4 K page is fixed.

The cache size is 8 K, which means that the cache index is calculated based on the 13-bit address. If the cache address is a virtual address, the 13-bit lower contains a virtual address, therefore, this method is called virtual index. The problem arises. When a physical address is used by different processes, because the MMU paging mechanism can only ensure the same 12-bit low, the 13th-bit is likely to be different, as a result, a physical address has two copies in the cache, which may cause synchronization problems.

For the cache alias problem, the current solution is guaranteed by the operating system. For virtual addresses of the same physical address in different process spaces, the difference between their virtual addresses must be an integer multiple of the cache size, which means that their 13th bits must be the same. At the same time, some CPU vendors are already developing monitoring modules, trying to solve similar synchronization problems on the hardware layer.

How MMU (TLB) and cache work

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.