Page 1 of 1
Updating the GDT
Posted: Sat Jan 15, 2011 10:18 am
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?
Re: Updating the GDT
Posted: Sat Jan 15, 2011 1:46 pm
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.
Re: Updating the GDT
Posted: Sat Jan 15, 2011 3:43 pm
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)
Re: Updating the GDT
Posted: Sat Jan 15, 2011 4:08 pm
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.
Re: Updating the GDT
Posted: Sat Jan 15, 2011 4:25 pm
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.