OSDev.org

The Place to Start for Operating System Developers
It is currently Wed May 01, 2024 7:48 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: About GDT
PostPosted: Mon Jun 02, 2008 10:27 pm 
Offline
Member
Member

Joined: Tue May 27, 2008 8:32 am
Posts: 35
Hi, I am trying to write the kernel by myself.
I followed the steps at http://www.jamesmolloy.co.uk.
Now I am going to write code about GDT.
Generally, how many GDT entries should be initialized before loade GDT to GDTR?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 03, 2008 2:24 am 
Offline
Member
Member
User avatar

Joined: Sun Oct 22, 2006 7:01 am
Posts: 2646
Location: Devon, UK
Hi,

Initially, you will just want 3 entries:

Code:
0: The NULL descriptor
1: A 32 bit ring 0 code descriptor
2: A 32 bit ring 0 data descriptor


Note that while entry 0 must be the NULL descriptor, you can make your own mind up about the order of everything else (although what I'm telling you is fairly conventional). With just these 3 entries, you can jump to Protected Mode.

When I load my 32 bit GDT, I also have entries 3 and 4, which are ring 3 code and data descriptors. After that, I have a single entry for each processor core (you will find later that an entry per CPU is "required" for multitasking - for the TSS).

If you are planning on returning to real mode for anything, you will also want at least one 16 bit code descriptor (to reset the limit of CS to a 16 bit limit). If you want to use long mode, you will need 64 bit code and data segments for ring 0 and ring 3.

So, realistically, your GDT will have 6 entries (including NULL) for a 32 bit PMode OS, plus one per additional core. Importantly, remember that other entries can be added later, but remember to adjust the GDTR limit if you do!

HTH,
Adam


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 03, 2008 3:57 am 
Offline
Member
Member

Joined: Tue May 27, 2008 8:32 am
Posts: 35
AJ wrote:
Hi,

Initially, you will just want 3 entries:

Code:
0: The NULL descriptor
1: A 32 bit ring 0 code descriptor
2: A 32 bit ring 0 data descriptor


Note that while entry 0 must be the NULL descriptor, you can make your own mind up about the order of everything else (although what I'm telling you is fairly conventional). With just these 3 entries, you can jump to Protected Mode.

When I load my 32 bit GDT, I also have entries 3 and 4, which are ring 3 code and data descriptors. After that, I have a single entry for each processor core (you will find later that an entry per CPU is "required" for multitasking - for the TSS).

If you are planning on returning to real mode for anything, you will also want at least one 16 bit code descriptor (to reset the limit of CS to a 16 bit limit). If you want to use long mode, you will need 64 bit code and data segments for ring 0 and ring 3.

So, realistically, your GDT will have 6 entries (including NULL) for a 32 bit PMode OS, plus one per additional core. Importantly, remember that other entries can be added later, but remember to adjust the GDTR limit if you do!

HTH,
Adam


Hi, thank you for your reply.But I still have another question.Whenever I need another GDT entry, I need to add them to GDT, then lode the new GDT to GDTR.
If I still want to keep the orignal GDT when I want to add a new GDT entry,Do I need to copy the old GDT into new one?
Why can not create a GDT large enough, save the time to load new GDT to GDTR?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 03, 2008 4:02 am 
Offline
Member
Member
User avatar

Joined: Sun Oct 22, 2006 7:01 am
Posts: 2646
Location: Devon, UK
You can. There are a couple of ways of doing this:

1. Create a GDT with more than enough entries. GDTR points to a structure with the base and size of this structure.

2. Create a minimal GDT to go to PMode. Once you have detected the CPU cores and therefore know the maximum number of entries you need, create your full GDT and reload GDTR. Discard the initial GDT.

With method 1, you need to detect the number of CPU cores prior to the PMode switch (or just install an oversized GDT to start with). I prefer method 2, because my boot loader establishes an initial GDT, but my kernel installs its own version of the GDT at boot time.

Cheers,
Adam


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: marcofreeman and 19 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