3 * - Physical memory manager
11 #define TRACE_ALLOCS 0 // Print trace messages on AllocPhys/DerefPhys
13 #define REFERENCE_BASE 0xE0400000
16 extern void gKernelEnd;
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);
26 Uint64 giPhysAlloc = 0; // Number of allocated pages
27 Uint64 giPageCount = 0; // Total number of pages
28 Uint64 giLastPossibleFree = 0; // Last possible free page (before all pages are used)
30 Uint32 gaSuperBitmap[1024]; // Blocks of 1024 Pages
31 Uint32 gaPageBitmap[1024*1024/32]; // Individual pages
32 Uint32 *gaPageReferences;
35 void MM_Install(tMBoot_Info *MBoot)
37 Uint kernelPages, num;
43 // --- Find largest address
44 MBoot->MMapAddr |= KERNEL_BASE;
45 ent = (void *)( MBoot->MMapAddr );
46 while( (Uint)ent < MBoot->MMapAddr + MBoot->MMapLength )
51 // If entry is RAM and is above `maxAddr`, change `maxAddr`
52 if(ent->Type == 1 && ent->Base + ent->Length > maxAddr)
53 maxAddr = ent->Base + ent->Length;
55 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size );
59 giPageCount = (MBoot->HighMem >> 2) + 256; // HighMem is a kByte value
62 giPageCount = maxAddr >> 12;
64 giLastPossibleFree = giPageCount - 1;
66 memsetd(gaPageBitmap, 0xFFFFFFFF, giPageCount/32);
68 // Set up allocateable space
69 ent = (void *)( MBoot->MMapAddr );
70 while( (Uint)ent < MBoot->MMapAddr + MBoot->MMapLength )
72 memsetd( &gaPageBitmap[ent->Base/(4096*32)], 0, ent->Length/(4096*32) );
73 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size );
76 // Get used page count (Kernel)
77 kernelPages = (Uint)&gKernelEnd - KERNEL_BASE - 0x100000;
78 kernelPages += 0xFFF; // Page Align
83 memsetd( &gaPageBitmap[0x100000/(4096*32)], -1, num );
84 gaPageBitmap[ 0x100000/(4096*32) + num ] = (1 << (kernelPages & 31)) - 1;
86 // Fill Superpage bitmap
87 num = kernelPages/(32*32);
88 memsetd( &gaSuperBitmap[0x100000/(4096*32*32)], -1, num );
89 gaSuperBitmap[ 0x100000/(4096*32*32) + num ] = (1 << ((kernelPages / 32) & 31)) - 1;
91 // Mark Multiboot's pages as taken
93 MM_RefPhys( (Uint)MBoot - KERNEL_BASE );
95 for(i = (MBoot->ModuleCount*sizeof(tMBoot_Module)+0xFFF)>12; i--; )
96 MM_RefPhys( MBoot->Modules + (i << 12) );
98 mods = (void*)(MBoot->Modules + KERNEL_BASE);
99 for(i = 0; i < MBoot->ModuleCount; i++)
101 num = (mods[i].End - mods[i].Start + 0xFFF) >> 12;
103 MM_RefPhys( (mods[i].Start & ~0xFFF) + (num<<12) );
106 // Allocate References
107 //LOG("Reference Pages %i", (giPageCount*4+0xFFF)>>12);
108 for(num = 0; num < (giPageCount*4+0xFFF)>>12; num++)
110 if( !MM_Allocate( REFERENCE_BASE + (num<<12) ) )
112 Panic("Oh, ****, no space for the reference pages, that's bad");
119 gaPageReferences = (void*)REFERENCE_BASE;
120 memsetd(gaPageReferences, 1, kernelPages);
121 for( num = kernelPages; num < giPageCount; num++ )
123 gaPageReferences[num] = (gaPageBitmap[ num / 32 ] >> (num&31)) & 1;
128 * \fn tPAddr MM_AllocPhys(void)
129 * \brief Allocates a physical page from the general pool
131 tPAddr MM_AllocPhys(void)
139 Mutex_Acquire( &glPhysAlloc );
144 const int addrClasses[] = {0,16,20,24,32,64};
145 const int numAddrClasses = sizeof(addrClasses)/sizeof(addrClasses[0]);
148 for( i = numAddrClasses; i -- > 1; )
150 // Log("Scanning %i (%i bits)", i, addrClasses[i]);
151 first = 1 << (addrClasses[i-1] - 12);
152 last = (1 << (addrClasses[i] - 12)) - 1;
153 // Range is above the last free page
154 if( first > giLastPossibleFree )
156 // Last possible free page is in the range
157 if( last > giLastPossibleFree )
158 last = giLastPossibleFree;
160 // Log(" first=%i,max=%i", first, last);
162 for( indx = first; indx < last; )
164 // Log("indx = %i (< %i?)", indx, last);
165 if( gaSuperBitmap[indx>>10] == -1 ) {
170 if( gaPageBitmap[indx>>5] == -1 ) {
175 if( gaPageBitmap[indx>>5] & (1 << (indx&31)) ) {
181 if( indx < last ) break;
183 giLastPossibleFree = first; // Well, we couldn't find any in this range
186 if( i <= 1 ) indx = -1;
187 // Log("indx = %i", indx);
192 LOG("giLastPossibleFree = %i", giLastPossibleFree);
193 for( indx = giLastPossibleFree; indx >= 0; )
195 if( gaSuperBitmap[indx>>10] == -1 ) {
200 if( gaPageBitmap[indx>>5] == -1 ) {
205 if( gaPageBitmap[indx>>5] & (1 << (indx&31)) ) {
212 giLastPossibleFree = indx;
213 LOG("indx = %i", indx);
215 c = giLastPossibleFree % 32;
216 b = (giLastPossibleFree / 32) % 32;
217 a = giLastPossibleFree / 1024;
219 LOG("a=%i,b=%i,c=%i", a, b, c);
220 for( ; gaSuperBitmap[a] == -1 && a >= 0; a-- );
222 Mutex_Release( &glPhysAlloc );
223 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p) - %lli/%lli used",
224 __builtin_return_address(0), giPhysAlloc, giPageCount);
228 for( ; gaSuperBitmap[a] & (1<<b); b-- );
229 for( ; gaPageBitmap[a*32+b] & (1<<c); c-- );
230 LOG("a=%i,b=%i,c=%i", a, b, c);
231 indx = (a << 10) | (b << 5) | c;
233 giLastPossibleFree = indx;
237 Mutex_Release( &glPhysAlloc );
238 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p) - %lli/%lli used (indx = %x)",
239 __builtin_return_address(0), giPhysAlloc, giPageCount, indx);
240 Log_Debug("PMem", "giLastPossibleFree = %lli", giLastPossibleFree);
245 if( indx > 0xFFFFF ) {
246 Panic("The fuck? Too many pages! (indx = 0x%x)", indx);
251 gaPageReferences[ indx ] = 1;
252 gaPageBitmap[ indx>>5 ] |= 1 << (indx&31);
260 if(gaPageBitmap[ indx>>5 ] == -1) {
261 gaSuperBitmap[indx>>10] |= 1 << ((indx>>5)&31);
265 Mutex_Release( &glPhysAlloc );
269 Log_Debug("PMem", "MM_AllocPhys: RETURN 0x%llx (%i free)", ret, giPageCount-giPhysAlloc);
275 * \fn tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
276 * \brief Allocate a range of physical pages
277 * \param Pages Number of pages to allocate
278 * \param MaxBits Maximum number of address bits to use
280 tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
286 ENTER("iPages iMaxBits", Pages, MaxBits);
293 if(MaxBits > PHYS_BITS) MaxBits = PHYS_BITS;
296 Mutex_Acquire( &glPhysAlloc );
298 // Set up search state
299 if( giLastPossibleFree > ((tPAddr)1 << (MaxBits-12)) ) {
300 sidx = (tPAddr)1 << (MaxBits-12);
303 sidx = giLastPossibleFree;
311 LOG("a=%i, b=%i, idx=%i, sidx=%i", a, b, idx, sidx);
314 for( ; gaSuperBitmap[a] == -1 && a --; ) b = 31;
316 Mutex_Release( &glPhysAlloc );
317 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
322 for( ; gaSuperBitmap[a] & (1 << b); b-- ) sidx = 31;
325 for( ; gaPageBitmap[idx] & (1 << sidx); sidx-- )
326 LOG("gaPageBitmap[%i] = 0x%08x", idx, gaPageBitmap[idx]);
328 LOG("idx = %i, sidx = %i", idx, sidx);
333 // Check if the gap is large enough
340 if( gaPageBitmap[idx] == -1 ) {
346 if( gaPageBitmap[idx] & (1 << sidx) ) {
348 if(sidx < 0) { sidx = 31; idx --; }
356 // Check if it is a free range
357 for( i = 0; i < Pages; i++ )
360 if( gaPageBitmap[idx] & (1 << sidx) )
364 if(sidx < 0) { sidx = 31; idx --; }
372 // Check if an address was found
374 Mutex_Release( &glPhysAlloc );
375 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
381 for( i = 0; i < Pages; i++ )
384 gaPageReferences[idx*32+sidx] = 1;
385 gaPageBitmap[ idx ] |= 1 << sidx;
388 if(sidx == 32) { sidx = 0; idx ++; }
392 ret = (idx << 17) | (sidx << 12);
395 if(gaPageBitmap[ idx ] == -1) gaSuperBitmap[idx/32] |= 1 << (idx%32);
398 Mutex_Release( &glPhysAlloc );
402 Log_Debug("PMem", "MM_AllocPhysRange: RETURN 0x%llx-0x%llx (%i free)",
403 ret, ret + (1<<Pages)-1, giPageCount-giPhysAlloc);
409 * \fn void MM_RefPhys(tPAddr PAddr)
411 void MM_RefPhys(tPAddr PAddr)
416 // We don't care about non-ram pages
417 if(PAddr >= giPageCount) return;
420 Mutex_Acquire( &glPhysAlloc );
422 // Reference the page
424 gaPageReferences[ PAddr ] ++;
427 gaPageBitmap[ PAddr / 32 ] |= 1 << (PAddr&31);
430 if(gaPageBitmap[ PAddr / 32 ] == -1)
431 gaSuperBitmap[PAddr/1024] |= 1 << ((PAddr/32)&31);
434 Mutex_Release( &glPhysAlloc );
438 * \fn void MM_DerefPhys(tPAddr PAddr)
439 * \brief Dereferences a physical page
441 void MM_DerefPhys(tPAddr PAddr)
446 // We don't care about non-ram pages
447 if(PAddr >= giPageCount) return;
449 // Check if it is freed
450 if(gaPageReferences[ PAddr ] == 0) {
451 Warning("MM_DerefPhys - Non-referenced memory dereferenced");
456 Mutex_Acquire( &glPhysAlloc );
458 if( giLastPossibleFree < PAddr )
459 giLastPossibleFree = PAddr;
462 gaPageReferences[ PAddr ] --;
464 // Mark as free in bitmaps
465 if( gaPageReferences[ PAddr ] == 0 )
468 Log_Debug("PMem", "MM_DerefPhys: Free'd 0x%x (%i free)", PAddr, giPageCount-giPhysAlloc);
470 //LOG("Freed 0x%x by %p\n", PAddr<<12, __builtin_return_address(0));
472 gaPageBitmap[ PAddr / 32 ] &= ~(1 << (PAddr&31));
473 if(gaPageReferences[ PAddr ] == 0)
474 gaSuperBitmap[ PAddr >> 10 ] &= ~(1 << ((PAddr >> 5)&31));
478 Mutex_Release( &glPhysAlloc );
482 * \fn int MM_GetRefCount(tPAddr Addr)
484 int MM_GetRefCount(tPAddr Addr)
489 // We don't care about non-ram pages
490 if(Addr >= giPageCount) return -1;
492 // Check if it is freed
493 return gaPageReferences[ Addr ];