4 * include/tpl_mm_phys.h
5 * Physical Memory Manager Template
10 * \file tpl_mm_phys_stack.h
11 * \brief Template physical memory manager
13 * An extensible physical memory manager
15 * Usage: Requires NUM_MM_PHYS_RANGES to be set to the number of address
17 * MAX_PHYS_PAGES - Used to calculate structure sizes
22 //void MM_InitPhys_Multiboot(tMBoot_Info *MBoot);
23 //tPAddr MM_AllocPhysRange(int Num, int Bits);
24 //tPAddr MM_AllocPhys(void);
25 //void MM_RefPhys(tPAddr PAddr);
26 //void MM_DerefPhys(tPAddr PAddr);
27 int MM_int_GetRangeID( tPAddr Addr );
28 int MM_int_IsPhysUnavail( tPageNum Page );
31 tMutex glPhysicalPages;
32 //Uint64 *gaPageStacks[NUM_MM_PHYS_RANGES]; // Page stacks
33 int giPageStackSizes[NUM_MM_PHYS_RANGES]; // Points to the first unused slot
34 Uint32 *gaiPageReferences = (void*)MM_PAGE_COUNTS; // Reference Counts
35 Uint64 giMaxPhysPage = 0; // Maximum Physical page
39 * \brief Initialise the physical memory map using a Multiboot 1 map
41 void MM_Tpl_InitPhys(int MaxRAMPage)
43 const int PAGE_SIZE = 0x1000;
44 const int pagesPerPageOnStack = PAGE_SIZE / sizeof(gaPageStack[0]);
48 // ENTER("pMBoot=%p", MBoot);
50 giMaxPhysPage = MaxRAMPage;
53 for( i = 0; i < NUM_MM_PHYS_RANGES; i ++ )
55 for( ; page < giPageRangeMax[i] && page < giMaxPhysPage; page ++ )
59 rangeSize = MM_int_IsPhysUnavail(page);
64 // Page is avaliable for use
66 // Check if the page stack is allocated
67 tVAddr stack_page = &gaPageStacks[i][giPageStackSizes[i]&~pagesPerPageOnStack];
68 if( !MM_GetPhysAddr( stack_page ) ) {
69 MM_Map( stack_page, page*PAGE_SIZE );
72 gaPageStacks[i][ giPageStackSizes[i] ] = page;
73 giPageStackSizes[i] ++;
81 void MM_DumpStatistics(void)
83 // TODO: PM Statistics for tpl_mm_phys_bitmap
87 * \brief Allocate a contiguous range of physical pages with a maximum
88 * bit size of \a MaxBits
89 * \param Pages Number of pages to allocate
90 * \param MaxBits Maximum size of the physical address
91 * \note If \a MaxBits is <= 0, any sized address is used (with preference
92 * to higher addresses)
94 tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
100 ENTER("iPages iBits", Pages, MaxBits);
102 if( MaxBits <= 0 || MaxBits >= 64 ) // Speedup for the common case
103 rangeID = MM_PHYS_MAX;
105 rangeID = MM_int_GetRangeID( (1LL << MaxBits) - 1 );
107 LOG("rangeID = %i", rangeID);
109 Mutex_Acquire(&glPhysicalPages);
111 // Check if the range actually has any free pages
112 while(giPageStackSizes[rangeID] == 0 && rangeID)
115 LOG("rangeID = %i", rangeID);
117 // What the? Oh, man. No free pages
118 if(giPageStackSizes[rangeID] == 0) {
119 Mutex_Release(&glPhysicalPages);
120 // TODO: Page out / attack the cache
122 Warning(" MM_AllocPhysRange: Out of free pages");
124 "Out of memory (unable to fulfil request for %i pages), zero remaining",
131 // Check if there is enough in the range
132 if(giPhysRangeFree[rangeID] >= Pages)
134 LOG("{%i,0x%x -> 0x%x}",
135 giPhysRangeFree[rangeID],
136 giPhysRangeFirst[rangeID], giPhysRangeLast[rangeID]
138 // Do a cheap scan, scanning upwards from the first free page in
141 addr = giPhysRangeFirst[ rangeID ];
142 while( addr <= giPhysRangeLast[ rangeID ] )
144 //Log(" MM_AllocPhysRange: addr = 0x%x", addr);
145 // Check the super bitmap
146 if( gaSuperBitmap[addr >> (6+6)] + 1 == 0 ) {
147 LOG("nFree = %i = 0 (super) (0x%x)", nFree, addr);
149 addr += 1LL << (6+6);
150 addr &= ~0xFFF; // (1LL << 6+6) - 1
153 // Check page block (64 pages)
154 if( gaMainBitmap[addr >> 6] + 1 == 0) {
155 LOG("nFree = %i = 0 (main) (0x%x)", nFree, addr);
161 // Check individual page
162 if( gaMainBitmap[addr >> 6] & (1LL << (addr & 63)) ) {
163 LOG("nFree = %i = 0 (page) (0x%x)", nFree, addr);
170 LOG("nFree(%i) == %i (0x%x)", nFree, Pages, addr);
174 LOG("nFree = %i", nFree);
175 // If we don't find a contiguous block, nFree will not be equal
176 // to Num, so we set it to zero and do the expensive lookup.
177 if(nFree != Pages) nFree = 0;
182 // Oops. ok, let's do an expensive check (scan down the list
183 // until a free range is found)
185 addr = giPhysRangeLast[ rangeID ];
187 Mutex_Release(&glPhysicalPages);
190 Warning(" MM_AllocPhysRange: Out of memory (unable to fulfil request for %i pages)", Pages);
192 "Out of memory (unable to fulfil request for %i pages)",
198 LOG("nFree = %i, addr = 0x%08x", nFree, addr);
200 // Mark pages as allocated
202 for( i = 0; i < Pages; i++, addr++ )
204 gaMainBitmap[addr >> 6] |= 1LL << (addr & 63);
205 rangeID = MM_int_GetRangeID(addr << 12);
206 giPhysRangeFree[ rangeID ] --;
207 LOG("%x == %x", addr, giPhysRangeFirst[ rangeID ]);
208 if(addr == giPhysRangeFirst[ rangeID ])
209 giPhysRangeFirst[ rangeID ] += 1;
211 ret = addr; // Save the return address
213 // Update super bitmap
214 Pages += addr & (64-1);
216 Pages = (Pages + (64-1)) & ~(64-1);
217 for( i = 0; i < Pages/64; i++ )
219 if( gaMainBitmap[ addr >> 6 ] + 1 == 0 )
220 gaSuperBitmap[addr>>12] |= 1LL << ((addr >> 6) & 63);
223 Mutex_Release(&glPhysicalPages);
224 LEAVE('x', ret << 12);
229 * \brief Allocate a single physical page, with no preference as to address
232 tPAddr MM_AllocPhys(void)
236 // Hack to allow allocation during setup
237 for(i = 0; i < NUM_STATIC_ALLOC; i++) {
238 if( gaiStaticAllocPages[i] ) {
239 tPAddr ret = gaiStaticAllocPages[i];
240 gaiStaticAllocPages[i] = 0;
241 Log("MM_AllocPhys: Return %x, static alloc %i", ret, i);
246 return MM_AllocPhysRange(1, -1);
250 * \brief Reference a physical page
252 void MM_RefPhys(tPAddr PAddr)
254 Uint64 page = PAddr >> 12;
256 if( PAddr >> 12 > giMaxPhysPage ) return ;
258 if( gaMainBitmap[ page >> 6 ] & (1LL << (page&63)) )
261 gaMultiBitmap[ page >> 6 ] |= 1LL << (page&63);
262 gaiPageReferences[ page ] ++;
267 gaMainBitmap[page >> 6] |= 1LL << (page&63);
268 if( gaMainBitmap[page >> 6 ] + 1 == 0 )
269 gaSuperBitmap[page>> 12] |= 1LL << ((page >> 6) & 63);
274 * \brief Dereference a physical page
276 void MM_DerefPhys(tPAddr PAddr)
278 Uint64 page = PAddr >> 12;
280 if( PAddr >> 12 > giMaxPhysPage ) return ;
282 if( gaMultiBitmap[ page >> 6 ] & (1LL << (page&63)) ) {
283 gaiPageReferences[ page ] --;
284 if( gaiPageReferences[ page ] == 1 )
285 gaMultiBitmap[ page >> 6 ] &= ~(1LL << (page&63));
286 if( gaiPageReferences[ page ] == 0 )
287 gaMainBitmap[ page >> 6 ] &= ~(1LL << (page&63));
290 gaMainBitmap[ page >> 6 ] &= ~(1LL << (page&63));
292 // Update the free counts if the page was freed
293 if( !(gaMainBitmap[ page >> 6 ] & (1LL << (page&63))) )
296 rangeID = MM_int_GetRangeID( PAddr );
297 giPhysRangeFree[ rangeID ] ++;
298 if( giPhysRangeFirst[rangeID] > page )
299 giPhysRangeFirst[rangeID] = page;
300 if( giPhysRangeLast[rangeID] < page )
301 giPhysRangeLast[rangeID] = page;
304 // If the bitmap entry is not -1, unset the bit in the super bitmap
305 if(gaMainBitmap[ page >> 6 ] + 1 != 0 ) {
306 gaSuperBitmap[page >> 12] &= ~(1LL << ((page >> 6) & 63));
311 * \brief Takes a physical address and returns the ID of its range
312 * \param Addr Physical address of page
313 * \return Range ID from eMMPhys_Ranges
315 int MM_int_GetRangeID( tPAddr Addr )
320 return MM_PHYS_32BIT;
322 return MM_PHYS_24BIT;
324 return MM_PHYS_20BIT;
326 return MM_PHYS_16BIT;