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) - %lli/%lli used",
170 __builtin_return_address(0), giPhysAlloc, giPageCount);
174 for( ; gaSuperBitmap[a] & (1<<b); b-- );
175 for( ; gaPageBitmap[a*32+b] & (1<<c); c-- );
176 LOG("a=%i,b=%i,c=%i", a, b, c);
177 indx = (a << 10) | (b << 5) | c;
181 Mutex_Release( &glPhysAlloc );
182 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p) - %lli/%lli used (indx = %x)",
183 __builtin_return_address(0), giPhysAlloc, giPageCount, indx);
184 Log_Debug("PMem", "giLastPossibleFree = %lli", giLastPossibleFree);
189 if( indx > 0xFFFFF ) {
190 Panic("The fuck? Too many pages! (indx = 0x%x)", indx);
195 gaPageReferences[ indx ] = 1;
196 gaPageBitmap[ indx>>5 ] |= 1 << (indx&31);
202 giLastPossibleFree = indx;
205 if(gaPageBitmap[ indx>>5 ] == -1)
206 gaSuperBitmap[indx>>10] |= 1 << ((indx>>5)&31);
209 Mutex_Release( &glPhysAlloc );
213 Log_Debug("PMem", "MM_AllocPhys: RETURN 0x%llx (%i free)", ret, giPageCount-giPhysAlloc);
219 * \fn tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
220 * \brief Allocate a range of physical pages
221 * \param Pages Number of pages to allocate
222 * \param MaxBits Maximum number of address bits to use
224 tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
230 ENTER("iPages iMaxBits", Pages, MaxBits);
237 if(MaxBits > PHYS_BITS) MaxBits = PHYS_BITS;
240 Mutex_Acquire( &glPhysAlloc );
242 // Set up search state
243 if( giLastPossibleFree > ((tPAddr)1 << (MaxBits-12)) ) {
244 sidx = (tPAddr)1 << (MaxBits-12);
247 sidx = giLastPossibleFree;
255 LOG("a=%i, b=%i, idx=%i, sidx=%i", a, b, idx, sidx);
258 for( ; gaSuperBitmap[a] == -1 && a --; ) b = 31;
260 Mutex_Release( &glPhysAlloc );
261 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
266 for( ; gaSuperBitmap[a] & (1 << b); b-- ) sidx = 31;
269 for( ; gaPageBitmap[idx] & (1 << sidx); sidx-- )
270 LOG("gaPageBitmap[%i] = 0x%08x", idx, gaPageBitmap[idx]);
272 LOG("idx = %i, sidx = %i", idx, sidx);
277 // Check if the gap is large enough
284 if( gaPageBitmap[idx] == -1 ) {
290 if( gaPageBitmap[idx] & (1 << sidx) ) {
292 if(sidx < 0) { sidx = 31; idx --; }
300 // Check if it is a free range
301 for( i = 0; i < Pages; i++ )
304 if( gaPageBitmap[idx] & (1 << sidx) )
308 if(sidx < 0) { sidx = 31; idx --; }
316 // Check if an address was found
318 Mutex_Release( &glPhysAlloc );
319 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
325 for( i = 0; i < Pages; i++ )
328 gaPageReferences[idx*32+sidx] = 1;
329 gaPageBitmap[ idx ] |= 1 << sidx;
332 if(sidx == 32) { sidx = 0; idx ++; }
336 ret = (idx << 17) | (sidx << 12);
339 if(gaPageBitmap[ idx ] == -1) gaSuperBitmap[idx/32] |= 1 << (idx%32);
342 Mutex_Release( &glPhysAlloc );
346 Log_Debug("PMem", "MM_AllocPhysRange: RETURN 0x%llx-0x%llx (%i free)",
347 ret, ret + (1<<Pages)-1, giPageCount-giPhysAlloc);
353 * \fn void MM_RefPhys(tPAddr PAddr)
355 void MM_RefPhys(tPAddr PAddr)
360 // We don't care about non-ram pages
361 if(PAddr >= giPageCount) return;
364 Mutex_Acquire( &glPhysAlloc );
366 // Reference the page
368 gaPageReferences[ PAddr ] ++;
371 gaPageBitmap[ PAddr / 32 ] |= 1 << (PAddr&31);
374 if(gaPageBitmap[ PAddr / 32 ] == -1)
375 gaSuperBitmap[PAddr/1024] |= 1 << ((PAddr/32)&31);
378 Mutex_Release( &glPhysAlloc );
382 * \fn void MM_DerefPhys(tPAddr PAddr)
383 * \brief Dereferences a physical page
385 void MM_DerefPhys(tPAddr PAddr)
390 // We don't care about non-ram pages
391 if(PAddr >= giPageCount) return;
393 // Check if it is freed
394 if(gaPageReferences[ PAddr ] == 0) {
395 Warning("MM_DerefPhys - Non-referenced memory dereferenced");
400 Mutex_Acquire( &glPhysAlloc );
402 if( giLastPossibleFree < PAddr )
403 giLastPossibleFree = PAddr;
406 gaPageReferences[ PAddr ] --;
408 // Mark as free in bitmaps
409 if( gaPageReferences[ PAddr ] == 0 )
412 Log_Debug("PMem", "MM_DerefPhys: Free'd 0x%x (%i free)", PAddr, giPageCount-giPhysAlloc);
414 //LOG("Freed 0x%x by %p\n", PAddr<<12, __builtin_return_address(0));
416 gaPageBitmap[ PAddr / 32 ] &= ~(1 << (PAddr&31));
417 if(gaPageReferences[ PAddr ] == 0)
418 gaSuperBitmap[ PAddr >> 10 ] &= ~(1 << ((PAddr >> 5)&31));
422 Mutex_Release( &glPhysAlloc );
426 * \fn int MM_GetRefCount(tPAddr Addr)
428 int MM_GetRefCount(tPAddr Addr)
433 // We don't care about non-ram pages
434 if(Addr >= giPageCount) return -1;
436 // Check if it is freed
437 return gaPageReferences[ Addr ];