Linux under Logical address-linear address-Physical address plot (GO)

Source: Internet
Author: User

one, logical address to linear address

The memory address that appears in the machine language instruction is a logical address that needs to be converted to a linear address and then converted to a physical address by the MMU (Memory management unit in the CPU) to be accessible.

We write the simplest Hello World program, compile with Gccs, and then decompile to see the following command:

mov 0x80495b0,%eax

The memory address here, 0X80495B0, is a logical address that must be added to the base address of the hidden DS data segment to form a linear address. This means that 0x80495b0 is an offset within the DS data segment of the current task.

In x86 protected mode, the segment information (subgrade linear address, length, permissions, etc.) is a segment descriptor of 8 bytes, the segment information cannot be stored directly in the segment register (the segment registers only 2 bytes). Intel's design is that the segment descriptor is centrally stored in the GDT or LDT, while the segment register holds the index value (index) of the segment descriptor within the GDT or Ldt .

Linux the logical address in is equal to the linear address . Why do you say that? Because all Linux segments (user code snippet, user data segment, kernel code snippet, kernel data segment) linear address all start from 0x00000000, length 4G, so linear address = logical address + 0x00000000, that is, logical address equals linear address.

In this case, Linux only uses the GDT, whether it is user tasks or kernel tasks, no use of the LDT. The 12th and 13 segment descriptors for GDT are __kernel_cs and __kernel_ds, and the 14th and 15 segment descriptors are __user_cs and __user_ds. Kernel tasks use __kernel_cs and __kernel_ds, and all user Tasks share __user_cs and __user_ds, meaning that you do not need to assign segment descriptors to each task separately. The kernel segment descriptor and user segment descriptor, although the starting linear address and length are the same, but DPL (descriptor privilege level) is not the same. The DPL value for __kernel_cs and __kernel_ds is 0 (highest privilege), and the DPL value for __user_cs and __user_ds is 3.

When debugging a program with GDB, use info reg to display the value of the current register:

CS 0x73 115

SS 0x7b 123

DS 0x7b 123

ES 0x7b 123

You can see that the DS value is 0x7b, and the conversion to binary is 00000000 01111011,ti field value is 0, which means using a GDT,GDT index value of 01111, or decimal 15, corresponding to the __user_data user Data segment descriptor within the GDT.

As can be seen from the above, Linux runs on x86 's segmented mechanism, but it is a clever way to bypass fragmentation.

Linux primarily implements memory management in a paging manner.

second, the linear address to the physical address

The previous said that the logical address in Linux is equal to the linear address, then the linear address how to correspond to the physical address? As we all know, it is through the paging mechanism, specifically, is through the page table lookup to correspond to the physical address.

The exact paging is a mechanism provided by the CPU, and Linux is using it to implement memory management only according to the rules of this mechanism.

In protected mode, the highest bit PG bit of the control register CR0 controls whether the paging management mechanism is in effect, and if pg=1, the paging mechanism takes effect, a page table lookup is required to convert the linear address to the physical address. If pg=0, the paging mechanism is not valid, and the linear address is directly the physical address.

The rationale for paging is to divide the memory into units of fixed size, each of which is called a page, and each page contains a 4k byte address space (for simplified analysis, we do not consider the case of extended paging). The starting address for each page is then 4k-byte aligned. To be able to convert to a physical address, we need to give the CPU a lookup table of the linear address to the physical address of the current task , the page table. Note that in order to achieve a flat virtual memory for each task, each task has its own page Catalog table and page table .

In order to conserve the memory space occupied by the page table, x86 translates the linear address into a physical address through a page catalog table and a page table two-level lookup.

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

The maximum 10-bit Directory page Catalog table offset, the middle 10-bit table is the page table offset, and the lowest 12-bit offset is the byte offset within the physical page.

The size of the page catalog table is 4k (exactly the size of a page), contains 1024 items, each item is 4 bytes (32 bits), and the content stored in the project is the Physical address of the page table . If the page table in the page catalog table is not already assigned, the physical address is filled with 0.

The size of the page table is also 4k, which also contains 1024 items, 4 bytes per item, and the content is the physical memory start address of the final physical page.

For each active task, you must first assign it a page catalog table and deposit the physical address of the page catalog table in CR3 registers. The page table can be allocated in advance, or it can be allocated at the time of use .

Or the Mov 0x80495b0, the address in the%EAX as an example to analyze the linear address to the physical address of the process.

When we say that the logical address in Linux is equal to the linear address, then the linear address we want to convert is 0x80495b0. The process of conversion is done automatically by the CPU, and what Linux has to do is prepare to convert the required page catalog tables and page tables (assuming that the process of allocating physical memory to the page Catalog table and page table is complex, and then analyzed later).

The kernel first fills the physical address of the page directory table of the current task into the CR3 register.

Linear address 0x80495b0 converted to binary is 0000 1000 0000 0100 1001 0101 1011 0000, up to 10 bits 0000 1000 00 Decimal is the 32nd item in the 32,CPU View page Catalog table, which contains the physical address of the page table. Linear address median 10 bit 00 0100 1001 Decimal is 73, the 73rd item of the page table stores the physical starting address of the final physical page. The physical page base address plus the lowest 12-bit offset in the linear addresses, the CPU finds the final corresponding physical internal deposit element of the linear address.

We know that the linear address of the user process in Linux can be addressed in the range of 0-3g, then it is necessary to first put this 3g virtual memory page table is established? In general, physical memory is much smaller than 3G, plus there are many processes running at the same time, it is impossible to create a 3G Linear Address page table for each process in advance. Linux uses a mechanism of CPU to solve this problem. After the process is created, we can fill in the table key values of the page catalog table 0,CPU when the table entry is found, if the contents of the tables are 0, a page fault is thrown, the process pauses execution, and the Linux kernel can allocate a physical page through a series of complex algorithms and populate the table with the address of the physical page. The process resumes execution. Of course the process is blinded in the process, and its own sense of access to physical memory is normal.

Related Article

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.