Enabling Paging

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.
Post Reply
pskyboy

Enabling Paging

Post by pskyboy »

Hey Guys

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);
Pype : i've edited your code to filter out commented code - for the sake of readability. Please check what i left is your current code. I know you won't forget to do it yourself next time :)
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Enabling Paging

Post by Pype.Clicker »

Code: Select all

kprintf((UINT)&pPageTable[k], 4);
seems weird to me ... Why are you trying to print a Page Table entry as a string ?? and what's the number after the "string" argument ? the string size ?

The "printf" function suggests that the format is like printf: ("%x", pageTable[k]), which doesn't seem to be the case here ... consider changing your function name :)
pskyboy

Re:Enabling Paging

Post by pskyboy »

Hey

No it actually displays the address of pPageTable[k]. Thsi is because i have an overloaded printf fucntion that will take a string or vlaue as an intput and display it. The number is the line to print it on the screen.

Peter
pskyboy

Re:Enabling Paging

Post by pskyboy »

It seems to pop up and say that there has been a exception error 14 which is a page fault. the weird thing is i have an interrupt set up for that but it doesn't seem to get called.
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:Enabling Paging

Post by distantvoices »

hum hom ... peter, in which order do you enable paging and install the exception handler for paging (it should be located by slot 14 of the idt)?

I usually get exception Nr. 14, when I send a pointer to holy nirvana in silicium hell.

stay safe.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
pskyboy

Re:Enabling Paging

Post by pskyboy »

Hey

I think i fiqured out what it was when i was mapping the page tables i wasn't mapping the lower 8mb where my kernel was properly and so as soon as an exception occured the location of the vector table had been mapped and so when it went to jump into it couldn't find it.

I have now mapped the lower 8Mb used by my kernel directly and this seems to have fixed the problem. My job now is to get it so i can remap my kernel to 3Gb see my other post.

Peter
Post Reply