2 AcessOS Microkernel Version
9 #define REFERENCE_BASE 0xE0400000
12 extern void gKernelEnd;
15 Uint32 MM_AllocPhys();
16 void MM_RefPhys(Uint32 Addr);
17 void MM_DerefPhys(Uint32 Addr);
22 Uint32 gaSuperBitmap[1024]; // Blocks of 1024 Pages
23 Uint32 gaPageBitmap[1024*1024/32]; // Individual pages
24 Uint32 *gaPageReferences;
27 void MM_Install(tMBoot_Info *MBoot)
29 Uint kernelPages, num;
34 giPageCount = (MBoot->HighMem >> 2) + 256; // HighMem is a kByte value
35 Log("giPageCount = %i", giPageCount);
37 // Get used page count
38 kernelPages = (Uint)&gKernelEnd - KERNEL_BASE;
39 kernelPages += 0xFFF; // Page Align
44 memsetd(gaPageBitmap, -1, num);
45 gaPageBitmap[ num ] = (1 << (kernelPages & 31)) - 1;
47 // Fill Superpage bitmap
48 num = kernelPages/(32*32);
49 memsetd(gaSuperBitmap, -1, num);
50 gaSuperBitmap[ num ] = (1 << ((kernelPages / 32) & 31)) - 1;
52 // Mark Multiboot's pages as taken
54 MM_RefPhys( (Uint)MBoot - KERNEL_BASE );
56 for(i = (MBoot->ModuleCount*sizeof(tMBoot_Module)+0xFFF)>12; i--; )
57 MM_RefPhys( MBoot->Modules + (i << 12) );
59 mods = (void*)(MBoot->Modules + KERNEL_BASE);
60 for(i = 0; i < MBoot->ModuleCount; i++)
62 num = (mods[i].End - mods[i].Start + 0xFFF) >> 12;
64 MM_RefPhys( (mods[i].Start & ~0xFFF) + (num<<12) );
67 // Allocate References
68 Log("Reference Pages %i", (giPageCount*4+0xFFF)>>12);
69 for(num = 0; num < (giPageCount*4+0xFFF)>>12; num++)
71 MM_Allocate( REFERENCE_BASE + (num<<12) );
75 gaPageReferences = (void*)REFERENCE_BASE;
76 memsetd(gaPageReferences, 1, kernelPages);
77 for( num = kernelPages; num < giPageCount; num++ )
79 gaPageReferences[num] = (gaPageBitmap[ num / 32 ] >> (num&31)) & 1;
84 * \fn tPAddr MM_AllocPhys()
85 * \brief Allocates a physical page
89 int num = giPageCount / 32 / 32;
96 for(a=0;gaSuperBitmap[a]==-1&&a<num;a++);
98 RELEASE( &giPhysAlloc );
99 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
102 for(b=0;gaSuperBitmap[a]&(1<<b);b++);
103 for(c=0;gaPageBitmap[a*32+b]&(1<<c);c++);
104 //for(c=0;gaPageReferences[a*32*32+b*32+c]>0;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 );
119 //LOG("ret = %x", ret);
124 * \fn void MM_RefPhys(tPAddr Addr)
126 void MM_RefPhys(tPAddr Addr)
131 // We don't care about non-ram pages
132 if(Addr >= giPageCount) return;
135 LOCK( &giPhysAlloc );
137 // Reference the page
139 gaPageReferences[ Addr ] ++;
142 gaPageBitmap[ Addr / 32 ] |= 1 << (Addr&31);
145 if(gaPageBitmap[ Addr / 32 ] == -1) gaSuperBitmap[Addr/1024] |= 1 << ((Addr/32)&31);
148 RELEASE( &giPhysAlloc );
152 * \fn void MM_DerefPhys(Uint32 Addr)
154 void MM_DerefPhys(tPAddr Addr)
159 // We don't care about non-ram pages
160 if(Addr >= giPageCount) return;
162 // Check if it is freed
163 if(gaPageReferences[ Addr ] == 0) {
164 Warning("MM_DerefPhys - Non-referenced memory dereferenced");
169 LOCK( &giPhysAlloc );
172 gaPageReferences[ Addr ] --;
174 // Mark as free in bitmaps
175 if( gaPageReferences[ Addr ] == 0 )
177 gaPageBitmap[ Addr / 32 ] &= ~(1 << (Addr&31));
178 if(gaPageReferences[ Addr ] == 0)
179 gaSuperBitmap[ Addr >> 10 ] &= ~(1 << ((Addr >> 5)&31));
183 RELEASE( &giPhysAlloc );
187 * \fn int MM_GetRefCount(tPAddr Addr)
189 int MM_GetRefCount(tPAddr Addr)
194 // We don't care about non-ram pages
195 if(Addr >= giPageCount) return -1;
197 // Check if it is freed
198 return gaPageReferences[ Addr ];