The GDT

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
overburn
Member
Member
Posts: 50
Joined: Sun Feb 22, 2009 9:15 am

The GDT

Post by overburn »

Hello,

I was wondering. Is it possible to set up a minimal version of the GDT in the bootloader , and then in the kernel implement paging directly, without setting the GDT again?
One Tequila, Two Tequila, Three Tequila, Floor!
User avatar
Chandra
Member
Member
Posts: 487
Joined: Sat Jul 17, 2010 12:45 am

Re: The GDT

Post by Chandra »

overburn wrote:Hello,

I was wondering. Is it possible to set up a minimal version of the GDT in the bootloader , and then in the kernel implement paging directly, without setting the GDT again?
Yes it is possible. Basically, a good idea would be to allow the '2nd stage loader' to setup the gdt. It is still possible to modify or add new entries to your GDT which was setup by the bootloader. Or simply, you can create a new GDT from inside your kernel depending upon your requirement.
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
User avatar
overburn
Member
Member
Posts: 50
Joined: Sun Feb 22, 2009 9:15 am

Re: The GDT

Post by overburn »

But hmm, as far as i know (probably wrong) , the GDT isn't used in the Paging model, so why have it at all after the kernel is loaded and paging is setup?

Or why not skip it altogether and set up paging directly?
One Tequila, Two Tequila, Three Tequila, Floor!
User avatar
Chandra
Member
Member
Posts: 487
Joined: Sat Jul 17, 2010 12:45 am

Re: The GDT

Post by Chandra »

overburn wrote:But hmm, as far as i know (probably wrong) , the GDT isn't used in the Paging model, so why have it at all after the kernel is loaded and paging is setup?

Or why not skip it altogether and set up paging directly?
The whole protection mechanism works on the basis of GDT even if you use paging. Paging is simply a mechanism to translate linear address to physical address. This is how 'Memory address mapping' works. Implementing 'Page level Protection' is your option and is generally considered the better way for memory protection. While paging is avoidable it is impossible (AFAIK) to avoid GDT, if you are ever going to run your kernel in Protected Mode. As a minimum, you should have a 'Flat' GDT setup.
You can check 'Intel's Manual' regarding how GDT and paging work together.
Last edited by Chandra on Sun Mar 06, 2011 7:40 am, edited 1 time in total.
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
Dario
Member
Member
Posts: 117
Joined: Sun Aug 31, 2008 12:39 pm

Re: The GDT

Post by Dario »

No, GDT(segmentation) is a must have in protected mode unlike paging. Usually bootloader sets up basic GDT with segments overlapping and stretching across the whole addressable space so that it can load the kernel at "any" point in the address space. Later, kernel can initialize GDT as needed. For example, Linux has 32 different segments per CPU as described in /arch/x86/include/asm/segment.h and has only 3 segments (__BOOT_CS, __BOOT_DS and __BOOT_TSS) for boot time.
____
Dario
Tosi
Member
Member
Posts: 255
Joined: Tue Jun 15, 2010 9:27 am
Location: Flyover State, United States
Contact:

Re: The GDT

Post by Tosi »

I would still recommend creating a GDT in the kernel, especially if you didn't write your own bootloader. For instance, I used multiboot and when my kernel starts up I can't make any assumptions about the GDT except that the segment registers contain valid segments, nor can I assume the stack will be valid. Also, if you intend to switch protection levels, you will need selectors and TSS segments for that, and those are stored in the GDT as well.
User avatar
overburn
Member
Member
Posts: 50
Joined: Sun Feb 22, 2009 9:15 am

Re: The GDT

Post by overburn »

Hmm, thanks guys.

So technically, if I setup the gdt in the kernel , i am independent from the bootloader, and also, if i roll my own bootloader, i could only declare a flat gdt in it, right?
One Tequila, Two Tequila, Three Tequila, Floor!
Tosi
Member
Member
Posts: 255
Joined: Tue Jun 15, 2010 9:27 am
Location: Flyover State, United States
Contact:

Re: The GDT

Post by Tosi »

If you roll your own bootloader, you can set up whatever you want in it. You can even have it not go to protected mode, and do that in the kernel instead. Then in your kernel, while you're still in real mode, you have full access to BIOS functions. Another thing you can do is have the bootloader load you in protected mode, and then drop down to real mode temporarily to copy the BIOS. This requires, as far as I am aware of, switching GDTs at least twice, and probably more times if you use a kernel loaded above 1 MB like I do.
I wouldn't recommend writing your own bootloader, especially if you are new, but that is an argument for another thread.
Post Reply