Page 1 of 1

The GDT

Posted: Sun Mar 06, 2011 6:17 am
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?

Re: The GDT

Posted: Sun Mar 06, 2011 6:53 am
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.

Re: The GDT

Posted: Sun Mar 06, 2011 7:20 am
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?

Re: The GDT

Posted: Sun Mar 06, 2011 7:32 am
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.

Re: The GDT

Posted: Sun Mar 06, 2011 7:40 am
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.

Re: The GDT

Posted: Sun Mar 06, 2011 8:47 am
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.

Re: The GDT

Posted: Sun Mar 06, 2011 9:24 am
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?

Re: The GDT

Posted: Sun Mar 06, 2011 10:27 am
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.