80386ASM programming foundation (10)

zhaozj2021-02-16  45

Mainly introduced segment descriptor, segment selection

In protection mode, the segment is the basis for realizing virtual addresses to linear address transformation. In the protected party, there are three parameters per segment: segment base, segment boundary, segment properties. Segment baset specifies the start address of the middle address space, the segment base length is 32 bits, so any one segment can start from any byte of the 32-bit linear address space, this point and the formar method different, The boundary of the required segment in the formula must be 16. The segment boundary is limited to the size of the specified segment, and the segment boundary limit is represented, and the segment boundary can be byte or 4K unit, which is called the particle size of the segment. When the paragraph limit is limited to byte, the range of the segment is 1 byte to 1m bytes; when the segment limit is in units of 4K bytes, the segment ranges from 4k to 4G. The limit of the segment is also the legality used to verify the offset address. For example, the base number of the segment A is 00123456h, the segment limit is 1000 hours, if the segment limit is limited to byte, the section is the range of 00123456H-00124456H; if The segment is limited to 4k bytes, then the section is the range of 00123456H-00223456H. In fact, the boundaries of the segment can also be used to verify the legality of the offset address, the above example is 1000 hours, then the scope of the offset address is 0-1000h, and if the offset address is not in this range, it will cause abnormal. It should be noted that the data segment is a bit special, because the offset range of the data segment is not only determined by the segment boundary, but also determines by the extension direction because it is necessary to take care of the stack segment (the stack segment is A special data segment, which is extended to the low-end address). If the segment limit is limit, the expansion direction of the segment is expanded to the high-end address, then we can conclude that it is an ordinary data segment, 0-limited is Effective offset range, and Limit is an invalid shift range; if the segment limit is limit, the expansion direction of the segment is extended to the low-end address, then it can be concluded that it is a stack segment. At this time, 0-limited is invalid. The offset range, the above is a valid offset range, just as a common data segment extending to the high-end address. In addition to the stack segment, other segments are naturally expanded to high-end.

Segment base, segment boundary and segment attributes These three parameters are described in the protection mode, and each of the lengths of each descriptor is 8 bytes, each segment has a corresponding descriptor. There are three descriptors in the protection mode: a memory segment descriptor, a system segment descriptor, a door descriptor.

A. Save Descriptor: The memory segment is the code segment and data segment directly executed by the program, and the memory segment descriptor is used to describe the memory segment, or it can be said to describe the code and data segment, its length is 8 bytes, this descriptor structure diagram:

Section 7 Chapter 6 Item 5 Byte 4th byte 3rd byte Question 2 Bytes 1st byte 0 byte

| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------- |

| Segment base address | | | | |

| High 8-bit | segment attributes | Segment base address low 24-bit | segment bound limited 16 digits |

| 24 ~ 31 | Segment attribute, occupying two | 0 ~ 23 | 0 ~ 15 |

| | | Bytes | | | | |

| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------- |

| | |

| | |

_________ | | ____________________________

| 15 14 13 12 11 8 7 6 5 3 0 |

| --- | --- | --- | --- | ------------- | --- | --- - | --- | ------ --------

| G | D | 0 | AVL | Duan Duan Daxie Tall | P | DPL | DT | TYPE |

| --- | --- | --- | --- | --- --------- | --- | ----- | --- | ------ --------

Segment bases and segment boundary are arranged in two domains of the descriptor, mainly to come to the attributes of the paragraph:

A.G (15th), this is the segment boundary granularity, that is, the segment boundary is based on bytes or in units of 4k bytes. G = 0 indicates that the segment boundary is byte, g = 1 indicates that the segment boundary is 4k bytes.

