4 * include/tpl_mm_phys_bitmap.h
5 * Physical Memory Manager Template
10 * Uses 4.125+PtrSize bytes per page
13 #define MM_PAGE_REFCOUNTS MM_PMM_BASE
14 #define MM_PAGE_NODES (MM_PMM_BASE+(MM_MAXPHYSPAGE*sizeof(Uint32)))
15 #define MM_PAGE_BITMAP (MM_PAGE_NODES+(MM_MAXPHYSPAGE*sizeof(void*)))
18 //void MM_InitPhys_Multiboot(tMBoot_Info *MBoot);
19 //tPAddr MM_AllocPhysRange(int Num, int Bits);
20 //tPAddr MM_AllocPhys(void);
21 //void MM_RefPhys(tPAddr PAddr);
22 //void MM_DerefPhys(tPAddr PAddr);
23 int MM_int_GetRangeID( tPAddr Addr );
24 int MM_int_GetMapEntry( void *Data, int Index, tPAddr *Start, tPAddr *Length );
25 void MM_Tpl_InitPhys(int MaxRAMPage, void *MemoryMap);
28 tMutex glPhysicalPages;
29 void **gapPageNodes = (void*)MM_PAGE_NODES; //!< Associated VFS Node for each page
30 Uint32 *gaiPageReferences = (void*)MM_PAGE_REFCOUNTS; // Reference Counts
31 Uint32 *gaPageBitmaps = (void*)MM_PAGE_BITMAP; // Used bitmap (1 == avail)
32 Uint64 giMaxPhysPage = 0; // Maximum Physical page
40 * \brief Initialise the physical memory manager with a passed memory map
42 void MM_Tpl_InitPhys(int MaxRAMPage, void *MemoryMap)
45 tPAddr rangeStart, rangeLen;
47 if( MM_PAGE_BITMAP + (MM_MAXPHYSPAGE/8) > MM_PMM_END ) {
48 Log_KernelPanic("PMM", "Config Error, PMM cannot fit data in allocated range");
51 giMaxPhysPage = MaxRAMPage;
53 // for( i = 0; i < MM_RANGE_MAX; i ++ )
54 // gaiPhysRangeFirstFree[i] = -1;
57 while( MM_int_GetMapEntry(MemoryMap, mapIndex++, &rangeStart, &rangeLen) )
61 LOG("Range %i, %P to %P", mapIndex-1, rangeStart, rangeLen);
62 rangeStart /= PAGE_SIZE;
63 rangeLen /= PAGE_SIZE;
65 giPhysNumFree += rangeLen;
67 LOG("rangeStart = 0x%x, rangeLen = 0x%x", rangeStart, rangeLen);
69 if( giPhysFirstFree == -1 || giPhysFirstFree > rangeStart )
70 giPhysFirstFree = rangeStart;
72 if( giPhysLastFree < rangeStart + rangeLen )
73 giPhysLastFree = rangeStart + rangeLen;
75 LOG("giPhysFirstFree = 0x%x, giPhysLastFree = 0x%x", giPhysFirstFree, giPhysLastFree);
77 bitmap_page = (tVAddr)&gaPageBitmaps[rangeStart/32];
78 bitmap_page &= ~(PAGE_SIZE-1);
80 // Only need to allocate bitmaps
81 if( !MM_GetPhysAddr( bitmap_page ) ) {
82 if( !MM_Allocate( bitmap_page ) ) {
83 Log_KernelPanic("PMM", "Out of memory during init, this is bad");
86 // memset( (void*)bitmap_page, 0, (rangeStart/8) & ~(PAGE_SIZE-1) );
87 memset( (void*)bitmap_page, 0, PAGE_SIZE );
91 for( ; (rangeStart & 31) && rangeLen > 0; rangeStart++, rangeLen-- ) {
92 gaPageBitmaps[rangeStart / 32] |= 1 << (rangeStart&31);
93 LOG("gaPageBitmaps[%i] = 0x%x", rangeStart/32, gaPageBitmaps[rangeStart/32]);
95 // Mark blocks of 32 as avail
96 for( ; rangeLen > 31; rangeStart += 32, rangeLen -= 32 ) {
97 gaPageBitmaps[rangeStart / 32] = -1;
100 for( ; rangeLen > 0; rangeStart ++, rangeLen -- ) {
101 gaPageBitmaps[rangeStart / 32] |= 1 << (rangeStart&31);
107 LOG("giPhysFirstFree = 0x%x, giPhysLastFree = 0x%x", giPhysFirstFree, giPhysLastFree);
112 * \brief Allocate a contiguous range of physical pages with a maximum
113 * bit size of \a MaxBits
114 * \param Pages Number of pages to allocate
115 * \param MaxBits Maximum size of the physical address
116 * \note If \a MaxBits is <= 0, any sized address is used (with preference
117 * to higher addresses)
119 tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
124 ENTER("iPages iBits", Pages, MaxBits);
126 Mutex_Acquire(&glPhysicalPages);
128 // Check if there is enough in the range
129 if(giPhysNumFree >= Pages)
131 LOG("{0x%x -> 0x%x}", giPhysFirstFree, giPhysLastFree);
132 // Do a cheap scan, scanning upwards from the first free page in
135 addr = giPhysFirstFree;
136 while( addr <= giPhysLastFree )
139 // Check the super bitmap
140 if( gaSuperBitmap[addr / (32*32)] == 0 )
142 LOG("nFree = %i = 0 (super) (0x%x)", nFree, addr);
145 addr &= ~(32*32-1); // (1LL << 6+6) - 1
149 LOG("gaPageBitmaps[%i] = 0x%x", addr/32, gaPageBitmaps[addr/32]);
150 // Check page block (32 pages)
151 if( gaPageBitmaps[addr / 32] == 0) {
152 LOG("nFree = %i = 0 (block) (0x%x)", nFree, addr);
158 // Check individual page
159 if( !(gaPageBitmaps[addr / 32] & (1LL << (addr & 31))) )
161 LOG("nFree = %i = 0 (page) (0x%x)", nFree, addr);
168 LOG("nFree(%i) == %i (1x%x)", nFree, Pages, addr);
172 LOG("nFree = %i", nFree);
173 // If we don't find a contiguous block, nFree will not be equal
174 // to Num, so we set it to zero and do the expensive lookup.
175 if(nFree != Pages) nFree = 0;
181 // Oops. ok, let's do an expensive check (scan down the list
182 // until a free range is found)
184 addr = gaiPhysRangeLastFree[ 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-Pages) << 12);
200 // Mark pages as allocated
202 for( i = 0; i < Pages; i++, addr++ )
205 gaPageBitmaps[addr / 32] &= ~(1 << (addr & 31));
206 // Maintain first possible free
208 if(addr == giPhysFirstFree)
209 giPhysFirstFree += 1;
211 LOG("if( MM_GetPhysAddr( %p ) )", &gaiPageReferences[addr]);
212 // Mark as referenced if the reference count page is valid
213 if( MM_GetPhysAddr( (tVAddr)&gaiPageReferences[addr] ) ) {
214 gaiPageReferences[addr] = 1;
217 ret = addr - Pages; // Save the return address
218 LOG("ret = %x", ret);
221 // Update super bitmap
222 Pages += addr & (32-1);
224 Pages = (Pages + (32-1)) & ~(32-1);
225 for( i = 0; i < Pages/32; i++ )
227 if( gaPageBitmaps[ addr / 32 ] + 1 == 0 )
228 gaSuperBitmap[addr / (32*32)] |= 1LL << ((addr / 32) & 31);
232 Mutex_Release(&glPhysicalPages);
233 LEAVE('x', ret << 12);
238 * \brief Allocate a single physical page, with no preference as to address size.
240 tPAddr MM_AllocPhys(void)
246 // Hack to allow allocation during setup
247 for(i = 0; i < NUM_STATIC_ALLOC; i++) {
248 if( gaiStaticAllocPages[i] ) {
249 tPAddr ret = gaiStaticAllocPages[i];
250 gaiStaticAllocPages[i] = 0;
251 Log("MM_AllocPhys: Return %x, static alloc %i", ret, i);
257 for( ret = 0; ret < giMaxPhysPage; ret ++ )
259 if( !MM_GetPhysAddr( (tVAddr)&gaPageBitmaps[ret/32] ) ) {
263 if( gaPageBitmaps[ret/32] == 0 ) {
267 if( gaPageBitmaps[ret/32] & (1 << (ret&31)) ) {
268 gaPageBitmaps[ret/32] &= ~(1 << (ret&31));
269 return ret * PAGE_SIZE;
272 Log_Error("PMM", "MM_AllocPhys failed duing init");
276 return MM_AllocPhysRange(1, -1);
280 * \brief Reference a physical page
282 void MM_RefPhys(tPAddr PAddr)
284 tPAddr page = PAddr / PAGE_SIZE;
286 if( page >= giMaxPhysPage ) return ;
288 if( gaPageBitmaps[ page / 32 ] & (1LL << (page&31)) )
291 gaPageBitmaps[page / 32] &= ~(1LL << (page&31));
293 if( gaPageBitmaps[page / 32] == 0 )
294 gaSuperBitmap[page / (32*32)] &= ~(1LL << ((page / 32) & 31));
299 tVAddr refpage = (tVAddr)&gaiPageReferences[page] & ~(PAGE_SIZE-1);
301 if( !MM_GetPhysAddr( refpage ) )
303 if( MM_Allocate(refpage) == 0 ) {
304 // Out of memory, can this be resolved?
305 // TODO: Reclaim memory
306 Log_Error("PMM", "Out of memory (MM_RefPhys)");
309 memset((void*)refpage, 0, PAGE_SIZE);
310 gaiPageReferences[page] = 2;
313 gaiPageReferences[ page ] ++;
317 int MM_GetRefCount(tPAddr PAddr)
320 if( MM_GetPhysAddr( (tVAddr)&gaiPageReferences[PAddr] ) ) {
321 return gaiPageReferences[PAddr];
324 if( gaPageBitmaps[ PAddr / 32 ] & (1LL << (PAddr&31)) ) {
332 * \brief Dereference a physical page
334 void MM_DerefPhys(tPAddr PAddr)
336 Uint64 page = PAddr >> 12;
338 if( PAddr >> 12 > giMaxPhysPage ) return ;
340 ENTER("PPAddr", PAddr);
342 if( MM_GetPhysAddr( (tVAddr)&gaiPageReferences[page] ) )
344 if( gaiPageReferences[page] > 0 )
345 gaiPageReferences[ page ] --;
346 if( gaiPageReferences[ page ] == 0 ) {
347 gaPageBitmaps[ page / 32 ] |= 1 << (page&31);
348 // TODO: Catch when all pages in this range have been dereferenced
352 gaPageBitmaps[ page / 32 ] |= 1 << (page&31);
353 // Clear node if needed
354 if( MM_GetPhysAddr( (tVAddr)&gapPageNodes[page] ) ) {
355 gapPageNodes[page] = NULL;
356 // TODO: Catch when all pages in this range are not using nodes
359 // Update the free counts if the page was freed
360 if( gaPageBitmaps[ page / 32 ] & (1LL << (page&31)) )
363 if( giPhysFirstFree == -1 || giPhysFirstFree > page )
364 giPhysFirstFree = page;
365 if( giPhysLastFree < page )
366 giPhysLastFree = page;
370 // If the bitmap entry is not zero, set the bit free in the super bitmap
371 if(gaPageBitmaps[ page / 32 ] != 0 ) {
372 gaSuperBitmap[page / (32*32)] |= 1LL << ((page / 32) & 31);
378 int MM_SetPageNode(tPAddr PAddr, void *Node)
380 tPAddr page = PAddr >> 12;
381 tVAddr node_page = ((tVAddr)&gapPageNodes[page]) & ~(PAGE_SIZE-1);
383 if( !MM_GetRefCount(PAddr) ) return 1;
385 if( !MM_GetPhysAddr(node_page) ) {
386 if( !MM_Allocate(node_page) )
388 memset( (void*)node_page, 0, PAGE_SIZE );
391 gapPageNodes[page] = Node;
395 int MM_GetPageNode(tPAddr PAddr, void **Node)
397 if( !MM_GetRefCount(PAddr) ) return 1;
400 if( !MM_GetPhysAddr( (tVAddr)&gapPageNodes[PAddr] ) ) {
405 *Node = gapPageNodes[PAddr];