Problems creating a new page dir and loading it to CR3

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
eino
Member
Member
Posts: 49
Joined: Fri Sep 16, 2011 10:00 am
Location: Finland

Problems creating a new page dir and loading it to CR3

Post by eino »

Hi... Been struggling with this for a while. The CPU tripplefaults.

I have a higher half kernel basically made with the Tim R. GDT trick. I'm beginning the preparations for multitasking which involves cloning a PD. I have a working (mostly) heap, paging and allocation seems to work. But now that I try to create a new page directory and load it's physical address to cr3 the cpu crashes.

There's something I don't quite understand for example:

Code: Select all

kernelpagedirPtr = (char *)kernelpagedir + 0x40000000;	// Translate the page directory from
								// virtual address to physical address
lowpagetablePtr = (char *)lowpagetable + 0x40000000;	// Same for the page table
Why do we need to translate pd from virtual to physical? Paging is not yet enabled at that point...


Here's my code.

Code: Select all

unsigned int kernelpagedir[1024] __attribute__ ((aligned (4096)));
unsigned int lowpagetable[1024] __attribute__ ((aligned (4096)));

void initializePaging () {
	
	void *kernelpagedirPtr = 0;
	void *lowpagetablePtr = 0;
	int k = 0;
	
	kernelpagedirPtr = (char *)kernelpagedir + 0x40000000;
	lowpagetablePtr = (char *)lowpagetable + 0x40000000;
	
	for (k = 0; k < 1024; k++)
	{
		lowpagetable[k] = (k * 4096) | 3;
		kernelpagedir[k] = 0;
	}
	
	kernelpagedir[0] = (unsigned int)lowpagetablePtr | 3;
	kernelpagedir[768] = (unsigned int)lowpagetablePtr | 3;

	kernelpagedir[1023] = ( (unsigned int)kernelpagedirPtr & 0xFFFFF000 ) | 3;
	
	active_page_directory = kernelpagedirPtr;

	enablePaging ();
	
	/*
	create a new page directory and try to set it to cr3
	*/
	unsigned long new_dir_addr_phys = cloneDirectory ();
	
	asm volatile( "cli" );
	active_page_directory = new_dir_addr_phys;
	
	asm volatile( "mov %0, %%cr3":: "b"( active_page_directory ) );
	
}

unsigned long cloneDirectory ( /* unsigned int *source_directory */ ) {

	unsigned long *page_directory = getFreeFrame(); //get address (phys) to one 0x1000 frame
	
	int i = 0;
	for( i = 0; i < 1024; i++ ) {
		page_directory[i] = 0 | 2; 
	}
	
	unsigned long *page_table = getFreeFrame();
	
	unsigned long address = 0;  // <--- Kernel is actually physically somewhere at the bottom so lets map 0-4 megs to a page table and then put this table to the PDE 768 (3gigs higher half)
	
	for( i = 0; i < 1024; i++ )
	{
		page_table[i] = address | 3
		address = address + 4096;
	}

	page_directory[0] = page_table; //<--- identity map the low 0-4 megs
	page_directory[0] |= 3;
	
	page_directory[768] = page_table;
	page_directory[1023] = ( ( unsigned long )page_directory & 0xFFFFF000 ) | 3;
	
	return page_directory;
}
Hopefully my question was well formed. If not please let me know =)

A million thanks, once again!
I'm Eino Tuominen from Finland, a web software dev learning low level stuff and reading / trying out kernel dev
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Problems creating a new page dir and loading it to CR3

Post by bluemoon »

I suggest to depart from tutorial design before going further stuff like multi-tasking.
The GDT trick, as you might already know, is a trick. Having all +0x40000000 around isn't that nice IMO.


If you separate physical page allocator, logical address space management then cloning page dir become as easy as:

Code: Select all

    uint32_t*pagedir = (uint32_t*)KADDR_NEWPROCESS_PDE; // hardcoded 4K address space for doing clone

    if ( (pagedir_addr = PMM_alloc()) == 0 ) goto fail;
    LMM_mmap_physical(pagedir, pagedir_addr, 4096, 0);
    memset ( pagedir, 0, 2048 );                        // zero application space
    memcpy ( &pagedir[512], &k_pagedir[512], 2048 -4 ); // clone kernel space
    pagedir[1023] = pagedir_addr +1;      // recursive page dir
    LMM_munmap(pagedir, 4096, kDontReleaseToPMM);
