Paging Thoughts

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
Kieran
Member
Member
Posts: 54
Joined: Mon Apr 11, 2005 11:00 pm

Paging Thoughts

Post 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
Last edited by Kieran on Fri Apr 25, 2008 4:50 pm, edited 1 time in total.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post 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?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Kieran
Member
Member
Posts: 54
Joined: Mon Apr 11, 2005 11:00 pm

Post by Kieran »

Note, I am not mapping thw whole address space. Only the actual available memory.
Post Reply