Neo wrote:
Man this is turning out to be a lot more complicated than i thought it would. Anyway i would like to collect all this info/bugs etc.. in one place so that it would be easier for anyone to learn from my mistakes.
Ok saying that let me try to put what i understand into perspective (Please correct me wherever i'm wrong). Lets say that a bootloader(GRUB??) has loaded my kernel to some address (1MB for e.g) and paging is not yet enabled,also GRUB only loads the kernel with FLAT GDT descriptors. My kernel has been linked to run at 3GB(0xC000_0000).
If you link your kernel to C000_0000 but load it at 0010_0000, you'll either need to:
- Use the GDT trick to make 0010_0000 appear at C000_0000 without paging
- Enable paging really early
You will need to make your code position-independent until it does one of these two things. Position-independent code isn't impossible to write (only global variable accesses need to be position-independent, not CALL or JMP) but it's a pain to do. Currently I prefer to get some paging enabled really early on -- in the assembler startup routine -- and not worry about it.
The kernel asm stub file first disables the IRQ's and remap's the PIC's(these are memory independent so they can be executed here)
This could be done before or after paging is enabled.
Next identity map the first linear 4MB to first physical 4MB.
Now map first 4MB of 0xC000_0000(3GB) to first 4MB of 0x100000(1MB)
Enable paging(how?? what values in CR3 etc..?)
The normal ones. Allocate two page tables: one to map 0000_0000 to 0040_0000, and one to map C000_0000 to C040_0000. Allocate a page directory. Enter the PTs into the PD. Load the address of the PD into CR3. Enable CR0.PG. All of this is completely standard.
Load the GDT with new segments whose base address is 0x4010_0000 (so that 0x4010_0000+0xC000_0000) gives 0x100000(1MB)
No. This is the GDT trick, which if you use it is only necessary before paging is enabled. And if you're happy with making your pre-paging code position-independent, it's not necessary at all.
Next i don't know what comes so Guru's out there could you fill it up?
Whatever you want
. From now on, you can implement your kernel design freely.