+ tMBoot_MMapEnt *mmapStart;
+ tMBoot_MMapEnt *ent;
+ Uint64 maxAddr = 0;
+ int numPages, superPages;
+ int i;
+ Uint64 base, size;
+ tVAddr vaddr;
+ tPAddr paddr, firstFreePage;
+
+ ENTER("pMBoot=%p", MBoot);
+
+ // Scan the physical memory map
+ // Looking for the top of physical memory
+ mmapStart = (void *)( KERNEL_BASE | MBoot->MMapAddr );
+ LOG("mmapStart = %p", mmapStart);
+ ent = mmapStart;
+ while( (Uint)ent < (Uint)mmapStart + MBoot->MMapLength )
+ {
+ // Adjust for the size of the entry
+ ent->Size += 4;
+ LOG("ent={Type:%i,Base:0x%x,Length:%x",
+ ent->Type, ent->Base, ent->Length);
+
+ // If entry is RAM and is above `maxAddr`, change `maxAddr`
+ if(ent->Type == 1 && ent->Base + ent->Length > maxAddr)
+ maxAddr = ent->Base + ent->Length;
+
+ // Go to next entry
+ ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size );
+ }
+
+ // Did we find a valid end?
+ if(maxAddr == 0) {
+ // No, darn, let's just use the HighMem hack
+ giMaxPhysPage = (MBoot->HighMem >> 2) + 256; // HighMem is a kByte value
+ }
+ else {
+ // Goodie, goodie gumdrops
+ giMaxPhysPage = maxAddr >> 12;
+ }
+ LOG("giMaxPhysPage = 0x%x", giMaxPhysPage);
+
+ // Find a contigous section of memory to hold it in
+ // - Starting from the end of the kernel
+ // - We also need a region for the super bitmap
+ superPages = ((giMaxPhysPage+64*8-1)/(64*8) + 0xFFF) >> 12;
+ numPages = (giMaxPhysPage + 7) / 8;
+ numPages = (numPages + 0xFFF) >> 12;
+ LOG("numPages = %i, superPages = %i", numPages, superPages);
+ if(maxAddr == 0)
+ {
+ int todo = numPages*2 + superPages;
+ // Ok, naieve allocation, just put it after the kernel
+ // - Allocated Bitmap
+ vaddr = MM_PAGE_BITMAP;
+ paddr = (tPAddr)&gKernelEnd - KERNEL_BASE;
+ while(todo )
+ {
+ // Allocate statics
+ for( i = 0; i < NUM_STATIC_ALLOC; i++) {
+ if(gaiStaticAllocPages[i] != 0) continue;
+ gaiStaticAllocPages[i] = paddr;
+ paddr += 0x1000;
+ }
+
+ MM_Map(vaddr, paddr);
+ vaddr += 0x1000;
+ paddr += 0x1000;
+
+ todo --;
+
+ if( todo == numPages + superPages )
+ vaddr = MM_PAGE_DBLBMP;
+ if( todo == superPages )
+ vaddr = MM_PAGE_SUPBMP;
+ }
+ }
+ // Scan for a nice range
+ else
+ {
+ int todo = numPages*2 + superPages;
+ paddr = 0;
+ vaddr = MM_PAGE_BITMAP;
+ // Scan!
+ for(
+ ent = mmapStart;
+ (Uint)ent < (Uint)mmapStart + MBoot->MMapLength;
+ ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size )
+ )
+ {
+ int avail;
+
+ // RAM only please
+ if( ent->Type != 1 )
+ continue;
+
+ // Let's not put it below the kernel, shall we?
+ if( ent->Base + ent->Size < (tPAddr)&gKernelBase )
+ continue;
+
+ LOG("%x <= %x && %x > %x",
+ ent->Base, (tPAddr)&gKernelBase,
+ ent->Base + ent->Size, (tPAddr)&gKernelEnd - KERNEL_BASE
+ );
+ // Check if the kernel is in this range
+ if( ent->Base <= (tPAddr)&gKernelBase
+ && ent->Base + ent->Length > (tPAddr)&gKernelEnd - KERNEL_BASE )
+ {
+ avail = ent->Length >> 12;
+ avail -= ((tPAddr)&gKernelEnd - KERNEL_BASE - ent->Base) >> 12;
+ paddr = (tPAddr)&gKernelEnd - KERNEL_BASE;
+ }
+ // No? then we can use all of the block
+ else
+ {
+ avail = ent->Length >> 12;
+ paddr = ent->Base;
+ }
+
+ Log(" MM_InitPhys_Multiboot: paddr=0x%x, avail=%i", paddr, avail);
+
+ // Map
+ while( todo && avail --)
+ {
+ // Static Allocations
+ for( i = 0; i < NUM_STATIC_ALLOC && avail; i++) {
+ if(gaiStaticAllocPages[i] != 0) continue;
+ gaiStaticAllocPages[i] = paddr;
+ paddr += 0x1000;
+ avail --;
+ }
+ if(!avail) break;
+
+ // Map
+ MM_Map(vaddr, paddr);
+ todo --;
+ vaddr += 0x1000;
+ paddr += 0x1000;
+
+ // Alter the destination address when needed
+ if(todo == superPages+numPages)
+ vaddr = MM_PAGE_DBLBMP;
+ if(todo == superPages)
+ vaddr = MM_PAGE_SUPBMP;
+ }
+
+ // Fast quit if there's nothing left to allocate
+ if( !todo ) break;
+ }
+ }
+ // Save the current value of paddr to simplify the allocation later
+ firstFreePage = paddr;
+
+ LOG("Clearing multi bitmap");
+ // Fill the bitmaps
+ memset(gaMultiBitmap, 0, numPages<<12);
+ // - initialise to one, then clear the avaliable areas
+ memset(gaMainBitmap, -1, numPages<<12);
+ LOG("Setting main bitmap");
+ // - Clear all Type=1 areas
+ LOG("Clearing valid regions");
+ for(
+ ent = mmapStart;
+ (Uint)ent < (Uint)mmapStart + MBoot->MMapLength;
+ ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size )
+ )
+ {
+ // Check if the type is RAM
+ if(ent->Type != 1) continue;
+
+ // Main bitmap
+ base = ent->Base >> 12;
+ size = ent->Size >> 12;
+
+ if(base & 63) {
+ Uint64 val = -1L << (base & 63);
+ gaMainBitmap[base / 64] &= ~val;
+ size -= (base & 63);
+ base += 64 - (base & 63);
+ }
+ memset( &gaMainBitmap[base / 64], 0, size/8 );
+ if( size & 7 ) {
+ Uint64 val = -1L << (size & 7);
+ val <<= (size/8)&7;
+ gaMainBitmap[base / 64] &= ~val;
+ }
+
+ // Super Bitmap
+ base = ent->Base >> 12;
+ size = ent->Size >> 12;
+ size = (size + (base & 63) + 63) >> 6;
+ base = base >> 6;
+ if(base & 63) {
+ Uint64 val = -1L << (base & 63);
+ gaSuperBitmap[base / 64] &= ~val;
+ size -= (base & 63);
+ base += 64 - (base & 63);
+ }
+ }
+
+ // Reference the used pages
+ base = (tPAddr)&gKernelBase >> 12;
+ size = firstFreePage >> 12;
+ memset( &gaMainBitmap[base / 64], -1, size/8 );
+ if( size & 7 ) {
+ Uint64 val = -1L << (size & 7);
+ val <<= (size/8)&7;
+ gaMainBitmap[base / 64] |= val;
+ }
+
+ // Free the unused static allocs
+ for( i = 0; i < NUM_STATIC_ALLOC; i++) {
+ if(gaiStaticAllocPages[i] != 0)
+ continue;
+ gaMainBitmap[ gaiStaticAllocPages[i] >> (12+6) ]
+ &= ~(1L << ((gaiStaticAllocPages[i]>>12)&63));
+ }
+
+ // Fill the super bitmap
+ LOG("Filling super bitmap");
+ memset(gaSuperBitmap, 0, superPages<<12);
+ for( base = 0; base < (size+63)/64; base ++)
+ {
+ if( gaMainBitmap[ base ] + 1 == 0 )
+ gaSuperBitmap[ base/64 ] |= 1L << (base&63);
+ }
+
+ // Set free page counts
+ for( base = 1; base < giMaxPhysPage; base ++ )
+ {
+ int rangeID;
+ // Skip allocated
+ if( gaMainBitmap[ base >> 6 ] & (1L << (base&63)) ) continue;
+
+ // Get range ID
+ rangeID = MM_int_GetRangeID( base << 12 );
+
+ // Increment free page count
+ giPhysRangeFree[ rangeID ] ++;
+
+ // Check for first free page in range
+ if(giPhysRangeFirst[ rangeID ] == 0)
+ giPhysRangeFirst[ rangeID ] = base;
+ // Set last (when the last free page is reached, this won't be
+ // updated anymore, hence will be correct)
+ giPhysRangeLast[ rangeID ] = base;
+ }
+
+ LEAVE('-');