Page 1 of 1

Paging is not enabled

Posted: Mon Jun 08, 2020 4:52 am
by mrjbom
Hi.
I'm trying to add paging to my system, I'm using this tutorial.
When calling enablePaging(), the system crashes.
Here's my code:

Code: Select all

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;
}
The addresses are aligned.
What's wrong?

Re: Paging is not enabled

Posted: Mon Jun 08, 2020 5:48 am
by iansjack
What does debugging tell you? Are the created page tables correct? Which instruction causes the fault? What exception does it produce?

Re: Paging is not enabled

Posted: Mon Jun 08, 2020 6:24 am
by mrjbom
iansjack wrote:What does debugging tell you? Are the created page tables correct? Which instruction causes the fault? What exception does it produce?
Everything before "pop %ebp" works fine

Code: Select all

.text
.global enablePaging
enablePaging:
push %ebp
mov %esp, %ebp
mov %cr0, %eax
or $0x80000000, %eax
mov %eax, %cr0
mov %ebp, %esp
pop %ebp <--------
ret

Re: Paging is not enabled

Posted: Mon Jun 08, 2020 7:00 am
by iansjack
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.

Re: Paging is not enabled

Posted: Mon Jun 08, 2020 7:57 am
by mrjbom
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?

Re: Paging is not enabled

Posted: Mon Jun 08, 2020 9:15 am
by nexos
If you are using bochs, then you check your log file to see the value of cr2.

Re: Paging is not enabled

Posted: Mon Jun 08, 2020 9:30 am
by iansjack
mrjbom wrote:
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.

Re: Paging is not enabled

Posted: Mon Jun 08, 2020 1:14 pm
by mrjbom
iansjack wrote:
mrjbom wrote:
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:

Code: Select all

uint16_t ss = 0; //stack start
uint32_t esp = 0; //stack end
__asm__ (
    "mov %%ss, %0" : "=r"(ss)
);
__asm__ (
    "mov %%esp, %0" : "=r"(esp)
);
Is this correct?

Re: Paging is not enabled

Posted: Mon Jun 08, 2020 1:38 pm
by iansjack
I don't know where your stack starts but the stack segment register and the current value of the stack pointer don't tell you that.

The stack occupies the pages that you set aside for it when you defined it - you did set up a stack, didn't you?

Honestly, you need to read the processor manuals, or at least skim through them.