X86 Assembly/Global Descriptor Table
From Wikibooks, the open-content textbooks collection
The Global Descriptor Table (GDT) is a table in memory that defines the actions of the processor segment registers. The GDT will define the characteristics of the different segment registers, it will define the characteristics of global memory, and it helps to ensure that the protected mode operates smoothly.
[edit] GDTR
The GDT is pointed to by a special register in the x86 chip, the GDT Register, or simply the GDTR. The GDTR is 48 bits long. The lower 16 bits tell the size of the GDT, and the upper 32 bits tell the location of the GDT in memory. Here is a layout of the GDTR:
|LIMIT|----BASE----|
LIMIT is the size of the GDT, and BASE is the starting address. LIMIT is 1 less than the length of the table, so if LIMIT has the value 15, then the GDT is 16 bytes long.
To load the GDTR, the instruction LGDT is used:
lgdt [gdtr]
Note that to complete the process of loading a new GDT, the segment registers need to be reloaded. The CS register must be loaded using a far jump:
flush_gdt: lgdt [gdtr] jmp 0x08:complete_flush complete_flush: mov ax, 0x10 mov ds, ax mov es, ax mov fs, ax mov gs, ax mov ss, ax ret
[edit] GDT
The GDT table contains a number of entries called Segment Descriptors. Each is 8 bytes long and contains information on the starting point of the segment, the length of the segment, and the access rights of the segment.
The following NASM-syntax code represents a single GDT entry:
struc gdt_entry_struct limit_low: resb 2 base_low: resb 2 base_middle: resb 1 access: resb 1 granularity: resb 1 base_high: resb 1 endstruc
[edit] LDT
Each separate program will receive, from the operating system, a number of different memory segments for use. The characteristics of each local memory segment are stored in a data structure called the Local Descriptor Table (LDT). The GDT contains pointers to each LDT.

