Do I have the right idea about paging?

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
xvedejas
Member
Member
Posts: 168
Joined: Thu Jun 04, 2009 5:01 pm

Do I have the right idea about paging?

Post 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?
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Do I have the right idea about paging?

Post 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.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

Re: Do I have the right idea about paging?

Post 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
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Do I have the right idea about paging?

Post 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 :)
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
gzaloprgm
Member
Member
Posts: 141
Joined: Sun Sep 23, 2007 4:53 pm
Location: Buenos Aires, Argentina
Contact:

Re: Do I have the right idea about paging?

Post 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
Visit https://gzalo.com : my web site with electronic circuits, articles, schematics, pcb, calculators, and other things related to electronics.
User avatar
kop99
Member
Member
Posts: 120
Joined: Fri May 15, 2009 2:58 am

Re: Do I have the right idea about paging?

Post by kop99 »

After full understanding of paging, you could implement memory manager.(ex, buddy, slab algorithm, et...)
Post Reply