x86interruptprotected-mode

Why set or not set the IDT when switching to protected mode?


When switching from real mode to i386 32-bit protected mode, what is the practical difference between not setting an IDT (i.e. not running an lidt instruction) or setting an empty IDT?

Which one is more useful, and which one should I do?


Solution

  • There's no practical difference. If you don't enable interrupts or cause any exceptions, then it's moot because the CPU will never use the IDT. If you do, then both cases will result in failure.

    Specifically, an interrupt or exception occurring when the IDT is empty (limit of 0) will cause a triple fault and reset the CPU. If you leave IDTR uninitialized, it is possible in principle that it's pointing to something that resembles a valid IDT enough that the CPU would actually jump somewhere if an interrupt or exception occurred, and you'd execute whatever garbage that happened to be. More likely, it doesn't, and an interrupt or exception would just cause a triple fault as before.

    So in practice, you must leave interrupts disabled, and avoid causing any exceptions, until you have set up a real (non-empty) interrupt descriptor table. Until then, it's irrelevant what you do with the IDTR, so you might as well not bother to initialize it.