OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Apr 18, 2024 7:13 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Hard time understanding recursive mapping
PostPosted: Thu Mar 12, 2015 5:03 pm 
Offline
Member
Member

Joined: Sat Oct 16, 2010 3:38 pm
Posts: 587
Previously when I had to modify the data of a page (such as a page table, page directory, etc, as they are also one page long each), I just mapped them to a page called an "ISP", and I switched the frame of the ISP whenever I had to work with a different page. This proved extremely inefficient when trying to copy address spaces (e.g. when calling fork()).

I'm trying to do recursive mapping. I don't need to do this for the entire PML4 - each process only gets one PDPT (mapped as the first PML4 entry) and I want to recursive-map that. So the equations for a PD and PT would be as follows:

Code:
addr_pdpt = 0xFFFFFFFFF000;
addr_pd = 0xFFFFFFE00000 + i_pd * 0x1000;
addr_pt = 0xFFFFC0000000 + i_pd * 0x200000 + i_pt * 0x1000;


But if I substitute 511 into i_pd, I get addr_pd = addr_pdpt, so the PD struct for the last directory would be at the same address as the address of the PDPT struct. Then how do I access the last PD so that I can map more tables into it (as tables in the last PD contain pages that map to PTs of other PDs).

Am I missing something here? If I treat the PDPT and the last PD as the same structure, then setting its first entry to some frame would mean that adress 0 would simultaneously contain the page data and all levels of paging structures, which clearly makes no sense.

Could someone show some sample code maybe? (or pseudo-code?)

Please help :(

P.S. I'm in 64-bit Long Mode.


Top
 Profile  
 
 Post subject: Re: Hard time understanding recursive mapping
PostPosted: Thu Mar 12, 2015 6:02 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 21, 2011 9:47 pm
Posts: 286
Location: Tustin, CA USA
You might take a look at this post: viewtopic.php?f=1&t=28734

_________________
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber


Top
 Profile  
 
 Post subject: Re: Hard time understanding recursive mapping
PostPosted: Thu Mar 12, 2015 10:34 pm 
Offline
Member
Member

Joined: Fri May 11, 2012 11:54 am
Posts: 53
Or this one: http://forum.osdev.org/viewtopic.php?f=15&t=25545


Top
 Profile  
 
 Post subject: Re: Hard time understanding recursive mapping
PostPosted: Fri Mar 13, 2015 3:13 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
There's an explanation, with diagrams, that might help here: http://pdosnew.csail.mit.edu/6.828/2014 ... /uvpt.html


Top
 Profile  
 
 Post subject: Re: Hard time understanding recursive mapping
PostPosted: Fri Mar 13, 2015 3:57 am 
Offline
Member
Member

Joined: Tue Feb 26, 2008 10:47 am
Posts: 89
Location: Sweden
And yet another one: http://thomasloven.com/blog/2012/06/Recursive-Page-Directory/
though not for long mode..

_________________
Blawg


Top
 Profile  
 
 Post subject: Re: Hard time understanding recursive mapping
PostPosted: Fri Mar 13, 2015 7:14 am 
Offline
Member
Member

Joined: Sat Oct 16, 2010 3:38 pm
Posts: 587
So, I take that mapping the PDPT's last entry onto itself would cause the last 1GB it encompasses to be mapped, and therefore each structure will be accessible, and then I can use the equations above to find each structure and therefore not bother about mapping the last PD, PT etc? But then I'm still confused, because only 1 frame gets allocated, and all those structures cannot be stored in 1 frame.

Just to make sure I understand: I initalize the PDPT as follows, correct?

Code:
PDPT *pdpt = getPDPT();
pdpt->entries[511].present = 1;
pdpt->entries[511].pdPhysFrame = pdptPhysFrame;


Then, if I want a pointer to a specific page table entry, I'd do the following, correct?

Code:
PTe *getPTE(uint64_t addr)
{
   uint64_t pageIndex = (addr & 0xFFF);
   addr >>= 12;
   uint64_t tableIndex = (addr & 0x1FF);
   addr >>= 12;
   uint64_t dirIndex = (addr & 0x1FF);

   PDPT *pdpt = (PDPT*) (0xFFFFFFFFF000);
   if (!pdpt->entries[dirIndex].present)
   {
      pdpt->entries[dirIndex].present = 1;
      pdpt->entries[dirIndex].pdPhysFrame = phmAllocFrame();
   };
   
   PD *pd = (PD*) (0xFFFFFFE00000 + dirIndex * 0x1000);
   if (!pd->entries[tableIndex].present)
   {
      pd->entries[tableIndex].present = 1;
      pd->entries[tableIndex].ptPhysFrame = phmAllocFrame();
   };
   
   PT *pt = (PT*) (0xFFFFC0000000 + dirIndex * 0x200000 + tableIndex * 0x1000);
   return &pt->entries[pageIndex];
};


Am I getting this right? The PDPT itself is the first entry of the PML4.


Top
 Profile  
 
 Post subject: Re: Hard time understanding recursive mapping
PostPosted: Fri Mar 13, 2015 2:04 pm 
Offline
Member
Member
User avatar

Joined: Tue Oct 17, 2006 11:33 pm
Posts: 3882
Location: Eindhoven
Yes. The trick is that you should see the table as containing itself 4 times -- every time mapped as the top (or bottom) entry. Accessing the next layer is done from the top or bottom as you first enter the location of that entry in the toplevel page table, then the next layer (which is now mappable as there is a page table for it!) and so on.


Top
 Profile  
 
 Post subject: Re: Hard time understanding recursive mapping
PostPosted: Fri Mar 13, 2015 2:16 pm 
Offline
Member
Member

Joined: Sat Oct 16, 2010 3:38 pm
Posts: 587
Candy wrote:
Yes. The trick is that you should see the table as containing itself 4 times -- every time mapped as the top (or bottom) entry. Accessing the next layer is done from the top or bottom as you first enter the location of that entry in the toplevel page table, then the next layer (which is now mappable as there is a page table for it!) and so on.


OK, thank you for the help. It appears to be working :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 29 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group