linux2.6 Memory Management--logical address translated to linear address (logical address, linear address, physical address, virtual address)

Source: Internet
Author: User

The physical and virtual storage spaces in the Linux system have a range of addresses ranging from 0x00000000 to 0xFFFFFFFF, with 4GB in total, but physical storage space is completely different from the virtual storage space layout. Linux runs in virtual storage space and is responsible for mapping the actual physical memory in the system, which is much less than 4GB, to the entire 4GB virtual storage space based on different requirements. Linux works primarily in protected mode. The 80x86 has undergone two stages from the logical address to the physical address transformation. The first stage uses a staging mechanism to transform the program's logical address into an address in the processor addressable memory space (called the Linear address space). The second-stage paging mechanism translates the linear address into a physical address. The first stage of the segmentation transformation mechanism is necessary, but the second stage of the paging mechanism is optional. If the paging mechanism is not turned on, then the linear address space produced by the fragmentation mechanism is mapped directly to the physical address space of the processor. Before conducting the study, it is important to define the concept clearly and not to confuse:

First, the basic concept

1. Physical Address : addresses placed on the addressing bus for memory chip-level memory unit addressing. On the addressing bus, if it is read, the circuit transmits the data in the physical memory of the corresponding address to the bus in accordance with the value of each bit of the address. In the case of writing, the circuit places the physical memory of the corresponding address on the data bus, based on the value of each bit of the address. The physical memory is addressed in bytes (8 bits), which is the final result address of the address transformation, represented by a 32-bit or 36-bit unsigned integer.

2. logical address : Refers to the part of the offset address that is generated by the program, and each logical address consists of a segment and an offset. In the C language pointer programming, you can read the pointer variable itself value (& operation), in fact, this value is the logical address, it is relative to your current process data segment address, not and absolute physical address coherence. Only in Intel Real mode will the logical address be equal to the physical address (because there is no fragmentation or paging mechanism for the real mode, the CPU does not perform automatic address translation), and the logic is the offset address of the code snippet in Intel protected mode (assuming that the code snippet, the data segment is exactly the same).

Linear address: is the middle tier between the logical address and the physical address transformation. The program code generates a logical address, or an offset address in a segment, with the base address of the corresponding segment generating a linear address, which is a 32-bit unsigned integer that can be used to represent up to 4GB of addresses, i.e. up to 4,294,967,296 memory units, in hexadecimal notation, 0x00000000 to OXFFFFFFFF. If the paging mechanism is enabled, the linear address can then be transformed to produce a physical address. If the paging mechanism is not enabled, then the linear address is directly the physical address. The Intel 80386 has a linear address space capacity of 4G (2 of 32 address bus addressing).

4. virtual memory : Refers to the amount of memory that the computer presents that is much larger than the actual memory. So it allows programmers to compile and run programs that are much larger in memory than the actual system. This enables many large projects to be implemented on systems with limited memory resources. A very proper analogy is that you don't need a long track to get a train from Shanghai to Beijing. You only need long enough rails (say 3 km) to complete this task. The way to do this is to put the rear rails immediately in front of the train, as long as your operation is fast enough to meet the requirements, the train will be able to run like a complete track. This is the task that virtual memory management needs to accomplish. in the present operating system, the use of MMU storage management technology, and MMU management address is virtual address, virtual memory is a computer system memory management technology. It allows the application to assume that it has contiguous available memory (a contiguous, complete address space), and in fact, it is usually separated into multiple physical memory fragments, and some are temporarily stored on external disk storage and exchanged for data when needed. Systems that use this technology make it easier to write large programs than systems that do not use virtual memory technology, and are more efficient at using real physical memory, such as RAM. Sometimes we also refer to logical addresses as virtual addresses. Because it is similar to the concept of virtual memory space, the logical address is independent of the actual physical memory capacity.

How to convert a logical address to a linear address

    Complete memory management with two key parts for protection and address transformation. The 80386 operating modes include the real address mode and the virtual address mode (Protected mode). Linux works primarily in protected mode. 80x86 has undergone two stages from the logical address to the physical address transformation. The first stage uses a staging mechanism to transform the program's logical address into an address in the processor addressable memory space (called the Linear address space). The second-stage paging mechanism translates the linear address into a physical address. The first stage of the segmentation transformation mechanism is necessary, but the second stage of the paging mechanism is optional. If the paging mechanism is not turned on, then the linear address space produced by the fragmentation mechanism is mapped directly to the physical address space of the processor.

A logical address consists of two parts, the segment identifier: The offset within the segment . A segment identifier is made up of a 16-bit long field called a segment selector. The first 13 bits are an index number. The following 3 bits contain hardware details, indicating whether the code segment register or the stack segment register or the data segment register, 1. The index number is the index of the segment descriptor (segment descriptor), and the segment descriptor address describes a segment. A number of segment descriptors, the group of an array, called "segment Descriptor Table", so that you can use the first 13 bits of the segment identifier, directly in the Segment descriptor list to find a specific segment descriptor , this sentence is very important, explain the specific role of the segment identifier, each segment descriptor consists of 8 bytes, 2 is shown, The most closely related topic is the base field, which represents the linear address that contains the first byte of a segment, that is, the linear address where a segment begins. In full reference to a word in the book, some global segment descriptors are placed in the Global Segment Descriptor List (GDT), and some parts, such as each process's own, are placed in the so-called "local segment Descriptor List (LDT)". When should the GDT be used, and when should the LDT be used? This is represented by the T1 field in the segment selector, = 0, which means that the address and size of LDT,GDT in memory are stored in the GDTR control register of the CPU with gdt,=1, while the LDT is in the LDTR register. There are several basic concepts in this process, which must be clear, such as segment selector, segment descriptor, local segment Descriptor table, global segment Descriptor list.

Figure 1 Segment Selector

Figure 2 Segment Descriptor

Figure 3 shows in detail how a logical address is converted to a corresponding linear address, given a complete logical address [segment selector: offset address within paragraph],
1, see segment selector t1=0 or 1, that is, first check the segment selector in the Ti field, to determine the segment description characters in which descriptor table , know the current to convert is the segment in the GDT (in this case, the fragment unit from the GDTR register to obtain the linear base of the GDT site) , or a segment in the LDT (in this case, the segment unit obtains the linear base address of the GDT from the LDTR register), and then, according to the corresponding register, its address and size.
2, because a segment descriptor is 8 bytes long, so her relative address in the GDT or LDT is the highest 13-bit value of the segment selector multiplied by 8 , take out the segment selector in the first 13 bits, you can find the corresponding segment descriptor in this array, so that it has offset, That is, the offset address is known.
3, the base + offset, is to convert the linear address.
for software, in principle, you need to prepare the necessary information for hardware conversion, you can let the hardware to complete the conversion. Figure 4 The logical address is converted to a linear address instance, the segment selector is 0x7b, points to the user data segment, the beginning address of the segment is 0x00000000, the logical offset address is 0x80495b0, and the final linear address is base + offset=0x80495b0.

Figure 3 Conversion of logical addresses

Figure 4 Logical address converted to a linear address instance

References: 1, "Deep Linux kernel Architecture", 2, "deep understanding of the Linux kernel", 3, "Linux kernel design and implementation", 4, "The Art of Linux kernel design";

linux2.6 Memory Management--logical address translated to linear address (logical address, linear address, physical address, virtual address)

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.