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 LOG("giLastPossibleFree = %i", giLastPossibleFree);
145 for( indx = giLastPossibleFree; indx >= 0; )
147 if( gaSuperBitmap[indx>>10] == -1 ) {
152 if( gaPageBitmap[indx>>5] == -1 ) {
157 if( gaPageBitmap[indx>>5] & (1 << (indx&31)) ) {
163 LOG("indx = %i", indx);
165 c = giLastPossibleFree % 32;
166 b = (giLastPossibleFree / 32) % 32;
167 a = giLastPossibleFree / 1024;
169 LOG("a=%i,b=%i,c=%i", a, b, c);
170 for( ; gaSuperBitmap[a] == -1 && a >= 0; a-- );
172 Mutex_Release( &glPhysAlloc );
173 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p) - %lli/%lli used",
174 __builtin_return_address(0), giPhysAlloc, giPageCount);
178 for( ; gaSuperBitmap[a] & (1<<b); b-- );
179 for( ; gaPageBitmap[a*32+b] & (1<<c); c-- );
180 LOG("a=%i,b=%i,c=%i", a, b, c);
181 indx = (a << 10) | (b << 5) | c;
185 Mutex_Release( &glPhysAlloc );
186 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p) - %lli/%lli used (indx = %x)",
187 __builtin_return_address(0), giPhysAlloc, giPageCount, indx);
188 Log_Debug("PMem", "giLastPossibleFree = %lli", giLastPossibleFree);
193 if( indx > 0xFFFFF ) {
194 Panic("The fuck? Too many pages! (indx = 0x%x)", indx);
199 gaPageReferences[ indx ] = 1;
200 gaPageBitmap[ indx>>5 ] |= 1 << (indx&31);
206 giLastPossibleFree = indx;
209 if(gaPageBitmap[ indx>>5 ] == -1)
210 gaSuperBitmap[indx>>10] |= 1 << ((indx>>5)&31);
213 Mutex_Release( &glPhysAlloc );
217 Log_Debug("PMem", "MM_AllocPhys: RETURN 0x%llx (%i free)", ret, giPageCount-giPhysAlloc);
223 * \fn tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
224 * \brief Allocate a range of physical pages
225 * \param Pages Number of pages to allocate
226 * \param MaxBits Maximum number of address bits to use
228 tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
234 ENTER("iPages iMaxBits", Pages, MaxBits);
241 if(MaxBits > PHYS_BITS) MaxBits = PHYS_BITS;
244 Mutex_Acquire( &glPhysAlloc );
246 // Set up search state
247 if( giLastPossibleFree > ((tPAddr)1 << (MaxBits-12)) ) {
248 sidx = (tPAddr)1 << (MaxBits-12);
251 sidx = giLastPossibleFree;
259 LOG("a=%i, b=%i, idx=%i, sidx=%i", a, b, idx, sidx);
262 for( ; gaSuperBitmap[a] == -1 && a --; ) b = 31;
264 Mutex_Release( &glPhysAlloc );
265 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
270 for( ; gaSuperBitmap[a] & (1 << b); b-- ) sidx = 31;
273 for( ; gaPageBitmap[idx] & (1 << sidx); sidx-- )
274 LOG("gaPageBitmap[%i] = 0x%08x", idx, gaPageBitmap[idx]);
276 LOG("idx = %i, sidx = %i", idx, sidx);
281 // Check if the gap is large enough
288 if( gaPageBitmap[idx] == -1 ) {
294 if( gaPageBitmap[idx] & (1 << sidx) ) {
296 if(sidx < 0) { sidx = 31; idx --; }
304 // Check if it is a free range
305 for( i = 0; i < Pages; i++ )
308 if( gaPageBitmap[idx] & (1 << sidx) )
312 if(sidx < 0) { sidx = 31; idx --; }
320 // Check if an address was found
322 Mutex_Release( &glPhysAlloc );
323 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
329 for( i = 0; i < Pages; i++ )
332 gaPageReferences[idx*32+sidx] = 1;
333 gaPageBitmap[ idx ] |= 1 << sidx;
336 if(sidx == 32) { sidx = 0; idx ++; }
340 ret = (idx << 17) | (sidx << 12);
343 if(gaPageBitmap[ idx ] == -1) gaSuperBitmap[idx/32] |= 1 << (idx%32);
346 Mutex_Release( &glPhysAlloc );
350 Log_Debug("PMem", "MM_AllocPhysRange: RETURN 0x%llx-0x%llx (%i free)",
351 ret, ret + (1<<Pages)-1, giPageCount-giPhysAlloc);
357 * \fn void MM_RefPhys(tPAddr PAddr)
359 void MM_RefPhys(tPAddr PAddr)
364 // We don't care about non-ram pages
365 if(PAddr >= giPageCount) return;
368 Mutex_Acquire( &glPhysAlloc );
370 // Reference the page
372 gaPageReferences[ PAddr ] ++;
375 gaPageBitmap[ PAddr / 32 ] |= 1 << (PAddr&31);
378 if(gaPageBitmap[ PAddr / 32 ] == -1)
379 gaSuperBitmap[PAddr/1024] |= 1 << ((PAddr/32)&31);
382 Mutex_Release( &glPhysAlloc );
386 * \fn void MM_DerefPhys(tPAddr PAddr)
387 * \brief Dereferences a physical page
389 void MM_DerefPhys(tPAddr PAddr)
394 // We don't care about non-ram pages
395 if(PAddr >= giPageCount) return;
397 // Check if it is freed
398 if(gaPageReferences[ PAddr ] == 0) {
399 Warning("MM_DerefPhys - Non-referenced memory dereferenced");
404 Mutex_Acquire( &glPhysAlloc );
406 if( giLastPossibleFree < PAddr )
407 giLastPossibleFree = PAddr;
410 gaPageReferences[ PAddr ] --;
412 // Mark as free in bitmaps
413 if( gaPageReferences[ PAddr ] == 0 )
416 Log_Debug("PMem", "MM_DerefPhys: Free'd 0x%x (%i free)", PAddr, giPageCount-giPhysAlloc);
418 //LOG("Freed 0x%x by %p\n", PAddr<<12, __builtin_return_address(0));
420 gaPageBitmap[ PAddr / 32 ] &= ~(1 << (PAddr&31));
421 if(gaPageReferences[ PAddr ] == 0)
422 gaSuperBitmap[ PAddr >> 10 ] &= ~(1 << ((PAddr >> 5)&31));
426 Mutex_Release( &glPhysAlloc );
430 * \fn int MM_GetRefCount(tPAddr Addr)
432 int MM_GetRefCount(tPAddr Addr)
437 // We don't care about non-ram pages
438 if(Addr >= giPageCount) return -1;
440 // Check if it is freed
441 return gaPageReferences[ Addr ];