B.D (14th), D is a very special bit, segment that describes the executable segment, to the low expansion data segment or by the SS register. In the descriptor describing the executable segment, the D bit determines the address and operation data default size used by the instruction. D = 1 indicates that the 32-bit address and 32-bit or 8-bit operands are used by default. For 32-bit code segment; D = 0 indicates that the 16-bit address and 16-bit operand or 8-bit operands are used by default, such code segments are referred to as a 16-bit code segment; in the low-extension data segment, d = 1 indicates that the upper limit of the segment is 4G, and the upper limit of the segment represents the segment is 64K; in the description of the SS register addressing, the bit determines where the implicit stack access instruction is used to use the stack pointer register. D = 1 means using a 32-bit stack pointer register ESP, D = 0 means using a 16-bit stack pointer SP, implicit stack access instructions refers to instructions that do not have obvious SP or ESP in those instructions, such as PUSH , POP, PUSHA, POPA, PUSHAD, POPAD are implicit stack access instructions.

C.0 (13th), this bit constant is 0, and the processor after 80386 is retained.

D. AVL (12th), the software can be utilized, mainly for keeping and future processing compatibility.

e. The 11th to 8th is the high segment limit of 4.

F.P (seventh), existing bit, P = 1 indicates that the descriptor is valid for the conversion address. P = 0 indicates that the descriptor is invalid for the conversion address, and if the descriptor will cause an exception.

G.DPL (Descriptor Privelege Level) Descriptor privilege level, a total of 2 bits, which specifies the privilege level of the segment, for privileged checks to determine whether to access this segment.

The type of the H.dt (Descriptor type) descriptor, DT = 0 represents a memory segment descriptor, DT = 0 represents a system segment descriptor and a gate descriptor.

I.Type, a total of 4 digits, explaining the specific properties of the storage segment:

TYPE0: Indicates whether the descriptor is accessed, marked with a A = 0 indicates that the descriptor is not accessed, and a = 1 means that the descriptor has been accessed.

TYPE1: OK according to Type3.

TYPE2: OK according to Type3.

TYPE3: Indicates that the segment described in the descriptor is whether the data segment or code segment is used, with E tag. E = 0 is a non-executable segment, the data segment, the corresponding descriptor is also a data segment descriptor. E = 1 is an executable period, that is, the code segment, the descriptor, the descriptor is the code segment descriptor. If Type3 = 0, that is, the descriptor is a data segment descriptor, TYPE1 indicates whether the data segment can be written, W-marked. W = 0 indicates that the corresponding data segment is not writable, read only. W = 1 indicates that the corresponding data segment can be written. TYPE2 indicates the expansion direction of the data segment and marks with an ED. ED = 0 indicates the high-end extension, and ED = 1 represents the low-end expansion.

If Type3 = 1, that is, the descriptor is a code segment descriptor, TYPE1 indicates whether the code segment is readable, marked with a symbol R. R = 0 indicates that the corresponding code segment is not readable, can only be executed, R = 1 represents the corresponding code readable executable. TYPE2 indicates whether the described code segment is a consistent code segment and is represented by c. C = 0 indicates that the code segment is not a consistent code segment, and C = 1 is a consistent code segment.

TYPE3-TYPE0 can be listed as a table:

________________________________________________________________

| 0000 | Read only |

| _______________________________________________________________ |

| 0001 | Read-only, visited |

| _______________________________________________________________ |

| 0010 | Readable, write |

| _______________________________________________________________ |

| 0011 | Reading and writing, visited |

| _______________________________________________________________ |

| 0100 | Read only, low expansion |

| _______________________________________________________________ |

| 0101 | Read only, low expansion |

| _______________________________________________________________ |

| 0110 | Read / write, to low expansion || _______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

| 0111 | Read / write, to low expansion, have been accessed |

| _______________________________________________________________ |

| 1000 | Execute |

| _______________________________________________________________ |

| 1001 | Execute, Access |

| _______________________________________________________________ |

| 1010 | Executable, readable |

| _______________________________________________________________ |

| 1011 | Executable, Readable, Access |

| _______________________________________________________________ |

