Addressing and segmented memory management mechanisms of intel X86 CPU Series

Source: Internet
Author: User
From: http://blog.csdn.net/tianzhhy/article/details/5802192

Category:
Operating System Principle

273 reading comments (0) favorites
Report

 

Http://www.cnitblog.com/ygb/articles/8872.html

 

Refer to <Linux kernel source code scenario analysis> and <Linux kernel version: 2.4.0>

Having any problems, send mails
Viloner@163.com

 

 

Addressing and segmented memory management mechanisms of intel X86 CPU Series

In the x86 series, 8086 and 8088 are 16-bit processors, while 80386 is a 32-bit processor from 80286, and 8088 is a series from 80386, that is, an intermediate step from 16-bit to 32-bit. Although 80286 is still a 16-bit processor, it starts the transition from "initial address mode" to "protection mode" in addressing mode.

When we say a CPU is "16-bit" or "32-bit", it refers to the width of the "self-reported logical unit" (ALU) in the processor. The data line section in the system bus, known as the "Data Bus", usually has the same width as ALU (with exceptions ). What about the "Address Bus" width? The most natural address bus width is the same as that of the Data Bus. This is because from the perspective of programming, an address, that is, a pointer, should preferably be the same as the length of an integer. However, considering the 8-bit CPU addressing capability, it is actually unrealistic because an 8-bit
Bit addresses can only be used to search for 256 different address units, which is obviously too small. Therefore, generally, the 8-bit CPU address bus is 16-bit. However, 16 bits are too small. Intel decides to use 1 MB of memory address space in its 16-bit CPU, that is, 8086, and the address bus width is determined accordingly, that is, 20 bits. However, there is a problem. Although the address bus width is 20 bits, the ALU width in the CPU is only 16 bits, that is to say, the pointer length directly calculated is 16 bits. How can we fill this gap? Intel designed a segmentation method that seems clever at the time.

Intel has set four "segment registers" in the 8086cpu: CS, DS, SS, and ES for executable code, namely instructions, Data, stacks, and others. Each segment register is 16 bits, which correspond to the high 16 bits in the address bus. The "internal address" in each "access" command is 16 bits, but the content in a segment register is automatically added to the CPU before the address bus is sent, form a 20-bit actual address. In this way, the conversion from a 16-bit internal address to a 20-bit actual address is realized, or the ing is realized ". Note that the content in the segment register corresponds
The high 16 bits in the 20-bit address bus, so In addition, the high 12 bits in the internal address are used together with the 16 bits in the segment register, while the low 4 bits in the internal address remain unchanged. However, this method is flawed, mainly because there is no address space protection mechanism. For each "base address" determined by the content of the segment register, a process is always able to access the 64 K bytes of continuous address space from then on, and cannot be limited. At the same time, commands that can be used to change the content of the segment register are not "privileged commands". That is to say, by changing the content of the segment register, a process can access any unit in the memory at will, there is no restriction at all. You cannot restrict the memory access of a process, or protect other processes and the system itself. Correspondingly,
If the CPU lacks the restrictions or protection for memory access, there will be no memory management, or a central processor in the modern sense. Because 8086 of this memory addressing method lacks protection for memory space, it is called the "real address mode" to distinguish it from the "protection mode" that emerged later ".

To address this defect of 8086, Intel has implemented its "protection mode" since 80286 ". At the same time, the 32-bit 80-bit CPU was also developed successfully soon. In this way, from 8088/8086 to 80386, we have completed a leap from the original 16-bit CPU to the modern 32-bit CPU, and 80286 has become an intermediate step of this leap.

80386 is a 32-bit CPU, that is, its ALU Data Bus is 32-bit, the most natural address bus width should also be consistent with the data bus. When the address bus width reaches 32 bits, its addressing capability reaches 4 GB, which seems sufficient for memory. Therefore, if a 32-bit CPU is designed, its structure can be very concise and natural. However, 80386 cannot do this. As a member of the product series, 80386 must maintain those segment registers, support the real address mode, and support the protection mode. Therefore,
Intel decided to build a protection mode based on the segment register and keep the segment register as 16 bits (so that the original four segment registers can be used ), however, two segment registers FS and GS are added. To implement the protection mode, it is not enough to use the segment register to determine a base address. At least one segment length is required, and other information, such as Access Permissions, is required. Therefore, what we need here is a data structure, not a simple base address. In this regard, the basic idea of Intel designers is to change the block register function in the protection mode, from a simple base address to a pointer to such a data structure. Therefore, when a memory access Command sends out a memory address,
The CPU converts the 32-bit logical address in the instruction to the 32-bit physical address according to the following process:

1. Determine which segment register to use based on the nature of the instruction. For example, the address of the transfer instruction is in the code segment, and the address of the Data instruction is in the data segment. This is the same as the real address mode.

2. Find the corresponding "segment description structure" based on the content of the segment register ".

3. Obtain the base address from "segment description structure.

4. Use the address in the instruction as the displacement to check whether the segment length exceeded the line compared with the segment length specified in the segment description structure;

