Interrupt descriptor table

Last updated

The interrupt descriptor table (IDT) is a data structure used by the x86 architecture to implement an interrupt vector table. The IDT is used by the processor to determine the memory addresses of the handlers to be executed on interrupts and exceptions.

Contents

The details in the description below apply specifically to the x86 architecture. Other architectures have similar data structures, but may behave differently.

The IDT consists of 256 interrupt vectors and the use of the IDT is triggered by three types of events: processor exceptions, hardware interrupts, and software interrupts, which together are referred to as interrupts:

Real mode

In real mode, the interrupt table is called IVT (interrupt vector table). Up to the 80286, the IVT always resided at the same location in memory, ranging from 0x0000 to 0x03ff, and consisted of 256 far pointers. Hardware interrupts may be mapped to any of the vectors by way of a programmable interrupt controller. On the 80286 and later, the size and locations of the IVT can be changed in the same way as it is done with the IDT (Interrupt descriptor table) in protected mode (i.e., via the LIDT (Load Interrupt Descriptor Table Register) instruction) though it does not change the format of it. [3]

BIOS interrupts

The BIOS provides simple real-mode access to a subset of hardware facilities by registering interrupt handlers. They are invoked as software interrupts with the INT assembly instruction and the parameters are passed via registers. These interrupts are used for various tasks like detecting the system memory layout, configuring VGA output and modes, and accessing the disk early in the boot process.

Protected and long mode

The IDT is an array of descriptors stored consecutively in memory and indexed by the vector number. It is not necessary to use all of the possible entries: it is sufficient to populate the table up to the highest interrupt vector used, and set the IDT length portion of the IDTR accordingly.

The IDTR register is used to store both the linear base address and the limit (length in bytes minus 1) of the IDT. When an interrupt occurs, the processor multiplies the interrupt vector by the entry size (8 for protected mode, 16 for long mode) and adds the result to the IDT base address. [4] If the address is inside the table, the DPL is checked and the interrupt is handled based on the gate type.

The descriptors may be either interrupt gates, trap gates or, for 32-bit protected mode only, task gates. Interrupt and trap gates point to a memory location containing code to execute by specifying both a segment (present in either the GDT or LDT) and an offset within that segment. The only difference between trap and interrupt gates is that interrupt gates will disable further processor handling of maskable hardware interrupts, making them suitable to handle hardware-generated interrupts (conversely, trap gates are useful for handling software interrupts and exceptions). A task gate will cause the currently active task-state segment to be switched, using the hardware task switch mechanism to effectively hand over use of the processor to another program, thread or process.

Common IDT layouts

Protected-mode exceptions and interrupts

In protected mode, the lowermost 32 interrupt vectors are reserved for CPU exceptions. Those are events which are trigerred in the CPU itself, instead of receiving interrupt from the outside hardware. However, some CPU exceptions, such as NMI or #MC, directly relate to events happening in other components of the computer. [5] [6] Interrupt vectors 0x20 to 0xff (hexadecimal) are left free for developer's usage for external interrupts. Interrupts with numbers below 0x20 should not be assigned for external interrupts.

