Set up GDT in Protected Mode (lgdtr question)

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
d92os
Posts: 1
Joined: Wed May 15, 2024 5:31 am

Set up GDT in Protected Mode (lgdtr question)

Post 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!
nullplan
Member
Member
Posts: 1760
Joined: Wed Aug 30, 2017 8:24 am

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

Post 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.
Carpe diem!
Post Reply