Question

I am referring to Broken Thorn's OS development tutorial. I am currently at the stage of executing the second stage bootloader to load the GDT and enter protected mode.

I understood how the GDT works and how to enter into protected mode. However, my confusion is with the first instruction executed after loading the cr0 register:

jmp 08h:Stage3      ; far jump to fix CS. Remember that the code selector is 0x8!

At this point, CS has not been loaded with a selector value that points to the code descriptor, and we are already in protected mode. This CS value could be different than the value being used to execute real mode instructions, and hence adding value of IP would also result in an address different from that of the jmp. So wouldn't this result in the execution of some garbage code based on the value of CS ? Or is there something I am missing ?

Was it helpful?

Solution

Every segment selector has shadow part which contains its actual base address. This hidden part is updated during segment loading instructions.

That means that before the far jump or some other instruction loading cs has been executed, it still has the base address set in real mode and eip offset is calculated relatively to it.

Read: Intel Software Developer Manual, vol.3, ch.3.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top