Paging question
Posted: Sat Jun 24, 2006 9:03 am
Hey, I have put together a fairly basic os (Interrupts, nice keyboard driver, GDT, IDT, and a large portion of libc ported) , and now I'm starting work on a memory manager, I chose paging because of it's supposed portability, and now I'm having a little trouble with implementation, ok I have a very very limited understanding of how paging works, here's how I see it:
A page directory is an array of page tables which is an array of pages, so in this tutorial http://osdever.net/tutorials/paging.php?the_id=43 I've compiled the snippets into a single file and here is what I have:
First off, this code does compile and doesn't crash my kernel , but with a warning about line 10 of map_pages(); which I'm not concerned about right now.
So my first question... do I have my write_cr3, write_cr0, and read_cr0 prototypes correct?
My second question, the author of this tutorial mentioned that this code only maps 4MB of pages into memory, and I see how that works, but how would I go about adapting this code to map the rest of my system memory?
I have access to the memory information GRUB provides, and I know how to find the number of bytes in the extended memory, number of pages and pages_tables to cover all of my system's memory, but I don't understand how I would modify this code to allow me to make an array of page tables.... It seems like it would be very easy to do, and I have tried, but I am missing something here beause my system just restarts if I don't have all of the page tables either present or not present
- Thanks in advance
A page directory is an array of page tables which is an array of pages, so in this tutorial http://osdever.net/tutorials/paging.php?the_id=43 I've compiled the snippets into a single file and here is what I have:
Code: Select all
#ifndef _MM0_C_
#define _MM0_C_
unsigned long *page_directory = (unsigned long *) 0x9C000;
/* the page table comes directly after the page directory (4kb aligned) */
/* 0x9D000 - 0x9C000 = 1000 <to> = 4096 bytes (4kb) */
unsigned long *page_table = (unsigned long *) 0x9D000;
/* externed from start.asm */
extern void write_cr3(unsigned long *addr);
extern void write_cr0(unsigned long addr);
extern unsigned long read_cr0(void);
void map_pages(void)
{
unsigned long address=0; /* holds the physical address of where a page is */
unsigned int i;
for(i=0; i<1024; i++)
{
page_table[i] = address | 3; // attribute set to: supervisor level, read/write, present(011 in binary)
address = address + 4096; // 4096 = 4kb
}
// fill the first entry of the page directory
page_directory[0] = page_table; // attribute set to: supervisor level, read/write, present(011 in binary)
page_directory[0] = page_directory[0] | 3;
for(i=1; i<1024; i++)
{
page_directory[i] = 0 | 2; // attribute set to: supervisor level, read/write, not present(010 in binary)
}
}
void enable_paging(void)
{
/* places the address of the page directory in segment register CR3 */
write_cr3(page_directory);
/* enable paging by ssetting the paging bit (31) in CR0 to '1' */
write_cr0(read_cr0() | 0x80000000);
}
void enable_mm0(void)
{
map_pages();
enable_paging();
printf(" - Paging Enabled.\n");
}
#endif
First off, this code does compile and doesn't crash my kernel , but with a warning about line 10 of map_pages(); which I'm not concerned about right now.
So my first question... do I have my write_cr3, write_cr0, and read_cr0 prototypes correct?
My second question, the author of this tutorial mentioned that this code only maps 4MB of pages into memory, and I see how that works, but how would I go about adapting this code to map the rest of my system memory?
I have access to the memory information GRUB provides, and I know how to find the number of bytes in the extended memory, number of pages and pages_tables to cover all of my system's memory, but I don't understand how I would modify this code to allow me to make an array of page tables.... It seems like it would be very easy to do, and I have tried, but I am missing something here beause my system just restarts if I don't have all of the page tables either present or not present
- Thanks in advance