Page 1 of 1

Paging Thoughts

Posted: Fri Apr 25, 2008 2:00 pm
by Kieran
Hi I want to Identity Map all available memory.

This is how I thaught to do it:

First I will break down the highest possible memory address (Provided by GRUB) into its component parts (as if it were a virtual PAGED address), telling me how many Page Directory Entries are used and how many Page Table Entries (In the final Page Directory) are used to reach this address.

Code: Select all

   EndAddress = Memory Length (in bytes)
   DirectoryEntries = bits 22-32 of EndAddrees
   TableEntries = bits 12-22 of EndAddress

So the total number of Page Table Entries = ((DirectoryEntries*0x400)+TableEntries)

All of my Page Table Entries will be stored in a single array with my Page Directory situated just before it:

NOTE: PageDirectory and PageTable, both 4Kb aligned

Code: Select all

   unsigned long *PageDirectory = (unsigned long *)0x400000;
   unsigned long *PageTable = (unsigned long *)0x401000;

So, If i was to Fill all of the Page Table Entries with:

Code: Select all

   i=0, address=0;
   for(i=0;i<=((DirectoryEntries*0x400)+TableEntries);i++)  {
      PageTable[i]= address | 3;
      Address=(Address+0x1000); // 4Kb in HEX
   }

Then fill in the Page Directory with:

NOTE: See above for definition of DirectoryEntries

Code: Select all

   i=0;
   for(i=0;i<=DirectoryEntries;i++) {
      PageDirectory[i]=PageTable[i*0x400];
      PageDirectory[i]=PageDirectory[i] | 3;
   }
   for(i=DirectoryEntries;i<=0x400;i++) {
      PageDirectory[i]=0 | 2;
   }  

Then Enable Paging with:

Code: Select all

   Set CR3 to address of PageDirectory
   Set PE bit in CR0

This should enable paging and have all available memory Identity Mapped...

I have tried and something is wrong.
I just can't figure out the problem

Posted: Fri Apr 25, 2008 2:29 pm
by Combuster
1) Is long 32 bits?
2) does (DirectoryEntries*0x400)+TableEntries equal 1024 * 1024
2b) DirectoryEntries and TableEntries should both equal 1024
3)

Code: Select all

PageDirectory[i]=PageTable[i*0x400];
needs an extra ampersand (and typecast)
4) you have a redundant for loop
5)

Code: Select all

   i=0 
do I see a missing semicolon?
6) do you have at least 12MB of memory installed?

Posted: Sat Apr 26, 2008 4:06 am
by Kieran
Note, I am not mapping thw whole address space. Only the actual available memory.