Whats a GDT?

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
Greg

Whats a GDT?

Post by Greg »

I've been looking through some simple OS code and
I've found references to a GDT and an asm command I
don't know lgdt. I assume this means load GDT.
So what is an GDT?
J. Weeks

RE:Whats a GDT?

Post by J. Weeks »

>On 2002-05-21 13:54:05, Greg wrote:
>I've been looking through some simple OS code and
>I've found references to a GDT and an asm command I
>don't know lgdt. I assume this means load GDT.
>So what is an GDT?

It's a global descriptor table. You know how
segments work in real mode, right? A segment
every 16 bytes, spanning 64k, right?

Well, in pmode, segments are a little different.
You can define where they start, how long they
are, and how they should be accessed (eg, are they
writeable?).

The GDT is the table that holds all the above
information. Each memory segment is described
by an 8 byte entry in the GDT (search this site
for the exact format (it's also in the intel
386+ docs, and of course, the source code you
have)).

So, whenever you want a chunk of memory to be
accessable, you must create a descriptor in the
GDT for that memory area (and your segment
register will hold the index of that descriptor
withen the table << 3 & RPL, where RPL is the
requested privelege level (see the Intel docs
for a better explaination... I'm not good with
explainations ;)

Also of note, the first entry in the GDT is always
a null descriptor, and simply contains 8 bytes of
0x00.

You could, conceviably, have only three descriptors
in the GDT. The first, null, descriptor, a code
segment, and a data segment (which can double as a
stack segment). Many people do this, creating
these segments to start at 0x0 and extend to
0xffffffff (2^32 = 4.29 GB).

Oh yeah, and you should know that there is a
granularity bit withen your descriptors to define
wether segment lengths should be in 4KB chunks,
or 4MB chunks. That's why the segment length is
only 20 bits... 2^20 = only 1048576 bytes, but
with a 4KB granularity, = the full 4.29GB, OR
with a 4MB granularity some even bigger number ;)

Hope that helps,
Jeff

PS: You're right, LGDT = Load GDT, and you pass
it a 48-bit long pointer... 16-bit segment +
32-bit offset.
Post Reply