Home System Tutorial LINUX Virtual addresses and physical addresses under Linux: concepts, conversions and applications

Virtual addresses and physical addresses under Linux: concepts, conversions and applications

Feb 14, 2024 pm 09:54 PM
linux linux tutorial linux system linux command shell script embeddedlinux Getting started with linux linux learning

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.

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; 
 }
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

What is vscode What is vscode for? What is vscode What is vscode for? Apr 15, 2025 pm 06:45 PM

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages ​​and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

How to use VSCode How to use VSCode Apr 15, 2025 pm 11:21 PM

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages ​​and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

See all articles