Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
bool init_vm_paging() {
page_directory_table = (uint32_t*)kmalloc(sizeof(int) * 1024);
if ((uint32_t)page_directory_table % 4096) {
dprintf("page_directory_table address is not aligned!\n");
kfree(page_directory_table);
return false;
}
first_page_table = (uint32_t*)kmalloc(sizeof(int) * 1024);
if ((uint32_t)first_page_table % 4096) {
dprintf("first_page_table address is not aligned!\n");
kfree(first_page_table);
return false;
}
for(uint32_t i = 0; i < 1024; i++) {
// This sets the following flags to the pages:
// Supervisor: Only kernel-mode can access them
// Write Enabled: It can be both read from and written to
// Not Present: The page table is not present
page_directory_table[i] = 0x00000002;
}
for(uint32_t i = 0; i < 1024; i++)
{
// As the address is page aligned, it will always leave 12 bits zeroed.
// Those bits are used by the attributes ;)
first_page_table[i] = (i * 0x1000) | 3; // attributes: supervisor level, read/write, present.
}
// attributes: supervisor level, read/write, present
page_directory_table[0] = ((unsigned int)first_page_table) | 3;
loadPageDirectory(page_directory_table);
enablePaging();
return true;
}
Looks like you haven't identity-mapped the memory area containing your stack. In which case the exception would be a page fault. In that case, examine register cr2 to determine which memory address is faulting. But first check that the value in register esp is a valid mapping.
iansjack wrote:Looks like you haven't identity-mapped the memory area containing your stack.
I'm not sure what you're talking about.
iansjack wrote:In which case the exception would be a page fault. In that case, examine register cr2 to determine which memory address is faulting. But first check that the value in register esp is a valid mapping.
How can I check the cr2 register if the system crashes?
iansjack wrote:Looks like you haven't identity-mapped the memory area containing your stack.
I'm not sure what you're talking about.
Your program has a stack, located somewhere in memory. If you haven't mapped the page containing that memory location in your page table then that's an immediate crash. And the mapping has to map the physical address to the same logical address (unless you change the value of the sp register appropriately). As your program crashes on a "pop" instruction it looks like it is almost certainly a stack fault.
I'm afraid that I get the impression from your many posts that you are just following code you see somewhere without a clear understanding of what you are doing, and why. You might usefully study the Intel manuals and try to solve these problems yourself. Most importantly - learn how to use a debugger and to understand what it is telling you.
iansjack wrote:Looks like you haven't identity-mapped the memory area containing your stack.
I'm not sure what you're talking about.
Your program has a stack, located somewhere in memory. If you haven't mapped the page containing that memory location in your page table then that's an immediate crash. And the mapping has to map the physical address to the same logical address (unless you change the value of the sp register appropriately). As your program crashes on a "pop" instruction it looks like it is almost certainly a stack fault.
I'm afraid that I get the impression from your many posts that you are just following code you see somewhere without a clear understanding of what you are doing, and why. You might usefully study the Intel manuals and try to solve these problems yourself. Most importantly - learn how to use a debugger and to understand what it is telling you.
I have to add addresses that belong to the stack to the page table?(first_page_table).
This is how I find the beginning and end of the stack: