OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 8:55 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: What is NULL Descriptor in 32 bit GDT
PostPosted: Fri Apr 24, 2015 12:41 am 
Offline

Joined: Fri Apr 24, 2015 12:29 am
Posts: 1
Location: Korea, Gyeong-Gi
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.


Top
 Profile  
 
 Post subject: Re: What is NULL Descriptor in 32 bit GDT
PostPosted: Fri Apr 24, 2015 1:16 am 
Offline
Member
Member
User avatar

Joined: Thu Mar 27, 2014 3:57 am
Posts: 568
Location: Moscow, Russia
It is just the first qword in the GDT. Unfortunately, I don't know, why is it needed, too.

_________________
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay


Top
 Profile  
 
 Post subject: Re: What is NULL Descriptor in 32 bit GDT
PostPosted: Fri Apr 24, 2015 1:42 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
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.

_________________
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]


Top
 Profile  
 
 Post subject: Re: What is NULL Descriptor in 32 bit GDT
PostPosted: Fri Apr 24, 2015 1:56 am 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
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?


Top
 Profile  
 
 Post subject: Re: What is NULL Descriptor in 32 bit GDT
PostPosted: Fri Apr 24, 2015 2:12 am 
Offline
Member
Member

Joined: Wed Jan 08, 2014 8:41 am
Posts: 100
Location: Moscow, Russia
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.


Top
 Profile  
 
 Post subject: Re: What is NULL Descriptor in 32 bit GDT
PostPosted: Fri Apr 24, 2015 2:21 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
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"

_________________
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]


Top
 Profile  
 
 Post subject: Re: What is NULL Descriptor in 32 bit GDT
PostPosted: Fri Apr 24, 2015 3:41 am 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
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

_________________
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.


Top
 Profile  
 
 Post subject: Re: What is NULL Descriptor in 32 bit GDT
PostPosted: Fri Apr 24, 2015 8:39 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
I'm getting old. My memory is failing me already.

_________________
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 163 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group