4 * Physical Memory Manager
13 MM_PHYS_16BIT, // Does anything need this?
14 MM_PHYS_20BIT, // Real-Mode
15 MM_PHYS_24BIT, // ISA DMA
16 MM_PHYS_32BIT, // x86 Hardware
17 MM_PHYS_MAX, // Doesn't care
22 extern char gKernelBase[];
23 extern char gKernelEnd[];
26 void MM_InitPhys_Multiboot(tMBoot_Info *MBoot);
27 tPAddr MM_AllocPhysRange(int Num, int Bits);
28 tPAddr MM_AllocPhys(void);
29 void MM_RefPhys(tPAddr PAddr);
30 void MM_DerefPhys(tPAddr PAddr);
31 int MM_int_GetRangeID( tPAddr Addr );
34 tMutex glPhysicalPages;
35 Uint64 *gaSuperBitmap = (void*)MM_PAGE_SUPBMP; // 1 bit = 64 Pages, 16 MiB Per Word
36 Uint64 *gaMainBitmap = (void*)MM_PAGE_BITMAP; // 1 bit = 1 Page, 256 KiB per Word
37 Uint64 *gaMultiBitmap = (void*)MM_PAGE_DBLBMP; // Each bit means that the page is being used multiple times
38 Uint32 *gaiPageReferences = (void*)MM_PAGE_COUNTS; // Reference Counts
39 tPAddr giFirstFreePage; // First possibly free page
40 Uint64 giPhysRangeFree[NUM_MM_PHYS_RANGES]; // Number of free pages in each range
41 Uint64 giPhysRangeFirst[NUM_MM_PHYS_RANGES]; // First free page in each range
42 Uint64 giPhysRangeLast[NUM_MM_PHYS_RANGES]; // Last free page in each range
43 Uint64 giMaxPhysPage = 0; // Maximum Physical page
44 // Only used in init, allows the init code to provide pages for use by
45 // the allocator before the bitmaps exist.
46 // 3 entries because the are three calls to MM_AllocPhys in MM_Map
47 #define NUM_STATIC_ALLOC 3
48 tPAddr gaiStaticAllocPages[NUM_STATIC_ALLOC] = {0};
52 * \brief Initialise the physical memory map using a Multiboot 1 map
54 void MM_InitPhys_Multiboot(tMBoot_Info *MBoot)
56 tMBoot_MMapEnt *mmapStart;
59 int numPages, superPages;
63 tPAddr paddr, firstFreePage;
65 ENTER("pMBoot=%p", MBoot);
67 // Scan the physical memory map
68 // Looking for the top of physical memory
69 mmapStart = (void *)( KERNEL_BASE | MBoot->MMapAddr );
70 LOG("mmapStart = %p", mmapStart);
72 while( (Uint)ent < (Uint)mmapStart + MBoot->MMapLength )
74 // Adjust for the size of the entry
76 LOG("ent={Type:%i,Base:0x%x,Length:%x",
77 ent->Type, ent->Base, ent->Length);
79 // If entry is RAM and is above `maxAddr`, change `maxAddr`
80 if(ent->Type == 1 && ent->Base + ent->Length > maxAddr)
81 maxAddr = ent->Base + ent->Length;
84 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size );
87 // Did we find a valid end?
89 // No, darn, let's just use the HighMem hack
90 giMaxPhysPage = (MBoot->HighMem >> 2) + 256; // HighMem is a kByte value
93 // Goodie, goodie gumdrops
94 giMaxPhysPage = maxAddr >> 12;
96 LOG("giMaxPhysPage = 0x%x", giMaxPhysPage);
98 // Find a contigous section of memory to hold it in
99 // - Starting from the end of the kernel
100 // - We also need a region for the super bitmap
101 superPages = ((giMaxPhysPage+64*8-1)/(64*8) + 0xFFF) >> 12;
102 numPages = (giMaxPhysPage + 7) / 8;
103 numPages = (numPages + 0xFFF) >> 12;
104 LOG("numPages = %i, superPages = %i", numPages, superPages);
107 int todo = numPages*2 + superPages;
108 // Ok, naieve allocation, just put it after the kernel
109 // - Allocated Bitmap
110 vaddr = MM_PAGE_BITMAP;
111 paddr = (tPAddr)&gKernelEnd - KERNEL_BASE;
115 for( i = 0; i < NUM_STATIC_ALLOC; i++) {
116 if(gaiStaticAllocPages[i] != 0) continue;
117 gaiStaticAllocPages[i] = paddr;
121 MM_Map(vaddr, paddr);
127 if( todo == numPages + superPages )
128 vaddr = MM_PAGE_DBLBMP;
129 if( todo == superPages )
130 vaddr = MM_PAGE_SUPBMP;
133 // Scan for a nice range
136 int todo = numPages*2 + superPages;
138 vaddr = MM_PAGE_BITMAP;
142 (Uint)ent < (Uint)mmapStart + MBoot->MMapLength;
143 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size )
152 // Let's not put it below the kernel, shall we?
153 if( ent->Base + ent->Size < (tPAddr)&gKernelBase )
156 LOG("%x <= %x && %x > %x",
157 ent->Base, (tPAddr)&gKernelBase,
158 ent->Base + ent->Size, (tPAddr)&gKernelEnd - KERNEL_BASE
160 // Check if the kernel is in this range
161 if( ent->Base <= (tPAddr)&gKernelBase
162 && ent->Base + ent->Length > (tPAddr)&gKernelEnd - KERNEL_BASE )
164 avail = ent->Length >> 12;
165 avail -= ((tPAddr)&gKernelEnd - KERNEL_BASE - ent->Base) >> 12;
166 paddr = (tPAddr)&gKernelEnd - KERNEL_BASE;
168 // No? then we can use all of the block
171 avail = ent->Length >> 12;
175 Log(" MM_InitPhys_Multiboot: paddr=0x%x, avail=%i", paddr, avail);
178 while( todo && avail --)
180 // Static Allocations
181 for( i = 0; i < NUM_STATIC_ALLOC && avail; i++) {
182 if(gaiStaticAllocPages[i] != 0) continue;
183 gaiStaticAllocPages[i] = paddr;
190 MM_Map(vaddr, paddr);
195 // Alter the destination address when needed
196 if(todo == superPages+numPages)
197 vaddr = MM_PAGE_DBLBMP;
198 if(todo == superPages)
199 vaddr = MM_PAGE_SUPBMP;
202 // Fast quit if there's nothing left to allocate
206 // Save the current value of paddr to simplify the allocation later
207 firstFreePage = paddr;
209 LOG("Clearing multi bitmap");
211 memset(gaMultiBitmap, 0, (numPages<<12)/8);
212 // - initialise to one, then clear the avaliable areas
213 memset(gaMainBitmap, -1, (numPages<<12)/8);
214 memset(gaSuperBitmap, -1, (numPages<<12)/(8*64));
215 LOG("Setting main bitmap");
216 // - Clear all Type=1 areas
217 LOG("Clearing valid regions");
220 (Uint)ent < (Uint)mmapStart + MBoot->MMapLength;
221 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size )
224 // Check if the type is RAM
225 if(ent->Type != 1) continue;
228 base = ent->Base >> 12;
229 size = ent->Size >> 12;
232 Uint64 val = -1LL << (base & 63);
233 gaMainBitmap[base / 64] &= ~val;
235 base += 64 - (base & 63);
237 memset( &gaMainBitmap[base / 64], 0, size/8 );
239 Uint64 val = -1LL << (size & 7);
241 gaMainBitmap[base / 64] &= ~val;
245 base = ent->Base >> 12;
246 size = ent->Size >> 12;
247 size = (size + (base & 63) + 63) >> 6;
250 Uint64 val = -1LL << (base & 63);
251 gaSuperBitmap[base / 64] &= ~val;
253 base += 64 - (base & 63);
257 // Reference the used pages
258 base = (tPAddr)&gKernelBase >> 12;
259 size = firstFreePage >> 12;
260 memset( &gaMainBitmap[base / 64], -1, size/8 );
262 Uint64 val = -1LL << (size & 7);
264 gaMainBitmap[base / 64] |= val;
267 // Free the unused static allocs
268 for( i = 0; i < NUM_STATIC_ALLOC; i++) {
269 if(gaiStaticAllocPages[i] != 0)
271 gaMainBitmap[ gaiStaticAllocPages[i] >> (12+6) ]
272 &= ~(1LL << ((gaiStaticAllocPages[i]>>12)&63));
275 // Fill the super bitmap
276 LOG("Filling super bitmap");
277 memset(gaSuperBitmap, 0, superPages<<12);
278 for( base = 0; base < (size+63)/64; base ++)
280 if( gaMainBitmap[ base ] + 1 == 0 )
281 gaSuperBitmap[ base/64 ] |= 1LL << (base&63);
284 // Set free page counts
285 for( base = 1; base < giMaxPhysPage; base ++ )
289 if( gaMainBitmap[ base >> 6 ] & (1LL << (base&63)) ) continue;
292 rangeID = MM_int_GetRangeID( base << 12 );
294 // Increment free page count
295 giPhysRangeFree[ rangeID ] ++;
297 // Check for first free page in range
298 if(giPhysRangeFirst[ rangeID ] == 0)
299 giPhysRangeFirst[ rangeID ] = base;
300 // Set last (when the last free page is reached, this won't be
301 // updated anymore, hence will be correct)
302 giPhysRangeLast[ rangeID ] = base;
309 * \brief Allocate a contiguous range of physical pages with a maximum
310 * bit size of \a MaxBits
311 * \param Pages Number of pages to allocate
312 * \param MaxBits Maximum size of the physical address
313 * \note If \a MaxBits is <= 0, any sized address is used (with preference
314 * to higher addresses)
316 tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
322 ENTER("iPages iBits", Pages, MaxBits);
324 if( MaxBits <= 0 || MaxBits >= 64 ) // Speedup for the common case
325 rangeID = MM_PHYS_MAX;
327 rangeID = MM_int_GetRangeID( (1LL << MaxBits) - 1 );
329 LOG("rangeID = %i", rangeID);
331 Mutex_Acquire(&glPhysicalPages);
333 // Check if the range actually has any free pages
334 while(giPhysRangeFree[rangeID] == 0 && rangeID)
337 LOG("rangeID = %i", rangeID);
339 // What the? Oh, man. No free pages
340 if(giPhysRangeFree[rangeID] == 0) {
341 Mutex_Release(&glPhysicalPages);
344 Warning(" MM_AllocPhysRange: Out of free pages");
346 "Out of memory (unable to fulfil request for %i pages), zero remaining",
353 // Check if there is enough in the range
354 if(giPhysRangeFree[rangeID] >= Pages)
356 LOG("{%i,0x%x -> 0x%x}",
357 giPhysRangeFree[rangeID],
358 giPhysRangeFirst[rangeID], giPhysRangeLast[rangeID]
360 // Do a cheap scan, scanning upwards from the first free page in
363 addr = giPhysRangeFirst[ rangeID ];
364 while( addr <= giPhysRangeLast[ rangeID ] )
366 //Log(" MM_AllocPhysRange: addr = 0x%x", addr);
367 // Check the super bitmap
368 if( gaSuperBitmap[addr >> (6+6)] + 1 == 0 ) {
369 LOG("nFree = %i = 0 (super) (0x%x)", nFree, addr);
371 addr += 1LL << (6+6);
372 addr &= ~0xFFF; // (1LL << 6+6) - 1
375 // Check page block (64 pages)
376 if( gaMainBitmap[addr >> 6] + 1 == 0) {
377 LOG("nFree = %i = 0 (main) (0x%x)", nFree, addr);
383 // Check individual page
384 if( gaMainBitmap[addr >> 6] & (1LL << (addr & 63)) ) {
385 LOG("nFree = %i = 0 (page) (0x%x)", nFree, addr);
392 LOG("nFree(%i) == %i (0x%x)", nFree, Pages, addr);
396 LOG("nFree = %i", nFree);
397 // If we don't find a contiguous block, nFree will not be equal
398 // to Num, so we set it to zero and do the expensive lookup.
399 if(nFree != Pages) nFree = 0;
404 // Oops. ok, let's do an expensive check (scan down the list
405 // until a free range is found)
407 addr = giPhysRangeLast[ rangeID ];
409 Mutex_Release(&glPhysicalPages);
412 Warning(" MM_AllocPhysRange: Out of memory (unable to fulfil request for %i pages)", Pages);
414 "Out of memory (unable to fulfil request for %i pages)",
420 LOG("nFree = %i, addr = 0x%08x", nFree, addr);
422 // Mark pages as allocated
424 for( i = 0; i < Pages; i++, addr++ )
426 gaMainBitmap[addr >> 6] |= 1LL << (addr & 63);
427 rangeID = MM_int_GetRangeID(addr << 12);
428 giPhysRangeFree[ rangeID ] --;
429 LOG("%x == %x", addr, giPhysRangeFirst[ rangeID ]);
430 if(addr == giPhysRangeFirst[ rangeID ])
431 giPhysRangeFirst[ rangeID ] += 1;
433 ret = addr; // Save the return address
435 // Update super bitmap
436 Pages += addr & (64-1);
438 Pages = (Pages + (64-1)) & ~(64-1);
439 for( i = 0; i < Pages/64; i++ )
441 if( gaMainBitmap[ addr >> 6 ] + 1 == 0 )
442 gaSuperBitmap[addr>>12] |= 1LL << ((addr >> 6) & 63);
445 Mutex_Release(&glPhysicalPages);
446 LEAVE('x', ret << 12);
451 * \brief Allocate a single physical page, with no preference as to address
454 tPAddr MM_AllocPhys(void)
458 // Hack to allow allocation during setup
459 for(i = 0; i < NUM_STATIC_ALLOC; i++) {
460 if( gaiStaticAllocPages[i] ) {
461 tPAddr ret = gaiStaticAllocPages[i];
462 gaiStaticAllocPages[i] = 0;
463 Log("MM_AllocPhys: Return %x, static alloc %i", ret, i);
468 return MM_AllocPhysRange(1, -1);
472 * \brief Reference a physical page
474 void MM_RefPhys(tPAddr PAddr)
476 Uint64 page = PAddr >> 12;
478 if( PAddr >> 12 > giMaxPhysPage ) return ;
480 if( gaMainBitmap[ page >> 6 ] & (1LL << (page&63)) )
483 gaMultiBitmap[ page >> 6 ] |= 1LL << (page&63);
484 gaiPageReferences[ page ] ++;
489 gaMainBitmap[page >> 6] |= 1LL << (page&63);
490 if( gaMainBitmap[page >> 6 ] + 1 == 0 )
491 gaSuperBitmap[page>> 12] |= 1LL << ((page >> 6) & 63);
496 * \brief Dereference a physical page
498 void MM_DerefPhys(tPAddr PAddr)
500 Uint64 page = PAddr >> 12;
502 if( PAddr >> 12 > giMaxPhysPage ) return ;
504 if( gaMultiBitmap[ page >> 6 ] & (1LL << (page&63)) ) {
505 gaiPageReferences[ page ] --;
506 if( gaiPageReferences[ page ] == 1 )
507 gaMultiBitmap[ page >> 6 ] &= ~(1LL << (page&63));
508 if( gaiPageReferences[ page ] == 0 )
509 gaMainBitmap[ page >> 6 ] &= ~(1LL << (page&63));
512 gaMainBitmap[ page >> 6 ] &= ~(1LL << (page&63));
514 // Update the free counts if the page was freed
515 if( !(gaMainBitmap[ page >> 6 ] & (1LL << (page&63))) )
518 rangeID = MM_int_GetRangeID( PAddr );
519 giPhysRangeFree[ rangeID ] ++;
520 if( giPhysRangeFirst[rangeID] > page )
521 giPhysRangeFirst[rangeID] = page;
522 if( giPhysRangeLast[rangeID] < page )
523 giPhysRangeLast[rangeID] = page;
526 // If the bitmap entry is not -1, unset the bit in the super bitmap
527 if(gaMainBitmap[ page >> 6 ] + 1 != 0 ) {
528 gaSuperBitmap[page >> 12] &= ~(1LL << ((page >> 6) & 63));
533 * \brief Takes a physical address and returns the ID of its range
534 * \param Addr Physical address of page
535 * \return Range ID from eMMPhys_Ranges
537 int MM_int_GetRangeID( tPAddr Addr )
542 return MM_PHYS_32BIT;
544 return MM_PHYS_24BIT;
546 return MM_PHYS_20BIT;
548 return MM_PHYS_16BIT;