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 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 Log("MBoot->MMapAddr = %08x", MBoot->MMapAddr);
43 MBoot->MMapAddr |= KERNEL_BASE;
44 ent = (void *)( MBoot->MMapAddr );
45 while( (Uint)ent < MBoot->MMapAddr + MBoot->MMapLength )
47 Log(" ent->Size = %08x", ent->Size);
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 );
94 Log("MBoot->ModuleCount = %i", MBoot->ModuleCount);
96 for(i = (MBoot->ModuleCount*sizeof(tMBoot_Module)+0xFFF)>12; i--; )
97 MM_RefPhys( MBoot->Modules + (i << 12) );
99 Log("MBoot->Modules = %p", MBoot->Modules);
100 mods = (void*)(MBoot->Modules + KERNEL_BASE);
101 for(i = 0; i < MBoot->ModuleCount; i++)
103 num = (mods[i].End - mods[i].Start + 0xFFF) >> 12;
105 MM_RefPhys( (mods[i].Start & ~0xFFF) + (num<<12) );
108 // Allocate References
109 //LOG("Reference Pages %i", (giPageCount*4+0xFFF)>>12);
110 for(num = 0; num < (giPageCount*4+0xFFF)>>12; num++)
112 MM_Allocate( REFERENCE_BASE + (num<<12) );
117 gaPageReferences = (void*)REFERENCE_BASE;
118 memsetd(gaPageReferences, 1, kernelPages);
119 for( num = kernelPages; num < giPageCount; num++ )
121 gaPageReferences[num] = (gaPageBitmap[ num / 32 ] >> (num&31)) & 1;
126 * \fn tPAddr MM_AllocPhys()
127 * \brief Allocates a physical page from the general pool
129 tPAddr MM_AllocPhys()
137 LOCK( &giPhysAlloc );
142 LOG("giLastPossibleFree = %i", giLastPossibleFree);
143 for( indx = giLastPossibleFree; indx >= 0; )
145 if( gaSuperBitmap[indx>>10] == -1 ) {
149 if( gaPageBitmap[indx>>5] == -1 ) {
154 if( gaPageBitmap[indx>>5] & (1 << (indx&31)) ) {
160 LOG("indx = %i", indx);
162 c = giLastPossibleFree % 32;
163 b = (giLastPossibleFree / 32) % 32;
164 a = giLastPossibleFree / 1024;
166 LOG("a=%i,b=%i,c=%i", a, b, c);
167 for( ; gaSuperBitmap[a] == -1 && a >= 0; a-- );
169 RELEASE( &giPhysAlloc );
170 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
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;
182 gaPageReferences[ indx ] = 1;
183 gaPageBitmap[ indx>>5 ] |= 1 << (indx&31);
188 giLastPossibleFree = indx;
191 if(gaPageBitmap[ indx>>5 ] == -1)
192 gaSuperBitmap[indx>>10] |= 1 << ((indx>>5)&31);
195 RELEASE( &giPhysAlloc );
198 //Log("MM_AllocPhys: RETURN 0x%x", ret);
203 * \fn tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
204 * \brief Allocate a range of physical pages
205 * \param Pages Number of pages to allocate
206 * \param MaxBits Maximum number of address bits to use
208 tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
214 ENTER("iPages iMaxBits", Pages, MaxBits);
221 if(MaxBits > PHYS_BITS) MaxBits = PHYS_BITS;
224 LOCK( &giPhysAlloc );
226 // Set up search state
227 if( giLastPossibleFree > ((tPAddr)1 << (MaxBits-12)) ) {
228 sidx = (tPAddr)1 << (MaxBits-12);
231 sidx = giLastPossibleFree;
239 LOG("a=%i, b=%i, idx=%i, sidx=%i", a, b, idx, sidx);
242 for( ; gaSuperBitmap[a] == -1 && a --; ) b = 31;
244 RELEASE( &giPhysAlloc );
245 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
250 for( ; gaSuperBitmap[a] & (1 << b); b-- ) sidx = 31;
253 for( ; gaPageBitmap[idx] & (1 << sidx); sidx-- )
254 LOG("gaPageBitmap[%i] = 0x%08x", idx, gaPageBitmap[idx]);
256 LOG("idx = %i, sidx = %i", idx, sidx);
261 // Check if the gap is large enough
268 if( gaPageBitmap[idx] == -1 ) {
274 if( gaPageBitmap[idx] & (1 << sidx) ) {
276 if(sidx < 0) { sidx = 31; idx --; }
284 // Check if it is a free range
285 for( i = 0; i < Pages; i++ )
288 if( gaPageBitmap[idx] & (1 << sidx) )
292 if(sidx < 0) { sidx = 31; idx --; }
300 // Check if an address was found
302 RELEASE( &giPhysAlloc );
303 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
309 for( i = 0; i < Pages; i++ )
312 gaPageReferences[idx*32+sidx] = 1;
313 gaPageBitmap[ idx ] |= 1 << sidx;
315 if(sidx == 32) { sidx = 0; idx ++; }
319 ret = (idx << 17) | (sidx << 12);
322 if(gaPageBitmap[ idx ] == -1) gaSuperBitmap[idx/32] |= 1 << (idx%32);
325 RELEASE( &giPhysAlloc );
332 * \fn void MM_RefPhys(tPAddr PAddr)
334 void MM_RefPhys(tPAddr PAddr)
339 // We don't care about non-ram pages
340 if(PAddr >= giPageCount) return;
343 LOCK( &giPhysAlloc );
345 // Reference the page
347 gaPageReferences[ PAddr ] ++;
350 gaPageBitmap[ PAddr / 32 ] |= 1 << (PAddr&31);
353 if(gaPageBitmap[ PAddr / 32 ] == -1)
354 gaSuperBitmap[PAddr/1024] |= 1 << ((PAddr/32)&31);
357 RELEASE( &giPhysAlloc );
361 * \fn void MM_DerefPhys(tPAddr PAddr)
362 * \brief Dereferences a physical page
364 void MM_DerefPhys(tPAddr PAddr)
369 // We don't care about non-ram pages
370 if(PAddr >= giPageCount) return;
372 // Check if it is freed
373 if(gaPageReferences[ PAddr ] == 0) {
374 Warning("MM_DerefPhys - Non-referenced memory dereferenced");
379 LOCK( &giPhysAlloc );
381 if( giLastPossibleFree < PAddr )
382 giLastPossibleFree = PAddr;
385 gaPageReferences[ PAddr ] --;
387 // Mark as free in bitmaps
388 if( gaPageReferences[ PAddr ] == 0 )
390 //LOG("Freed 0x%x by %p\n", PAddr<<12, __builtin_return_address(0));
391 gaPageBitmap[ PAddr / 32 ] &= ~(1 << (PAddr&31));
392 if(gaPageReferences[ PAddr ] == 0)
393 gaSuperBitmap[ PAddr >> 10 ] &= ~(1 << ((PAddr >> 5)&31));
397 RELEASE( &giPhysAlloc );
401 * \fn int MM_GetRefCount(tPAddr Addr)
403 int MM_GetRefCount(tPAddr Addr)
408 // We don't care about non-ram pages
409 if(Addr >= giPageCount) return -1;
411 // Check if it is freed
412 return gaPageReferences[ Addr ];