Page 1 of 1

Do I have the right idea about paging?

Posted: Sun Jun 28, 2009 11:58 am
by xvedejas
I'm not sure what needs to be done next to move on to some real memory management, or if the code I've written so far is correct.

Code: Select all

#include <system.h>

/* Page Directory is an array of 1024 Entries, each pointing to a page table
 * 
 * Page Table is an array of 1024 Entries, each pointing to a page's phys addr
 */

struct page_directory_entry
{
	 // pointer to a page table
	struct page_table_entry *page_table[1024];
};

struct page_table_entry
{
	 // pointer to page's physical address.
	unsigned long page;
};

struct page_directory_entry *page_directory[1024];

void init_paging()
{
	__asm__ __volatile__("mov %1, %eax" :: "r"(page_directory) : "%eax");
	__asm__ __volatile__("mov %eax, %cr3" ::: "%eax");
	__asm__ __volatile__("mov %cr0, %eax" ::: "%eax");
	__asm__ __volatile__("or %eax, 0x80000000" ::: "%eax");
	__asm__ __volatile__("mov %eax, %cr0" ::: "%eax");
}
Have I got the idea right, at least?

Re: Do I have the right idea about paging?

Posted: Sun Jun 28, 2009 12:01 pm
by Troy Martin
I suggest you read the wiki and JamesM's tutorials for more information. They're invaluable sources of information for this kind of stuff.

EDIT: Oh, and the Intel manuals are wonderful too.

Re: Do I have the right idea about paging?

Posted: Sun Jun 28, 2009 2:31 pm
by yemista
Yea you gotta read a lot first. That code works, but if you understand paging, then you understand what the whole point of it is and why it is used. 32 bit processors have the ability to access 4gb of memory without paging; paging exists for a whole lot of other reasons. Your code shows us that you know roughly what the definition of a directory and table are, and how to tell the processor to use paging. Also your code will crash the machine as is, because your tables havnt been initialized. Paging is actually real simple, but its not intuitive and can take a while to get your head around it

Re: Do I have the right idea about paging?

Posted: Sun Jun 28, 2009 3:06 pm
by neon
Hm, the only thing thats actually "wrong" with that code is that page_table_entry contains more then just a frame address. With that it would triple fault immediately do to the currently running code being unpaged. Also, I personally prefer separating page table entries from page directory entries do to them having different purposes and different (although slightly) formats.

Other then that the code looks good so far :)

Re: Do I have the right idea about paging?

Posted: Sun Jun 28, 2009 4:37 pm
by gzaloprgm
Also, struct page_table_entry *page_table[1024];

You won't be able to access the page tables as an array (because the pointer does not only contain the address, but also some flags). You can read more about paging in Intel's software developer manual, Volume 3a, chapter 3.7.6

Re: Do I have the right idea about paging?

Posted: Sun Jun 28, 2009 7:01 pm
by kop99
After full understanding of paging, you could implement memory manager.(ex, buddy, slab algorithm, et...)