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
Whatever5k

Paging

Post by Whatever5k »

I'm currently worrying about Paging and wondering if this Paging example from K.J. is sufficient:

Code: Select all

void paging()
{
        unsigned long *page_directory = (unsigned long *) 0x9C000;
        unsigned long *page_table;
        unsigned long address=0; // holds the physical address of where a page is
        unsigned int i;

        // The entire page directory takes up 1024 double words.
        // We take the actual address of the page directory and add 1024x4(to get the number of bytes) to it.
        // Our page table will start directly after the page directory this way.
        page_table = page_directory + (1024 * 4);

        // map the first 4MB of memory
        for(i=0; i<1023; i++)
        {
                page_table[i] = address | 3; // attribute set to: supervisor level, read/write, present(011 in binary)
                address = address + 4096; // 4096 = 4kb
        };

        // fill the first entry of the page directory
        page_directory[0] = page_table; // attribute set to: supervisor level, read/write, present(011 in binary)
        page_directory[0] = page_directory[0] | 3;
        // fill the rest of the page directory
        for(i=1; i<1023; i++)
        {
                page_directory[i] = 0 | 2; // attribute set to: supervisor level, read/write, not present(010 in binary)
        };

        // write_cr3, read_cr3, write_cr0, and read_cr0 all come from the assembly functions
        write_cr3(&page_directory); // put that page directory address into CR3
        write_cr0 (read_cr0() | 0x80000000); // set the paging bit in CR0 to 1

        // go celebrate or something 'cause PAGING IS ENABLED!!!!!!!!!!
};
Ok, read_cr0, write_cr0, write_cr3 and read_cr3 are assembly functions declared in another file, don't worry about those.
So it initializes the Page Tables + Page Directories and enables Paging. But what's about a Page Fault. It's missing there, isn't it? Anything else missing?
K.J.

Re:Paging

Post by K.J. »

It assumes that you will take care of a page fault yourself(when I wrote that code, I just had ints disabled). Be looking for one more update to that code though. There is a bug a found a short while ago.

K.J.
Post Reply