3 * - Physical memory manager
11 #define TRACE_ALLOCS 0 // Print trace messages on AllocPhys/DerefPhys
15 extern char gKernelEnd[];
16 extern void Proc_PrintBacktrace(void);
19 void MM_Install(tMBoot_Info *MBoot);
20 //tPAddr MM_AllocPhys(void);
21 //tPAddr MM_AllocPhysRange(int Pages, int MaxBits);
22 //void MM_RefPhys(tPAddr PAddr);
23 //void MM_DerefPhys(tPAddr PAddr);
24 // int MM_GetRefCount(tPAddr PAddr);
28 Uint64 giPhysAlloc = 0; // Number of allocated pages
29 Uint64 giPageCount = 0; // Total number of pages
30 Uint64 giLastPossibleFree = 0; // Last possible free page (before all pages are used)
32 Uint32 gaSuperBitmap[1024]; // Blocks of 1024 Pages
33 Uint32 gaPageBitmap[1024*1024/32]; // Individual pages
34 int *gaPageReferences;
35 void **gaPageNodes = (void*)MM_PAGENODE_BASE;
36 #define REFENT_PER_PAGE (0x1000/sizeof(gaPageReferences[0]))
39 void MM_Install(tMBoot_Info *MBoot)
41 Uint kernelPages, num;
47 // --- Find largest address
48 MBoot->MMapAddr |= KERNEL_BASE;
49 ent = (void *)( MBoot->MMapAddr );
50 while( (Uint)ent < MBoot->MMapAddr + MBoot->MMapLength )
55 // If entry is RAM and is above `maxAddr`, change `maxAddr`
56 if(ent->Type == 1 && ent->Base + ent->Length > maxAddr)
57 maxAddr = ent->Base + ent->Length;
59 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size );
63 giPageCount = (MBoot->HighMem >> 2) + 256; // HighMem is a kByte value
66 giPageCount = maxAddr >> 12;
68 giLastPossibleFree = giPageCount - 1;
70 memsetd(gaPageBitmap, 0xFFFFFFFF, giPageCount/32);
72 // Set up allocateable space
73 ent = (void *)( MBoot->MMapAddr );
74 while( (Uint)ent < MBoot->MMapAddr + MBoot->MMapLength )
76 memsetd( &gaPageBitmap[ent->Base/(4096*32)], 0, ent->Length/(4096*32) );
77 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size );
80 // Get used page count (Kernel)
81 kernelPages = (Uint)&gKernelEnd - KERNEL_BASE - 0x100000;
82 kernelPages += 0xFFF; // Page Align
84 giPhysAlloc += kernelPages; // Add to used count
88 memsetd( &gaPageBitmap[0x100000/(4096*32)], -1, num );
89 gaPageBitmap[ 0x100000/(4096*32) + num ] = (1 << (kernelPages & 31)) - 1;
91 // Fill Superpage bitmap
92 num = kernelPages/(32*32);
93 memsetd( &gaSuperBitmap[0x100000/(4096*32*32)], -1, num );
94 gaSuperBitmap[ 0x100000/(4096*32*32) + num ] = (1 << ((kernelPages / 32) & 31)) - 1;
96 // Mark Multiboot's pages as taken
98 MM_RefPhys( (Uint)MBoot - KERNEL_BASE );
100 for(i = (MBoot->ModuleCount*sizeof(tMBoot_Module)+0xFFF)>12; i--; )
101 MM_RefPhys( MBoot->Modules + (i << 12) );
103 mods = (void*)(MBoot->Modules + KERNEL_BASE);
104 for(i = 0; i < MBoot->ModuleCount; i++)
106 num = (mods[i].End - mods[i].Start + 0xFFF) >> 12;
108 MM_RefPhys( (mods[i].Start & ~0xFFF) + (num<<12) );
111 gaPageReferences = (void*)MM_REFCOUNT_BASE;
113 Log_Log("PMem", "Physical memory set up (%lli pages of ~%lli MiB used)",
114 giPhysAlloc, (giPageCount*4)/1024
119 * \fn tPAddr MM_AllocPhys(void)
120 * \brief Allocates a physical page from the general pool
122 tPAddr MM_AllocPhys(void)
130 Mutex_Acquire( &glPhysAlloc );
135 const int addrClasses[] = {0,16,20,24,32,64};
136 const int numAddrClasses = sizeof(addrClasses)/sizeof(addrClasses[0]);
139 for( i = numAddrClasses; i -- > 1; )
141 first = 1UL << (addrClasses[i-1] - 12);
142 last = (1UL << (addrClasses[i] - 12)) - 1;
143 // Range is above the last free page
144 if( first > giLastPossibleFree )
146 // Last possible free page is in the range
147 if( last > giLastPossibleFree )
148 last = giLastPossibleFree;
151 for( indx = first; indx < last; )
153 if( gaSuperBitmap[indx>>10] == -1 ) {
158 if( gaPageBitmap[indx>>5] == -1 ) {
163 if( gaPageBitmap[indx>>5] & (1 << (indx&31)) ) {
169 if( indx < last ) break;
171 giLastPossibleFree = first; // Well, we couldn't find any in this range
174 if( i <= 1 ) indx = -1;
179 LOG("giLastPossibleFree = %i", giLastPossibleFree);
180 for( indx = giLastPossibleFree; indx >= 0; )
182 if( gaSuperBitmap[indx>>10] == -1 ) {
187 if( gaPageBitmap[indx>>5] == -1 ) {
192 if( gaPageBitmap[indx>>5] & (1 << (indx&31)) ) {
199 giLastPossibleFree = indx;
200 LOG("indx = %i", indx);
202 c = giLastPossibleFree % 32;
203 b = (giLastPossibleFree / 32) % 32;
204 a = giLastPossibleFree / 1024;
206 LOG("a=%i,b=%i,c=%i", a, b, c);
207 for( ; gaSuperBitmap[a] == -1 && a >= 0; a-- );
209 Mutex_Release( &glPhysAlloc );
210 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p) - %lli/%lli used",
211 __builtin_return_address(0), giPhysAlloc, giPageCount);
215 for( ; gaSuperBitmap[a] & (1<<b); b-- );
216 for( ; gaPageBitmap[a*32+b] & (1<<c); c-- );
217 LOG("a=%i,b=%i,c=%i", a, b, c);
218 indx = (a << 10) | (b << 5) | c;
220 giLastPossibleFree = indx;
224 Mutex_Release( &glPhysAlloc );
225 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p) - %lli/%lli used (indx = %x)",
226 __builtin_return_address(0), giPhysAlloc, giPageCount, indx);
227 Log_Debug("PMem", "giLastPossibleFree = %lli", giLastPossibleFree);
232 if( indx > 0xFFFFF ) {
233 Panic("The fuck? Too many pages! (indx = 0x%x)", indx);
236 if( indx >= giPageCount ) {
237 Mutex_Release( &glPhysAlloc );
238 Log_Error("PMem", "MM_AllocPhys - indx(%i) > giPageCount(%i)", indx, giPageCount);
244 if( MM_GetPhysAddr( (tVAddr)&gaPageReferences[indx] ) )
245 gaPageReferences[indx] = 1;
246 gaPageBitmap[ indx>>5 ] |= 1 << (indx&31);
254 if(gaPageBitmap[ indx>>5 ] == -1) {
255 gaSuperBitmap[indx>>10] |= 1 << ((indx>>5)&31);
259 Mutex_Release( &glPhysAlloc );
264 Log_Debug("PMem", "MM_AllocPhys: RETURN %P (%i free)", ret, giPageCount-giPhysAlloc);
265 Proc_PrintBacktrace();
272 * \fn tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
273 * \brief Allocate a range of physical pages
274 * \param Pages Number of pages to allocate
275 * \param MaxBits Maximum number of address bits to use
277 tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
283 ENTER("iPages iMaxBits", Pages, MaxBits);
290 if(MaxBits > PHYS_BITS) MaxBits = PHYS_BITS;
293 Mutex_Acquire( &glPhysAlloc );
295 // Set up search state
296 if( giLastPossibleFree > ((tPAddr)1 << (MaxBits-12)) ) {
297 sidx = (tPAddr)1 << (MaxBits-12);
300 sidx = giLastPossibleFree;
308 LOG("a=%i, b=%i, idx=%i, sidx=%i", a, b, idx, sidx);
311 for( ; gaSuperBitmap[a] == -1 && a --; ) b = 31;
313 Mutex_Release( &glPhysAlloc );
314 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
319 for( ; gaSuperBitmap[a] & (1 << b); b-- ) sidx = 31;
322 for( ; gaPageBitmap[idx] & (1 << sidx); sidx-- )
323 LOG("gaPageBitmap[%i] = 0x%08x", idx, gaPageBitmap[idx]);
325 LOG("idx = %i, sidx = %i", idx, sidx);
330 // Check if the gap is large enough
337 if( gaPageBitmap[idx] == -1 ) {
343 if( gaPageBitmap[idx] & (1 << sidx) ) {
345 if(sidx < 0) { sidx = 31; idx --; }
353 // Check if it is a free range
354 for( i = 0; i < Pages; i++ )
357 if( gaPageBitmap[idx] & (1 << sidx) )
361 if(sidx < 0) { sidx = 31; idx --; }
369 // Check if an address was found
371 Mutex_Release( &glPhysAlloc );
372 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
378 for( i = 0; i < Pages; i++ )
380 if( MM_GetPhysAddr( (tVAddr)&gaPageReferences[idx*32+sidx] ) )
381 gaPageReferences[idx*32+sidx] = 1;
382 gaPageBitmap[ idx ] |= 1 << sidx;
385 if(sidx == 32) { sidx = 0; idx ++; }
389 ret = (idx << 17) | (sidx << 12);
392 if(gaPageBitmap[ idx ] == -1) gaSuperBitmap[idx/32] |= 1 << (idx%32);
395 Mutex_Release( &glPhysAlloc );
399 Log_Debug("PMem", "MM_AllocPhysRange: RETURN 0x%llx-0x%llx (%i free)",
400 ret, ret + (1<<Pages)-1, giPageCount-giPhysAlloc);
406 * \fn void MM_RefPhys(tPAddr PAddr)
408 void MM_RefPhys(tPAddr PAddr)
413 // We don't care about non-ram pages
414 if(PAddr >= giPageCount) return;
417 Mutex_Acquire( &glPhysAlloc );
419 // Reference the page
420 if( gaPageReferences )
422 if( MM_GetPhysAddr( (tVAddr)&gaPageReferences[PAddr] ) == 0 )
425 tVAddr addr = ((tVAddr)&gaPageReferences[PAddr]) & ~0xFFF;
426 // Log_Debug("PMem", "MM_RefPhys: Allocating info for %X", PAddr);
427 Mutex_Release( &glPhysAlloc );
428 if( MM_Allocate( addr ) == 0 ) {
429 Log_KernelPanic("PMem",
430 "MM_RefPhys: Out of physical memory allocating info for %X",
434 Mutex_Acquire( &glPhysAlloc );
436 base = PAddr & ~(1024-1);
437 for( i = 0; i < 1024; i ++ ) {
438 gaPageReferences[base + i] = (gaPageBitmap[(base+i)/32] & (1 << (base+i)%32)) ? 1 : 0;
441 gaPageReferences[ PAddr ] ++;
444 // If not already used
445 if( !(gaPageBitmap[ PAddr / 32 ] & 1 << (PAddr&31)) ) {
448 gaPageBitmap[ PAddr / 32 ] |= 1 << (PAddr&31);
452 if(gaPageBitmap[ PAddr / 32 ] == -1)
453 gaSuperBitmap[PAddr/1024] |= 1 << ((PAddr/32)&31);
456 Mutex_Release( &glPhysAlloc );
460 * \fn void MM_DerefPhys(tPAddr PAddr)
461 * \brief Dereferences a physical page
463 void MM_DerefPhys(tPAddr PAddr)
468 // We don't care about non-ram pages
469 if(PAddr >= giPageCount) return;
471 // Check if it is freed
472 if( !(gaPageBitmap[PAddr / 32] & (1 << PAddr%32)) ) {
473 Log_Warning("MMVirt", "MM_DerefPhys - Non-referenced memory dereferenced");
478 Mutex_Acquire( &glPhysAlloc );
480 if( giLastPossibleFree < PAddr )
481 giLastPossibleFree = PAddr;
484 if( !MM_GetPhysAddr( (tVAddr)&gaPageReferences[PAddr] ) || (-- gaPageReferences[PAddr]) == 0 )
487 Log_Debug("PMem", "MM_DerefPhys: Free'd %P (%i free)", PAddr<<12, giPageCount-giPhysAlloc);
488 Proc_PrintBacktrace();
490 //LOG("Freed 0x%x by %p\n", PAddr<<12, __builtin_return_address(0));
492 gaPageBitmap[ PAddr / 32 ] &= ~(1 << (PAddr&31));
493 if(gaPageBitmap[ PAddr / 32 ] == 0)
494 gaSuperBitmap[ PAddr >> 10 ] &= ~(1 << ((PAddr >> 5)&31));
496 if( MM_GetPhysAddr( (tVAddr) &gaPageNodes[PAddr] ) )
498 gaPageNodes[PAddr] = NULL;
499 // TODO: Free Node Page when fully unused
504 Mutex_Release( &glPhysAlloc );
508 * \fn int MM_GetRefCount(tPAddr Addr)
510 int MM_GetRefCount(tPAddr PAddr)
515 // We don't care about non-ram pages
516 if(PAddr >= giPageCount) return -1;
518 if( MM_GetPhysAddr( (tVAddr)&gaPageReferences[PAddr] ) == 0 )
519 return (gaPageBitmap[PAddr / 32] & (1 << PAddr%32)) ? 1 : 0;
521 // Check if it is freed
522 return gaPageReferences[ PAddr ];
525 int MM_SetPageNode(tPAddr PAddr, void *Node)
529 if( MM_GetRefCount(PAddr) == 0 ) return 1;
533 block_addr = (tVAddr) &gaPageNodes[PAddr];
534 block_addr &= ~(PAGE_SIZE-1);
536 if( !MM_GetPhysAddr( block_addr ) )
538 if( !MM_Allocate( block_addr ) ) {
539 Log_Warning("PMem", "Unable to allocate Node page");
542 memset( (void*)block_addr, 0, PAGE_SIZE );
545 gaPageNodes[PAddr] = Node;
546 // Log("gaPageNodes[0x%x] = %p", PAddr, Node);
550 int MM_GetPageNode(tPAddr PAddr, void **Node)
552 if( MM_GetRefCount(PAddr) == 0 ) return 1;
555 if( !MM_GetPhysAddr( (tVAddr) &gaPageNodes[PAddr] ) ) {
559 *Node = gaPageNodes[PAddr];