Page 1 of 1

GDT?

Posted: Thu Oct 03, 2002 1:14 pm
by gtsphere
Global Discripter Table

can someone explain why we need one??

i don't understnad the reasoning behind one

thanks in advance

Re:GDT?

Posted: Thu Oct 03, 2002 1:20 pm
by Tom
You need one because ( as far as I know ) it tells PMode where everything is. ( Correct me if i'm wrong ).

Re:GDT?

Posted: Thu Oct 03, 2002 7:44 pm
by dronkit
In real-mode (rmode) you have access to memory via
segments which are up to 64Kbytes in size, for example:
07c0:0000.

In protected mode (pmode) memory is still segmented, but
each one of this segments can be up to 4GB in size.

In rmode you have direct access to physical memory by
multplying the segment value by 16 and adding the offset,
for example:

07c0:0000 = 07c0 * 16 + 0 = 7c00.

In pmode, the segment register value is just an offset inside the GDT.

(sic) from Intel 386 programmers manual:
"A descriptor table is simply a memory array of 8-byte entries that contain descriptors, as Figure 5-5 shows. A descriptor table is variable in length and may contain up to 8192 (2^(13)) descriptors. The first entry of the GDT (INDEX=0) is not used by the processor, however.

The processor locates the GDT and the current LDT in memory by means of the GDTR and LDTR registers. These registers store the base addresses of the tables in the linear address space and store the segment limits. The
instructions LGDT and SGDT give access to the GDTR; the instructions LLDT and SLDT give access to the LDTR."

LGDT loads a 40-byte register (GDTR) with the start address
of the GDT. GDTR contains the GDT base (24 bits) and the GDT limit (16 bits).

The limit field of descriptors for descriptor tables is used by the processor to prevent programs from selecting a table entry outside the descriptor table.The limit of a descriptor table identifies the last valid byte of the last descriptor in the table. Since each descriptor is eight bytes long, the limit value is N*8 - 1 for a table that can contain up to
N descriptors.

The first entry in the GDT is 00h, the second is 08h, then 10h, etc.. The reason that the entries progress in this manner is because the 3 least significant bits are used for other information. So to find the index into the GDT you do a segment & 0xfff8 (DT = Selector & 0xfff8).

For all types of segments except expand-down data segments, the value of the limit is one less than the size(expressed in bytes) of the segment. The processor causes a general-protection exception in any of these cases:
* Attempt to access a memory byte at an address > limit.
* Attempt to access a memory word at an address >= limit.
* Attempt to access a memory doubleword at an address >= (limit-2).

(sic) from Intel 386 programmers manual:
" The TYPE field of a descriptor has two functions: 1. It distinguishes among different descriptor formats. 2. It specifies the intended usage of a segment.
Besides the descriptors for data and executable segments commonly used by applications programs, the 80386 has descriptors for special segments used by the operating system and for gates. Table 6-1 lists all the types defined
for system segments and gates. Note that not all descriptors define segments; gate descriptors have a different purpose that is discussed later in this chapter."

To make a long story short: You need a GDT in pmode so
you can use segments. Each segment is now a pointer
inside the GDT. The memory referenced by this pointer is an 8 byte data-structure wich contains several need information to know which kind of memory you want to access to. For example, protection (execute, read, access privilege), presence (so you can interchange memory pages from disk-to-memory), type (code, stack, data), privilege level (0-3), etc. This will tell the OS if some buggy or malicius code is trying to access something they
shouldn't be.

Generally speaking, you have one (and only one) GDT with 3 or 4 descriptors (kernel code, data code, kernel stack or video memory), one or none IDT, and several tasks with one or more LDT's.