Paging problem

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
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Paging problem

Post by Jeko »

I have a problem in the implementation of paging.
My bootloader loads the kernel at 0x10000, but when I must enable paging the kernel must be at the virtual address 0xC0000000.
How can I do?

This is my code:

Code: Select all

void enable_paging(void)
{
	unsigned int ctrl;

	asm volatile("mov %%cr0, %0":"=r"(ctrl));

	ctrl |= 0x80000000;
	asm volatile("mov %0, %%cr0"::"r"(ctrl));

	asm volatile("movw $0x10,%%ax     \n"
	"movw %%ax,%%ds      \n"
	"movw %%ax,%%es      \n"
	"movw %%ax,%%fs      \n"
	"movw %%ax,%%gs      \n"
	"movw %%ax,%%ss      \n"
	"ljmp $0x08,$next    \n"
	"nop\n" "nop\n"
	"next:\n":::"%eax");
	return;
}

void init_paging(void)
{
	unsigned long flags;

	save_flags(flags);

	unsigned long *page_dir = (unsigned long*)KERNEL_PDE;
	unsigned long *page_table = (unsigned long*)KERNEL_PTE;

	unsigned long addr;
	unsigned long real_addr = KERNEL_PHYS_ADDR;
	unsigned long pd_addr;

	//Azzera tutte le directory
	for(addr=0; addr<1024; addr++)
		page_dir[addr] = 0 | READ_WRITE;


	for(addr=0; addr<1024; addr++)
	{
		page_table[addr] = real_addr | kernel_pages_flag;
		real_addr += 4096;
	}

	page_dir[768] = (unsigned long)page_table | kernel_pages_flag;

	page_dir[1023] = (unsigned long)page_dir;
	page_dir[1023] = page_dir[1023] | kernel_pages_flag;

	pd_addr = (unsigned long)page_dir & 0xFFFFF000;
	asm volatile("movl %0, %%cr3\n" : : "r"(pd_addr));

	enable_paging();
	enable_global_pages();

	//Flush the whole TLB cache
	flush_tlb_all();

	restore_flags(flags);
}
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

See the HigherHalfKernel pages on the Wiki.
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post by frank »

Okay what exactly is the question are you asking? Is it

1. How do I set up the page directory and page tables in order to have the kernel mapped at 0xC0000000?

2. How do I run the code to map the kernel to 0xC0000000 when the kernel thinks that it is mapped at 0xC0000000 that is how do I get code to run that thinks it's mapped in at 0xC0000000 before paging is enabled?

or

3. How do I make the kernel think that it is mapped in at 0xC0000000?
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Post by Jeko »

frank wrote:Okay what exactly is the question are you asking? Is it

1. How do I set up the page directory and page tables in order to have the kernel mapped at 0xC0000000?

2. How do I run the code to map the kernel to 0xC0000000 when the kernel thinks that it is mapped at 0xC0000000 that is how do I get code to run that thinks it's mapped in at 0xC0000000 before paging is enabled?

or

3. How do I make the kernel think that it is mapped in at 0xC0000000?
My kernel is linked at 0xC0000000, but in the boot loader the kernel is loaded at 0x10000 and after it goes to protected mode. I must enable paging first and after call the kernel?
Post Reply