From f4a0cf6830201fa07469049a2b524cd23962c9d8 Mon Sep 17 00:00:00 2001 From: Alex Huddleston Date: Wed, 28 Jun 2017 00:20:59 -0500 Subject: [PATCH] All implemented. Cannot resolve issue with process_mem_pool. Turning in anyway. --- MP3/MP3_Sources/kernel.C | 6 +-- MP3/MP3_Sources/page_table.C | 73 ++++++++++++++++++++++++++++-------- 2 files changed, 60 insertions(+), 19 deletions(-) diff --git a/MP3/MP3_Sources/kernel.C b/MP3/MP3_Sources/kernel.C index 3e28c6d..48fe953 100755 --- a/MP3/MP3_Sources/kernel.C +++ b/MP3/MP3_Sources/kernel.C @@ -171,9 +171,9 @@ int main() { int i; for (i=0; ipage_directory + (unsigned long)current_page_table->page_directory[0] + (unsigned long)((unsigned long *)current_page_table->page_directory[0])[0]); write_cr3((unsigned long)current_page_table->page_directory); write_cr0(read_cr0() | 0x80000000); paging_enabled = read_cr0(); @@ -84,21 +81,65 @@ void PageTable::enable_paging() void PageTable::handle_fault(REGS * _r) { - unsigned long temp_addr = (kernel_mem_pool->get_frames(1)); - ((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; + // Initializing some variables to make things easier. + unsigned long * temp_pd = (unsigned long *) (current_page_table->page_directory); + unsigned long * temp_pt = (unsigned long *) (*temp_pd); - Console::puts("\nerror code: "); -Console::puti(_r->err_code); + unsigned long pt_index = (read_cr2() >> 12) & 0x3FFFFF; + unsigned long pd_index = read_cr2() >> 22; + + // If there isn't a page table present. + if(!(temp_pd[pd_index] & 1)) + { + Console::puts("\nPage Table not Present.\n"); + + unsigned long * new_pt = (unsigned long *) (kernel_mem_pool->get_frames(1) << 12); + + // Set new page table entries as present. + // Start at beginning address for the page table + // this should be pd_index << 22. + unsigned long temp_addr = pd_index << 22; + for(unsigned int i = 0; i < 1024; i++) + { + new_pt[i] = temp_addr | 3; + temp_addr += 4096; + } + + // Add page table to page directory. + temp_pd[pd_index] = (unsigned long) new_pt; + temp_pd[pd_index] = temp_pd[pd_index] | 3; + } + + /* + // Why is this commented out? + + // Unfortunately, for whatever reason, any time I try to call + // process_mem_pool->get_frames() the entire program crashes. + // I've tried evaluating lines immediately afterwards, adding + // for(;;);, the program ignores this and continues to crash. + // Since I have no idea why this is happening, I can only assume + // I did something wrong in cont_frame_pool or that this is just + // an issue I didn't address properly somewhere and didn't see + // addressed in the instructions. Regardless, I left what I believe + // would be the implementation of handle_fault in the case that I + // needed to allocate a frame in process_mem_pool in order to + // allocate a page for a process commented out so I could at least + // show that I knew how it would be implemented. + + if(read_cr2() >> 22 > 0) + { + if(((unsigned long *)temp_pd[pd_index])[pt_index] & 1) + { + Console::puts("\nPage not present.\n"); + unsigned long * new_page = (unsigned long *) (process_mem_pool->get_frames(1) << 12); + + // Set new page as page table entry. + ((unsigned long *)temp_pd[pd_index])[pt_index] = (unsigned long) new_page | 3; + } + } + */ - unsigned char mask = 0x00; - Console::puts("\naddress: "); -Console::puti(read_cr2()); - - Console::puts("\naddress: "); -Console::puti(read_cr3()); -for(;;); Console::puts("\nhandled page fault\n"); }