5. Determine whether the permission is unauthorized based on the nature of the instruction and the access permission in the segment descriptor;

6. Finally, the address in the instruction is used as the displacement and added to the base address of the segment to obtain the physical address.

Although the segment description structure is stored in the memory, it is loaded into a group of "shadow" structures in the CPU in actual use, while the CPU is running, it uses its "shadow" in the CPU ". From the perspective of protection, when the internal address (or "Logical Address") is converted into a physical address, A row comparison must be performed at a certain stage to access non-privileged user programs by playing with certain tricks (such as modifying the content of segment registers, modify the content of the segment description structure) to illegally access the space or system space of other processes, thus implementing protection.

After understanding this idea, the 80386 segmented memory management mechanism is easier to understand. The following is the actual implementation of this mechanism.

First, two registers are added to 80 Gb/s CPU: global segment description table register GDTR, and local segment description table register ldtr, it can be used to point to a segment description structure array stored in the memory, or to a segment description table. Since these two registers are newly added, there is no problem of compatibility with the original commands, the dedicated commands accessing these two registers are designed as "privileged commands ".

On this basis, the 13-bit high of the segment register is used as the subscript (INDEX) of the specific description structure in the access segment description table, as shown in


Segment register Definition

RPL: a two-digit binary number with a request privilege level. The privilege level is the privilege level of the segment to be accessed.

Ti: Table indicator. If the value is 0, select the descriptor from gdt; if the value is 1, select the descriptor from LDT.

Index: Index. Indicates the sequence number of the access descriptor in the segment descriptor table. A total of 213 = 8192.

The segment description table pointer in GDTR or ldtr is combined with the subscript given in the segment register to determine where the specific segment description table item is in memory, remove the low 3 bits of the segment register content and add them to the base address in GDTR or ldtr to get the start address of the description table item. Therefore, you cannot modify the content of the description table to play a protection role. The size of each segment description table item is 8 bytes, and each description table item contains the base address and segment size of the segment, and then

Add other information, as shown in the following figure:


8-byte segment: Description of table items

The B31-B24 and B23-B16 in the structure are bit16 ~ Bit23 and bit24 ~ Bit31. and l19 ~ L16 and L15 ~ L0 indicates the segment length (Limit) bit0 ~ Bit15 and bit16 ~ Bit19.

G: granularity bit.

When G = 1, the limit length is in the unit of pages;

When G = 0, the limit length is in bytes.

D: Default operand width.

D = 1, which is a 32-bit data operation segment;

When d = 1, it is a 16-bit data operation segment.

AVL: available space.

This bit is reserved for use by the operating system or application.

DPL is a two-bit segment, while type is a four-bit segment. They are defined as follows:


P: Bit

If it is equal to 1, the segment has been loaded into the memory;

If it is equal to 0, it indicates that the segment is not in the memory. An exception occurs when you access this segment. N

DPL: descriptor privilege level, indicating the privilege level of this segment

S: descriptor type bit

When the value is 1, this segment is a code segment, data segment, or stack segment;

If it is 0, it is the system segment descriptor.

E: executable bit to distinguish between code segments and data segments

When S = 0 and E = 1, this is a code segment that can be executed.

When S = 0 and E = 0, this is a data segment or stack segment and cannot be executed.

When E = 0, the two following values are ed and W;

If e = 1, the two following values are C and R.

ED: Extended Direction bit

When the value is 0, the segment is extended from the low address to the high address, and the offset is less than or equal to the limit length.

When the value is 1, the segment is extended from the high address to the low address, and the offset must be greater than the limit length.

W: Write allowed bit

If the value is 0, data segment cannot be written;

If the value is 1, this data segment can be written.

C: consistent bit

When the value is 0, this segment is not a consistent code segment

When the value is 1, this segment is consistent with the Code segment

R: Read allowed bit

When the value is 0, the content of this segment cannot be read.

When the value is 1, the content of this segment can be read.

A: Access bit

1 indicates that the segment has been accessed

0 indicates that the segment has not been accessed.

You can also use a piece of "pseudo code" to describe the entire segment description structure:

Segment description structure:
Typedef struct {

Unsigned int base_24_31: 8; // The base address can contain up to 8 digits.
Unsigned int G: 1; // granularity table segment length unit: [0] bytes [1] 4kb
Unsigned int d_ B: 1; // default operation size access mode [0] 16-bit [1] 32-bit
Unsigned int unused: 1; // fixed to 0
Unsigned int AVL: 1 // avaliable, available for System Software
Unsigned int seg_limit_16_19: 4; // the maximum length of the segment is 4 bits.
Unsigned int P: 1; // segment present, [0] the content of this segment is not in memory
Unsigned int dp1: 2; // descriptor privilege level, required to access this section
Unsigned int S: 1; // description item type [1] system [0] Code/Data
Unsigned int type: 4 // segment type, used together with the s flag
Unsigned int base_0_23: 24; // The base address is 24 characters lower.
Unsigned int seg_limit_0_15: 16; // The 16-bit lower part length

} Descriptor;

