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
33 int *gaPageReferences;
34 void **gaPageNodes = (void*)MM_PAGENODE_BASE;
35 #define REFENT_PER_PAGE (0x1000/sizeof(gaPageReferences[0]))
38 void MM_Install(tMBoot_Info *MBoot)
40 Uint kernelPages, num;
46 // --- Find largest address
47 MBoot->MMapAddr |= KERNEL_BASE;
48 ent = (void *)( MBoot->MMapAddr );
49 while( (Uint)ent < MBoot->MMapAddr + MBoot->MMapLength )
54 // If entry is RAM and is above `maxAddr`, change `maxAddr`
55 if(ent->Type == 1 && ent->Base + ent->Length > maxAddr)
56 maxAddr = ent->Base + ent->Length;
58 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size );
62 giPageCount = (MBoot->HighMem >> 2) + 256; // HighMem is a kByte value
65 giPageCount = maxAddr >> 12;
67 giLastPossibleFree = giPageCount - 1;
69 memsetd(gaPageBitmap, 0xFFFFFFFF, giPageCount/32);
71 // Set up allocateable space
72 ent = (void *)( MBoot->MMapAddr );
73 while( (Uint)ent < MBoot->MMapAddr + MBoot->MMapLength )
75 memsetd( &gaPageBitmap[ent->Base/(4096*32)], 0, ent->Length/(4096*32) );
76 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size );
79 // Get used page count (Kernel)
80 kernelPages = (Uint)&gKernelEnd - KERNEL_BASE - 0x100000;
81 kernelPages += 0xFFF; // Page Align
86 memsetd( &gaPageBitmap[0x100000/(4096*32)], -1, num );
87 gaPageBitmap[ 0x100000/(4096*32) + num ] = (1 << (kernelPages & 31)) - 1;
89 // Fill Superpage bitmap
90 num = kernelPages/(32*32);
91 memsetd( &gaSuperBitmap[0x100000/(4096*32*32)], -1, num );
92 gaSuperBitmap[ 0x100000/(4096*32*32) + num ] = (1 << ((kernelPages / 32) & 31)) - 1;
94 // Mark Multiboot's pages as taken
96 MM_RefPhys( (Uint)MBoot - KERNEL_BASE );
98 for(i = (MBoot->ModuleCount*sizeof(tMBoot_Module)+0xFFF)>12; i--; )
99 MM_RefPhys( MBoot->Modules + (i << 12) );
101 mods = (void*)(MBoot->Modules + KERNEL_BASE);
102 for(i = 0; i < MBoot->ModuleCount; i++)
104 num = (mods[i].End - mods[i].Start + 0xFFF) >> 12;
106 MM_RefPhys( (mods[i].Start & ~0xFFF) + (num<<12) );
109 gaPageReferences = (void*)MM_REFCOUNT_BASE;
111 Log_Log("PMem", "Physical memory set up");
115 * \fn tPAddr MM_AllocPhys(void)
116 * \brief Allocates a physical page from the general pool
118 tPAddr MM_AllocPhys(void)
126 Mutex_Acquire( &glPhysAlloc );
131 const int addrClasses[] = {0,16,20,24,32,64};
132 const int numAddrClasses = sizeof(addrClasses)/sizeof(addrClasses[0]);
135 for( i = numAddrClasses; i -- > 1; )
137 first = 1 << (addrClasses[i-1] - 12);
138 last = (1 << (addrClasses[i] - 12)) - 1;
139 // Range is above the last free page
140 if( first > giLastPossibleFree )
142 // Last possible free page is in the range
143 if( last > giLastPossibleFree )
144 last = giLastPossibleFree;
147 for( indx = first; indx < last; )
149 if( gaSuperBitmap[indx>>10] == -1 ) {
154 if( gaPageBitmap[indx>>5] == -1 ) {
159 if( gaPageBitmap[indx>>5] & (1 << (indx&31)) ) {
165 if( indx < last ) break;
167 giLastPossibleFree = first; // Well, we couldn't find any in this range
170 if( i <= 1 ) indx = -1;
175 LOG("giLastPossibleFree = %i", giLastPossibleFree);
176 for( indx = giLastPossibleFree; indx >= 0; )
178 if( gaSuperBitmap[indx>>10] == -1 ) {
183 if( gaPageBitmap[indx>>5] == -1 ) {
188 if( gaPageBitmap[indx>>5] & (1 << (indx&31)) ) {
195 giLastPossibleFree = indx;
196 LOG("indx = %i", indx);
198 c = giLastPossibleFree % 32;
199 b = (giLastPossibleFree / 32) % 32;
200 a = giLastPossibleFree / 1024;
202 LOG("a=%i,b=%i,c=%i", a, b, c);
203 for( ; gaSuperBitmap[a] == -1 && a >= 0; a-- );
205 Mutex_Release( &glPhysAlloc );
206 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p) - %lli/%lli used",
207 __builtin_return_address(0), giPhysAlloc, giPageCount);
211 for( ; gaSuperBitmap[a] & (1<<b); b-- );
212 for( ; gaPageBitmap[a*32+b] & (1<<c); c-- );
213 LOG("a=%i,b=%i,c=%i", a, b, c);
214 indx = (a << 10) | (b << 5) | c;
216 giLastPossibleFree = indx;
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( (tVAddr)&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 );
259 Log_Debug("PMem", "MM_AllocPhys: RETURN 0x%llx (%i free)", ret, giPageCount-giPhysAlloc);
265 * \fn tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
266 * \brief Allocate a range of physical pages
267 * \param Pages Number of pages to allocate
268 * \param MaxBits Maximum number of address bits to use
270 tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
276 ENTER("iPages iMaxBits", Pages, MaxBits);
283 if(MaxBits > PHYS_BITS) MaxBits = PHYS_BITS;
286 Mutex_Acquire( &glPhysAlloc );
288 // Set up search state
289 if( giLastPossibleFree > ((tPAddr)1 << (MaxBits-12)) ) {
290 sidx = (tPAddr)1 << (MaxBits-12);
293 sidx = giLastPossibleFree;
301 LOG("a=%i, b=%i, idx=%i, sidx=%i", a, b, idx, sidx);
304 for( ; gaSuperBitmap[a] == -1 && a --; ) b = 31;
306 Mutex_Release( &glPhysAlloc );
307 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
312 for( ; gaSuperBitmap[a] & (1 << b); b-- ) sidx = 31;
315 for( ; gaPageBitmap[idx] & (1 << sidx); sidx-- )
316 LOG("gaPageBitmap[%i] = 0x%08x", idx, gaPageBitmap[idx]);
318 LOG("idx = %i, sidx = %i", idx, sidx);
323 // Check if the gap is large enough
330 if( gaPageBitmap[idx] == -1 ) {
336 if( gaPageBitmap[idx] & (1 << sidx) ) {
338 if(sidx < 0) { sidx = 31; idx --; }
346 // Check if it is a free range
347 for( i = 0; i < Pages; i++ )
350 if( gaPageBitmap[idx] & (1 << sidx) )
354 if(sidx < 0) { sidx = 31; idx --; }
362 // Check if an address was found
364 Mutex_Release( &glPhysAlloc );
365 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
371 for( i = 0; i < Pages; i++ )
373 if( MM_GetPhysAddr( (tVAddr)&gaPageReferences[idx*32+sidx] ) )
374 gaPageReferences[idx*32+sidx] = 1;
375 gaPageBitmap[ idx ] |= 1 << sidx;
378 if(sidx == 32) { sidx = 0; idx ++; }
382 ret = (idx << 17) | (sidx << 12);
385 if(gaPageBitmap[ idx ] == -1) gaSuperBitmap[idx/32] |= 1 << (idx%32);
388 Mutex_Release( &glPhysAlloc );
392 Log_Debug("PMem", "MM_AllocPhysRange: RETURN 0x%llx-0x%llx (%i free)",
393 ret, ret + (1<<Pages)-1, giPageCount-giPhysAlloc);
399 * \fn void MM_RefPhys(tPAddr PAddr)
401 void MM_RefPhys(tPAddr PAddr)
406 // We don't care about non-ram pages
407 if(PAddr >= giPageCount) return;
410 Mutex_Acquire( &glPhysAlloc );
412 // Reference the page
413 if( gaPageReferences )
415 if( MM_GetPhysAddr( (tVAddr)&gaPageReferences[PAddr] ) == 0 ) {
416 tVAddr addr = ((tVAddr)&gaPageReferences[PAddr]) & ~0xFFF;
417 Log_Debug("PMem", "MM_RefPhys: Allocating info for %X", PAddr);
418 Mutex_Release( &glPhysAlloc );
419 if( MM_Allocate( addr ) == 0 ) {
420 Log_KernelPanic("PMem", "MM_RefPhys: Out of physical memory");
422 Mutex_Acquire( &glPhysAlloc );
423 memset( (void*)addr, 0, 0x1000 );
425 gaPageReferences[ PAddr ] ++;
429 gaPageBitmap[ PAddr / 32 ] |= 1 << (PAddr&31);
432 if(gaPageBitmap[ PAddr / 32 ] == -1)
433 gaSuperBitmap[PAddr/1024] |= 1 << ((PAddr/32)&31);
436 Mutex_Release( &glPhysAlloc );
440 * \fn void MM_DerefPhys(tPAddr PAddr)
441 * \brief Dereferences a physical page
443 void MM_DerefPhys(tPAddr PAddr)
448 // We don't care about non-ram pages
449 if(PAddr >= giPageCount) return;
451 // Check if it is freed
452 if( !(gaPageBitmap[PAddr / 32] & (1 << PAddr%32)) ) {
453 Log_Warning("MMVirt", "MM_DerefPhys - Non-referenced memory dereferenced");
458 Mutex_Acquire( &glPhysAlloc );
460 if( giLastPossibleFree < PAddr )
461 giLastPossibleFree = PAddr;
464 if( !MM_GetPhysAddr( (tVAddr)&gaPageReferences[PAddr] ) || (-- gaPageReferences[PAddr]) == 0 )
467 Log_Debug("PMem", "MM_DerefPhys: Free'd 0x%x (%i free)", PAddr, giPageCount-giPhysAlloc);
469 //LOG("Freed 0x%x by %p\n", PAddr<<12, __builtin_return_address(0));
471 gaPageBitmap[ PAddr / 32 ] &= ~(1 << (PAddr&31));
472 if(gaPageBitmap[ PAddr / 32 ] == 0)
473 gaSuperBitmap[ PAddr >> 10 ] &= ~(1 << ((PAddr >> 5)&31));
476 if( MM_GetPhysAddr( (tVAddr) &gaPageNodes[PAddr] ) )
478 gaPageNodes[PAddr] = NULL;
479 // TODO: Free Node Page when fully unused
483 Mutex_Release( &glPhysAlloc );
487 * \fn int MM_GetRefCount(tPAddr Addr)
489 int MM_GetRefCount(tPAddr PAddr)
494 // We don't care about non-ram pages
495 if(PAddr >= giPageCount) return -1;
497 if( MM_GetPhysAddr( (tVAddr)&gaPageReferences[PAddr] ) == 0 )
498 return (gaPageBitmap[PAddr / 32] & (1 << PAddr%32)) ? 1 : 0;
500 // Check if it is freed
501 return gaPageReferences[ PAddr ];
504 int MM_SetPageNode(tPAddr PAddr, void *Node)
508 if( MM_GetRefCount(PAddr) == 0 ) return 1;
512 block_addr = (tVAddr) &gaPageNodes[PAddr];
513 block_addr &= ~(PAGE_SIZE-1);
515 if( !MM_GetPhysAddr( block_addr ) )
517 if( !MM_Allocate( block_addr ) ) {
518 Log_Warning("PMem", "Unable to allocate Node page");
521 memset( (void*)block_addr, 0, PAGE_SIZE );
524 gaPageNodes[PAddr] = Node;
528 int MM_GetPageNode(tPAddr PAddr, void **Node)
530 if( MM_GetRefCount(PAddr) == 0 ) {
533 if( !MM_GetPhysAddr( (tVAddr) &gaPageNodes[PAddr] ) ) {
537 *Node = gaPageNodes[PAddr];