Page 1 of 1
What is NULL Descriptor in 32 bit GDT
Posted: Fri Apr 24, 2015 12:41 am
by JeongUkJae
Hey all,
I'm studying about operating system. And I have some questions about GDT
A book says "When Operating system enters to Protected Mode, Operating System requires Global Descriptor Table.". But, I don't know what NULL Descriptor does and why NULL Descriptor is needed
Is there anyone who helps me?
Thanks.
Re: What is NULL Descriptor in 32 bit GDT
Posted: Fri Apr 24, 2015 1:16 am
by Roman
It is just the first qword in the GDT. Unfortunately, I don't know, why is it needed, too.
Re: What is NULL Descriptor in 32 bit GDT
Posted: Fri Apr 24, 2015 1:42 am
by Combuster
In the segmentation scheme, you need a value to load to invalidate a segment register. This value is zero, and you can set that regardless of what's actually in the GDT.
This also means that because of this special behaviour, you can't use the first entry of the GDT.
Re: What is NULL Descriptor in 32 bit GDT
Posted: Fri Apr 24, 2015 1:56 am
by alexfru
Combuster wrote:This value is zero, and you can set that regardless of what's actually in the GDT.
I don't recall it documented this way (the NULL descriptor may contain anything). Is it really so?
Re: What is NULL Descriptor in 32 bit GDT
Posted: Fri Apr 24, 2015 2:12 am
by Icee
alexfru wrote:I don't recall it documented this way (the NULL descriptor may contain anything). Is it really so?
Yes. Here's a quote from the AMD Manuals:
AMD Manual vol. 2 s. 4.5.1 wrote:Null Selector. Null selectors have a selector index of 0 and TI=0, corresponding to the first entry in the GDT. However, null selectors do not reference the first GDT entry but are instead used to invalidate unused segment registers. A general-protection exception (#GP) occurs if a reference is made to use a segment register containing a null selector in non-64-bit mode. By initializing unused segment registers with null selectors software can trap references to unused segments.
Intel:
Intel Manual vol. 3A s. 3.4.2 wrote:The first entry of the GDT is not used by the processor. A segment selector that points to this entry of the GDT (that is, a segment selector with an index of 0 and the TI flag set to 0) is used as a “null segment selector.” The processor does not generate an exception when a segment register (other than the CS or SS registers) is loaded with a null selector. It does, however, generate an exception when a segment register holding a null selector is used to access memory. A null selector can be used to initialize unused segment registers. Loading the CS or SS register with a null segment selector causes a general-protection exception (#GP) to be generated.
Re: What is NULL Descriptor in 32 bit GDT
Posted: Fri Apr 24, 2015 2:21 am
by Combuster
The difference is that loading NULL into a segment register doesn't cause an exception, whereas loading a non-null index that points to a zeroed GDT entry does.
In practice, there's a hack in the wild that repurposes the 0th GDT entry as the size-offset pair used for LGDT (to save 6 bytes of bootloader space) with no known ill effects other than relying on undocumented behaviour - when that documentation was still lacking. The idea is that the CPU does not need to access the GDT in memory if there are 8 bytes of zeroes there "by definition"
Re: What is NULL Descriptor in 32 bit GDT
Posted: Fri Apr 24, 2015 3:41 am
by Brendan
Hi,
Combuster wrote:In practice, there's a hack in the wild that repurposes the 0th GDT entry as the size-offset pair used for LGDT (to save 6 bytes of bootloader space) with no known ill effects other than relying on undocumented behaviour - when that documentation was still lacking. The idea is that the CPU does not need to access the GDT in memory if there are 8 bytes of zeroes there "by definition"
That is not a hack, and does not rely on undocumented behaviour. It has been documented by Intel since the 80386.
From the 80386 Programmer's Guide (highlighting is mine):
Intel wrote: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.
Cheers,
Brendan
Re: What is NULL Descriptor in 32 bit GDT
Posted: Fri Apr 24, 2015 8:39 am
by Combuster
I'm getting old. My memory is failing me already.