Updating the GDT

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Updating the GDT

Post by mariuszp »

I'm not sure if this is really "OS Design & Theory".

My question is quite simple, but I can't find the answer to it.
I am making a new OS, where I really need to change segments at run-time.
I know that in Protected Mode all segments are stored in the GDT.
Is it possible to possible to change the GDT and then reload it?
Tosi
Member
Member
Posts: 255
Joined: Tue Jun 15, 2010 9:27 am
Location: Flyover State, United States
Contact:

Re: Updating the GDT

Post by Tosi »

Instead of having to change GDT entries at run-time, you could just add multiple entries, for instance, if you're doing a higher-half kernel.
Then all that is needed is to load the segment registers with the right selector offset. The intel manual says that you can have a maximum of 8192 (!) descriptors apparently. I don't think it would be a good idea to modify GDT entries once it's been loaded, especially for descriptors that are in use though.
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Re: Updating the GDT

Post by mariuszp »

But the segments that I need to add (and delete) cannot be determined at boot-time (they are calculated later on...)

Anyway, can I change the segments that are not in use? e.g. modify the user-mode code segment when it is not being used at that specific moment???

BTW, I just tried changing an unused segment (which is within GDT limits), and it does not seem to cause any trouble... (at least oon qemu)
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Updating the GDT

Post by Combuster »

Any time a segment register is written (not necessarily changed!), it reads the corresponding entry in the GDT or LDT. You don't need to reload GDTR for that to work, you just modify the GDT currently in use and reload the appropriate segment registers.

Live editing of the GDT is however prone to reentrancy issues and race conditions, so be careful.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Re: Updating the GDT

Post by mariuszp »

Combuster wrote:Any time a segment register is written (not necessarily changed!), it reads the corresponding entry in the GDT or LDT. You don't need to reload GDTR for that to work, you just modify the GDT currently in use and reload the appropriate segment registers.

Live editing of the GDT is however prone to reentrancy issues and race conditions, so be careful.
Right. Thanks.
Post Reply