Pregunta

I saw in many many oses (and some bootloader), they all disable interrupt (cli) before switch to protected mode from real mode. Why we need do that?

¿Fue útil?

Solución

BIOSes use PIT interrupt (IRQ0) to track time. As soon as you enter protected mode, real mode interrupt handling is no longer valid; CPU in protected mode requires protected mode IDT (Interrupt Descriptor Table). Upon entering protected mode, IDT limit in IDTR (IDT Register) is set to 0 (any interrupt number makes CPU generate an exception), so as soon as PIT (or anything else) generates an interrupt, the CPU will generate an exception, which will make another exception generated, triggering #DF (double fault) and, by consequence, #TF (triple fault).

Also, IRQ0 happening in protected mode will trigger #DE (divide exception) ISR (interrupt service routine), as interrupt vectors from 0 to 31 are reserved for exceptions in protected mode.

So, the (most probable, as other interrupts than PIT might happen too) order of things that happen is like this (note: this assumes that PIT interrupt will be triggered first, but, as I said before, it can essentially be any interrupt, each will lead to #DF and triple fault):

  1. PE bit is set in CR0.
  2. PIT interrupt happens, PIC (Programmable Interrupt Controller) gets signal on it's pin #0.
  3. PIC remapping isn't set, so it triggers IRQ0 on the CPU.
  4. IRQ0 (= #DE) attempts to execute interrupt handler, but IDT's limit is 0, so (IIRC) #GP (General Protection fault) is generated.
  5. IDT's limit is 0, so #DF is generated.
  6. IDT's limit is 0, so #TF is generated.
  7. CPU either stops or reboots.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top