Taking the bitwise field type as an example, ": 4" indicates that its width is 4 bits. The size of the entire data structure is 64-bit, that is, 8 bytes.

When reading and writing memory units, the CPU needs to check whether the content of the segment descriptor is consistent with the current operation, greatly reducing the CPU operation efficiency. To solve this problem, the CPU internally sets the segment descriptor high-speed cache, which can be seen as an extension of the segment register. The expanded segment register is divided into two parts, one of which is visible (for the Program) and the other is invisible, just like the original segment register, is used to put the space of the Shadow description item, this part is exclusively used inside the CPU. During instruction execution, gdt or LDT is required only when the value of the segment register changes.
. If the value of the segment register remains unchanged, the segment descriptor in the high-speed cache (that is, the part that expands the segment register) can be directly referenced, thus avoiding frequent reading of the segment descriptor in the primary memory. Improves CPU efficiency.

On the basis of segment-level memory management of 80386, if each segment register points to the same descriptive item, the base address is set to 0 in this descriptive item, and the segment length is set to the maximum, this forms an entire segment that covers the entire 32-bit address space from 0. Because the base address is 0, the physical address is the same as the logical address, and the address that the CPU puts on the address bus is the address given in the command. Such an address is different from a "hierarchical" address composed of "segment register/displacement", so intel calls it a "flat" address. Linux
The source code of the kernel (or more precisely GCC) uses a flat address. It should be pointed out that the use of flat addresses does not mean to bypass the complete segment Memory Management Mechanism of segment description tables and segment registers, but is only a special case of segment memory management.

With 80386 hardware support for segment-based memory management, You can implement segment-based virtual memory management. As mentioned above, when the content of a segment register changes, the CPU should find the corresponding segment description item based on the new segment register content and the content of GDTR or ldtr and load it into the CPU. In some processes, the CPU will check the P flag in this description (indicating "present"). If the P Flag is 0, indicates that the part of the description is not in the memory (that is, somewhere on the disk). At this time, the CPU generates an exception.
(Exception, similar to interruption), and the corresponding service program can read the content of this segment from the disk swap area to a certain place in the memory, and set the base address in the description accordingly, set the P Flag to 1. correspondingly, a storage segment that is not currently used in the memory can be written to the disk, and the P Flag in its description is changed to 0.

The support for segmented memory management is only part of the i386 protection mode. If there is no separation between the system status and the user status, and the establishment of privileged commands (which can only be used in the system status), even with the aforementioned segment memory management, it cannot be protected. As mentioned above, the settings of privileged commands are all privileged commands if the commands for loading and storing GDTR and ldtr, lgdt/lldt, and sgdt/sldt. It is precisely because these privileged commands can only be used in the system state (that is, in the kernel of the operating system) that the user program cannot be changed.
The content of GDTR and ldtr cannot determine the location of its segment description table in the memory, nor can it access the space of its segment description table (it can only be accessed in the system status ), thus, the protection mechanism of the system cannot be broken by modifying the segment description. Then, how does 80386 separate the system status and user status, and provides a mechanism for switching between the two states?

80386 is not just divided into the system status and user status as the general CPU does, but into four privileged levels, of which level 0 is the highest and Level 3 is the lowest. Each Command also has its applicable level. As mentioned above, lgdt can only be used in a 0-level state, while general input/output commands (In, out) it is set to level 0 or level 1. Generally, the user's applications are all three levels. In general, the current running level of a program is determined by the partial description of its code segment (that is, the partial segment description pointed by the segment register CS ).
The DPL field is determined (DPL indicates "descriptor privilege level "). Of course, the DPL field of each description item is set from the kernel level 0. The DPL field described in the global segment is different. It indicates the required level.

As mentioned above, the high 13 BITs in the 16-bit segment register are used as subscripts to access the segment description table. What does the low 3 BITs do? The following uses a piece of pseudocode to explain:

Typedef struct
{

Unsignedshort seg_idx: 13;/* subscript of the 13-bit segment Description */

Unsignedshort Ti: 1;/* segment description table directive, 0 indicates gdt, 1 indicates LDT */

Unsignedshort RPL: 2;/* Requested privilege level, required priority */

} Segment register;

When the Ti bit in the segment register CS is 1, it indicates that the global segment is used to describe the table. If it is 0, it indicates that the local segment is used to describe the table, while RPL indicates the required permissions. When the content of a segment register is changed, the CPU checks the content, to ensure that the current execution permission of the program and the required permissions specified by the segment register are not lower than the permissions of the memory segment to be accessed DPL.

How to switch between different execution permissions will be discussed in Process Height, system calls, And interrupt handling. Besides the global segment description table pointer GDTR and the local segment description table pointer ldtr, in fact, the i386cpu also has an interrupt vector table pointer register idtr, and a process (in Intel terminology, it is called a "task", task) the related register Tr and the "task status segment" tss that describes the task status.



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.