3 * - Physical memory manager
12 #define REFERENCE_BASE 0xE0400000
15 extern void gKernelEnd;
18 tPAddr MM_AllocPhys();
19 tPAddr MM_AllocPhysRange(int Pages, int MaxBits);
20 void MM_RefPhys(tPAddr Addr);
21 void MM_DerefPhys(tPAddr Addr);
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
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()
123 * \brief Allocates a physical page from the general pool
125 tPAddr MM_AllocPhys()
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)
211 if(MaxBits < 0) return 0;
212 if(MaxBits > PHYS_BITS) MaxBits = PHYS_BITS;
215 LOCK( &giPhysAlloc );
217 // Set up search state
218 if( giLastPossibleFree > ((tPAddr)1 << (MaxBits-12)) ) {
219 sidx = (tPAddr)1 << (MaxBits-12);
222 sidx = giLastPossibleFree;
230 for( ; gaSuperBitmap[a] == -1 && a --; );
232 RELEASE( &giPhysAlloc );
233 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
236 for( ; gaSuperBitmap[a] & (1 << b); b-- );
238 for( ; gaPageBitmap[idx] & (1 << sidx); sidx-- );
240 // Check if the gap is large enough
247 if( gaPageBitmap[idx] == -1 ) {
253 if( gaPageBitmap[idx] & (1 << sidx) ) {
255 if(sidx < 0) { sidx = 31; idx --; }
263 // Check if it is a free range
264 for( i = 0; i < Pages; i++ )
267 if( gaPageBitmap[idx] & (1 << sidx) )
271 if(sidx < 0) { sidx = 31; idx --; }
279 // Check if an address was found
281 RELEASE( &giPhysAlloc );
282 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
286 for( i = 0; i < Pages; i++ )
289 gaPageReferences[idx*32+sidx] = 1;
290 gaPageBitmap[ idx ] |= 1 << sidx;
292 if(sidx == 32) { sidx = 0; idx ++; }
296 ret = (idx << 17) | (sidx << 12);
299 if(gaPageBitmap[ idx ] == -1) gaSuperBitmap[idx/32] |= 1 << (idx%32);
302 RELEASE( &giPhysAlloc );
308 * \fn void MM_RefPhys(tPAddr Addr)
310 void MM_RefPhys(tPAddr Addr)
315 // We don't care about non-ram pages
316 if(Addr >= giPageCount) return;
319 LOCK( &giPhysAlloc );
321 // Reference the page
323 gaPageReferences[ Addr ] ++;
326 gaPageBitmap[ Addr / 32 ] |= 1 << (Addr&31);
329 if(gaPageBitmap[ Addr / 32 ] == -1) gaSuperBitmap[Addr/1024] |= 1 << ((Addr/32)&31);
332 RELEASE( &giPhysAlloc );
336 * \fn void MM_DerefPhys(Uint32 Addr)
338 void MM_DerefPhys(tPAddr Addr)
343 // We don't care about non-ram pages
344 if(Addr >= giPageCount) return;
346 // Check if it is freed
347 if(gaPageReferences[ Addr ] == 0) {
348 Warning("MM_DerefPhys - Non-referenced memory dereferenced");
353 LOCK( &giPhysAlloc );
355 if( giLastPossibleFree < Addr )
356 giLastPossibleFree = Addr;
359 gaPageReferences[ Addr ] --;
361 // Mark as free in bitmaps
362 if( gaPageReferences[ Addr ] == 0 )
364 //LOG("Freed 0x%x by %p\n", Addr<<12, __builtin_return_address(0));
365 gaPageBitmap[ Addr / 32 ] &= ~(1 << (Addr&31));
366 if(gaPageReferences[ Addr ] == 0)
367 gaSuperBitmap[ Addr >> 10 ] &= ~(1 << ((Addr >> 5)&31));
371 RELEASE( &giPhysAlloc );
375 * \fn int MM_GetRefCount(tPAddr Addr)
377 int MM_GetRefCount(tPAddr Addr)
382 // We don't care about non-ram pages
383 if(Addr >= giPageCount) return -1;
385 // Check if it is freed
386 return gaPageReferences[ Addr ];