3 * - Physical memory manager
10 #define REFERENCE_BASE 0xE0400000
13 extern void gKernelEnd;
16 Uint32 MM_AllocPhys();
17 void MM_RefPhys(Uint32 Addr);
18 void MM_DerefPhys(Uint32 Addr);
23 Uint32 gaSuperBitmap[1024]; // Blocks of 1024 Pages
24 Uint32 gaPageBitmap[1024*1024/32]; // Individual pages
25 Uint32 *gaPageReferences;
28 void MM_Install(tMBoot_Info *MBoot)
30 Uint kernelPages, num;
35 giPageCount = (MBoot->HighMem >> 2) + 256; // HighMem is a kByte value
36 LOG("giPageCount = %i", giPageCount);
38 // Get used page count
39 kernelPages = (Uint)&gKernelEnd - KERNEL_BASE;
40 kernelPages += 0xFFF; // Page Align
45 memsetd(gaPageBitmap, -1, num);
46 gaPageBitmap[ num ] = (1 << (kernelPages & 31)) - 1;
48 // Fill Superpage bitmap
49 num = kernelPages/(32*32);
50 memsetd(gaSuperBitmap, -1, num);
51 gaSuperBitmap[ num ] = (1 << ((kernelPages / 32) & 31)) - 1;
53 // Mark Multiboot's pages as taken
55 MM_RefPhys( (Uint)MBoot - KERNEL_BASE );
57 for(i = (MBoot->ModuleCount*sizeof(tMBoot_Module)+0xFFF)>12; i--; )
58 MM_RefPhys( MBoot->Modules + (i << 12) );
60 mods = (void*)(MBoot->Modules + KERNEL_BASE);
61 for(i = 0; i < MBoot->ModuleCount; i++)
63 num = (mods[i].End - mods[i].Start + 0xFFF) >> 12;
65 MM_RefPhys( (mods[i].Start & ~0xFFF) + (num<<12) );
68 // Allocate References
69 LOG("Reference Pages %i", (giPageCount*4+0xFFF)>>12);
70 for(num = 0; num < (giPageCount*4+0xFFF)>>12; num++)
72 MM_Allocate( REFERENCE_BASE + (num<<12) );
76 gaPageReferences = (void*)REFERENCE_BASE;
77 memsetd(gaPageReferences, 1, kernelPages);
78 for( num = kernelPages; num < giPageCount; num++ )
80 gaPageReferences[num] = (gaPageBitmap[ num / 32 ] >> (num&31)) & 1;
85 * \fn tPAddr MM_AllocPhys()
86 * \brief Allocates a physical page
90 int num = giPageCount / 32 / 32;
97 for(a=0;gaSuperBitmap[a]==-1&&a<num;a++);
99 RELEASE( &giPhysAlloc );
100 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
103 for(b=0;gaSuperBitmap[a]&(1<<b);b++);
104 for(c=0;gaPageBitmap[a*32+b]&(1<<c);c++);
105 //for(c=0;gaPageReferences[a*32*32+b*32+c]>0;c++);
109 gaPageReferences[a*32*32+b*32+c] = 1;
110 gaPageBitmap[ a*32+b ] |= 1 << c;
113 ret = (a << 22) + (b << 17) + (c << 12);
116 if(gaPageBitmap[ a*32+b ] == -1) gaSuperBitmap[a] |= 1 << b;
119 RELEASE( &giPhysAlloc );
120 //LOG("Allocated 0x%x\n", ret);
121 //LOG("ret = %x", ret);
126 * \fn void MM_RefPhys(tPAddr Addr)
128 void MM_RefPhys(tPAddr Addr)
133 // We don't care about non-ram pages
134 if(Addr >= giPageCount) return;
137 LOCK( &giPhysAlloc );
139 // Reference the page
141 gaPageReferences[ Addr ] ++;
144 gaPageBitmap[ Addr / 32 ] |= 1 << (Addr&31);
147 if(gaPageBitmap[ Addr / 32 ] == -1) gaSuperBitmap[Addr/1024] |= 1 << ((Addr/32)&31);
150 RELEASE( &giPhysAlloc );
154 * \fn void MM_DerefPhys(Uint32 Addr)
156 void MM_DerefPhys(tPAddr Addr)
161 // We don't care about non-ram pages
162 if(Addr >= giPageCount) return;
164 // Check if it is freed
165 if(gaPageReferences[ Addr ] == 0) {
166 Warning("MM_DerefPhys - Non-referenced memory dereferenced");
171 LOCK( &giPhysAlloc );
174 gaPageReferences[ Addr ] --;
176 // Mark as free in bitmaps
177 if( gaPageReferences[ Addr ] == 0 )
179 //LOG("Freed 0x%x by %p\n", Addr<<12, __builtin_return_address(0));
180 gaPageBitmap[ Addr / 32 ] &= ~(1 << (Addr&31));
181 if(gaPageReferences[ Addr ] == 0)
182 gaSuperBitmap[ Addr >> 10 ] &= ~(1 << ((Addr >> 5)&31));
186 RELEASE( &giPhysAlloc );
190 * \fn int MM_GetRefCount(tPAddr Addr)
192 int MM_GetRefCount(tPAddr Addr)
197 // We don't care about non-ram pages
198 if(Addr >= giPageCount) return -1;
200 // Check if it is freed
201 return gaPageReferences[ Addr ];