Paging is not enabled

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.
Post Reply
User avatar
mrjbom
Member
Member
Posts: 317
Joined: Sun Jul 21, 2019 7:34 am

Paging is not enabled

Post 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?
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Paging is not enabled

Post by iansjack »

What does debugging tell you? Are the created page tables correct? Which instruction causes the fault? What exception does it produce?
User avatar
mrjbom
Member
Member
Posts: 317
Joined: Sun Jul 21, 2019 7:34 am

Re: Paging is not enabled

Post 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
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Paging is not enabled

Post 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.
User avatar
mrjbom
Member
Member
Posts: 317
Joined: Sun Jul 21, 2019 7:34 am

Re: Paging is not enabled

Post 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?
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: Paging is not enabled

Post by nexos »

If you are using bochs, then you check your log file to see the value of cr2.
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Paging is not enabled

Post 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.
User avatar
mrjbom
Member
Member
Posts: 317
Joined: Sun Jul 21, 2019 7:34 am

Re: Paging is not enabled

Post 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?
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Paging is not enabled

Post 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.
Post Reply