Page 1 of 1

Paging problem

Posted: Fri Jul 27, 2007 11:12 am
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);
}

Posted: Fri Jul 27, 2007 5:14 pm
by pcmattman
See the HigherHalfKernel pages on the Wiki.

Posted: Fri Jul 27, 2007 7:14 pm
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?

Posted: Sat Jul 28, 2007 10:25 am
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?