Page 1 of 1

Higher-Half Kernel, GDT, Address Translation, Snuh?

Posted: Thu Jul 31, 2008 6:57 pm
by -m32
Sorry if this has been asked before....

Recently I managed to move my kernel to the "higher-half" (at 0xC0100000) using a CS and DS with a base of 0x40000000 (ala wiki), and I noticed, after enabling paging, that if I don't identity map the space at 0x100000 to the end of the kernel's image (say 0x200000 for the sake of argument), the code will cause a page fault when I try to lgdt (specifically when I try to change the DS after doing an lgdt). -- even if I pass lgdt a virtual address in the higher-half space -- i know the CPU needs a physical address and I assume the address is translated if a virtual one is used....

Ok, fine.

But, does this mean, that I will have to map 0x100000 to 0x200000 AND 0xC0100000 to 0xC0200000 into every process' memory space in order to avoid a page fault (triple fault) when I enter my interrupt handler (which changes the CS and DS selectors)?

If that's the case, and BOTH memory ranges must be mapped in, then what's the point of the higher-half approach if the kernel (or at least the part containing the GDT and IDT) has to be mapped into the 0x100000 space just to function??

Hopefully that's clear enough :roll: :)

Re: Higher-Half Kernel, GDT, Address Translation, Snuh?

Posted: Fri Aug 01, 2008 12:58 am
by jnc100
1) lgdt requires a linear address
2) the reason you must identity map the kernel is that before enabling paging eip is somewhere around 0x100000. When you enable paging, eip must still point to a valid address, else you get a page fault when trying to fetch the next instruction. You need to then jmp/call to somewhere in the higher half to set eip up properly for your kernel. Obviously the higher half code needs to be linked to run in the higher half. Once you're running code from 0xc0000000 and up then you don't need the gdt entries for the lower half anymore.

Once paging is set up, you should be using segments with a base of 0x0 as the paging system manages the mapping now. That means doing something like lgdt [address_of_higher_half_gdt, e.g. 0xc0010000] would work fine.

Regards,
John.

Re: Higher-Half Kernel, GDT, Address Translation, Snuh?

Posted: Fri Aug 01, 2008 6:02 am
by -m32
jnc100 wrote: 1) lgdt requires a linear address
Yes, as per the intel docs on lgdt. I was speculating that even tough I can pass a "linear" address to lgdt, the CPU still requires physical access to the GDT.
jnc100 wrote: 2) the reason you must identity map the kernel is that before enabling paging eip is somewhere around 0x100000. When you enable paging, eip must still point to a valid address, else you get a page fault when trying to fetch the next instruction. You need to then jmp/call to somewhere in the higher half to set eip up properly for your kernel. Obviously the higher half code needs to be linked to run in the higher half. Once you're running code from 0xc0000000 and up then you don't need the gdt entries for the lower half anymore.
I do use a temp GDT and jump to code at 0xC0000000, and I do discard this GDT once done. The EIP is only in the 0x100000 space until I call jmp 0x80:higherhalf - in fact it's set to 0xC010053 right before I enable paging. And the higher half code is linked to run in the higher half.
jnc100 wrote:Once paging is set up, you should be using segments with a base of 0x0 as the paging system manages the mapping now. That means doing something like lgdt [address_of_higher_half_gdt, e.g. 0xc0010000] would work fine.
I turned paging on, then switched to segments using a base of 0x0 (new GDT). And I do use "lgdt hhaddr". But if I don't identity map the kernel at 0x100000 (it's _actual_ load address), triple-fault.

Re: Higher-Half Kernel, GDT, Address Translation, Snuh?

Posted: Fri Aug 01, 2008 6:39 am
by -m32
Hmmm, I think I understand what's going on... paging's enabled but, because the CS & DS offsets are still set to 0x40000000 (before changing to the new GDT with a base of 0x0), this is still adding a "layer" of address translation. So, even though I'm "identity mapping" the 0x100000 space, it's still being translated to 0xC0100000 by the DS offset...

That makes sense, for some reason I was thinking that once paging is enabled, it didn't matter anymore what the offset of the CS and DS segments were.

Someone please correct me if my revelation is wrong ;)