Code: Select all
__asm__ __volatile__("mov %0, %%cr0":: "r"(cr0));
Code: Select all
bool switch_page_directory(page_directory_t *dir)
{
if(!dir){puts("r u kidding me");
return false;}
current_directory = dir;
__asm__ __volatile__("mov %0, %%cr3":: "r"(&dir->pde));puts("1");
u32int cr0;
__asm__ __volatile__("mov %%cr0, %0": "=r"(cr0));puts("2");
cr0 |= 0x80000000; // Enable paging!
__asm__ __volatile__("mov %0, %%cr0":: "r"(cr0));//puts("3");
return true;
}
void initialize_virtual_mm()
{
//default ptable
page_table_t *ptable = (page_table_t *)alloc_frame();
memset(ptable,0,sizeof(page_table_t));
//ptable for mapping kernel to 3gb virtual addr
page_table_t *ptable1 = (page_table_t *)alloc_frame();
memset(ptable1,0,sizeof(page_table_t));
pd_entry pde0=0x0;
pd_entry_set_ptable(&pde0,(phys_addr)ptable);
pd_entry_add_attrib(&pde0,PDE_PRESENT);
pd_entry_add_attrib(&pde0,PDE_WRITABLE);
u32int i,frame,virtual; //identity mapping the ist 4mb i.e allocating 1st ptable to kernel and BIOS stuff
frame=i=0;
for (i=0, frame=0x0, virtual=0x00000000; i<1024; i++, frame+=4096, virtual+=4096) {
//! create a new page
pt_entry page=0;
pt_entry_add_attrib (&page, PTE_PRESENT);
pt_entry_set_frame (&page, frame);
//! ...and add it to the page table
ptable->pte [PAGE_TABLE_INDEX (virtual) ] = page;
}
//mapping the kernel which is above 1mb to 3gb virtual
for(i=0,frame=0x100000,virtual=0xc0000000;i<1024;i++,frame+=4096,virtual+=4096)
{
pt_entry pte=0;
pt_entry_set_frame(&pte,frame);
pt_entry_add_attrib(&pte,PTE_PRESENT);
ptable1->pte[PAGE_TABLE_INDEX(virtual)] = pte;
}
kernel_directory = (page_directory_t *)alloc_frame();
memset(kernel_directory,0,sizeof(page_directory_t));
//mapping the ptable for 3gb virtual addr to its appropriate pde
pd_entry* entry = &kernel_directory->pde [PAGE_DIR_INDEX (0xc0000000) ];
pd_entry_add_attrib (entry, PDE_PRESENT);
pd_entry_add_attrib (entry, PDE_WRITABLE);
pd_entry_set_ptable (entry, (phys_addr)ptable1);
//mapping the ptable for 0-4mb virtual addr to its appropriate pde
pd_entry* entry2 = &kernel_directory->pde [PAGE_DIR_INDEX (0x00000000) ];
puts("\nentry2 val: ");puthex((u32int)*entry2);
pd_entry_add_attrib (entry2, PDE_PRESENT);
pd_entry_add_attrib (entry2, PDE_WRITABLE);
pd_entry_set_ptable (entry2, (phys_addr)ptable);
isr_install_handler(14,page_fault);
switch_page_directory(kernel_directory);
}