Page 1 of 1
About GDT
Posted: Mon Jun 02, 2008 10:27 pm
by huxuelei
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?
Posted: Tue Jun 03, 2008 2:24 am
by AJ
Hi,
Initially, you will just want 3 entries:
Code: Select all
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
Posted: Tue Jun 03, 2008 3:57 am
by huxuelei
AJ wrote:Hi,
Initially, you will just want 3 entries:
Code: Select all
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?
Posted: Tue Jun 03, 2008 4:02 am
by AJ
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