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.
I'm having a system hang when I enable paging and I'm not sure whats going on. I have checked to confirm that the physical memory manager is providing correctly aligned pages. Any ideas or tips would be great!
I am identity mapping up the the end of the kernel and its initial heap and I am mapping on page directory onto the last 4 MB of virtual memory for easy access.
VirtMemMgr::VirtMemMgr(void* nonIdentPagingStart)
{
system.console<<"Configuring Virtual/Physical Memory Managers... ";
phyMem = new PhyMemMgr(nonIdentPagingStart);
uint* pageDir = (uint*)phyMem->getPage();
uint* pageTable = (uint*)phyMem->getPage();
//map the first 4MB of memory to page table
uint i = 0;
//map exiting memory one to one
for(; i < (uint)nonIdentPagingStart; i += 4096)
pageTable[i] = i | GLBL_SUPER_RW_PRESENT;
kCurPTE = i >> 12;
//map remainder of PDE to address 0 and mark GLBL_SUPER_RO_NOTPRESENT
//which will for our purposes mean unbacked virtual memory
for(; i < 0x400000; i += 4096)
pageTable[i] = 0 | GLBL_SUPER_RO_NOTPRESENT;
pageDir[0] = (uint)pageTable | SUPER_RW_PRESENT;
for(uint j = 1; j < 1023; i++)
pageDir[i] = 0 | SUPER_RO_NOTPRESENT;
//Map TPE to last 4 MB of VM
mappedPTE = (uint*)0xFFC00000;
pageDir[1023] = (uint)pageDir;
//enable paging
writeCR3((uint)pageDir);
writeCR0(readCR0() | 0x80000000);
system.console<<"done"<<endl;
}
Have you debugged? - what does Bochs output when your kernel crashes, or does your kernel just go unresponsive (it might be executing code where it shouldn't be...)? Also, make sure that the page mappings are being done properly.
Other than that, check if you're interfacing your ASM and C++ code correctly.
Hope this helps!
"Sufficiently advanced stupidity is indistinguishable from malice."
Thanks for the advice. I have been using asm extensively so far so I am rather confident that the C++/ASM linking is working OK. As to what exactly happens, I should have said, sorry. The machine locks up between the last two lines of the function I posted above, VirtMemMgr::VirtMemMgr.
It looks like the problem is with your identity mapping code for the first 4mb worth of page tables, you're increasing your index value by 4096 so it's probably writing an entry every 16384 bytes instead of every 4. You're also trying to map the memory you've marked as not present in the same fashion.
I'm not sure how your phyMem->getPage() code works but if it's only allocating 4096 bytes to your page tables you're overrunning it by a good margin, make sure your page allocator knows you're using 1024 pages otherwise you'll likely end up corrupting your page tables later on.
I think I had changed the way I was handling my loops half way though and forgot to go back and correct it. In case anyone is interested the working code is posted below: