Problems creating a new page dir and loading it to CR3
Posted: Sun Mar 04, 2012 7:44 am
Hi... Been struggling with this for a while. The CPU tripplefaults.
I have a higher half kernel basically made with the Tim R. GDT trick. I'm beginning the preparations for multitasking which involves cloning a PD. I have a working (mostly) heap, paging and allocation seems to work. But now that I try to create a new page directory and load it's physical address to cr3 the cpu crashes.
There's something I don't quite understand for example:
Why do we need to translate pd from virtual to physical? Paging is not yet enabled at that point...
Here's my code.
Hopefully my question was well formed. If not please let me know =)
A million thanks, once again!
I have a higher half kernel basically made with the Tim R. GDT trick. I'm beginning the preparations for multitasking which involves cloning a PD. I have a working (mostly) heap, paging and allocation seems to work. But now that I try to create a new page directory and load it's physical address to cr3 the cpu crashes.
There's something I don't quite understand for example:
Code: Select all
kernelpagedirPtr = (char *)kernelpagedir + 0x40000000; // Translate the page directory from
// virtual address to physical address
lowpagetablePtr = (char *)lowpagetable + 0x40000000; // Same for the page tableHere's my code.
Code: Select all
unsigned int kernelpagedir[1024] __attribute__ ((aligned (4096)));
unsigned int lowpagetable[1024] __attribute__ ((aligned (4096)));
void initializePaging () {
void *kernelpagedirPtr = 0;
void *lowpagetablePtr = 0;
int k = 0;
kernelpagedirPtr = (char *)kernelpagedir + 0x40000000;
lowpagetablePtr = (char *)lowpagetable + 0x40000000;
for (k = 0; k < 1024; k++)
{
lowpagetable[k] = (k * 4096) | 3;
kernelpagedir[k] = 0;
}
kernelpagedir[0] = (unsigned int)lowpagetablePtr | 3;
kernelpagedir[768] = (unsigned int)lowpagetablePtr | 3;
kernelpagedir[1023] = ( (unsigned int)kernelpagedirPtr & 0xFFFFF000 ) | 3;
active_page_directory = kernelpagedirPtr;
enablePaging ();
/*
create a new page directory and try to set it to cr3
*/
unsigned long new_dir_addr_phys = cloneDirectory ();
asm volatile( "cli" );
active_page_directory = new_dir_addr_phys;
asm volatile( "mov %0, %%cr3":: "b"( active_page_directory ) );
}
unsigned long cloneDirectory ( /* unsigned int *source_directory */ ) {
unsigned long *page_directory = getFreeFrame(); //get address (phys) to one 0x1000 frame
int i = 0;
for( i = 0; i < 1024; i++ ) {
page_directory[i] = 0 | 2;
}
unsigned long *page_table = getFreeFrame();
unsigned long address = 0; // <--- Kernel is actually physically somewhere at the bottom so lets map 0-4 megs to a page table and then put this table to the PDE 768 (3gigs higher half)
for( i = 0; i < 1024; i++ )
{
page_table[i] = address | 3
address = address + 4096;
}
page_directory[0] = page_table; //<--- identity map the low 0-4 megs
page_directory[0] |= 3;
page_directory[768] = page_table;
page_directory[1023] = ( ( unsigned long )page_directory & 0xFFFFF000 ) | 3;
return page_directory;
}A million thanks, once again!