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 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 Uint32 *gaPageReferences;
37 void MM_Install(tMBoot_Info *MBoot)
39 Uint kernelPages, num;
45 // --- Find largest address
46 MBoot->MMapAddr |= KERNEL_BASE;
47 ent = (void *)( MBoot->MMapAddr );
48 while( (Uint)ent < MBoot->MMapAddr + MBoot->MMapLength )
53 // If entry is RAM and is above `maxAddr`, change `maxAddr`
54 if(ent->Type == 1 && ent->Base + ent->Length > maxAddr)
55 maxAddr = ent->Base + ent->Length;
57 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size );
61 giPageCount = (MBoot->HighMem >> 2) + 256; // HighMem is a kByte value
64 giPageCount = maxAddr >> 12;
66 giLastPossibleFree = giPageCount - 1;
68 memsetd(gaPageBitmap, 0xFFFFFFFF, giPageCount/32);
70 // Set up allocateable space
71 ent = (void *)( MBoot->MMapAddr );
72 while( (Uint)ent < MBoot->MMapAddr + MBoot->MMapLength )
74 memsetd( &gaPageBitmap[ent->Base/(4096*32)], 0, ent->Length/(4096*32) );
75 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size );
78 // Get used page count (Kernel)
79 kernelPages = (Uint)&gKernelEnd - KERNEL_BASE - 0x100000;
80 kernelPages += 0xFFF; // Page Align
85 memsetd( &gaPageBitmap[0x100000/(4096*32)], -1, num );
86 gaPageBitmap[ 0x100000/(4096*32) + num ] = (1 << (kernelPages & 31)) - 1;
88 // Fill Superpage bitmap
89 num = kernelPages/(32*32);
90 memsetd( &gaSuperBitmap[0x100000/(4096*32*32)], -1, num );
91 gaSuperBitmap[ 0x100000/(4096*32*32) + num ] = (1 << ((kernelPages / 32) & 31)) - 1;
93 // Mark Multiboot's pages as taken
95 MM_RefPhys( (Uint)MBoot - KERNEL_BASE );
97 for(i = (MBoot->ModuleCount*sizeof(tMBoot_Module)+0xFFF)>12; i--; )
98 MM_RefPhys( MBoot->Modules + (i << 12) );
100 mods = (void*)(MBoot->Modules + KERNEL_BASE);
101 for(i = 0; i < MBoot->ModuleCount; i++)
103 num = (mods[i].End - mods[i].Start + 0xFFF) >> 12;
105 MM_RefPhys( (mods[i].Start & ~0xFFF) + (num<<12) );
108 // Allocate References
109 //LOG("Reference Pages %i", (giPageCount*4+0xFFF)>>12);
110 for(num = 0; num < (giPageCount*4+0xFFF)>>12; num++)
112 if( !MM_Allocate( REFERENCE_BASE + (num<<12) ) )
114 Panic("Oh, ****, no space for the reference pages, that's bad");
121 gaPageReferences = (void*)REFERENCE_BASE;
122 memsetd(gaPageReferences, 1, kernelPages);
123 for( num = kernelPages; num < giPageCount; num++ )
125 gaPageReferences[num] = (gaPageBitmap[ num / 32 ] >> (num&31)) & 1;
130 * \fn tPAddr MM_AllocPhys(void)
131 * \brief Allocates a physical page from the general pool
133 tPAddr MM_AllocPhys(void)
141 Mutex_Acquire( &glPhysAlloc );
146 const int addrClasses[] = {0,16,20,24,32,64};
147 const int numAddrClasses = sizeof(addrClasses)/sizeof(addrClasses[0]);
150 for( i = numAddrClasses; i -- > 1; )
152 // Log("Scanning %i (%i bits)", i, addrClasses[i]);
153 first = 1 << (addrClasses[i-1] - 12);
154 last = (1 << (addrClasses[i] - 12)) - 1;
155 // Range is above the last free page
156 if( first > giLastPossibleFree )
158 // Last possible free page is in the range
159 if( last > giLastPossibleFree )
160 last = giLastPossibleFree;
162 // Log(" first=%i,max=%i", first, last);
164 for( indx = first; indx < last; )
166 // Log("indx = %i (< %i?)", indx, last);
167 if( gaSuperBitmap[indx>>10] == -1 ) {
172 if( gaPageBitmap[indx>>5] == -1 ) {
177 if( gaPageBitmap[indx>>5] & (1 << (indx&31)) ) {
183 if( indx < last ) break;
185 giLastPossibleFree = first; // Well, we couldn't find any in this range
188 if( i <= 1 ) indx = -1;
189 // Log("indx = %i", indx);
194 LOG("giLastPossibleFree = %i", giLastPossibleFree);
195 for( indx = giLastPossibleFree; indx >= 0; )
197 if( gaSuperBitmap[indx>>10] == -1 ) {
202 if( gaPageBitmap[indx>>5] == -1 ) {
207 if( gaPageBitmap[indx>>5] & (1 << (indx&31)) ) {
214 giLastPossibleFree = indx;
215 LOG("indx = %i", indx);
217 c = giLastPossibleFree % 32;
218 b = (giLastPossibleFree / 32) % 32;
219 a = giLastPossibleFree / 1024;
221 LOG("a=%i,b=%i,c=%i", a, b, c);
222 for( ; gaSuperBitmap[a] == -1 && a >= 0; a-- );
224 Mutex_Release( &glPhysAlloc );
225 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p) - %lli/%lli used",
226 __builtin_return_address(0), giPhysAlloc, giPageCount);
230 for( ; gaSuperBitmap[a] & (1<<b); b-- );
231 for( ; gaPageBitmap[a*32+b] & (1<<c); c-- );
232 LOG("a=%i,b=%i,c=%i", a, b, c);
233 indx = (a << 10) | (b << 5) | c;
235 giLastPossibleFree = indx;
239 Mutex_Release( &glPhysAlloc );
240 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p) - %lli/%lli used (indx = %x)",
241 __builtin_return_address(0), giPhysAlloc, giPageCount, indx);
242 Log_Debug("PMem", "giLastPossibleFree = %lli", giLastPossibleFree);
247 if( indx > 0xFFFFF ) {
248 Panic("The fuck? Too many pages! (indx = 0x%x)", indx);
253 gaPageReferences[ indx ] = 1;
254 gaPageBitmap[ indx>>5 ] |= 1 << (indx&31);
262 if(gaPageBitmap[ indx>>5 ] == -1) {
263 gaSuperBitmap[indx>>10] |= 1 << ((indx>>5)&31);
267 Mutex_Release( &glPhysAlloc );
271 Log_Debug("PMem", "MM_AllocPhys: RETURN 0x%llx (%i free)", ret, giPageCount-giPhysAlloc);
277 * \fn tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
278 * \brief Allocate a range of physical pages
279 * \param Pages Number of pages to allocate
280 * \param MaxBits Maximum number of address bits to use
282 tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
288 ENTER("iPages iMaxBits", Pages, MaxBits);
295 if(MaxBits > PHYS_BITS) MaxBits = PHYS_BITS;
298 Mutex_Acquire( &glPhysAlloc );
300 // Set up search state
301 if( giLastPossibleFree > ((tPAddr)1 << (MaxBits-12)) ) {
302 sidx = (tPAddr)1 << (MaxBits-12);
305 sidx = giLastPossibleFree;
313 LOG("a=%i, b=%i, idx=%i, sidx=%i", a, b, idx, sidx);
316 for( ; gaSuperBitmap[a] == -1 && a --; ) b = 31;
318 Mutex_Release( &glPhysAlloc );
319 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
324 for( ; gaSuperBitmap[a] & (1 << b); b-- ) sidx = 31;
327 for( ; gaPageBitmap[idx] & (1 << sidx); sidx-- )
328 LOG("gaPageBitmap[%i] = 0x%08x", idx, gaPageBitmap[idx]);
330 LOG("idx = %i, sidx = %i", idx, sidx);
335 // Check if the gap is large enough
342 if( gaPageBitmap[idx] == -1 ) {
348 if( gaPageBitmap[idx] & (1 << sidx) ) {
350 if(sidx < 0) { sidx = 31; idx --; }
358 // Check if it is a free range
359 for( i = 0; i < Pages; i++ )
362 if( gaPageBitmap[idx] & (1 << sidx) )
366 if(sidx < 0) { sidx = 31; idx --; }
374 // Check if an address was found
376 Mutex_Release( &glPhysAlloc );
377 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
383 for( i = 0; i < Pages; i++ )
386 gaPageReferences[idx*32+sidx] = 1;
387 gaPageBitmap[ idx ] |= 1 << sidx;
390 if(sidx == 32) { sidx = 0; idx ++; }
394 ret = (idx << 17) | (sidx << 12);
397 if(gaPageBitmap[ idx ] == -1) gaSuperBitmap[idx/32] |= 1 << (idx%32);
400 Mutex_Release( &glPhysAlloc );
404 Log_Debug("PMem", "MM_AllocPhysRange: RETURN 0x%llx-0x%llx (%i free)",
405 ret, ret + (1<<Pages)-1, giPageCount-giPhysAlloc);
411 * \fn void MM_RefPhys(tPAddr PAddr)
413 void MM_RefPhys(tPAddr PAddr)
418 // We don't care about non-ram pages
419 if(PAddr >= giPageCount) return;
422 Mutex_Acquire( &glPhysAlloc );
424 // Reference the page
426 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(gaPageReferences[ PAddr ] == 0) {
453 Warning("MM_DerefPhys - Non-referenced memory dereferenced");
458 Mutex_Acquire( &glPhysAlloc );
460 if( giLastPossibleFree < PAddr )
461 giLastPossibleFree = PAddr;
464 gaPageReferences[ PAddr ] --;
466 // Mark as free in bitmaps
467 if( gaPageReferences[ PAddr ] == 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(gaPageReferences[ PAddr ] == 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 // Check if it is freed
495 return gaPageReferences[ PAddr ];