3 * - Physical memory manager
11 #define TRACE_ALLOCS 0 // Print trace messages on AllocPhys/DerefPhys
15 extern void gKernelEnd;
18 void MM_Install(tMBoot_Info *MBoot);
19 //tPAddr MM_AllocPhys(void);
20 //tPAddr MM_AllocPhysRange(int Pages, int MaxBits);
21 //void MM_RefPhys(tPAddr PAddr);
22 //void MM_DerefPhys(tPAddr PAddr);
23 // int MM_GetRefCount(tPAddr PAddr);
27 Uint64 giPhysAlloc = 0; // Number of allocated pages
28 Uint64 giPageCount = 0; // Total number of pages
29 Uint64 giLastPossibleFree = 0; // Last possible free page (before all pages are used)
31 Uint32 gaSuperBitmap[1024]; // Blocks of 1024 Pages
32 Uint32 gaPageBitmap[1024*1024/32]; // Individual pages
38 #define INFO_PER_PAGE (0x1000/sizeof(gaPageInfo[0]))
41 void MM_Install(tMBoot_Info *MBoot)
43 Uint kernelPages, num;
49 // --- Find largest address
50 MBoot->MMapAddr |= KERNEL_BASE;
51 ent = (void *)( MBoot->MMapAddr );
52 while( (Uint)ent < MBoot->MMapAddr + MBoot->MMapLength )
57 // If entry is RAM and is above `maxAddr`, change `maxAddr`
58 if(ent->Type == 1 && ent->Base + ent->Length > maxAddr)
59 maxAddr = ent->Base + ent->Length;
61 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size );
65 giPageCount = (MBoot->HighMem >> 2) + 256; // HighMem is a kByte value
68 giPageCount = maxAddr >> 12;
70 giLastPossibleFree = giPageCount - 1;
72 memsetd(gaPageBitmap, 0xFFFFFFFF, giPageCount/32);
74 // Set up allocateable space
75 ent = (void *)( MBoot->MMapAddr );
76 while( (Uint)ent < MBoot->MMapAddr + MBoot->MMapLength )
78 memsetd( &gaPageBitmap[ent->Base/(4096*32)], 0, ent->Length/(4096*32) );
79 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size );
82 // Get used page count (Kernel)
83 kernelPages = (Uint)&gKernelEnd - KERNEL_BASE - 0x100000;
84 kernelPages += 0xFFF; // Page Align
89 memsetd( &gaPageBitmap[0x100000/(4096*32)], -1, num );
90 gaPageBitmap[ 0x100000/(4096*32) + num ] = (1 << (kernelPages & 31)) - 1;
92 // Fill Superpage bitmap
93 num = kernelPages/(32*32);
94 memsetd( &gaSuperBitmap[0x100000/(4096*32*32)], -1, num );
95 gaSuperBitmap[ 0x100000/(4096*32*32) + num ] = (1 << ((kernelPages / 32) & 31)) - 1;
97 // Mark Multiboot's pages as taken
99 MM_RefPhys( (Uint)MBoot - KERNEL_BASE );
101 for(i = (MBoot->ModuleCount*sizeof(tMBoot_Module)+0xFFF)>12; i--; )
102 MM_RefPhys( MBoot->Modules + (i << 12) );
104 mods = (void*)(MBoot->Modules + KERNEL_BASE);
105 for(i = 0; i < MBoot->ModuleCount; i++)
107 num = (mods[i].End - mods[i].Start + 0xFFF) >> 12;
109 MM_RefPhys( (mods[i].Start & ~0xFFF) + (num<<12) );
112 gaPageInfo = (void*)MM_PAGEINFO_BASE;
114 Log_Log("PMem", "Physical memory set up");
118 * \fn tPAddr MM_AllocPhys(void)
119 * \brief Allocates a physical page from the general pool
121 tPAddr MM_AllocPhys(void)
129 Mutex_Acquire( &glPhysAlloc );
134 const int addrClasses[] = {0,16,20,24,32,64};
135 const int numAddrClasses = sizeof(addrClasses)/sizeof(addrClasses[0]);
138 for( i = numAddrClasses; i -- > 1; )
140 first = 1 << (addrClasses[i-1] - 12);
141 last = (1 << (addrClasses[i] - 12)) - 1;
142 // Range is above the last free page
143 if( first > giLastPossibleFree )
145 // Last possible free page is in the range
146 if( last > giLastPossibleFree )
147 last = giLastPossibleFree;
150 for( indx = first; indx < last; )
152 if( gaSuperBitmap[indx>>10] == -1 ) {
157 if( gaPageBitmap[indx>>5] == -1 ) {
162 if( gaPageBitmap[indx>>5] & (1 << (indx&31)) ) {
168 if( indx < last ) break;
170 giLastPossibleFree = first; // Well, we couldn't find any in this range
173 if( i <= 1 ) indx = -1;
178 LOG("giLastPossibleFree = %i", giLastPossibleFree);
179 for( indx = giLastPossibleFree; indx >= 0; )
181 if( gaSuperBitmap[indx>>10] == -1 ) {
186 if( gaPageBitmap[indx>>5] == -1 ) {
191 if( gaPageBitmap[indx>>5] & (1 << (indx&31)) ) {
198 giLastPossibleFree = indx;
199 LOG("indx = %i", indx);
201 c = giLastPossibleFree % 32;
202 b = (giLastPossibleFree / 32) % 32;
203 a = giLastPossibleFree / 1024;
205 LOG("a=%i,b=%i,c=%i", a, b, c);
206 for( ; gaSuperBitmap[a] == -1 && a >= 0; a-- );
208 Mutex_Release( &glPhysAlloc );
209 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p) - %lli/%lli used",
210 __builtin_return_address(0), giPhysAlloc, giPageCount);
214 for( ; gaSuperBitmap[a] & (1<<b); b-- );
215 for( ; gaPageBitmap[a*32+b] & (1<<c); c-- );
216 LOG("a=%i,b=%i,c=%i", a, b, c);
217 indx = (a << 10) | (b << 5) | c;
219 giLastPossibleFree = indx;
223 Mutex_Release( &glPhysAlloc );
224 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p) - %lli/%lli used (indx = %x)",
225 __builtin_return_address(0), giPhysAlloc, giPageCount, indx);
226 Log_Debug("PMem", "giLastPossibleFree = %lli", giLastPossibleFree);
231 if( indx > 0xFFFFF ) {
232 Panic("The fuck? Too many pages! (indx = 0x%x)", indx);
235 if( indx >= giPageCount ) {
236 Mutex_Release( &glPhysAlloc );
237 Log_Error("PMem", "MM_AllocPhys - indx(%i) > giPageCount(%i)", indx, giPageCount);
243 if( MM_GetPhysAddr( (tVAddr)&gaPageInfo[indx] ) )
244 gaPageInfo[ indx ].ReferenceCount = 1;
245 gaPageBitmap[ indx>>5 ] |= 1 << (indx&31);
253 if(gaPageBitmap[ indx>>5 ] == -1) {
254 gaSuperBitmap[indx>>10] |= 1 << ((indx>>5)&31);
258 Mutex_Release( &glPhysAlloc );
262 Log_Debug("PMem", "MM_AllocPhys: RETURN 0x%llx (%i free)", ret, giPageCount-giPhysAlloc);
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)
279 ENTER("iPages iMaxBits", Pages, MaxBits);
286 if(MaxBits > PHYS_BITS) MaxBits = PHYS_BITS;
289 Mutex_Acquire( &glPhysAlloc );
291 // Set up search state
292 if( giLastPossibleFree > ((tPAddr)1 << (MaxBits-12)) ) {
293 sidx = (tPAddr)1 << (MaxBits-12);
296 sidx = giLastPossibleFree;
304 LOG("a=%i, b=%i, idx=%i, sidx=%i", a, b, idx, sidx);
307 for( ; gaSuperBitmap[a] == -1 && a --; ) b = 31;
309 Mutex_Release( &glPhysAlloc );
310 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
315 for( ; gaSuperBitmap[a] & (1 << b); b-- ) sidx = 31;
318 for( ; gaPageBitmap[idx] & (1 << sidx); sidx-- )
319 LOG("gaPageBitmap[%i] = 0x%08x", idx, gaPageBitmap[idx]);
321 LOG("idx = %i, sidx = %i", idx, sidx);
326 // Check if the gap is large enough
333 if( gaPageBitmap[idx] == -1 ) {
339 if( gaPageBitmap[idx] & (1 << sidx) ) {
341 if(sidx < 0) { sidx = 31; idx --; }
349 // Check if it is a free range
350 for( i = 0; i < Pages; i++ )
353 if( gaPageBitmap[idx] & (1 << sidx) )
357 if(sidx < 0) { sidx = 31; idx --; }
365 // Check if an address was found
367 Mutex_Release( &glPhysAlloc );
368 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
374 for( i = 0; i < Pages; i++ )
376 if( MM_GetPhysAddr( (tVAddr)&gaPageInfo[idx*32+sidx] ) )
377 gaPageInfo[idx*32+sidx].ReferenceCount = 1;
378 gaPageBitmap[ idx ] |= 1 << sidx;
381 if(sidx == 32) { sidx = 0; idx ++; }
385 ret = (idx << 17) | (sidx << 12);
388 if(gaPageBitmap[ idx ] == -1) gaSuperBitmap[idx/32] |= 1 << (idx%32);
391 Mutex_Release( &glPhysAlloc );
395 Log_Debug("PMem", "MM_AllocPhysRange: RETURN 0x%llx-0x%llx (%i free)",
396 ret, ret + (1<<Pages)-1, giPageCount-giPhysAlloc);
402 * \fn void MM_RefPhys(tPAddr PAddr)
404 void MM_RefPhys(tPAddr PAddr)
409 // We don't care about non-ram pages
410 if(PAddr >= giPageCount) return;
413 Mutex_Acquire( &glPhysAlloc );
415 // Reference the page
418 if( MM_GetPhysAddr( (tVAddr)&gaPageInfo[PAddr] ) == 0 ) {
419 tVAddr addr = ((tVAddr)&gaPageInfo[PAddr]) & ~0xFFF;
420 Log_Debug("PMem", "MM_RefPhys: Info not allocated %llx", PAddr);
421 Mutex_Release( &glPhysAlloc );
422 if( MM_Allocate( addr ) == 0 ) {
423 Log_KernelPanic("PMem", "MM_RefPhys: Out of physical memory");
425 Mutex_Acquire( &glPhysAlloc );
426 memset( (void*)addr, 0, 0x1000 );
428 gaPageInfo[ PAddr ].ReferenceCount ++;
432 gaPageBitmap[ PAddr / 32 ] |= 1 << (PAddr&31);
435 if(gaPageBitmap[ PAddr / 32 ] == -1)
436 gaSuperBitmap[PAddr/1024] |= 1 << ((PAddr/32)&31);
439 Mutex_Release( &glPhysAlloc );
443 * \fn void MM_DerefPhys(tPAddr PAddr)
444 * \brief Dereferences a physical page
446 void MM_DerefPhys(tPAddr PAddr)
451 // We don't care about non-ram pages
452 if(PAddr >= giPageCount) return;
454 // Check if it is freed
455 if( !(gaPageBitmap[PAddr / 32] & (1 << PAddr%32)) ) {
456 Log_Warning("MMVirt", "MM_DerefPhys - Non-referenced memory dereferenced");
461 Mutex_Acquire( &glPhysAlloc );
463 if( giLastPossibleFree < PAddr )
464 giLastPossibleFree = PAddr;
467 if( !MM_GetPhysAddr( (tVAddr)&gaPageInfo[PAddr] ) || (-- gaPageInfo[PAddr].ReferenceCount) == 0 )
470 Log_Debug("PMem", "MM_DerefPhys: Free'd 0x%x (%i free)", PAddr, giPageCount-giPhysAlloc);
472 //LOG("Freed 0x%x by %p\n", PAddr<<12, __builtin_return_address(0));
474 gaPageBitmap[ PAddr / 32 ] &= ~(1 << (PAddr&31));
475 if(gaPageBitmap[ PAddr / 32 ] == 0)
476 gaSuperBitmap[ PAddr >> 10 ] &= ~(1 << ((PAddr >> 5)&31));
480 Mutex_Release( &glPhysAlloc );
484 * \fn int MM_GetRefCount(tPAddr Addr)
486 int MM_GetRefCount(tPAddr PAddr)
491 // We don't care about non-ram pages
492 if(PAddr >= giPageCount) return -1;
494 if( MM_GetPhysAddr( (tVAddr)&gaPageInfo[PAddr] ) == 0 )
495 return (gaPageBitmap[PAddr / 32] & (1 << PAddr%32)) ? 1 : 0;
497 // Check if it is freed
498 return gaPageInfo[ PAddr ].ReferenceCount;
502 * \brief Sets the node and offset associated with a page
504 int MM_SetPageInfo(tPAddr PAddr, void *Node, Uint64 Offset)
508 // Page doesn't exist
509 if( !(gaPageBitmap[PAddr / 32] & (1 << PAddr%32)) )
511 // Allocate info block
512 if( MM_GetPhysAddr( (tVAddr)&gaPageInfo[PAddr] ) == 0 )
514 tVAddr addr = ((tVAddr)&gaPageInfo[PAddr]) & ~0xFFF;
515 Log_Debug("PMem", "MM_SetPageInfo: Info not allocated %llx", PAddr);
516 if( MM_Allocate( addr ) == 0 ) {
517 Log_KernelPanic("PMem", "MM_SetPageInfo: Out of physical memory");
519 memset( (void*)addr, 0, 0x1000);
522 gaPageInfo[ PAddr ].Node = Node;
523 gaPageInfo[ PAddr ].Offset = Offset;
529 * \brief Gets the Node/Offset of a page
531 int MM_GetPageInfo(tPAddr PAddr, void **Node, Uint64 *Offset)
535 // Page doesn't exist
536 if( !(gaPageBitmap[PAddr / 32] & (1 << PAddr%32)) )
538 // Info is zero if block is not allocated
539 if( MM_GetPhysAddr( (tVAddr)&gaPageInfo[PAddr] ) == 0 )
541 if(Node) *Node = NULL;
542 if(Offset) *Offset = 0;
546 if(Node) *Node = gaPageInfo[ PAddr ].Node;
547 if(Offset) *Offset = gaPageInfo[ PAddr ].Offset;