4 * include/tpl_mm_phys.h
5 * Physical Memory Manager Template
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] ++;
82 * \brief Allocate a contiguous range of physical pages with a maximum
83 * bit size of \a MaxBits
84 * \param Pages Number of pages to allocate
85 * \param MaxBits Maximum size of the physical address
86 * \note If \a MaxBits is <= 0, any sized address is used (with preference
87 * to higher addresses)
89 tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
95 ENTER("iPages iBits", Pages, MaxBits);
97 if( MaxBits <= 0 || MaxBits >= 64 ) // Speedup for the common case
98 rangeID = MM_PHYS_MAX;
100 rangeID = MM_int_GetRangeID( (1LL << MaxBits) - 1 );
102 LOG("rangeID = %i", rangeID);
104 Mutex_Acquire(&glPhysicalPages);
106 // Check if the range actually has any free pages
107 while(giPageStackSizes[rangeID] == 0 && rangeID)
110 LOG("rangeID = %i", rangeID);
112 // What the? Oh, man. No free pages
113 if(giPageStackSizes[rangeID] == 0) {
114 Mutex_Release(&glPhysicalPages);
115 // TODO: Page out / attack the cache
117 Warning(" MM_AllocPhysRange: Out of free pages");
119 "Out of memory (unable to fulfil request for %i pages), zero remaining",
126 // Check if there is enough in the range
127 if(giPhysRangeFree[rangeID] >= Pages)
129 LOG("{%i,0x%x -> 0x%x}",
130 giPhysRangeFree[rangeID],
131 giPhysRangeFirst[rangeID], giPhysRangeLast[rangeID]
133 // Do a cheap scan, scanning upwards from the first free page in
136 addr = giPhysRangeFirst[ rangeID ];
137 while( addr <= giPhysRangeLast[ rangeID ] )
139 //Log(" MM_AllocPhysRange: addr = 0x%x", addr);
140 // Check the super bitmap
141 if( gaSuperBitmap[addr >> (6+6)] + 1 == 0 ) {
142 LOG("nFree = %i = 0 (super) (0x%x)", nFree, addr);
144 addr += 1LL << (6+6);
145 addr &= ~0xFFF; // (1LL << 6+6) - 1
148 // Check page block (64 pages)
149 if( gaMainBitmap[addr >> 6] + 1 == 0) {
150 LOG("nFree = %i = 0 (main) (0x%x)", nFree, addr);
156 // Check individual page
157 if( gaMainBitmap[addr >> 6] & (1LL << (addr & 63)) ) {
158 LOG("nFree = %i = 0 (page) (0x%x)", nFree, addr);
165 LOG("nFree(%i) == %i (0x%x)", nFree, Pages, addr);
169 LOG("nFree = %i", nFree);
170 // If we don't find a contiguous block, nFree will not be equal
171 // to Num, so we set it to zero and do the expensive lookup.
172 if(nFree != Pages) nFree = 0;
177 // Oops. ok, let's do an expensive check (scan down the list
178 // until a free range is found)
180 addr = giPhysRangeLast[ rangeID ];
182 Mutex_Release(&glPhysicalPages);
185 Warning(" MM_AllocPhysRange: Out of memory (unable to fulfil request for %i pages)", Pages);
187 "Out of memory (unable to fulfil request for %i pages)",
193 LOG("nFree = %i, addr = 0x%08x", nFree, addr);
195 // Mark pages as allocated
197 for( i = 0; i < Pages; i++, addr++ )
199 gaMainBitmap[addr >> 6] |= 1LL << (addr & 63);
200 rangeID = MM_int_GetRangeID(addr << 12);
201 giPhysRangeFree[ rangeID ] --;
202 LOG("%x == %x", addr, giPhysRangeFirst[ rangeID ]);
203 if(addr == giPhysRangeFirst[ rangeID ])
204 giPhysRangeFirst[ rangeID ] += 1;
206 ret = addr; // Save the return address
208 // Update super bitmap
209 Pages += addr & (64-1);
211 Pages = (Pages + (64-1)) & ~(64-1);
212 for( i = 0; i < Pages/64; i++ )
214 if( gaMainBitmap[ addr >> 6 ] + 1 == 0 )
215 gaSuperBitmap[addr>>12] |= 1LL << ((addr >> 6) & 63);
218 Mutex_Release(&glPhysicalPages);
219 LEAVE('x', ret << 12);
224 * \brief Allocate a single physical page, with no preference as to address
227 tPAddr MM_AllocPhys(void)
231 // Hack to allow allocation during setup
232 for(i = 0; i < NUM_STATIC_ALLOC; i++) {
233 if( gaiStaticAllocPages[i] ) {
234 tPAddr ret = gaiStaticAllocPages[i];
235 gaiStaticAllocPages[i] = 0;
236 Log("MM_AllocPhys: Return %x, static alloc %i", ret, i);
241 return MM_AllocPhysRange(1, -1);
245 * \brief Reference a physical page
247 void MM_RefPhys(tPAddr PAddr)
249 Uint64 page = PAddr >> 12;
251 if( PAddr >> 12 > giMaxPhysPage ) return ;
253 if( gaMainBitmap[ page >> 6 ] & (1LL << (page&63)) )
256 gaMultiBitmap[ page >> 6 ] |= 1LL << (page&63);
257 gaiPageReferences[ page ] ++;
262 gaMainBitmap[page >> 6] |= 1LL << (page&63);
263 if( gaMainBitmap[page >> 6 ] + 1 == 0 )
264 gaSuperBitmap[page>> 12] |= 1LL << ((page >> 6) & 63);
269 * \brief Dereference a physical page
271 void MM_DerefPhys(tPAddr PAddr)
273 Uint64 page = PAddr >> 12;
275 if( PAddr >> 12 > giMaxPhysPage ) return ;
277 if( gaMultiBitmap[ page >> 6 ] & (1LL << (page&63)) ) {
278 gaiPageReferences[ page ] --;
279 if( gaiPageReferences[ page ] == 1 )
280 gaMultiBitmap[ page >> 6 ] &= ~(1LL << (page&63));
281 if( gaiPageReferences[ page ] == 0 )
282 gaMainBitmap[ page >> 6 ] &= ~(1LL << (page&63));
285 gaMainBitmap[ page >> 6 ] &= ~(1LL << (page&63));
287 // Update the free counts if the page was freed
288 if( !(gaMainBitmap[ page >> 6 ] & (1LL << (page&63))) )
291 rangeID = MM_int_GetRangeID( PAddr );
292 giPhysRangeFree[ rangeID ] ++;
293 if( giPhysRangeFirst[rangeID] > page )
294 giPhysRangeFirst[rangeID] = page;
295 if( giPhysRangeLast[rangeID] < page )
296 giPhysRangeLast[rangeID] = page;
299 // If the bitmap entry is not -1, unset the bit in the super bitmap
300 if(gaMainBitmap[ page >> 6 ] + 1 != 0 ) {
301 gaSuperBitmap[page >> 12] &= ~(1LL << ((page >> 6) & 63));
306 * \brief Takes a physical address and returns the ID of its range
307 * \param Addr Physical address of page
308 * \return Range ID from eMMPhys_Ranges
310 int MM_int_GetRangeID( tPAddr Addr )
315 return MM_PHYS_32BIT;
317 return MM_PHYS_24BIT;
319 return MM_PHYS_20BIT;
321 return MM_PHYS_16BIT;