Expand description
§gdt
This module sets up and loads the Global Descriptor Table (GDT) for x86_64 systems.
§What is the GDT?
The Global Descriptor Table (GDT) is a fundamental data structure used by x86 CPUs to define the characteristics of the various memory segments used by the system. Each entry in the GDT describes a segment, including its base address, size, access permissions, and type (code, data, etc). In modern 64-bit (x86_64) systems, segmentation is mostly unused, but the GDT is still required for certain features:
- Defining code and data segments for kernel and user modes
- Setting up the Task State Segment (TSS) for handling interrupts and exceptions
- Providing separate stacks for critical exceptions (via the Interrupt Stack Table, IST)
§Why do we need a GDT?
Even though 64-bit mode uses a flat address space, the CPU still requires a GDT to be loaded. The GDT is also necessary for features like privilege separation (kernel vs user), and for configuring the TSS, which is used for stack switching on interrupts.
§How does this module work?
- Statically allocates and initializes the GDT and TSS
- Sets up segment descriptors for kernel and user code/data
- Configures the TSS with dedicated stacks for critical exceptions (double fault, NMI)
- Loads the GDT and updates the segment registers
This is typically called early in kernel initialization, before enabling interrupts.