3 * - Physical memory manager
11 #define TRACE_ALLOCS 0 // Print trace messages on AllocPhys/DerefPhys
15 extern void gKernelEnd;
16 extern void Proc_PrintBacktrace(void);
19 void MM_Install(tMBoot_Info *MBoot);
20 //tPAddr MM_AllocPhys(void);
21 //tPAddr MM_AllocPhysRange(int Pages, int MaxBits);
22 //void MM_RefPhys(tPAddr PAddr);
23 //void MM_DerefPhys(tPAddr PAddr);
24 // int MM_GetRefCount(tPAddr PAddr);
28 Uint64 giPhysAlloc = 0; // Number of allocated pages
29 Uint64 giPageCount = 0; // Total number of pages
30 Uint64 giLastPossibleFree = 0; // Last possible free page (before all pages are used)
32 Uint32 gaSuperBitmap[1024]; // Blocks of 1024 Pages
33 Uint32 gaPageBitmap[1024*1024/32]; // Individual pages
34 int *gaPageReferences;
35 void **gaPageNodes = (void*)MM_PAGENODE_BASE;
36 #define REFENT_PER_PAGE (0x1000/sizeof(gaPageReferences[0]))
39 void MM_Install(tMBoot_Info *MBoot)
41 Uint kernelPages, num;
47 // --- Find largest address
48 MBoot->MMapAddr |= KERNEL_BASE;
49 ent = (void *)( MBoot->MMapAddr );
50 while( (Uint)ent < MBoot->MMapAddr + MBoot->MMapLength )
55 // If entry is RAM and is above `maxAddr`, change `maxAddr`
56 if(ent->Type == 1 && ent->Base + ent->Length > maxAddr)
57 maxAddr = ent->Base + ent->Length;
59 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size );
63 giPageCount = (MBoot->HighMem >> 2) + 256; // HighMem is a kByte value
66 giPageCount = maxAddr >> 12;
68 giLastPossibleFree = giPageCount - 1;
70 memsetd(gaPageBitmap, 0xFFFFFFFF, giPageCount/32);
72 // Set up allocateable space
73 ent = (void *)( MBoot->MMapAddr );
74 while( (Uint)ent < MBoot->MMapAddr + MBoot->MMapLength )
76 memsetd( &gaPageBitmap[ent->Base/(4096*32)], 0, ent->Length/(4096*32) );
77 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size );
80 // Get used page count (Kernel)
81 kernelPages = (Uint)&gKernelEnd - KERNEL_BASE - 0x100000;
82 kernelPages += 0xFFF; // Page Align
87 memsetd( &gaPageBitmap[0x100000/(4096*32)], -1, num );
88 gaPageBitmap[ 0x100000/(4096*32) + num ] = (1 << (kernelPages & 31)) - 1;
90 // Fill Superpage bitmap
91 num = kernelPages/(32*32);
92 memsetd( &gaSuperBitmap[0x100000/(4096*32*32)], -1, num );
93 gaSuperBitmap[ 0x100000/(4096*32*32) + num ] = (1 << ((kernelPages / 32) & 31)) - 1;
95 // Mark Multiboot's pages as taken
97 MM_RefPhys( (Uint)MBoot - KERNEL_BASE );
99 for(i = (MBoot->ModuleCount*sizeof(tMBoot_Module)+0xFFF)>12; i--; )
100 MM_RefPhys( MBoot->Modules + (i << 12) );
102 mods = (void*)(MBoot->Modules + KERNEL_BASE);
103 for(i = 0; i < MBoot->ModuleCount; i++)
105 num = (mods[i].End - mods[i].Start + 0xFFF) >> 12;
107 MM_RefPhys( (mods[i].Start & ~0xFFF) + (num<<12) );
110 gaPageReferences = (void*)MM_REFCOUNT_BASE;
112 Log_Log("PMem", "Physical memory set up");
116 * \fn tPAddr MM_AllocPhys(void)
117 * \brief Allocates a physical page from the general pool
119 tPAddr MM_AllocPhys(void)
127 Mutex_Acquire( &glPhysAlloc );
132 const int addrClasses[] = {0,16,20,24,32,64};
133 const int numAddrClasses = sizeof(addrClasses)/sizeof(addrClasses[0]);
136 for( i = numAddrClasses; i -- > 1; )
138 first = 1 << (addrClasses[i-1] - 12);
139 last = (1 << (addrClasses[i] - 12)) - 1;
140 // Range is above the last free page
141 if( first > giLastPossibleFree )
143 // Last possible free page is in the range
144 if( last > giLastPossibleFree )
145 last = giLastPossibleFree;
148 for( indx = first; indx < last; )
150 if( gaSuperBitmap[indx>>10] == -1 ) {
155 if( gaPageBitmap[indx>>5] == -1 ) {
160 if( gaPageBitmap[indx>>5] & (1 << (indx&31)) ) {
166 if( indx < last ) break;
168 giLastPossibleFree = first; // Well, we couldn't find any in this range
171 if( i <= 1 ) indx = -1;
176 LOG("giLastPossibleFree = %i", giLastPossibleFree);
177 for( indx = giLastPossibleFree; indx >= 0; )
179 if( gaSuperBitmap[indx>>10] == -1 ) {
184 if( gaPageBitmap[indx>>5] == -1 ) {
189 if( gaPageBitmap[indx>>5] & (1 << (indx&31)) ) {
196 giLastPossibleFree = indx;
197 LOG("indx = %i", indx);
199 c = giLastPossibleFree % 32;
200 b = (giLastPossibleFree / 32) % 32;
201 a = giLastPossibleFree / 1024;
203 LOG("a=%i,b=%i,c=%i", a, b, c);
204 for( ; gaSuperBitmap[a] == -1 && a >= 0; a-- );
206 Mutex_Release( &glPhysAlloc );
207 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p) - %lli/%lli used",
208 __builtin_return_address(0), giPhysAlloc, giPageCount);
212 for( ; gaSuperBitmap[a] & (1<<b); b-- );
213 for( ; gaPageBitmap[a*32+b] & (1<<c); c-- );
214 LOG("a=%i,b=%i,c=%i", a, b, c);
215 indx = (a << 10) | (b << 5) | c;
217 giLastPossibleFree = indx;
221 Mutex_Release( &glPhysAlloc );
222 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p) - %lli/%lli used (indx = %x)",
223 __builtin_return_address(0), giPhysAlloc, giPageCount, indx);
224 Log_Debug("PMem", "giLastPossibleFree = %lli", giLastPossibleFree);
229 if( indx > 0xFFFFF ) {
230 Panic("The fuck? Too many pages! (indx = 0x%x)", indx);
233 if( indx >= giPageCount ) {
234 Mutex_Release( &glPhysAlloc );
235 Log_Error("PMem", "MM_AllocPhys - indx(%i) > giPageCount(%i)", indx, giPageCount);
241 if( MM_GetPhysAddr( (tVAddr)&gaPageReferences[indx] ) )
242 gaPageReferences[indx] = 1;
243 gaPageBitmap[ indx>>5 ] |= 1 << (indx&31);
251 if(gaPageBitmap[ indx>>5 ] == -1) {
252 gaSuperBitmap[indx>>10] |= 1 << ((indx>>5)&31);
256 Mutex_Release( &glPhysAlloc );
261 Log_Debug("PMem", "MM_AllocPhys: RETURN %P (%i free)", ret, giPageCount-giPhysAlloc);
262 Proc_PrintBacktrace();
269 * \fn tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
270 * \brief Allocate a range of physical pages
271 * \param Pages Number of pages to allocate
272 * \param MaxBits Maximum number of address bits to use
274 tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
280 ENTER("iPages iMaxBits", Pages, MaxBits);
287 if(MaxBits > PHYS_BITS) MaxBits = PHYS_BITS;
290 Mutex_Acquire( &glPhysAlloc );
292 // Set up search state
293 if( giLastPossibleFree > ((tPAddr)1 << (MaxBits-12)) ) {
294 sidx = (tPAddr)1 << (MaxBits-12);
297 sidx = giLastPossibleFree;
305 LOG("a=%i, b=%i, idx=%i, sidx=%i", a, b, idx, sidx);
308 for( ; gaSuperBitmap[a] == -1 && a --; ) b = 31;
310 Mutex_Release( &glPhysAlloc );
311 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
316 for( ; gaSuperBitmap[a] & (1 << b); b-- ) sidx = 31;
319 for( ; gaPageBitmap[idx] & (1 << sidx); sidx-- )
320 LOG("gaPageBitmap[%i] = 0x%08x", idx, gaPageBitmap[idx]);
322 LOG("idx = %i, sidx = %i", idx, sidx);
327 // Check if the gap is large enough
334 if( gaPageBitmap[idx] == -1 ) {
340 if( gaPageBitmap[idx] & (1 << sidx) ) {
342 if(sidx < 0) { sidx = 31; idx --; }
350 // Check if it is a free range
351 for( i = 0; i < Pages; i++ )
354 if( gaPageBitmap[idx] & (1 << sidx) )
358 if(sidx < 0) { sidx = 31; idx --; }
366 // Check if an address was found
368 Mutex_Release( &glPhysAlloc );
369 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
375 for( i = 0; i < Pages; i++ )
377 if( MM_GetPhysAddr( (tVAddr)&gaPageReferences[idx*32+sidx] ) )
378 gaPageReferences[idx*32+sidx] = 1;
379 gaPageBitmap[ idx ] |= 1 << sidx;
382 if(sidx == 32) { sidx = 0; idx ++; }
386 ret = (idx << 17) | (sidx << 12);
389 if(gaPageBitmap[ idx ] == -1) gaSuperBitmap[idx/32] |= 1 << (idx%32);
392 Mutex_Release( &glPhysAlloc );
396 Log_Debug("PMem", "MM_AllocPhysRange: RETURN 0x%llx-0x%llx (%i free)",
397 ret, ret + (1<<Pages)-1, giPageCount-giPhysAlloc);
403 * \fn void MM_RefPhys(tPAddr PAddr)
405 void MM_RefPhys(tPAddr PAddr)
410 // We don't care about non-ram pages
411 if(PAddr >= giPageCount) return;
414 Mutex_Acquire( &glPhysAlloc );
416 // Reference the page
417 if( gaPageReferences )
419 if( MM_GetPhysAddr( (tVAddr)&gaPageReferences[PAddr] ) == 0 )
422 tVAddr addr = ((tVAddr)&gaPageReferences[PAddr]) & ~0xFFF;
423 // Log_Debug("PMem", "MM_RefPhys: Allocating info for %X", PAddr);
424 Mutex_Release( &glPhysAlloc );
425 if( MM_Allocate( addr ) == 0 ) {
426 Log_KernelPanic("PMem", "MM_RefPhys: Out of physical memory allocating info for %X", PAddr*PAGE_SIZE);
428 Mutex_Acquire( &glPhysAlloc );
430 base = PAddr & ~(1024-1);
431 for( i = 0; i < 1024; i ++ ) {
432 gaPageReferences[base + i] = (gaPageBitmap[(base+i)/32] & (1 << (base+i)%32)) ? 1 : 0;
435 gaPageReferences[ PAddr ] ++;
439 gaPageBitmap[ PAddr / 32 ] |= 1 << (PAddr&31);
442 if(gaPageBitmap[ PAddr / 32 ] == -1)
443 gaSuperBitmap[PAddr/1024] |= 1 << ((PAddr/32)&31);
446 Mutex_Release( &glPhysAlloc );
450 * \fn void MM_DerefPhys(tPAddr PAddr)
451 * \brief Dereferences a physical page
453 void MM_DerefPhys(tPAddr PAddr)
458 // We don't care about non-ram pages
459 if(PAddr >= giPageCount) return;
461 // Check if it is freed
462 if( !(gaPageBitmap[PAddr / 32] & (1 << PAddr%32)) ) {
463 Log_Warning("MMVirt", "MM_DerefPhys - Non-referenced memory dereferenced");
468 Mutex_Acquire( &glPhysAlloc );
470 if( giLastPossibleFree < PAddr )
471 giLastPossibleFree = PAddr;
474 if( !MM_GetPhysAddr( (tVAddr)&gaPageReferences[PAddr] ) || (-- gaPageReferences[PAddr]) == 0 )
477 Log_Debug("PMem", "MM_DerefPhys: Free'd %P (%i free)", PAddr<<12, giPageCount-giPhysAlloc);
478 Proc_PrintBacktrace();
480 //LOG("Freed 0x%x by %p\n", PAddr<<12, __builtin_return_address(0));
482 gaPageBitmap[ PAddr / 32 ] &= ~(1 << (PAddr&31));
483 if(gaPageBitmap[ PAddr / 32 ] == 0)
484 gaSuperBitmap[ PAddr >> 10 ] &= ~(1 << ((PAddr >> 5)&31));
486 if( MM_GetPhysAddr( (tVAddr) &gaPageNodes[PAddr] ) )
488 gaPageNodes[PAddr] = NULL;
489 // TODO: Free Node Page when fully unused
494 Mutex_Release( &glPhysAlloc );
498 * \fn int MM_GetRefCount(tPAddr Addr)
500 int MM_GetRefCount(tPAddr PAddr)
505 // We don't care about non-ram pages
506 if(PAddr >= giPageCount) return -1;
508 if( MM_GetPhysAddr( (tVAddr)&gaPageReferences[PAddr] ) == 0 )
509 return (gaPageBitmap[PAddr / 32] & (1 << PAddr%32)) ? 1 : 0;
511 // Check if it is freed
512 return gaPageReferences[ PAddr ];
515 int MM_SetPageNode(tPAddr PAddr, void *Node)
519 if( MM_GetRefCount(PAddr) == 0 ) return 1;
523 block_addr = (tVAddr) &gaPageNodes[PAddr];
524 block_addr &= ~(PAGE_SIZE-1);
526 if( !MM_GetPhysAddr( block_addr ) )
528 if( !MM_Allocate( block_addr ) ) {
529 Log_Warning("PMem", "Unable to allocate Node page");
532 memset( (void*)block_addr, 0, PAGE_SIZE );
535 gaPageNodes[PAddr] = Node;
536 // Log("gaPageNodes[0x%x] = %p", PAddr, Node);
540 int MM_GetPageNode(tPAddr PAddr, void **Node)
542 if( MM_GetRefCount(PAddr) == 0 ) return 1;
545 if( !MM_GetPhysAddr( (tVAddr) &gaPageNodes[PAddr] ) ) {
549 *Node = gaPageNodes[PAddr];