Why does segment and paging mechanism exist in linux?

Source: Internet
Author: User


In linux, why do we need the segmentation and paging mechanisms? First, let's talk about the real mode and protection mode on linux. Why do we need to divide the real and protection modes? What are their differences? First, the real mode and the protection mode are the two working modes of the CPU. At the beginning, the CPU started to work in real mode. After some mechanism, the CPU jumps to the protection mode. The access space is extended. to return from the protection mode to the actual mode, you can only restart the instance. Intel 8086 is a 16-bit CPU. It has only 16-bit registers, 16-bit data bus, and 20-bit address bus. It can only run in real mode, the physical address = segment value * 16 + offset segment value and offset are both 16-bit addressing capabilities with 1 MB (2 ^ 16*2 ^ 4 + offset.
Since 80386, the CPU has 32-bit address lines, so the addressing space can reach 4 GB. In terms of addressing, the 16-bit register method is not enough. A new method must be developed. In protection mode, although the CPU still uses the original 16-bit cs and ds registers to represent the segment value of the memory address, the segment value in protection mode is only an index, A table that points to a data structure is GDT (or LDT ). In the protection mode, the CPU has a huge addressing capability and provides better hardware protection for a powerful 32-bit operating system. The actual mode and protection mode are actually a set of modes designed for compatibility with the CPU performance after the CPU performance increases. Is a legacy product of history. In fact, the CPU working mode also has a virtual X86 mode. The virtual X86 mode can switch with the protection mode, and the virtual mode can only be switched to the real mode through the reset signal. -------------------------------------- Why segment? The introduction of segments mainly expands the memory address. The address of the program does not need the original hard encoding, and debugging is easier. When the x86 CPU is working in the protection mode, you can use all 32 address lines to access 4 GB memory, because 80386 of all general registers are 32-bit, therefore, any general-purpose register can be used for indirect addressing, and any memory address in 4G space can be accessed without segmentation. However, this does not mean that registers are no longer useful in this period. In fact, segment registers are even more useful. Although there are no segment restrictions in addressing, in protection mode, can an address space be written and can be written to code with a certain priority, whether the execution is allowed or not, and so on, the protection issue arises. To solve these problems, you must define some security attributes for an address space. Segment registers are used in this case. However, parameters in the lower part of the design attribute and protection mode require too much information to be expressed, which can only be expressed with 64-bit long data. We call the 64-bit attribute data a segment descriptor. As mentioned above, it contains three variables: segment physical first address, segment boundary, segment attribute 80386 segment registers are 16 bits (Note: General registers are 32 bits in protection mode, but segment registers are not changed, the 64-bit segment descriptor in protection mode cannot be put down.

How can this problem be solved? The method is to store the segment descriptors of all segments in a specified location in the memory to form a segment Descriptor Table (Descriptor Table). The 16-bit segment register is used for index information, the information in the segment register is no longer the segment address, but the segment Selection Sub (Selector ). You can "select" a project in the segment descriptor table to obtain all the information of the segment. Where is the segment descriptor table stored? 80386 introduces two new registers to manage segment descriptors, namely GDT and LDT. In this way, use the following steps to experience the addressing mechanism in the protection mode. 1. Select the sub-Selector in the segment register. 2. Store the first address of the segment descriptor table in the GDT. 3. Select the sub-register. according to the first address in GDT, you can find the corresponding segment descriptor 4. The segment descriptor contains the physical first address of the segment, and the first address of the segment in the memory is 5, plus the offset, find the linear address of the data stored in this segment (which is the real physical address only when the segmentation mechanism is used ). As described above, GDT is used to provide a segmented storage mechanism, which is provided by the segment register and the descriptor in GDT. Because you need to be able to access 4 GB memory, in 32-Bit mode, the physical address cannot be equal to the segment value * 16 + offset (this method addressing 1 MB, the designer points the segment value * 16 to a table item in a data structure, which is GDT (or LDT). The table item defines the starting address of the segment in detail, the logic address in the format of "segment: Offset" is converted into a linear address through the segment mechanism. if there is only a segmentation mechanism, this address is the physical address of the corresponding real memory. -------------------------------------- Why is paging required? In fact, its main purpose is to implement virtual memory. As mentioned above, linear addresses are linear addresses. If only the segmentation mechanism is available, linear addresses are physical address segments and pagination are implemented through the memory control unit (MMU. Segmentation converts logical addresses to linear addresses, while paging converts linear addresses to physical addresses. For efficiency purposes, www.2cto.com divides a linear address into a fixed length, called a page. What is consistent with the page length is a page frame (physical page ). Each page contains a page. The data structure that maps linear addresses to physical addresses is called a page table. The page table is stored in the primary storage and initialized by the kernel before paging is enabled. In addition, if you copy a program and debug it at the same time. You will find that, from the variable address to the register value, almost all of these two programs are the same, and these same addresses are not obfuscated at all, but they fulfill their respective responsibilities, this is also the credit of the paging mechanism. In addition, because of the paging mechanism, the program uses linear address space instead of physical addresses. It seems that the operating system bit application provides a platform independent of hardware (physical memory), and the application does not have to worry about the actual amount of physical memory, you don't have to worry about the memory you are using, or even whether an address is in the physical memory or in the hard disk. You only need to apply for an application like an operating system, and the operating system is solely responsible for the conversion.

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.