6

我试图了解机器如何从开机到运行内核。从我收集到的信息来看,在启动期间切换到保护模式以访问更多可寻址内存是很有用的,即使我们最终会切换到更传统的虚拟内存计划,其中页目录和页表以及分段关闭.

似乎要切换到保护模式,必须做 3 件事:

  1. 建立一个全局描述符表(gdt)并使用lgdt指令加载它
  2. 将控制寄存器 CR0 中的 PE 标志/位设置为启用(即值 1)
  3. 执行跳远ljmp

我想知道将段寄存器和指令指针转换为与 gdt 一起使用的索引和偏移量的逻辑。这个逻辑是硬件完成的吗?如果是,那是哪块硬件,为什么要执行ljmp部分过程?为什么不简单地将 CR0 中的 PE 标志设置为启用保护模式(没有后续ljmp)?

4

1 回答 1

6

The first question could be: Why didn't Intel design the chip in a way that setting PE will enter protected mode?

The answer: This would not really be possible; it would assume that the CS register contains a selector whose base address is 0x10*CS.

In other words: If the address "mov CR0,EAX" is located at address 0x0100:0x1200 then the next instruction executed will be at address 0x0100:0x1203. So switching to protected mode will only be possible in conjunction with a jump instruction; otherwise switching PE itself would do an unwanted jump (from 0x0100:0x1203 Real Mode to 0x0100:0x1203 Protected Mode).

Technically the CPU internally stores the selector information of all selectors used. Whenever a selector register changes then the limit, base and so on are loaded. This means that loading the CS register is required for updating the base, limit and so of the CS register. This means: A far jump must be done (because this will load the CS register). Maybe a RETF would also work...

I'm not sure if loading the other segment registers (for example DS) would already work before the far jump so if you load the DS register before the far jump the base address and limit will be taken from the GDT. Would be nice to try this out...

于 2014-10-31T21:26:54.230 回答