2017-06-26 23:58:19 -05:00
|
|
|
#include "assert.H"
|
|
|
|
#include "exceptions.H"
|
|
|
|
#include "console.H"
|
|
|
|
#include "paging_low.H"
|
|
|
|
#include "page_table.H"
|
|
|
|
|
|
|
|
PageTable * PageTable::current_page_table = NULL;
|
|
|
|
unsigned int PageTable::paging_enabled = 0;
|
|
|
|
ContFramePool * PageTable::kernel_mem_pool = NULL;
|
|
|
|
ContFramePool * PageTable::process_mem_pool = NULL;
|
|
|
|
unsigned long PageTable::shared_size = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PageTable::init_paging(ContFramePool * _kernel_mem_pool,
|
|
|
|
ContFramePool * _process_mem_pool,
|
|
|
|
const unsigned long _shared_size)
|
|
|
|
{
|
|
|
|
kernel_mem_pool = _kernel_mem_pool;
|
|
|
|
process_mem_pool = process_mem_pool;
|
|
|
|
shared_size = _shared_size;
|
|
|
|
|
|
|
|
Console::puts("\nInitialized Paging System\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
PageTable::PageTable()
|
|
|
|
{
|
2017-06-27 18:41:45 -05:00
|
|
|
unsigned long * temp_page_directory = (unsigned long *) ((kernel_mem_pool->get_frames(1)) << 12); // Getting frame address to assign to page directory.
|
2017-06-26 23:58:19 -05:00
|
|
|
|
2017-06-27 18:41:45 -05:00
|
|
|
unsigned long * page_table = (unsigned long *) ((kernel_mem_pool->get_frames(1)) << 12); // Get another frame for the page table.
|
2017-06-26 23:58:19 -05:00
|
|
|
|
2017-06-27 02:35:43 -05:00
|
|
|
unsigned long tempaddr = 0; // Temporary address iterator.
|
2017-06-26 23:58:19 -05:00
|
|
|
|
|
|
|
// We need to map the first 4MB.
|
2017-06-27 04:07:08 -05:00
|
|
|
for(unsigned int i = 0; i < 512; i++)
|
|
|
|
{
|
|
|
|
page_table[i] = tempaddr | 3; // set bits 0 and 1 to present and read/write respectively
|
|
|
|
tempaddr += 4096; // 4096 = 4kb
|
|
|
|
}
|
|
|
|
|
|
|
|
for(unsigned int i = 512; i < 1024; i++)
|
2017-06-26 23:58:19 -05:00
|
|
|
{
|
|
|
|
page_table[i] = tempaddr | 3; // set bits 0 and 1 to present and read/write respectively
|
|
|
|
tempaddr += 4096; // 4096 = 4kb
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Fill first entry of page directory.
|
2017-06-27 02:35:43 -05:00
|
|
|
temp_page_directory[0] = (unsigned long)page_table;
|
|
|
|
temp_page_directory[0] = temp_page_directory[0] | 3;
|
2017-06-26 23:58:19 -05:00
|
|
|
|
|
|
|
for(unsigned int j = 1; j < 1024; j++)
|
|
|
|
{
|
2017-06-27 02:35:43 -05:00
|
|
|
temp_page_directory[j] = 0 | 2;
|
2017-06-26 23:58:19 -05:00
|
|
|
}
|
|
|
|
|
2017-06-27 18:41:45 -05:00
|
|
|
//page_directory = (unsigned long *) ((unsigned long)temp_page_directory + (unsigned long)page_table + (unsigned long)page_table[0]);
|
2017-06-27 02:35:43 -05:00
|
|
|
page_directory = temp_page_directory;
|
|
|
|
|
2017-06-26 23:58:19 -05:00
|
|
|
current_page_table = this;
|
|
|
|
|
|
|
|
Console::puts("\nConstructed Page Table object\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PageTable::load()
|
|
|
|
{
|
2017-06-27 18:41:45 -05:00
|
|
|
//write_cr3((unsigned long)page_directory + (unsigned long)page_directory[0] + (unsigned long)((unsigned long *)page_directory[0])[0]);
|
2017-06-27 02:35:43 -05:00
|
|
|
write_cr3((unsigned long)page_directory);
|
2017-06-27 18:41:45 -05:00
|
|
|
Console::puti((unsigned long)page_directory);
|
|
|
|
//for(;;);
|
2017-06-26 23:58:19 -05:00
|
|
|
current_page_table = this;
|
|
|
|
Console::puts("\nLoaded page table\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageTable::enable_paging()
|
|
|
|
{
|
2017-06-27 18:41:45 -05:00
|
|
|
//write_cr3((unsigned long)current_page_table->page_directory + (unsigned long)current_page_table->page_directory[0] + (unsigned long)((unsigned long *)current_page_table->page_directory[0])[0]);
|
2017-06-27 02:35:43 -05:00
|
|
|
write_cr3((unsigned long)current_page_table->page_directory);
|
2017-06-26 23:58:19 -05:00
|
|
|
write_cr0(read_cr0() | 0x80000000);
|
2017-06-27 02:35:43 -05:00
|
|
|
paging_enabled = read_cr0();
|
2017-06-26 23:58:19 -05:00
|
|
|
Console::puts("\nEnabled paging\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageTable::handle_fault(REGS * _r)
|
|
|
|
{
|
2017-06-27 04:07:08 -05:00
|
|
|
unsigned long temp_addr = (kernel_mem_pool->get_frames(1));
|
|
|
|
|
2017-06-27 03:18:49 -05:00
|
|
|
((unsigned long *) ((unsigned long *) current_page_table->page_directory)[0])[temp_addr]
|
|
|
|
= ((unsigned long *) ((unsigned long *) current_page_table->page_directory)[0])[temp_addr] | 3;
|
|
|
|
|
2017-06-26 23:58:19 -05:00
|
|
|
Console::puts("\nerror code: ");
|
2017-06-27 04:07:08 -05:00
|
|
|
Console::puti(_r->err_code);
|
2017-06-26 23:58:19 -05:00
|
|
|
|
|
|
|
unsigned char mask = 0x00;
|
2017-06-27 04:07:08 -05:00
|
|
|
Console::puts("\naddress: ");
|
|
|
|
Console::puti(read_cr2());
|
2017-06-27 02:46:52 -05:00
|
|
|
|
2017-06-27 04:07:08 -05:00
|
|
|
Console::puts("\naddress: ");
|
|
|
|
Console::puti(read_cr3());
|
2017-06-27 18:41:45 -05:00
|
|
|
for(;;);
|
2017-06-26 23:58:19 -05:00
|
|
|
Console::puts("\nhandled page fault\n");
|
|
|
|
}
|
|
|
|
|