3 * - Physical memory manager
10 #include <semaphore.h>
13 #define TRACE_ALLOCS 0 // Print trace messages on AllocPhys/DerefPhys
15 static const int addrClasses[] = {0,16,20,24,32,64};
16 static const int numAddrClasses = sizeof(addrClasses)/sizeof(addrClasses[0]);
19 extern void Proc_PrintBacktrace(void);
22 void MM_Install(int NPMemRanges, tPMemMapEnt *PMemRanges);
23 //tPAddr MM_AllocPhys(void);
24 //tPAddr MM_AllocPhysRange(int Pages, int MaxBits);
25 //void MM_RefPhys(tPAddr PAddr);
26 //void MM_DerefPhys(tPAddr PAddr);
27 // int MM_GetRefCount(tPAddr PAddr);
31 Uint64 giPhysAlloc = 0; // Number of allocated pages
32 Uint64 giPageCount = 0; // Total number of pages
33 Uint64 giLastPossibleFree = 0; // Last possible free page (before all pages are used)
34 Uint64 giTotalMemorySize = 0; // Total number of allocatable pages
36 Uint32 gaSuperBitmap[1024]; // Blocks of 1024 Pages
37 Uint32 gaPageBitmap[1024*1024/32]; // Individual pages
38 int *gaPageReferences;
39 void **gaPageNodes = (void*)MM_PAGENODE_BASE;
40 #define REFENT_PER_PAGE (0x1000/sizeof(gaPageReferences[0]))
43 void MM_Install(int NPMemRanges, tPMemMapEnt *PMemRanges)
47 // --- Find largest address
48 for( Uint i = 0; i < NPMemRanges; i ++ )
50 tPMemMapEnt *ent = &PMemRanges[i];
51 // If entry is RAM and is above `maxAddr`, change `maxAddr`
52 if(ent->Type == PMEMTYPE_FREE || ent->Type == PMEMTYPE_USED)
54 if(ent->Start + ent->Length > maxAddr)
55 maxAddr = ent->Start + ent->Length;
56 giTotalMemorySize += ent->Length >> 12;
59 LOG("giTotalMemorySize = %lli KiB", giTotalMemorySize*4);
60 LOG("maxAddr = 0x%X", maxAddr);
63 if( maxAddr > (1ULL << 32) ) {
64 maxAddr = (1ULL << 32);
67 giPageCount = maxAddr >> 12;
68 giLastPossibleFree = giPageCount - 1;
69 memsetd(gaPageBitmap, 0xFFFFFFFF, giPageCount/32);
71 // Set up allocateable space
72 for( Uint i = 0; i < NPMemRanges; i ++ )
74 tPMemMapEnt *ent = &PMemRanges[i];
75 if( ent->Type == PMEMTYPE_FREE )
77 Uint64 startpg = ent->Start / PAGE_SIZE;
78 Uint64 pgcount = ent->Length / PAGE_SIZE;
79 // Ignore start addresses >32 bits
80 if( startpg > (1 << 20) )
82 // Clip lengths to 32-bit address space
83 if( startpg + pgcount > (1<<20) )
84 pgcount = (1<<20) - startpg;
86 while( startpg % 32 && pgcount ) {
87 gaPageBitmap[startpg/32] &= ~(1U << (startpg%32));
91 memsetd( &gaPageBitmap[startpg/32], 0, pgcount/32 );
92 startpg += pgcount - pgcount%32;
93 pgcount -= pgcount - pgcount%32;
95 gaPageBitmap[startpg/32] &= ~(1U << (startpg%32));
100 else if( ent->Type == PMEMTYPE_USED )
103 giPhysAlloc += ent->Length / PAGE_SIZE;
107 // Fill Superpage bitmap
108 // - A set bit means that there are no free pages in this block of 32
109 for( Uint i = 0; i < (giPageCount+31)/32; i ++ )
111 if( gaPageBitmap[i] + 1 == 0 ) {
112 gaSuperBitmap[i/32] |= (1 << i%32);
116 gaPageReferences = (void*)MM_REFCOUNT_BASE;
118 Log_Debug("PMem", "maxAddr = %P", maxAddr);
119 Log_Log("PMem", "Physical memory set up (%lli pages of ~%lli MiB used)",
120 giPhysAlloc, (giTotalMemorySize*PAGE_SIZE)/(1024*1024)
124 void MM_DumpStatistics(void)
127 for( i = 1; i < numAddrClasses; i ++ )
129 int first = (i == 1 ? 0 : (1UL << (addrClasses[i-1] - 12)));
130 int last = (1UL << (addrClasses[i] - 12)) - 1;
135 if( last > giPageCount )
138 int total = last - first + 1;
140 for( pg = first; pg < last; pg ++ )
142 if( !MM_GetPhysAddr(&gaPageReferences[pg]) || gaPageReferences[pg] == 0 ) {
146 totalRefs += gaPageReferences[pg];
147 if(gaPageReferences[pg] > 1)
151 int nUsed = (total - nFree);
152 Log_Log("MMPhys", "%ipbit - %i/%i used, %i reused, %i average reference count",
153 addrClasses[i], nUsed, total, nMultiRef,
154 nMultiRef ? (totalRefs-(nUsed - nMultiRef)) / nMultiRef : 0
157 if( last == giPageCount )
160 Log_Log("MMPhys", "%lli/%lli total pages used, 0 - %i possible free range",
161 giPhysAlloc, giTotalMemorySize, giLastPossibleFree);
165 * \fn tPAddr MM_AllocPhys(void)
166 * \brief Allocates a physical page from the general pool
168 tPAddr MM_AllocPhys(void)
175 Mutex_Acquire( &glPhysAlloc );
181 for( i = numAddrClasses; i -- > 1; )
183 first = 1UL << (addrClasses[i-1] - 12);
184 last = (1UL << (addrClasses[i] - 12)) - 1;
185 // Range is above the last free page
186 if( first > giLastPossibleFree )
188 // Last possible free page is in the range
189 if( last > giLastPossibleFree )
190 last = giLastPossibleFree;
193 for( indx = first; indx < last; )
195 if( gaSuperBitmap[indx>>10] == -1 ) {
200 if( gaPageBitmap[indx>>5] == -1 ) {
205 if( gaPageBitmap[indx>>5] & (1 << (indx&31)) ) {
211 if( indx < last ) break;
213 giLastPossibleFree = first; // Well, we couldn't find any in this range
216 if( i <= 1 ) indx = -1;
220 Mutex_Release( &glPhysAlloc );
221 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p) - %lli/%lli used (indx = %x)",
222 __builtin_return_address(0), giPhysAlloc, giPageCount, indx);
223 Log_Debug("PMem", "giLastPossibleFree = %lli", giLastPossibleFree);
228 if( indx > 0xFFFFF ) {
229 Panic("The fuck? Too many pages! (indx = 0x%x)", indx);
232 if( indx >= giPageCount ) {
233 Mutex_Release( &glPhysAlloc );
234 Log_Error("PMem", "MM_AllocPhys - indx(%i) > giPageCount(%i)", indx, giPageCount);
240 if( MM_GetPhysAddr( &gaPageReferences[indx] ) )
241 gaPageReferences[indx] = 1;
242 gaPageBitmap[ indx>>5 ] |= 1 << (indx&31);
250 if(gaPageBitmap[ indx>>5 ] == -1) {
251 gaSuperBitmap[indx>>10] |= 1 << ((indx>>5)&31);
255 Mutex_Release( &glPhysAlloc );
260 Log_Debug("PMem", "MM_AllocPhys: RETURN %P (%i free)", ret, giPageCount-giPhysAlloc);
261 Proc_PrintBacktrace();
268 * \fn tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
269 * \brief Allocate a range of physical pages
270 * \param Pages Number of pages to allocate
271 * \param MaxBits Maximum number of address bits to use
273 tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
278 ENTER("iPages iMaxBits", Pages, MaxBits);
285 if(MaxBits > PHYS_BITS) MaxBits = PHYS_BITS;
288 Mutex_Acquire( &glPhysAlloc );
290 // Set up search state
291 if( giLastPossibleFree > ((tPAddr)1 << (MaxBits-12)) ) {
292 sidx = (tPAddr)1 << (MaxBits-12);
295 sidx = giLastPossibleFree;
300 // Check if the gap is large enough
307 if( gaPageBitmap[idx] == -1 ) {
313 if( gaPageBitmap[idx] & (1 << sidx) ) {
315 if(sidx < 0) { sidx = 31; idx --; }
323 // Check if it is a free range
324 for( i = 0; i < Pages; i++ )
327 if( gaPageBitmap[idx] & (1 << sidx) )
331 if(sidx < 0) { sidx = 31; idx --; }
339 // Check if an address was found
341 Mutex_Release( &glPhysAlloc );
342 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
348 for( i = 0; i < Pages; i++ )
350 if( MM_GetPhysAddr( &gaPageReferences[idx*32+sidx] ) )
351 gaPageReferences[idx*32+sidx] = 1;
352 gaPageBitmap[ idx ] |= 1 << sidx;
355 if(sidx == 32) { sidx = 0; idx ++; }
359 ret = (idx << 17) | (sidx << 12);
362 if(gaPageBitmap[ idx ] == -1) gaSuperBitmap[idx/32] |= 1 << (idx%32);
365 Mutex_Release( &glPhysAlloc );
369 Log_Debug("PMem", "MM_AllocPhysRange: RETURN 0x%llx-0x%llx (%i free)",
370 ret, ret + (1<<Pages)-1, giPageCount-giPhysAlloc);
376 * \fn void MM_RefPhys(tPAddr PAddr)
378 void MM_RefPhys(tPAddr PAddr)
383 // We don't care about non-ram pages
384 if(PAddr >= giPageCount) return;
387 Mutex_Acquire( &glPhysAlloc );
389 // Reference the page
390 if( gaPageReferences )
392 if( MM_GetPhysAddr( &gaPageReferences[PAddr] ) == 0 )
395 tVAddr addr = ((tVAddr)&gaPageReferences[PAddr]) & ~0xFFF;
396 // Log_Debug("PMem", "MM_RefPhys: Allocating info for %X", PAddr);
397 Mutex_Release( &glPhysAlloc );
398 if( MM_Allocate( addr ) == 0 ) {
399 Log_KernelPanic("PMem",
400 "MM_RefPhys: Out of physical memory allocating info for %X",
404 Mutex_Acquire( &glPhysAlloc );
406 base = PAddr & ~(1024-1);
407 for( i = 0; i < 1024; i ++ ) {
408 gaPageReferences[base + i] = (gaPageBitmap[(base+i)/32] & (1 << (base+i)%32)) ? 1 : 0;
411 gaPageReferences[ PAddr ] ++;
414 // If not already used
415 if( !(gaPageBitmap[ PAddr / 32 ] & 1 << (PAddr&31)) ) {
418 gaPageBitmap[ PAddr / 32 ] |= 1 << (PAddr&31);
422 if(gaPageBitmap[ PAddr / 32 ] == -1)
423 gaSuperBitmap[PAddr/1024] |= 1 << ((PAddr/32)&31);
426 Mutex_Release( &glPhysAlloc );
430 * \fn void MM_DerefPhys(tPAddr PAddr)
431 * \brief Dereferences a physical page
433 void MM_DerefPhys(tPAddr PAddr)
438 // We don't care about non-ram pages
439 if(PAddr >= giPageCount) return;
441 // Check if it is freed
442 if( !(gaPageBitmap[PAddr / 32] & (1 << PAddr%32)) ) {
443 Log_Warning("MMVirt", "MM_DerefPhys - Non-referenced memory dereferenced");
448 Mutex_Acquire( &glPhysAlloc );
450 if( giLastPossibleFree < PAddr )
451 giLastPossibleFree = PAddr;
454 if( !MM_GetPhysAddr( &gaPageReferences[PAddr] ) || (-- gaPageReferences[PAddr]) == 0 )
457 Log_Debug("PMem", "MM_DerefPhys: Free'd %P (%i free)", PAddr<<12, giPageCount-giPhysAlloc);
458 Proc_PrintBacktrace();
460 //LOG("Freed 0x%x by %p\n", PAddr<<12, __builtin_return_address(0));
462 gaPageBitmap[ PAddr / 32 ] &= ~(1 << (PAddr&31));
463 if(gaPageBitmap[ PAddr / 32 ] == 0)
464 gaSuperBitmap[ PAddr >> 10 ] &= ~(1 << ((PAddr >> 5)&31));
466 if( MM_GetPhysAddr( &gaPageNodes[PAddr] ) )
468 gaPageNodes[PAddr] = NULL;
469 // TODO: Free Node Page when fully unused
474 Mutex_Release( &glPhysAlloc );
478 * \fn int MM_GetRefCount(tPAddr Addr)
480 int MM_GetRefCount(tPAddr PAddr)
485 // We don't care about non-ram pages
486 if(PAddr >= giPageCount) return -1;
488 if( MM_GetPhysAddr( &gaPageReferences[PAddr] ) == 0 )
489 return (gaPageBitmap[PAddr / 32] & (1 << PAddr%32)) ? 1 : 0;
491 // Check if it is freed
492 return gaPageReferences[ PAddr ];
495 int MM_SetPageNode(tPAddr PAddr, void *Node)
499 if( MM_GetRefCount(PAddr) == 0 ) return 1;
503 block_addr = (tVAddr) &gaPageNodes[PAddr];
504 block_addr &= ~(PAGE_SIZE-1);
506 if( !MM_GetPhysAddr( (void*)block_addr ) )
508 if( !MM_Allocate( block_addr ) ) {
509 Log_Warning("PMem", "Unable to allocate Node page");
512 memset( (void*)block_addr, 0, PAGE_SIZE );
515 gaPageNodes[PAddr] = Node;
516 // Log("gaPageNodes[0x%x] = %p", PAddr, Node);
520 int MM_GetPageNode(tPAddr PAddr, void **Node)
522 if( MM_GetRefCount(PAddr) == 0 ) return 1;
525 if( !MM_GetPhysAddr( &gaPageNodes[PAddr] ) ) {
529 *Node = gaPageNodes[PAddr];