| 1100 | Execute, unanimous code segment |

| _______________________________________________________________ |

| 1101 | Execute, unanimous code segment, have been accessed |

| _______________________________________________________________ |

| 1110 | Executable, readable, unanimous code segment |

| _______________________________________________________________ |

| 1111 | Executable, readable, unanimous code segment, have been accessed |

| _______________________________________________________________ |

The structure of the memory segment descriptor can be defined: Descriptor Struct

Segment_limitl16 dw 0; 16-bit limit

Segment_Basel16 DW 0; 16-bit

Segment_basem8 db 0; 8 digits of the section base

Segment_baseh8 db 0; segment base address high 8 digits

Segment_attributes dw 0; segment attribute

Descriptor ENDS

There are multiple sections of a task, and each segment has a descriptor. Therefore, in 80386, in order to facilitate management of these segment descriptors, the descriptor forms a linear table called a descriptor table. There are three descriptors on 80386: GDT (Global Descriptor Table), LDT (LOCAL Descriptor Table), IDT (InterRupt Descriptor Table). In the entire system, the global descriptor table GDT and interrupt descriptors are only one, and the local descriptor table can be made from several sheets. Each descriptor table forms a special 16-bit data segment, such a special data segment can have 8192 descriptors, which use which segment descriptor, determined by the segment selection. Each task has its own partial descriptor table LDT, which contains its own code segment, data segment, stack segment, and some door descriptors used by this task. As the task is switched, the LDT is also switched. The GDT contains a descriptor of each task that may or can access, typically contains a descriptor describing the code segment, data segment, and stack segments used in the operating system, and also includes a descriptor descriptor LDT. When the task is switched, the GDT is not switched. The entire virtual address space of a task can be divided into two halves, half space descriptors in the global descriptor table GDT, half-empty descriptors in the local descriptor table LDT. Since the global descriptor table and the local descriptor table can include up to 8192 descriptors, and the maximum length of the segment described in each descriptor is 4G, so the maximum virtual address space is: 8192 * 4G * 2 = 64TB .

Segment Selecto is used to determine which descriptor using the descriptor table. The logical address in the form of form is determined by the segment address * 16, and the virtual address space is determined by the segment selector and segment offset in the protection mode, and the segment selector is replaced by the segment value. In fact, the segment base address can be determined by the segment selector. The high-selection of the selector 13 is the index number in the descriptor table, which is used to determine the descriptor because it is 13 bits, so there is a maximum of 2 to 8192 descriptors, index number: 0-8191. The tag TI indication is a read descriptor from a global descriptor or from a local descriptor table. Ti = 0 Indicates that the descriptor is read from the global descriptor table, and Ti = 1 indicates the reading descriptor from the local descriptor table. RPL represents the request privilege level for privilege check. Assume the segment selector is 88h, then the privilege level of the request is 0, and the descriptor is read from the global description table, and the index number of the descriptor is 11h. There is a special selection called an empty selector, its index = 0 (i.e., 13 digits is 0), Ti = 0, and the RPL can be arbitrary. An abnormality occurs when the memory is accessed with an empty selector. Empty selector corresponds to the 0th descriptor in the global description table, so the 0 descriptor in the global descriptor table is always not accessed. If ti = 1, then it is not empty to select the child, which is specified is the 0th descriptor in the current local descriptor table. In order to get the basic information (segment base, segment boundary, segment attribute) from the segment selection, starting from 80386 to each segment register on the hardware, writing a program for us It is not programmable. With this high-speed buffer register, whenever the selection sub-load register is selected, the processor will automatically load the corresponding descriptor in the descriptor table, and put the information of the descriptor to the high speed buffer register. Accelerated access speed, the following is a structural diagram of segment selectors: 15 ___________________________________________________0

| | TI | RPL |

| _____________________________________________________ | ___ | ________ |

转载请注明原文地址:https://www.9cbs.com/read-27084.html

New Post(0)