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);
24 Uint64 giPhysAlloc = 0; // Number of allocated pages
25 Uint64 giPageCount = 0; // Total number of pages
26 Uint64 giLastPossibleFree = 0; // Last possible free page (before all pages are used)
28 Uint32 gaSuperBitmap[1024]; // Blocks of 1024 Pages
29 Uint32 gaPageBitmap[1024*1024/32]; // Individual pages
30 Uint32 *gaPageReferences;
33 void MM_Install(tMBoot_Info *MBoot)
35 Uint kernelPages, num;
41 // --- Find largest address
42 MBoot->MMapAddr |= KERNEL_BASE;
43 ent = (void *)( MBoot->MMapAddr );
44 while( (Uint)ent < MBoot->MMapAddr + MBoot->MMapLength )
49 // If entry is RAM and is above `maxAddr`, change `maxAddr`
50 if(ent->Type == 1 && ent->Base + ent->Length > maxAddr)
51 maxAddr = ent->Base + ent->Length;
53 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size );
57 giPageCount = (MBoot->HighMem >> 2) + 256; // HighMem is a kByte value
60 giPageCount = maxAddr >> 12;
62 giLastPossibleFree = giPageCount - 1;
64 memsetd(gaPageBitmap, 0xFFFFFFFF, giPageCount/32);
66 // Set up allocateable space
67 ent = (void *)( MBoot->MMapAddr );
68 while( (Uint)ent < MBoot->MMapAddr + MBoot->MMapLength )
70 memsetd( &gaPageBitmap[ent->Base/(4096*32)], 0, ent->Length/(4096*32) );
71 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size );
74 // Get used page count (Kernel)
75 kernelPages = (Uint)&gKernelEnd - KERNEL_BASE - 0x100000;
76 kernelPages += 0xFFF; // Page Align
81 memsetd( &gaPageBitmap[0x100000/(4096*32)], -1, num );
82 gaPageBitmap[ 0x100000/(4096*32) + num ] = (1 << (kernelPages & 31)) - 1;
84 // Fill Superpage bitmap
85 num = kernelPages/(32*32);
86 memsetd( &gaSuperBitmap[0x100000/(4096*32*32)], -1, num );
87 gaSuperBitmap[ 0x100000/(4096*32*32) + num ] = (1 << ((kernelPages / 32) & 31)) - 1;
89 // Mark Multiboot's pages as taken
91 MM_RefPhys( (Uint)MBoot - KERNEL_BASE );
93 for(i = (MBoot->ModuleCount*sizeof(tMBoot_Module)+0xFFF)>12; i--; )
94 MM_RefPhys( MBoot->Modules + (i << 12) );
96 mods = (void*)(MBoot->Modules + KERNEL_BASE);
97 for(i = 0; i < MBoot->ModuleCount; i++)
99 num = (mods[i].End - mods[i].Start + 0xFFF) >> 12;
101 MM_RefPhys( (mods[i].Start & ~0xFFF) + (num<<12) );
104 // Allocate References
105 //LOG("Reference Pages %i", (giPageCount*4+0xFFF)>>12);
106 for(num = 0; num < (giPageCount*4+0xFFF)>>12; num++)
108 MM_Allocate( REFERENCE_BASE + (num<<12) );
113 gaPageReferences = (void*)REFERENCE_BASE;
114 memsetd(gaPageReferences, 1, kernelPages);
115 for( num = kernelPages; num < giPageCount; num++ )
117 gaPageReferences[num] = (gaPageBitmap[ num / 32 ] >> (num&31)) & 1;
122 * \fn tPAddr MM_AllocPhys(void)
123 * \brief Allocates a physical page from the general pool
125 tPAddr MM_AllocPhys(void)
133 LOCK( &giPhysAlloc );
138 LOG("giLastPossibleFree = %i", giLastPossibleFree);
139 for( indx = giLastPossibleFree; indx >= 0; )
141 if( gaSuperBitmap[indx>>10] == -1 ) {
145 if( gaPageBitmap[indx>>5] == -1 ) {
150 if( gaPageBitmap[indx>>5] & (1 << (indx&31)) ) {
156 LOG("indx = %i", indx);
158 c = giLastPossibleFree % 32;
159 b = (giLastPossibleFree / 32) % 32;
160 a = giLastPossibleFree / 1024;
162 LOG("a=%i,b=%i,c=%i", a, b, c);
163 for( ; gaSuperBitmap[a] == -1 && a >= 0; a-- );
165 RELEASE( &giPhysAlloc );
166 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
170 for( ; gaSuperBitmap[a] & (1<<b); b-- );
171 for( ; gaPageBitmap[a*32+b] & (1<<c); c-- );
172 LOG("a=%i,b=%i,c=%i", a, b, c);
173 indx = (a << 10) | (b << 5) | c;
178 gaPageReferences[ indx ] = 1;
179 gaPageBitmap[ indx>>5 ] |= 1 << (indx&31);
184 giLastPossibleFree = indx;
187 if(gaPageBitmap[ indx>>5 ] == -1)
188 gaSuperBitmap[indx>>10] |= 1 << ((indx>>5)&31);
191 RELEASE( &giPhysAlloc );
194 //Log("MM_AllocPhys: RETURN 0x%x", ret);
199 * \fn tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
200 * \brief Allocate a range of physical pages
201 * \param Pages Number of pages to allocate
202 * \param MaxBits Maximum number of address bits to use
204 tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
210 ENTER("iPages iMaxBits", Pages, MaxBits);
217 if(MaxBits > PHYS_BITS) MaxBits = PHYS_BITS;
220 LOCK( &giPhysAlloc );
222 // Set up search state
223 if( giLastPossibleFree > ((tPAddr)1 << (MaxBits-12)) ) {
224 sidx = (tPAddr)1 << (MaxBits-12);
227 sidx = giLastPossibleFree;
235 LOG("a=%i, b=%i, idx=%i, sidx=%i", a, b, idx, sidx);
238 for( ; gaSuperBitmap[a] == -1 && a --; ) b = 31;
240 RELEASE( &giPhysAlloc );
241 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
246 for( ; gaSuperBitmap[a] & (1 << b); b-- ) sidx = 31;
249 for( ; gaPageBitmap[idx] & (1 << sidx); sidx-- )
250 LOG("gaPageBitmap[%i] = 0x%08x", idx, gaPageBitmap[idx]);
252 LOG("idx = %i, sidx = %i", idx, sidx);
257 // Check if the gap is large enough
264 if( gaPageBitmap[idx] == -1 ) {
270 if( gaPageBitmap[idx] & (1 << sidx) ) {
272 if(sidx < 0) { sidx = 31; idx --; }
280 // Check if it is a free range
281 for( i = 0; i < Pages; i++ )
284 if( gaPageBitmap[idx] & (1 << sidx) )
288 if(sidx < 0) { sidx = 31; idx --; }
296 // Check if an address was found
298 RELEASE( &giPhysAlloc );
299 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
305 for( i = 0; i < Pages; i++ )
308 gaPageReferences[idx*32+sidx] = 1;
309 gaPageBitmap[ idx ] |= 1 << sidx;
311 if(sidx == 32) { sidx = 0; idx ++; }
315 ret = (idx << 17) | (sidx << 12);
318 if(gaPageBitmap[ idx ] == -1) gaSuperBitmap[idx/32] |= 1 << (idx%32);
321 RELEASE( &giPhysAlloc );
328 * \fn void MM_RefPhys(tPAddr PAddr)
330 void MM_RefPhys(tPAddr PAddr)
335 // We don't care about non-ram pages
336 if(PAddr >= giPageCount) return;
339 LOCK( &giPhysAlloc );
341 // Reference the page
343 gaPageReferences[ PAddr ] ++;
346 gaPageBitmap[ PAddr / 32 ] |= 1 << (PAddr&31);
349 if(gaPageBitmap[ PAddr / 32 ] == -1)
350 gaSuperBitmap[PAddr/1024] |= 1 << ((PAddr/32)&31);
353 RELEASE( &giPhysAlloc );
357 * \fn void MM_DerefPhys(tPAddr PAddr)
358 * \brief Dereferences a physical page
360 void MM_DerefPhys(tPAddr PAddr)
365 // We don't care about non-ram pages
366 if(PAddr >= giPageCount) return;
368 // Check if it is freed
369 if(gaPageReferences[ PAddr ] == 0) {
370 Warning("MM_DerefPhys - Non-referenced memory dereferenced");
375 LOCK( &giPhysAlloc );
377 if( giLastPossibleFree < PAddr )
378 giLastPossibleFree = PAddr;
381 gaPageReferences[ PAddr ] --;
383 // Mark as free in bitmaps
384 if( gaPageReferences[ PAddr ] == 0 )
386 //LOG("Freed 0x%x by %p\n", PAddr<<12, __builtin_return_address(0));
387 gaPageBitmap[ PAddr / 32 ] &= ~(1 << (PAddr&31));
388 if(gaPageReferences[ PAddr ] == 0)
389 gaSuperBitmap[ PAddr >> 10 ] &= ~(1 << ((PAddr >> 5)&31));
393 RELEASE( &giPhysAlloc );
397 * \fn int MM_GetRefCount(tPAddr Addr)
399 int MM_GetRefCount(tPAddr Addr)
404 // We don't care about non-ram pages
405 if(Addr >= giPageCount) return -1;
407 // Check if it is freed
408 return gaPageReferences[ Addr ];