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++);
108 gaPageReferences[a*32*32+b*32+c] = 1;
109 gaPageBitmap[ a*32+b ] |= 1 << c;
112 ret = (a << 22) + (b << 17) + (c << 12);
115 if(gaPageBitmap[ a*32+b ] == -1) gaSuperBitmap[a] |= 1 << b;
118 RELEASE( &giPhysAlloc );
124 * \fn tPAddr MM_AllocPhysRange(int Pages)
125 * \brief Allocate a range of physical pages
126 * \param Pages Number of pages to allocate
128 tPAddr MM_AllocPhysRange(int Pages)
130 int num = giPageCount / 32 / 32;
134 LOCK( &giPhysAlloc );
137 for(a=0;gaSuperBitmap[a]==-1&&a<num;a++);
139 RELEASE( &giPhysAlloc );
140 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
143 for(b=0;gaSuperBitmap[a]&(1<<b);b++);
144 for(c=0;gaPageBitmap[a*32+b]&(1<<c);c++);
148 gaPageReferences[a*32*32+b*32+c] = 1;
149 gaPageBitmap[ a*32+b ] |= 1 << c;
152 ret = (a << 22) + (b << 17) + (c << 12);
155 if(gaPageBitmap[ a*32+b ] == -1) gaSuperBitmap[a] |= 1 << b;
158 RELEASE( &giPhysAlloc );
164 * \fn void MM_RefPhys(tPAddr Addr)
166 void MM_RefPhys(tPAddr Addr)
171 // We don't care about non-ram pages
172 if(Addr >= giPageCount) return;
175 LOCK( &giPhysAlloc );
177 // Reference the page
179 gaPageReferences[ Addr ] ++;
182 gaPageBitmap[ Addr / 32 ] |= 1 << (Addr&31);
185 if(gaPageBitmap[ Addr / 32 ] == -1) gaSuperBitmap[Addr/1024] |= 1 << ((Addr/32)&31);
188 RELEASE( &giPhysAlloc );
192 * \fn void MM_DerefPhys(Uint32 Addr)
194 void MM_DerefPhys(tPAddr Addr)
199 // We don't care about non-ram pages
200 if(Addr >= giPageCount) return;
202 // Check if it is freed
203 if(gaPageReferences[ Addr ] == 0) {
204 Warning("MM_DerefPhys - Non-referenced memory dereferenced");
209 LOCK( &giPhysAlloc );
212 gaPageReferences[ Addr ] --;
214 // Mark as free in bitmaps
215 if( gaPageReferences[ Addr ] == 0 )
217 //LOG("Freed 0x%x by %p\n", Addr<<12, __builtin_return_address(0));
218 gaPageBitmap[ Addr / 32 ] &= ~(1 << (Addr&31));
219 if(gaPageReferences[ Addr ] == 0)
220 gaSuperBitmap[ Addr >> 10 ] &= ~(1 << ((Addr >> 5)&31));
224 RELEASE( &giPhysAlloc );
228 * \fn int MM_GetRefCount(tPAddr Addr)
230 int MM_GetRefCount(tPAddr Addr)
235 // We don't care about non-ram pages
236 if(Addr >= giPageCount) return -1;
238 // Check if it is freed
239 return gaPageReferences[ Addr ];