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!!!!!!!!!!
};
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?