Logical Address, linear address, physical address

Source: Internet
Author: User

Logic address to linear address

The memory addresses in machine language commands are logical addresses, which need to be converted to linear addresses and then converted to physical addresses by MMU (Memory Management Unit in the CPU.

We will write the simplest Hello World Program and compile it with GCC. After disassembly, we will see the following command:

MoV 0x80495b0, % eax

The memory address 0x80495b0 is a logical address. A linear address can be formed only by adding the base address of the hidden DS data segment. That is, 0x80495b0 is the offset in the DS data segment of the current task.

In x86 protection mode, segment information (segment-based linear address, length, permission, etc.) occupies 8 bytes, segment information cannot be directly stored in segment registers (segment registers are only 2 bytes ). Intel is designed to store segment descriptors in gdt or LDT, while segment registers store the index values of segment descriptors in gdt or LDT ).

In Linux, the logical address is equal to the linear address. Why? Because the linear addresses of all Linux segments (user segments, user data segments, kernel segments, and kernel data segments) Start from 0x00000000, with a length of 4 GB, in this way, the linear address = logical address + 0 × 00000000, that is, the logical address is equal to the linear address.

In this case, Linux only uses gdt, and LDT is not used for user tasks or kernel tasks. The 12th and 13 segment descriptors of gdt are _ kernel_cs and _ kernel_ds, and the 14th and 15 segment descriptors are _ user_cs and _ user_ds. The kernel tasks use _ kernel_cs and _ kernel_ds. All User Tasks share _ user_cs and _ user_ds. That is to say, you do not need to assign a segment descriptor to each task. Although the starting linear address and length of the kernel segment descriptor and user segment descriptor are the same, the DPL (descriptor privileged level) is different. _ Kernel_cs and _ kernel_ds
The DPL value of is 0 (the highest privilege), and the DPL value of __user_cs and _ user_ds is 3.

When you debug a program using GDB, use info reg to display the value of the current Register:

CS 0 × 73 115

 
SS 0x7b 123

 
DS 0x7b 123

 
Es 0x7b 123

We can see that the DS value is 0x7b, converted to binary 00000000 01111011, And the Ti field value is 0, which indicates that gdt is used, and the gdt index value is 01111, that is, decimal 15, it corresponds to the _ user_ds user data segment descriptor in gdt.

From the above we can see that Linux runs on the x86 segmentation mechanism, but it bypasses the segmentation in a clever way.

Linux manages memory by page.

Figure 1-2 logical address to linear address

2. Linear address to physical address

As mentioned above, in Linux, logical addresses are equal to linear addresses. How do linear addresses correspond to physical addresses? As we all know, it is through the paging mechanism. Specifically, it is to find the corresponding physical address through the page table.

To be accurate, paging is a mechanism provided by the CPU. Linux uses it to implement memory management based on the rules of this mechanism.

In protection mode, the highest PG bit of the control register Cr0 controls whether the paging management mechanism takes effect. If Pg = 1, the paging mechanism takes effect, you need to query the page table to convert the linear address to the physical address. If Pg = 0, the paging mechanism is invalid, and the linear address is directly used as the physical address.

The basic principle of paging is to divide the memory into several fixed units, each of which is called a page. Each page contains 4 K address space (for simplified analysis, we do not consider extending pages ). In this way, the starting address of each page is 4 K Bytes aligned. To convert the address to a physical address, we need to provide the CPU with the linear address of the current task to the physical address query table, that is, the page table ). Note: To achieve smooth virtual memory for each task, each task has its own page Directory table and page table.

To save memory space occupied by page tables, x86 converts a linear address to a physical address through the page Directory table and page table searches.

A 32-bit linear address is divided into three parts:

The maximum 10-bit directory page table offset, the middle 10-bit table is the page table offset, and the lowest 12-bit offset is the byte offset in the physical page.

The size of the page Directory table is 4 K (just the size of a page), which contains 1024 items, each of which is 4 bytes (32 bits ), the content stored in the project is the physical address of the page table. If the page table in the page Directory is not allocated, enter 0 for the physical address.

The size of the page table is 4 kb, which also contains 1024 items, 4 bytes for each item. The content is the starting address of the physical memory of the final physical page.

For each active task, you must first allocate a page Directory table to it and store the physical address of the page Directory table to the S3. Page tables can be allocated in advance or distributed when used.

Take the address in mov 0x80495b0, % eax as an example to analyze the process of converting a linear address to a physical address.

As mentioned above, the logical address in Linux is equal to the linear address, so the linear address to be converted is 0x80495b0. The conversion process is automatically completed by the CPU. What Linux needs to do is prepare the page Directory table and page table required for conversion (assuming that the conversion process is ready, the process of allocating physical memory to the page Directory table and page table is complex and will be analyzed later ).

The kernel first fills in the physical address of the page Directory table of the current task in the S3.

After the linear address 0x80495b0 is converted into a binary value, the value is 0000 1000 0000 0100 1001 0101 1011 0000, the value of the 10-bit decimal value of 0000 1000 00 is 32, and the CPU displays 32nd items in the directory table on the page, it stores the physical address of the page table. In the linear address center, the 10-digit 00 0100 decimal value is 73, and the 1001 Items in the page table store the physical start address of the final physical page. The base address of the physical page plus the minimum 12-bit offset of the linear address, the CPU finds the physical memory unit corresponding to the linear address.

We know that the linear address range of user processes in Linux is 0-3G. Do we need to set up all the 3G Virtual Memory Page tables in advance? Under normal circumstances, the physical memory is much smaller than 3g, and many processes are running at the same time, it is impossible to create a 3G linear address page table for each process in advance. Linux uses a CPU mechanism to solve this problem. After the process is created, we can enter 0 for the table item values in the page Directory table. When the CPU is searching for the page table, if the table item content is 0, a page exception is thrown, the process is paused. At this time, the Linux kernel can allocate a physical page through a series of complex algorithms, enter the address of the physical page in the table, and resume the process. Of course, the process is blinded in this process, and it still feels that it has accessed the physical memory normally.

Figure 1-2 paging Mechanism

Address: http://blogs.ejb.cc/archives/5956/difference-between-the-logical-addresses-linear-addresses-and-physical-addresses

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.