Why do interrupts need to be disabled before switching to protected mode from real mode?

為{幸葍}努か 提交于 2019-12-10 13:36:59

问题


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?


回答1:


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.


来源:https://stackoverflow.com/questions/16536035/why-do-interrupts-need-to-be-disabled-before-switching-to-protected-mode-from-re

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!