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 MM_Allocate( REFERENCE_BASE + (num<<12) );
115 gaPageReferences = (void*)REFERENCE_BASE;
116 memsetd(gaPageReferences, 1, kernelPages);
117 for( num = kernelPages; num < giPageCount; num++ )
119 gaPageReferences[num] = (gaPageBitmap[ num / 32 ] >> (num&31)) & 1;
124 * \fn tPAddr MM_AllocPhys(void)
125 * \brief Allocates a physical page from the general pool
127 tPAddr MM_AllocPhys(void)
135 Mutex_Acquire( &glPhysAlloc );
140 LOG("giLastPossibleFree = %i", giLastPossibleFree);
141 for( indx = giLastPossibleFree; indx >= 0; )
143 if( gaSuperBitmap[indx>>10] == -1 ) {
148 if( gaPageBitmap[indx>>5] == -1 ) {
153 if( gaPageBitmap[indx>>5] & (1 << (indx&31)) ) {
159 LOG("indx = %i", indx);
161 c = giLastPossibleFree % 32;
162 b = (giLastPossibleFree / 32) % 32;
163 a = giLastPossibleFree / 1024;
165 LOG("a=%i,b=%i,c=%i", a, b, c);
166 for( ; gaSuperBitmap[a] == -1 && a >= 0; a-- );
168 Mutex_Release( &glPhysAlloc );
169 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
173 for( ; gaSuperBitmap[a] & (1<<b); b-- );
174 for( ; gaPageBitmap[a*32+b] & (1<<c); c-- );
175 LOG("a=%i,b=%i,c=%i", a, b, c);
176 indx = (a << 10) | (b << 5) | c;
180 Mutex_Release( &glPhysAlloc );
181 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
186 if( indx > 0xFFFFF ) {
187 Panic("The fuck? Too many pages! (indx = 0x%x)", indx);
192 gaPageReferences[ indx ] = 1;
193 gaPageBitmap[ indx>>5 ] |= 1 << (indx&31);
199 giLastPossibleFree = indx;
202 if(gaPageBitmap[ indx>>5 ] == -1)
203 gaSuperBitmap[indx>>10] |= 1 << ((indx>>5)&31);
206 Mutex_Release( &glPhysAlloc );
210 Log_Debug("PMem", "MM_AllocPhys: RETURN 0x%llx (%i free)", ret, giPageCount-giPhysAlloc);
216 * \fn tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
217 * \brief Allocate a range of physical pages
218 * \param Pages Number of pages to allocate
219 * \param MaxBits Maximum number of address bits to use
221 tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
227 ENTER("iPages iMaxBits", Pages, MaxBits);
234 if(MaxBits > PHYS_BITS) MaxBits = PHYS_BITS;
237 Mutex_Acquire( &glPhysAlloc );
239 // Set up search state
240 if( giLastPossibleFree > ((tPAddr)1 << (MaxBits-12)) ) {
241 sidx = (tPAddr)1 << (MaxBits-12);
244 sidx = giLastPossibleFree;
252 LOG("a=%i, b=%i, idx=%i, sidx=%i", a, b, idx, sidx);
255 for( ; gaSuperBitmap[a] == -1 && a --; ) b = 31;
257 Mutex_Release( &glPhysAlloc );
258 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
263 for( ; gaSuperBitmap[a] & (1 << b); b-- ) sidx = 31;
266 for( ; gaPageBitmap[idx] & (1 << sidx); sidx-- )
267 LOG("gaPageBitmap[%i] = 0x%08x", idx, gaPageBitmap[idx]);
269 LOG("idx = %i, sidx = %i", idx, sidx);
274 // Check if the gap is large enough
281 if( gaPageBitmap[idx] == -1 ) {
287 if( gaPageBitmap[idx] & (1 << sidx) ) {
289 if(sidx < 0) { sidx = 31; idx --; }
297 // Check if it is a free range
298 for( i = 0; i < Pages; i++ )
301 if( gaPageBitmap[idx] & (1 << sidx) )
305 if(sidx < 0) { sidx = 31; idx --; }
313 // Check if an address was found
315 Mutex_Release( &glPhysAlloc );
316 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
322 for( i = 0; i < Pages; i++ )
325 gaPageReferences[idx*32+sidx] = 1;
326 gaPageBitmap[ idx ] |= 1 << sidx;
329 if(sidx == 32) { sidx = 0; idx ++; }
333 ret = (idx << 17) | (sidx << 12);
336 if(gaPageBitmap[ idx ] == -1) gaSuperBitmap[idx/32] |= 1 << (idx%32);
339 Mutex_Release( &glPhysAlloc );
343 Log_Debug("PMem", "MM_AllocPhysRange: RETURN 0x%llx-0x%llx (%i free)",
344 ret, ret + (1<<Pages)-1, giPageCount-giPhysAlloc);
350 * \fn void MM_RefPhys(tPAddr PAddr)
352 void MM_RefPhys(tPAddr PAddr)
357 // We don't care about non-ram pages
358 if(PAddr >= giPageCount) return;
361 Mutex_Acquire( &glPhysAlloc );
363 // Reference the page
365 gaPageReferences[ PAddr ] ++;
368 gaPageBitmap[ PAddr / 32 ] |= 1 << (PAddr&31);
371 if(gaPageBitmap[ PAddr / 32 ] == -1)
372 gaSuperBitmap[PAddr/1024] |= 1 << ((PAddr/32)&31);
375 Mutex_Release( &glPhysAlloc );
379 * \fn void MM_DerefPhys(tPAddr PAddr)
380 * \brief Dereferences a physical page
382 void MM_DerefPhys(tPAddr PAddr)
387 // We don't care about non-ram pages
388 if(PAddr >= giPageCount) return;
390 // Check if it is freed
391 if(gaPageReferences[ PAddr ] == 0) {
392 Warning("MM_DerefPhys - Non-referenced memory dereferenced");
397 Mutex_Acquire( &glPhysAlloc );
399 if( giLastPossibleFree < PAddr )
400 giLastPossibleFree = PAddr;
403 gaPageReferences[ PAddr ] --;
405 // Mark as free in bitmaps
406 if( gaPageReferences[ PAddr ] == 0 )
409 Log_Debug("PMem", "MM_DerefPhys: Free'd 0x%x (%i free)", PAddr, giPageCount-giPhysAlloc);
411 //LOG("Freed 0x%x by %p\n", PAddr<<12, __builtin_return_address(0));
413 gaPageBitmap[ PAddr / 32 ] &= ~(1 << (PAddr&31));
414 if(gaPageReferences[ PAddr ] == 0)
415 gaSuperBitmap[ PAddr >> 10 ] &= ~(1 << ((PAddr >> 5)&31));
419 Mutex_Release( &glPhysAlloc );
423 * \fn int MM_GetRefCount(tPAddr Addr)
425 int MM_GetRefCount(tPAddr Addr)
430 // We don't care about non-ram pages
431 if(Addr >= giPageCount) return -1;
433 // Check if it is freed
434 return gaPageReferences[ Addr ];