When i enable paging i get a 3rd exception error and i can't see what i am doing wrong which would cause this. Here is the code im using to enable paging:-
Code: Select all
void CPhysicalManager::EnablePaging()
{
clear_screen();
kprintf("Enabling Paging", 0);
//check page directory adress is 4Kb aligned
if((PAGE_DIRECTORY_ADDR % 0x1000) != 0)
{
//Throw up an error if the page adress isn't 4K aligned
kprintf("Problem enabling paging address not 4K aligned", 10);
return;
}
unsigned long *pPageDirectory = (unsigned long *) PAGE_DIRECTORY_ADDR; //0x9C000;
unsigned long *pPageTable = (unsigned long *) PAGE_TABLE_ADDR; //0x9D000; // the page table comes right after the page directory
unsigned long uiAddress = 0; // holds the physical address of where a page is
UINT i = 0;
UINT k = 0;
UINT uiTotalMemory = uiTotalPages << 12;
//Map the first 8Mb of memory we are using for kernel
//0-4Mb
for(k=0; k<0x400; k++)
{
pPageTable[k] = uiAddress | 2; // attribute set to: supervisor level, read/write, present(011 in binary)
uiAddress += 0x1000; // 4096 = 4kb
kprintf((UINT)&pPageTable[k], 4);
kprintf((UINT)pPageTable[k], 7);
}
//4-8Mb
for(k=0x400; k<0x800; k++)
{
pPageTable[k] = uiAddress | 2; // attribute set to: supervisor level, read/write, present(011 in binary)
uiAddress += 0x1000; // 4096 = 4kb
kprintf((UINT)&pPageTable[k], 5);
kprintf((UINT)pPageTable[k], 8);
}
//When above the physical memory set the pages as not present
pPageDirectory[0] = (unsigned long)pPageTable; // attribute set to: supervisor level, read/write, present(011 in binary)
pPageDirectory[0] |= 3;
kprintf(pPageDirectory[0], 10);
pPageDirectory[1] = (unsigned long)((UINT)pPageTable + PAGE_TABLE_SIZE); // attribute set to: supervisor level, read/write, present(011 in binary)
pPageDirectory[1] |= 3;
kprintf(pPageDirectory[1], 11);
kprintf((UINT)pPageDirectory, 12);
//Set all Page Directories as not-present
for(i=2; i<0x400; i++)
{
//When above the physical memory set the pages as not present
pPageDirectory[i] = 0; // attribute set to: supervisor level, read/write, present(011 in binary)
pPageDirectory[i] |= 2;
}
kprintf("Mapped Tables", 1);
// write_cr3, read_cr3, write_cr0, and read_cr0 all come from the assembly functions
write_cr3((unsigned long)PAGE_DIRECTORY_ADDR); // put that page directory address into CR3
kprintf(read_cr3(), 14);
write_cr0(read_cr0() | 0x80000000); // set the paging bit in CR0 to 1
kprintf("Paging Enabled, 4Gb allocated", 2);