GDT, LDT and IDT

The three important system tables GDT, LDT and IDT. First of all, it is explained that these three tables are built in memory by the operating system or system programmers, not where they are solidified, so they can be read and written in theory. .

All three tables are descriptor tables. The descriptor table is composed of several descriptors. Each descriptor occupies 8 bytes of memory space. There can be a maximum of (8K) 8129 descriptors in each descriptor table. The descriptor describes the size, address and various states of a segment. There are three types of descriptor tables, namely the global descriptor table GDT, the local descriptor table LDT and the interrupt descriptor table IDT.

GDT table and IDT table

In the entire system, there is only one global descriptor table GDT (one processor corresponds to one GDT), and the GDT can be placed anywhere in the memory. The system uses the GDTR register to store the base address of the current GDT table. Use the LDTR register to store the address of the LDT table.

register Load instruction Save instruction
GDTR register LGDT SGDT
LDTR register LLDT SLDT
IDTR register LITTLE SIDT

Since each process has its own set of program segment, data segment, and stack segment, with the local descriptor table, the program segment, data segment, and stack segment of each process can be packaged together, and it can be achieved by changing LDTR Access to segments of different processes. With the switching of tasks, the current local descriptor table LDT of the system also switches. Through LDT, the private segments of each task can be isolated from other tasks, so as to achieve the purpose of protection. Through GDT, the segments that each task needs to use can be shared.

We can understand GDT and LDT as follows: GDT is the first-level descriptor table, and LDT is the second-level descriptor table.
Insert picture description here
Recommend two good articles:
GDT, LDT, GDTR, LDTR detailed explanation, including your thorough understanding

Briefly explain how Windows uses the FS segment register. In
this article, the actual operation from the FS value query GDT table and LDT table is converted to a virtual address, which can deepen the impression of the segment register.
The process is as follows :
select the sub-index to determine the serial number of the descriptor,
query the descriptor to determine the segment base address
, the sum of the segment base address and the offset is the linear address

IDT table

There is only one IDT table in the entire system, and the GDT table can also be placed in any register in the memory. The IDTR register stores the base address of the IDT table. The x86CPU can support up to 256 types of interrupt ISR (interrupt handler), each entry is 8 bytes. Intel specifies or reserves the role of the first 32 interrupt numbers, and the operating system can specify the role of the remaining interrupt numbers.

A new interrupt can be generated during the interrupt process. Interrupts have priority, and high-priority interrupts can "interrupt" low-priority interrupts. Some ISRs cannot be interrupted. You can use STI (set interrupt-enable flag) and CLI (clear interrupt-enable flag) to set the IF flag to enable and disable interrupts.
!idt -a command can see the addresses of all interrupt processing functions.

Another tip: each entry of IVT in real mode is four bytes. The address is from 0x0h-0x3FFh and up to 256 IVT.
Int 0x13h IVT offset address: 0x4ch
Int 0x2Eh IDT offset address: 0x170h (KiSystemService)

Insert picture description here

typedef struct _IDTR //IDT基址
{
    
    
    USHORT limit; //范围 占8位
    ULONG base;  //基地址 占32位 PIDT_ENTRY类型指针
}IDTR,*PIDTR;

Each member of the IDT table is an 8-byte data structure _IDT_ENTRY.

typedef struct _IDT_ENTRY
{
    
    
    USHORT offset_low; //中断处理函数地址低16位
    USHORT selector;
    UCHAR  reserved;
    UCHAR  type:4;
    UCHAR  always0:1;
    UCHAR  dpl:2;
    UCHAR  present:1;
    USHORT offset_high;//中断处理函数地址低16位
}IDT_ENTRY,*PIDT_ENTRY;//+3.offset_high<<16+offset_low //int 3 中断处理函数地址

references

GDT, LDT, GDTR, LDTR detailed explanation, including your thorough understanding
http://www.techbulo.com/708.html

A simple explanation of how Windows uses the FS segment register
https://bbs.pediy.com/thread-159935.htm

Guess you like

Origin blog.csdn.net/qq_43312649/article/details/109674318