In Linux systems, memory management is a very important topic, which involves aspects such as program operation, performance and security. A core concept in memory management is virtual address and physical address, which represent the logical view of a program and the actual layout of memory, respectively. The conversion between virtual addresses and physical addresses is a key process of memory management. It allows programs to better utilize memory resources and improve memory access efficiency and protection. But, do you really understand virtual addresses and physical addresses? Do you know their definitions, characteristics and differences? Do you know how to convert between virtual and physical addresses in Linux? This article will introduce you to the relevant knowledge of virtual addresses and physical addresses under Linux in detail, so that you can better use and understand these two memory addresses under Linux.
The application can only provide a virtual address. You can also obtain the physical address through the following method. Of course, you must call the driver.
Linux uses the concept of page tables to manage virtual space. When the kernel processes a virtual address, it must convert it into a physical address before the processor can access it. The virtual address can be found layer by layer through the Linux page table operation macro to find the physical address. Simply put, the virtual address needs to be segmented. Each segment of the address serves as an index pointing to the page table, and the last level page table points to the physical address.
In order to be compatible with various processors, Linux versions after 2.6.11 adopt a four-level page table structure:
PGD: Page Global Directory, page global directory, is a top-level page table.
PUD: Page Upper Directory, the upper-level directory of the page, is the second-level page table
PMD: Page Middle Directory, the page middle directory, is the third-level page table.
PTE: Page Table Entry, page table, the last level page table, points to the physical page.
The physical page can be found by accessing PGD through the data structure mm_struct, as shown in Figure 4-8. The process of finding the physical address according to the page table is shown in 4-9.
Figure 4-level page used by Linux
The simplified conversion code is as follows:
static int vir2phy(unsigned long va) { struct task_struct *pcb_tmp; pcb_tmp = current; pgd_tmp = pgd_offset(pcb_tmp->mm,va); pud_tmp = pud_offset(pgd_tmp,va); pmd_tmp = pmd_offset(pud_tmp,va); pte_tmp = pte_offset_kernel(pmd_tmp,va); pa = (pte_val(*pte_tmp) & PAGE_MASK) |(va & ~PAGE_MASK); return pa; }
pgd_offset(mm, addr) receives the memory descriptor address mm and linear address addr as parameters. This macro generates the linear address of the corresponding entry in the page global directory of address addr;
This page global directory can be found through a pointer in the memory descriptor mm.
pud_offset(pgd, addr) The parameters are the pointer pgd pointing to the page global directory entry and the linear address addr. This macro generates the linear address corresponding to the directory entry addr in the directory above the page. In a two- or three-level paging system, this macro generates pgd, the address of a page global directory entry.
pmd_offset(pud, addr) receives the pointer pud pointing to the page's upper directory entry and the linear address addr as parameters. This macro generates the offset address of the directory entry addr within the page's intermediate directory. In a two- or three-level paging system, it generates pud, the address of the page global directory entry.
pte_offset_kernel(dir, addr) The linear address addr has a corresponding item in the page intermediate directory dir. This macro generates this corresponding item, which is the linear address of the page table. Additionally, this macro is only used on the main kernel page table.
Through this article, you should have an in-depth understanding of virtual addresses and physical addresses under Linux, and know their definitions, characteristics and differences. You should also understand the principles, methods and functions of conversion between virtual addresses and physical addresses, and how to correctly convert between virtual addresses and physical addresses under Linux. We recommend that you use virtual addresses to write and run programs when using a Linux system to improve program portability and security. At the same time, we also remind you to pay attention to some potential problems and challenges when using Linux systems, such as memory fragmentation, memory leaks, memory mapping, etc. I hope this article can help you better use the Linux system and allow you to enjoy the advantages and convenience of virtual addresses and physical addresses under Linux.
The above is the detailed content of Virtual addresses and physical addresses under Linux: concepts, conversions and applications. For more information, please follow other related articles on the PHP Chinese website!