x86 Interrupt descriptor table items [7] [ edit table]
Int. №Mnem.TypeErr. code [a] NameSource
hex dec
0x000#DEFaultNo Divide Error DIV and IDIV instructions.
0x011#DBTrapNoDebug ExceptionInstruction, data, and I/O breakpoints; single-step; and others.
0x022NMI [b] InterruptNoNMI Interrupt Nonmaskable external interrupt.
0x033#BPTrapNo Breakpoint INT3 instruction.
0x044#OFTrapNo Overflow INTO instruction.
0x055#BRFaultNoBOUND Range ExceededBOUND instruction.
0x066#UDFaultNo Invalid Opcode (Undefined Opcode)UD instruction or reserved opcode.
0x077#NMFaultNoDevice Not Available (No Math Coprocessor)Floating-point or WAIT/FWAIT instruction.
0x088#DFAbortYes (zero) Double Fault Any instruction that can generate an exception, an NMI, or an INTR.
0x099FaultNo Coprocessor Segment Overrun (reserved)Floating-point instruction.
0x0A10#TSFaultYesInvalid TSS Task switch or TSS access.
0x0B11#NPFaultYes Segment Not PresentLoading segment registers or accessing system segments.
0x0C12#SSFaultYesStack-Segment FaultStack operations and SS register loads.
0x0D13#GPFaultYes General Protection Any memory reference and other protection checks.
0x0E14#PFFaultYes Page Fault Any memory reference.
0x0F15NoIntel reserved. Do not use.
0x1016#MFFaultNo x87 FPU Floating-Point Error (Math Fault) x87 FPU floating-point or WAIT/FWAIT instruction.
0x1117#ACFaultYes (zero)Alignment CheckAny data reference in memory.
0x1218#MCAbortNoMachine CheckError codes (if any) and source are model dependent.
0x1319#XMFaultNo SIMD Floating-Point Exception SSE/SSE2/SSE3 floating-point instructions
0x1420#VEFaultNoVirtualization ExceptionEPT violations
0x1521#CPFaultYesControl Protection ExceptionRET, IRET, RSTORSSP, and SETSSBSY instructions can generate this exception. When CET indirect branch tracking is enabled, this exception can be generated due to a missing ENDBRANCH instruction at target of an indirect call or jump.
0x16

0x1f
22

31
Un­knownReserved for future use as CPU exception vectors.
0x20

0xff
32

255
InterruptNoExternal interrupts.
    1. This column determines whether the interrupt pushes an exception code to the interrupt handler stack, or not. For some exceptions, this pushes only a zero number
    2. The official Intel documentation does not assign an official mnemonic to this interrupt, but abbreviation “NMI” is widely used to refer to this interrupt, even in the Intel docs itself.

    IBM PC layout

    The IBM PC (BIOS and MS-DOS runtime) does not follow the official Intel layout beyond the first five exception vectors implemented in the original 8086. Interrupt 5 is already used for handling the Print Screen key, IRQ 0-7 is mapped to INT_NUM 0x08-0x0F, and BIOS is using most of the vectors in the 0x10-0x1F range as part of its API. [8]

    Hooking

    Some Windows programs hook calls to the IDT. This involves writing a kernel mode driver that intercepts calls to the IDT and adds in its own processing. This has never been officially supported by Microsoft, but was not programmatically prevented on its operating systems until 64-bit versions of Windows, where a driver that attempts to use a kernel mode hook will cause the machine to bug check. [9]

    See also

    References

    1. "Exceptions - OSDev Wiki". wiki.osdev.org. Retrieved 2021-04-17.
    2. Friesen, Brandon. "IRQs and PICs". Bran's Kernel Development Tutorial. Retrieved 6 June 2024.
    3. Intel® 64 and IA-32 Architectures Software Developer’s Manual, 20.1.4 Interrupt and Exception Handling
    4. Intel® 64 and IA-32 Architectures Software Developer’s Manual, 6.12.1 Exception- or Interrupt-Handler Procedures
    5. Intel Corporation (April 2022). Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 3 (3A, 3B, 3C & 3D): System Programming Guide. Intel Corporation. pp. 6-1 to 6-58.
    6. "Exceptions - OSDev Wiki". wiki.osdev.org. Retrieved 2021-04-17.
    7. Intel Corporation (April 2022). Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 3 (3A, 3B, 3C & 3D): System Programming Guide. Intel Corporation. pp. 6-1 to 6-58.
    8. Jurgens, David. "Interrupt Table as Implemented by System BIOS/DOS". HelpPC Reference Library. Retrieved 6 June 2024.
    9. "Patching Policy for x64-Based Systems". Microsoft . If the operating system detects one of these modifications or any other unauthorized patch, it will generate a bug check and shut down the system.
    General