Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

GDT

This document describes the Global Descriptor Table (GDT) implementation in the Kyronix kernel. It is the child of x86-64 Architecture.

Source

kernel/arch/x86_64/gdt.c

Overview

The GDT provides segment descriptors for kernel and user code/data, plus per-CPU Task State Segment (TSS) descriptors. In long mode, segments are mostly flat (base=0, limit=4 GiB), but the TSS is essential for interrupt stack switching and I/O permission bitmap.

GDT Structure

typedef struct {
    gdt_entry_t entries[5];         // null, kcode, kdata, udata, ucode
    tss_desc_t  tss[MAX_CPUS];     // one TSS descriptor per CPU
} gdt_t;  // aligned(16)

Segment Values

EntrySelectorEncoded ValueMeaning
Kernel code0x080x00AF9A000000FFFF64-bit, ring 0, executable, readable
Kernel data0x100x00CF92000000FFFF64-bit, ring 0, writable
User data0x180x00CFF2000000FFFF64-bit, ring 3, writable
User code0x200x00AFFA000000FFFF64-bit, ring 3, executable, readable

TSS Structure

The TSS (104 bytes on x86-64) provides:

  • rsp0: Ring 0 stack pointer, used on privilege level transitions (interrupts from ring 3)
  • ist[7]: Interrupt Stack Table entries for critical handlers (Double Fault, NMI)
  • iopb_offset: Offset to I/O permission bitmap
typedef struct {
    uint32_t reserved0;
    uint64_t rsp0, rsp1, rsp2;
    uint64_t reserved1;
    uint64_t ist[7];
    uint64_t reserved2;
    uint16_t reserved3;
    uint16_t iopb_offset;  // = sizeof(tss_t)
} tss_t;  // 104 bytes, packed

Key Functions

FunctionDescription
gdt_init()Initialize GDT, BSP TSS, load GDT and TSS on BSP
gdt_ap_load(cpu_id)Load GDT and TSS on an Application Processor
gdt_set_kernel_stack(rsp0)Update TSS rsp0 for the current CPU
tss_init(tss)Initialize TSS fields (IST stacks, IOPB offset)

IST Stacks

IST IndexStackSizeAssigned To
1g_ist_df16 KiBDouble Fault (#8)
2g_ist_nmi16 KiBNMI (vector 2)

NOTE: IST stacks provide dedicated, non-overflowable stacks for the most critical exceptions, preventing kernel stack corruption during Double Faults.

Last reviewed: 2026-07-22