Very good!!! "Write the OS series from scratch" Implements a-GDT (1) "Turn"

Source: Internet
Author: User

Transferred from:

Rights statement: This article for Bo Master original article, without Bo Master permission not reproduced. (Article Source:

Directory (?) [-]

    1. Introducing GDT
      1. What is a GDT?
      2. Descriptor structure

In this article we have completed the following:

    • Introducing GDT
    • Introduction to segment Descriptors
    • Implementation of a segment descriptor
What is the introduction of GDTGDT?

The GDT (Global descriptor Table) is a data structure used to provide a segment storage mechanism, which is provided together with a segment register and a descriptor in the GDT.

In the protection mode, although the register now has 32 bits, but we still use "segment: Offset" in the form of addressing, but the concept of "paragraph" is equivalent to the GDT, the segment value is still 16-bit CS, DS and other registers are represented, but this period value is only equivalent to an index, point to a data structure, This data structure is a GDT table item, which defines the starting address, bounds, attributes, and so on of the segment, which is also called the Descriptor (descriptor)

Descriptor structure
    • Code Snippets and data segment descriptors

      A segment descriptor is a 8-byte structure that contains information such as segment base, Segment bounds, segment properties, and so on

      • Segment Base (32-bit): Represents the Physical address
      • Segment Bounds (20 bits): Indicates the length of the segment (not the address, but the byte length)
      • Segment Properties (12 bits): System, gate, data, etc. properties

      Let's implement this structure:

; a descriptor;3 parameters:;1. Segment Base:32-bit (4 bytes);2. Segment Boundary: Low20 bits;3. Properties:12-bit (low in high-byte4-bit always0)%macro Descriptor3. Define macro descriptor, there are3 parameter DW%2 & 0FFFFh, with parameters2 of Low16-bit populate a word DW%1 & 0FFFFh, with parameters1 of Low 16 bit fills a word db (%1 >> 16) & 0FFh; 1 17- 25 bits populate a byte DW ( (%2 >> 8) & 0f00h) | (%3 & 0F0FFH); 2 with parameters 17- 21 bits and parameters 3 13- 16-bit populated with a word DW (%1 >> 24) & 0FFh; 1" with parameters  32-bit fills a word%endmacro        
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

From the code we can parse the valid bits of each parameter: (f is valid)

    • Segment Base: 0xFFFFFFFF
    • Segment Boundary: 0X000FFFFF
    • Property: 0X0000F0FF

      We got the structure as shown:

Let's describe the role of each person:

  • No. 0, 1 bytes: Indicates segment bounds
  • 2nd, 3, 4 bytes: Represents the segment base
  • 5th, 6 bytes more complex

      • 5th byte (from low to high):
        • 0-3:type, describing the specific properties of the bucket described by the bucket descriptor
    S Segment Type Type value Description
    0 Data segment 0000 Read-only
    0 Data segment 0001 Read-only, visited
    0 Data segment 0010 Read/write
    0 Data segment 0011 Read/write, visited
    0 Data segment 0100 Read-only, down-scaling
    0 Data segment 0101 Read-only, down-scaling, visited
    0 Data segment 0110 Write, scale down
    0 Data segment 0111 Write, scale down, visited
    0 Code snippet 1000 Only perform
    0 Code snippet 1001 Execute only, visited
    0 Code snippet 1010 Execute/Read
    0 Code snippet 1011 Execute/Read, visited
    0 Code snippet 1100 Execute only, consistent code segment
    0 Code snippet 1101 Execute only, consistent code segment, visited
    0 Code snippet 1110 Execute/Read, consistent code segment
    0 Code snippet 1111 Execute/Read, consistent code segment, visited
    1 System segment 0000 (Not defined)
    1 System segment 0001 Available in 286TSS
    1 System segment 0010 LDT
    1 System segment 0011 Busy 286TSS
    1 System segment 0100 286 Call Gate
    1 System segment 0101 Mission Gate
    1 System segment 0110 286 Interrupt Gate
    1 System segment 0111 286 Trap Door
    1 System segment 1000 (Not defined)
    1 System segment 1001 Available in 386TSS
    1 System segment 1010 (Not defined)
    1 System segment 1011 Busy 386TSS
    1 System segment 1100 386 Call Gate
    1 System segment 1101 (Not defined)
    1 System segment 1110 386 Interrupt Gate
    1 System segment 1111 386 Trap Door
    • A 4:s that describes the type of descriptor. For bucket descriptors, S=1 is distinguished from the system segment descriptor and Gate descriptor (s=0).
    • 5-6:DPL, which represents the descriptor privilege level (descriptor Privilege levels), a total of 2 bits. It specifies the privileged level of the described segment, which is used for privilege checking to determine whether the segment can be accessed.
    • 7:p, Existence (Present) bit.
      • P=1 indicates that the descriptor is valid for address translation, or that the segment described by the descriptor exists, that is, in memory
      • P=0 indicates that the descriptor is not valid for address translation, that is, the segment does not exist. Using this descriptor for memory access causes an exception.
      • 6th byte
    • 0-3: paragraph bounds
    • 4:AVL, the software can take advantage of bits. 80386 No provision is made for the use of the bit, and Intel also warrants that any future processors that are developed and manufactured will not be defined or specified for the use of that bit as long as they are compatible with 80386.
    • 5:0 (not defined)
    • The 6:D/B,D bit is a very special bit that has different meanings in the three descriptors that describe the executable segment, the down-extended data segment, or the segment addressed by the SS register (usually the stack segment).
      1. In the descriptor describing the executable segment, the D-bit determines the address used by the instruction and the default size of the operand
        • D=1 indicates that the directive uses 32-bit addresses and 32-bit or 8-bit operands by default, which is also called a 32-bit code snippet;
        • D=0 means that by default, 16-bit addresses and 16-bit or 8-bit operands are used, which is also known as 16-bit code snippets, which are compatible with 80286. You can use the address size prefix and operand size prefixes to change the default address or operand size, respectively.
      2. In the descriptor for the down-extended data segment, the D-bit determines the upper boundary of the segment
        • D=1 indicates that the upper boundary of the segment is 4G
        • D=0 indicates that the upper boundary of the segment is 64K, which is to be compatible with 80286
      3. In describing the segment descriptor addressed by the SS Register, the D-bit determines what stack pointer registers are used by the implicit stack-access instruction (such as push and pop instructions)
        • D=1 represents the use of the 32-bit stack pointer register esp;
        • The d=0 represents the use of a 16-bit stack pointer register SP, which is compatible with 80286
    • 7:g, segment boundary granularity (granularity) bit

      • G=0 indicates the bounds granularity is byte
      • G=1 indicates a boundary granularity of 4K bytes

      Note that boundary granularity is only valid for segment bounds, invalid for subgrade address, and Subgrade address is always in bytes

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Article Source:

Very good!!! "Write OS series from scratch" Implements a-GDT (1) "Go"

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: 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.