3 * - Physical memory manager
12 #define REFERENCE_BASE 0xE0400000
15 extern void gKernelEnd;
18 tPAddr MM_AllocPhys(void);
19 tPAddr MM_AllocPhysRange(int Pages, int MaxBits);
20 void MM_RefPhys(tPAddr PAddr);
21 void MM_DerefPhys(tPAddr PAddr);
25 Uint64 giPhysAlloc = 0; // Number of allocated pages
26 Uint64 giPageCount = 0; // Total number of pages
27 Uint64 giLastPossibleFree = 0; // Last possible free page (before all pages are used)
29 Uint32 gaSuperBitmap[1024]; // Blocks of 1024 Pages
30 Uint32 gaPageBitmap[1024*1024/32]; // Individual pages
31 Uint32 *gaPageReferences;
34 void MM_Install(tMBoot_Info *MBoot)
36 Uint kernelPages, num;
42 // --- Find largest address
43 MBoot->MMapAddr |= KERNEL_BASE;
44 ent = (void *)( MBoot->MMapAddr );
45 while( (Uint)ent < MBoot->MMapAddr + MBoot->MMapLength )
50 // If entry is RAM and is above `maxAddr`, change `maxAddr`
51 if(ent->Type == 1 && ent->Base + ent->Length > maxAddr)
52 maxAddr = ent->Base + ent->Length;
54 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size );
58 giPageCount = (MBoot->HighMem >> 2) + 256; // HighMem is a kByte value
61 giPageCount = maxAddr >> 12;
63 giLastPossibleFree = giPageCount - 1;
65 memsetd(gaPageBitmap, 0xFFFFFFFF, giPageCount/32);
67 // Set up allocateable space
68 ent = (void *)( MBoot->MMapAddr );
69 while( (Uint)ent < MBoot->MMapAddr + MBoot->MMapLength )
71 memsetd( &gaPageBitmap[ent->Base/(4096*32)], 0, ent->Length/(4096*32) );
72 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size );
75 // Get used page count (Kernel)
76 kernelPages = (Uint)&gKernelEnd - KERNEL_BASE - 0x100000;
77 kernelPages += 0xFFF; // Page Align
82 memsetd( &gaPageBitmap[0x100000/(4096*32)], -1, num );
83 gaPageBitmap[ 0x100000/(4096*32) + num ] = (1 << (kernelPages & 31)) - 1;
85 // Fill Superpage bitmap
86 num = kernelPages/(32*32);
87 memsetd( &gaSuperBitmap[0x100000/(4096*32*32)], -1, num );
88 gaSuperBitmap[ 0x100000/(4096*32*32) + num ] = (1 << ((kernelPages / 32) & 31)) - 1;
90 // Mark Multiboot's pages as taken
92 MM_RefPhys( (Uint)MBoot - KERNEL_BASE );
94 for(i = (MBoot->ModuleCount*sizeof(tMBoot_Module)+0xFFF)>12; i--; )
95 MM_RefPhys( MBoot->Modules + (i << 12) );
97 mods = (void*)(MBoot->Modules + KERNEL_BASE);
98 for(i = 0; i < MBoot->ModuleCount; i++)
100 num = (mods[i].End - mods[i].Start + 0xFFF) >> 12;
102 MM_RefPhys( (mods[i].Start & ~0xFFF) + (num<<12) );
105 // Allocate References
106 //LOG("Reference Pages %i", (giPageCount*4+0xFFF)>>12);
107 for(num = 0; num < (giPageCount*4+0xFFF)>>12; num++)
109 MM_Allocate( REFERENCE_BASE + (num<<12) );
114 gaPageReferences = (void*)REFERENCE_BASE;
115 memsetd(gaPageReferences, 1, kernelPages);
116 for( num = kernelPages; num < giPageCount; num++ )
118 gaPageReferences[num] = (gaPageBitmap[ num / 32 ] >> (num&31)) & 1;
123 * \fn tPAddr MM_AllocPhys(void)
124 * \brief Allocates a physical page from the general pool
126 tPAddr MM_AllocPhys(void)
134 Mutex_Acquire( &glPhysAlloc );
139 LOG("giLastPossibleFree = %i", giLastPossibleFree);
140 for( indx = giLastPossibleFree; indx >= 0; )
142 if( gaSuperBitmap[indx>>10] == -1 ) {
146 if( gaPageBitmap[indx>>5] == -1 ) {
151 if( gaPageBitmap[indx>>5] & (1 << (indx&31)) ) {
157 LOG("indx = %i", indx);
159 c = giLastPossibleFree % 32;
160 b = (giLastPossibleFree / 32) % 32;
161 a = giLastPossibleFree / 1024;
163 LOG("a=%i,b=%i,c=%i", a, b, c);
164 for( ; gaSuperBitmap[a] == -1 && a >= 0; a-- );
166 Mutex_Release( &glPhysAlloc );
167 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
171 for( ; gaSuperBitmap[a] & (1<<b); b-- );
172 for( ; gaPageBitmap[a*32+b] & (1<<c); c-- );
173 LOG("a=%i,b=%i,c=%i", a, b, c);
174 indx = (a << 10) | (b << 5) | c;
179 gaPageReferences[ indx ] = 1;
180 gaPageBitmap[ indx>>5 ] |= 1 << (indx&31);
185 giLastPossibleFree = indx;
188 if(gaPageBitmap[ indx>>5 ] == -1)
189 gaSuperBitmap[indx>>10] |= 1 << ((indx>>5)&31);
192 Mutex_Release( &glPhysAlloc );
195 //Log("MM_AllocPhys: RETURN 0x%x", ret);
200 * \fn tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
201 * \brief Allocate a range of physical pages
202 * \param Pages Number of pages to allocate
203 * \param MaxBits Maximum number of address bits to use
205 tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
211 ENTER("iPages iMaxBits", Pages, MaxBits);
218 if(MaxBits > PHYS_BITS) MaxBits = PHYS_BITS;
221 Mutex_Acquire( &glPhysAlloc );
223 // Set up search state
224 if( giLastPossibleFree > ((tPAddr)1 << (MaxBits-12)) ) {
225 sidx = (tPAddr)1 << (MaxBits-12);
228 sidx = giLastPossibleFree;
236 LOG("a=%i, b=%i, idx=%i, sidx=%i", a, b, idx, sidx);
239 for( ; gaSuperBitmap[a] == -1 && a --; ) b = 31;
241 Mutex_Release( &glPhysAlloc );
242 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
247 for( ; gaSuperBitmap[a] & (1 << b); b-- ) sidx = 31;
250 for( ; gaPageBitmap[idx] & (1 << sidx); sidx-- )
251 LOG("gaPageBitmap[%i] = 0x%08x", idx, gaPageBitmap[idx]);
253 LOG("idx = %i, sidx = %i", idx, sidx);
258 // Check if the gap is large enough
265 if( gaPageBitmap[idx] == -1 ) {
271 if( gaPageBitmap[idx] & (1 << sidx) ) {
273 if(sidx < 0) { sidx = 31; idx --; }
281 // Check if it is a free range
282 for( i = 0; i < Pages; i++ )
285 if( gaPageBitmap[idx] & (1 << sidx) )
289 if(sidx < 0) { sidx = 31; idx --; }
297 // Check if an address was found
299 Mutex_Release( &glPhysAlloc );
300 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
306 for( i = 0; i < Pages; i++ )
309 gaPageReferences[idx*32+sidx] = 1;
310 gaPageBitmap[ idx ] |= 1 << sidx;
312 if(sidx == 32) { sidx = 0; idx ++; }
316 ret = (idx << 17) | (sidx << 12);
319 if(gaPageBitmap[ idx ] == -1) gaSuperBitmap[idx/32] |= 1 << (idx%32);
322 Mutex_Release( &glPhysAlloc );
329 * \fn void MM_RefPhys(tPAddr PAddr)
331 void MM_RefPhys(tPAddr PAddr)
336 // We don't care about non-ram pages
337 if(PAddr >= giPageCount) return;
340 Mutex_Acquire( &glPhysAlloc );
342 // Reference the page
344 gaPageReferences[ PAddr ] ++;
347 gaPageBitmap[ PAddr / 32 ] |= 1 << (PAddr&31);
350 if(gaPageBitmap[ PAddr / 32 ] == -1)
351 gaSuperBitmap[PAddr/1024] |= 1 << ((PAddr/32)&31);
354 Mutex_Release( &glPhysAlloc );
358 * \fn void MM_DerefPhys(tPAddr PAddr)
359 * \brief Dereferences a physical page
361 void MM_DerefPhys(tPAddr PAddr)
366 // We don't care about non-ram pages
367 if(PAddr >= giPageCount) return;
369 // Check if it is freed
370 if(gaPageReferences[ PAddr ] == 0) {
371 Warning("MM_DerefPhys - Non-referenced memory dereferenced");
376 Mutex_Acquire( &glPhysAlloc );
378 if( giLastPossibleFree < PAddr )
379 giLastPossibleFree = PAddr;
382 gaPageReferences[ PAddr ] --;
384 // Mark as free in bitmaps
385 if( gaPageReferences[ PAddr ] == 0 )
387 //LOG("Freed 0x%x by %p\n", PAddr<<12, __builtin_return_address(0));
388 gaPageBitmap[ PAddr / 32 ] &= ~(1 << (PAddr&31));
389 if(gaPageReferences[ PAddr ] == 0)
390 gaSuperBitmap[ PAddr >> 10 ] &= ~(1 << ((PAddr >> 5)&31));
394 Mutex_Release( &glPhysAlloc );
398 * \fn int MM_GetRefCount(tPAddr Addr)
400 int MM_GetRefCount(tPAddr Addr)
405 // We don't care about non-ram pages
406 if(Addr >= giPageCount) return -1;
408 // Check if it is freed
409 return gaPageReferences[ Addr ];