Segmentation mechanism of Linux memory addressing

Source: Internet
Author: User

Http://blog.xiaohansong.com/2015/10/03/Linux memory addressing segment mechanism/

, the beginning address of the segment, the length of the segment, and so on, while in protected mode it is more complex. IA32 combine them in a 8-byte number representation, called a descriptor.
The structure of a generic segment descriptor for IA32
As you can see from the diagram, a segment descriptor indicates the 32-bit base address of the segment and the 20-bit segment bounds (that is, the section length). Here we focus only on the base address and the segment bounds, and the other properties are skipped.

Segment Descriptor Descriptor

A variety of user descriptors and system descriptors are placed in the corresponding Global descriptor table, local descriptor tables, and interrupt descriptor lists. The descriptor table (that is, the paragraph list) defines all the segments of the IA32 system. All descriptor characters themselves occupy a memory space of a multiple of 8 bytes, with a space size of 8 bytes (at least one descriptor) to 64K bytes (at most 8K) descriptors.

    1. Global Descriptor Descriptor (GDT)
      Global descriptor Table GDT, which includes the task gate, break gate, and Trap Gate descriptor, contains descriptors for those segments that are common to all tasks in the system descriptor. Its first 8-byte position is not used.
    2. Interrupt descriptor Table IDT (Interrupt descriptor)
      The interrupt descriptor Table, IDT (Interrupt descriptor), contains 256 gate descriptors. IDT can contain only the task gate, the interrupt gate and the trap Gate descriptor, although the IDT table can be up to 64K bytes long, but can only access the descriptor within 2K bytes, that is, 256 descriptors, this number is to maintain compatibility with 8086.
    3. Local Descriptor Descriptor (LDT)
      The local descriptor Table, descriptor, contains the descriptors associated with a given task, each of which has a LDT for each task. With the LDT, you can isolate the code, data, and other tasks for a given task. The local descriptor for each task The LDT itself is also represented by a descriptor, called the LDT descriptor, which contains information about the local descriptor list and is placed in the global descriptor descriptor GDT.
Summarize

The IA32 memory addressing mechanism completes the conversion from the logical address – the linear address – to the physical address. Where the value in the segment register of the logical address provides the segment descriptor, and then obtains the segment base and segment bounds from the segment descriptor, and then adds the offset of the logical address, the linear address is obtained, and the linear address gets the physical address through the paging mechanism.
First, we need to make it clear that the fragmentation mechanism is the addressing method provided by IA32, which is hardware-level. That is, whether you are Windows or Linux, as long as you use IA32 CPU access to memory, you have to go through the MMU conversion process to obtain a physical address, that is, must go through the logical address-linear address-physical address conversion.

Implementation of fragmentation in Linux

There is so much to say about the implementation of the segmentation mechanism, in fact, for Linux, there is no egg. Because Linux basically does not use a fragmentation mechanism, or that the fragmentation mechanism in Linux is designed to be compatible with IA32 hardware.

The segment mechanism of the Intel microprocessor was proposed starting from 8086, when the segment mechanism introduced resolved the conversion from the internal CPU to the 16-bit address to the 20-bit real address. To maintain this compatibility, 386 still uses the segment mechanism, but is much more complex than before. Therefore, the design of the Linux kernel does not all use the Intel-provided segment scheme, only a limited use of the fragmentation mechanism. This not only simplifies the design of the Linux kernel, but also creates the conditions for porting Linux to other platforms because many RISC processors do not support the segment mechanism. However, understanding the knowledge of the segment mechanism is the only way to enter the Linux kernel.

Starting with version 2.2, Linux allows all processes (or tasks) to use the same logical address space, so it is not necessary to use the local descriptor descriptor for the LDT. However, the LDT is also used in the kernel, which only runs wine in VM86 mode, because it is used to simulate programs running WINODWS software or DOS software on Linux.

Any address given on the IA32 is a virtual address, that is, any address is given by the "selector: Offset" method, which is the basic feature of the memory access mode of the segment mechanism. Therefore, it is not possible to avoid using the segment mechanism when designing an operating system on IA32. A virtual address will eventually be converted to a linear address by the "segment base address + offset" approach. However, because the majority of hardware platforms do not support the segment mechanism, only support paging mechanism, so in order to make Linux more portable, we need to remove the segment mechanism and only use paging mechanism. Unfortunately, IA32 specifies that the segment mechanism is not forbidden, so it is not possible to bypass it to give the address of the linear address space directly. Helpless, the Linux designers simply let the base address of the segment is 0, while the boundary of the segment is 4GB, when any given an offset, the equation is "0+ offset = linear address", that is, "offset = linear address". In addition, because the paragraph mechanism stipulates "offset <4GB", so the range of offsets is 0H~FFFFFFFFH, which is exactly the linear address space, that is, the virtual address directly mapped to the linear address, we refer to the virtual address and the linear address refers to the same address. It seems that Linux has cleverly bypassed the segment mechanism without bypassing the segment mechanism.

In addition, because the IA32 segment mechanism also stipulates that different segments must be created for both the code snippet and the data segment, Linux must create a segment descriptor for both the code snippet and the data segment with a base address of 0 and a segment boundary of 4GB respectively. Not only that, because the Linux kernel is running at privileged level 0, and the user program is running at privileged 3, according to the IA32 segment protection mechanism, the privileged Class 3 program is unable to access the privileged 0 segment, so Linux must create its own code snippets and data segments for the kernel user program. This means that Linux must create 4 segment descriptors--privileged level 0 code snippet and data segment, privileged level 3 code snippet and data segment.

Summarize

The segmentation mechanism is the characteristic of IA32 architecture CPU, and is not the inevitable choice of the operating system addressing mode. Linux in order to cross-platform, clever bypass mechanism, mainly using paging mechanism to address.

Resources
Deep analysis of the Linux kernel source code

Segmentation mechanism of Linux memory addressing

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.