Page 1 of 1
Basic Help - GDT stuff
Posted: Wed Nov 30, 2005 12:00 am
by ffe4
Hi,
I've read a lot of things about pmode (internet, intel manuals, tanebaum, etc) but no one tells me what's the real behavior of the GDT in a real-world operating system. Someone can please explain me how a real-world GDT works (segmented memory)? How many segments do usually people create?
Re: Basic Help - GDT stuff
Posted: Wed Nov 30, 2005 12:00 am
by JAAman
most people have only 4 proper segments:
ring0 code
ring0 data
ring3 code
ring3 data
all of these are generally set to FLAT mode as discribed in intel vol3 section 3.2.2
however this is not the only use of the GDT: most OSs also have a TSS -- only one as they use software task-switching and only need the TSS for switching stacks
if they use VBE2 then they also need a couple segments for the PMode16 calls (not sure if VBE3 needs this -- haven't gotten that far myself)
if they use VMode then they may need more for that
Re: Basic Help - GDT stuff
Posted: Tue Dec 06, 2005 12:00 am
by Da_Maestro
The GDT doesn't really do very much in most real world operating systems. It is designed for use in a segmented memory environment, and most languages these days (namely C/C++) don't natively support segmentation.
The GDT is useful for holding TSS information. Contrary to what JAAman says, most modern operating systems (Windows/Linux/BeOS/BSD etc...) use multiple TSS segments for multiple tasks.
So for most practical implementations the GDT has two memory segments in it. Both cover the entire memory space, one for code execution and the other for random memory access. If you want to support multi-tasking in the future, expand your GDT so that it contains enough space for your TSS segments (Mine has enough for 40 tasks simultaneously, which is more than enough for a home brew OS).
Adam
Re: Basic Help - GDT stuff
Posted: Wed Dec 07, 2005 12:00 am
by JAAman
not sure if windows uses multiple TSSs (mabey thats why winXP likes to have so much RAM -- its useing 64MB just for TSSs!!) or not but i ment most as in the majority, not most as in the ones most people use
no reason to use multiple TSS entries in the GDT even if you do have sepatate TSSs for each process
even OSs that use hard-switching (arn't many of them -- and no popular ones) only need two -- just load the unused selector with the destination TSS just before a switch (it gets you around the low task limit imposed by the max entries in the GDT)
Re: Basic Help - GDT stuff
Posted: Mon Dec 12, 2005 12:00 am
by Da_Maestro
Using two TSSes is such a waste of time. My GDT has 40 entries which is only 320 bytes. That's small enough to fit statically compiled in the kernel image. Heck it could be made at compile time if you are clever enough.
The TSS segments themselves are large, and copying them around takes too much time.
Trying to change the TSS entries in the GDT for each task switch is a TERRIBLE hack...if you read the intel manuals properly the 386 was clearly not designed for this, and you risk having unfixable bugs in your kernel.
If you do it the way it was intended, you can take advantage of other nifty features, such as hardware task switching (where the CPU switches tasks on an interrupt automatically).
As for real world operating systems: I know that linux does it properly, with multiple TSS entries and multiple TSS segments.
Windows was created by Microsoft...for all we know it connects to microsoft.com to download the latest update everytime it does a task switch; Who knows what other garbage they've hacked up
Adam
Re: Basic Help - GDT stuff
Posted: Mon Dec 12, 2005 12:00 am
by blackcatcoder
I've done it by setting up a tss selector for each task!
It works very good !
You only need some work around with the busy bit and so on. But if it runs it runs and runs
I took me 1 month to get things right !
Benjamin