User avatar
eino
Member
Member
Posts: 49
Joined: Fri Sep 16, 2011 10:00 am
Location: Finland

Re: Problems creating a new page dir and loading it to CR3

Post by eino »

Thanks for that!

I've made some progress with this. I'll be posting the solution when it "fully" works.
I'm Eino Tuominen from Finland, a web software dev learning low level stuff and reading / trying out kernel dev
User avatar
eino
Member
Member
Posts: 49
Joined: Fri Sep 16, 2011 10:00 am
Location: Finland

Re: Problems creating a new page dir and loading it to CR3

Post by eino »

So... This is just a step to the direction. I have had too much other stuff to do so this is going forwards a bit slower than I wished. I'll post this just incase someone is having a same sort of problem. Just to give direction or an idea, that is.

Gonna be posting the full solution as soon as I have time more time for this project again =)

Code: Select all

void initializePaging () {
	
	// Pointers to the page directory and the page table
	void *kernelpagedirPtr = 0;
	void *lowpagetablePtr = 0;
	int k = 0;
	

	kernelpagedirPtr = (char *)kernelpagedir + 0x40000000;	// Translate the page directory from virtual address to physical address
	lowpagetablePtr = (char *)lowpagetable + 0x40000000;	// Same for the page table

	for (k = 0; k < 1024; k++)
	{
		lowpagetable[k] = (k * 4096) | 3;	// ...map the first 4MB of memory into the page table...
		kernelpagedir[k] = 0;			// ...and clear the page directory entries
	}
	
	// Fills the addresses 0...4MB and 3072MB...3076MB of the page directory
	// with the same page table
	kernelpagedir[0] = (unsigned int)lowpagetablePtr | 3;
	kernelpagedir[768] = (unsigned int)lowpagetablePtr | 3;

	kernelpagedir[1023] = ( (unsigned int)kernelpagedirPtr & 0xFFFFF000 ) | 3;

	active_page_directory = kernelpagedirPtr;

	kpdptr = kernelpagedirPtr;

	enablePaging ();

	/*****
	
	TEST DIRECTORY CLONING
	
	*****/

	test_page_directory = cloneDirectory();
	
	active_page_directory = test_page_directory;
	asm volatile( "mov %0, %%cr3":: "b"( active_page_directory ) );
	
}

unsigned long cloneDirectory ( /* unsigned int *source_directory */ ) {
	
	/*****
	
	HEAVY WIP
	
	******/
	
	unsigned int pagedir[1024] __attribute__ ((aligned (4096)));
	unsigned int newlowpagetable[1024] __attribute__ ((aligned (4096)));	

	// Pointers to the page directory and the page table
	void *pagedirPtr = 0;
	void *lowpagetablePtr = 0;
	int k = 0;
	
	
	pagedirPtr = (char *)pagedir + 0x40000000;	// Translate the page directory from virtual address to physical address
	lowpagetablePtr = (char *)newlowpagetable + 0x40000000;	// Same for the page table

	for (k = 0; k < 1024; k++)
	{
		newlowpagetable[k] = (k * 4096) | 3;	// ...map the first 4MB of memory into the page table...
		pagedir[k] = 0 | 3;			// ...and clear the page directory entries
	}

	pagedir[0] = (unsigned int)lowpagetablePtr | 3;

	pagedir[768] = (unsigned int)lowpagetablePtr | 3;
	
	pagedir[1023] = ( (unsigned int)pagedirPtr & 0xFFFFF000 ) | 3;
	

	return pagedirPtr;

	
}
I'm Eino Tuominen from Finland, a web software dev learning low level stuff and reading / trying out kernel dev
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Problems creating a new page dir and loading it to CR3

Post by bluemoon »

Spot a bug, pagedir and his friends are local variable, which the life ends as soon as the function returns.
So, the CR3 will reference to random content as the OS runs :P
User avatar
eino
Member
Member
Posts: 49
Joined: Fri Sep 16, 2011 10:00 am
Location: Finland

Re: Problems creating a new page dir and loading it to CR3

Post by eino »

That is correct. But since I've been asked if I could post what I've got (so far) I figured it won't hurt anyone even if this is still in babysteps. =)
I'm Eino Tuominen from Finland, a web software dev learning low level stuff and reading / trying out kernel dev
Post Reply