using the virtual memory manager

If you are new to OS Development, plan on spending some time here first before going into the other forums.

Moderator: Moderators

Post Reply
Posts: 12
Joined: Fri Apr 06, 2012 7:24 am

using the virtual memory manager

Post by vjain20 » Tue May 15, 2012 6:32 am

I am thinking of implementing a heap allocator on top of virtual memory manager. However I am wondering
if it is possible to access the virtual memory manager only through the interface. The function provided the VMM to allocate
a page is

Code: Select all

bool vmmngr_alloc_page (pt_entry* e) {
   //! allocate a free physical frame
   void* p = pmmngr_alloc_block ();
   if (!p)
      return false;
   //! map it to the page
   pt_entry_set_frame (e, (physical_addr)p);
   pt_entry_add_attrib (e, I86_PTE_PRESENT);
   return true;

But this takes a pt_entry which means that any other kernel code which wants to use the services the VMM will have to
directly access the Page Directory to get the pt_entry first and then call this function. Though this is not a big issue
and I can write another function which takes a virtual address and retrieves the pt_entry itself but I just want to confirm if that is the only option.

Vaibhav Jain

User avatar
Site Admin
Posts: 465
Joined: Sat Oct 20, 2007 7:58 pm

Re: using the virtual memory manager

Post by Mike » Tue May 15, 2012 10:11 pm


The VMM designed for the series is over-complicated in this regard. It would be more beneficial to allocate a region and map it into the address space for the heap allocator to use. The VMM provided by the series includes a function that allocates the pages automatically - virMemMgr_MapPage - which can be used to map any physical address to a virtual address. With this in mind, we can allocate a continuous region of physical memory using pmmngr_alloc_blocks and then map the region with virMemMgr_MapPage to be used as heap memory. Your heap allocator can work with the heap memory.

To summarize, you could allocate a region calling pmmngr_alloc_blocks and then map the region (by looping) by calling virMemMgr_MapPage which can be your heap.

Posts: 12
Joined: Fri Apr 06, 2012 7:24 am

Re: using the virtual memory manager

Post by vjain20 » Tue May 15, 2012 11:22 pm


Posts: 12
Joined: Fri Apr 06, 2012 7:24 am

Re: using the virtual memory manager

Post by vjain20 » Sat May 19, 2012 10:10 am


I have a query about the vmmngr_map_page function.
Following is the implementation

Code: Select all

void vmmngr_map_page (void* phys, void* virt) {

   //! get page directory
   pdirectory* pageDirectory = vmmngr_get_directory ();

   //! get page table
   pd_entry* e = &pageDirectory->m_entries [PAGE_DIRECTORY_INDEX ((uint32_t) virt) ];
   if ( (*e & I86_PTE_PRESENT) != I86_PTE_PRESENT) {

      //! page table not present, allocate it
      ptable* table = (ptable*) pmmngr_alloc_block ();
      if (!table)

      //! clear page table
      memset (table, 0, sizeof(ptable));

      //! create a new entry
      pd_entry* entry =
         &pageDirectory->m_entries [PAGE_DIRECTORY_INDEX ( (uint32_t) virt) ];

      //! map in the table (Can also just do *entry |= 3) to enable these bits
      pd_entry_add_attrib (entry, I86_PDE_PRESENT);
      pd_entry_add_attrib (entry, I86_PDE_WRITABLE);
      pd_entry_set_frame (entry, (physical_addr)table);

   //! get table*****************************
   ptable* table = (ptable*) PAGE_GET_PHYSICAL_ADDRESS ( e );

   //! get page******************************
   pt_entry* page = &table->m_entries [ PAGE_TABLE_INDEX ( (uint32_t) virt) ];

   //! map it in (Can also do (*page |= 3 to enable..)
   pt_entry_set_frame ( page, (physical_addr) phys);
   pt_entry_add_attrib ( page, I86_PTE_PRESENT);

In the above code the lines (marked by **)

//! get table
ptable* table = (ptable*) PAGE_GET_PHYSICAL_ADDRESS ( e );

//! get page
pt_entry* page = &table->m_entries [ PAGE_TABLE_INDEX ( (uint32_t) virt) ];

are using the physical address of page table as its virtual address. I guess this is under the assumption that
memory area occupied by the page table is identity-mapped. Am I right ?
Also, I am wondering if this can cause problem at some point of time because not all the memory is identity-mapped.

Post Reply