Page 1 of 1

Set up GDT in Protected Mode (lgdtr question)

Posted: Wed May 15, 2024 5:43 am
by d92os
Hi all, I'm experimenting with writing some simple bootloaders and currently I'm trying to switch to protected mode from real mode.
When reading the https://wiki.osdev.org/Global_Descriptor_Table wiki page, I stumbled upon something that made me wonder and question my understanding so far.

Specifically when loading the 6-byte register gdtr via lgdtr, the wiki says for the offset:
" Offset: The linear address of the GDT (not the physical address, paging applies)."

I don't get how paging applies at this point, when we still have not set page tables (or even disable paging because we can do so in protected mode).
I was under the impression that the offset at this point is the physical address of the start of the GDT.

Thanks!

Re: Set up GDT in Protected Mode (lgdtr question)

Posted: Tue Jun 11, 2024 12:34 pm
by nullplan
If you have not yet enabled paging, then yes, linear and physical address are going to be the same. But once you do enable paging, that is no longer true, and you must load the GDT again with its linear address. When you enable paging, the GDTR remains the same, so when a segment register is loaded afterwards, it will try to look up the segment descriptor under the given address. But if that was a physical address and now you have paging active, it is not going to find anything there and fault because of it.