X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Farch%2Fx86_64%2Fmm_phys.c;h=0130773f122516fc56beccc20bb20e7777a03194;hb=05e8b329edc7c55eec967c3caba1982c173025e3;hp=1e7c4c2a7ab6e731802b991bee9b784dd281c7f4;hpb=423a80d361af804155c2918a477850a49ce41869;p=tpg%2Facess2.git diff --git a/Kernel/arch/x86_64/mm_phys.c b/Kernel/arch/x86_64/mm_phys.c index 1e7c4c2a..0130773f 100644 --- a/Kernel/arch/x86_64/mm_phys.c +++ b/Kernel/arch/x86_64/mm_phys.c @@ -19,8 +19,8 @@ enum eMMPhys_Ranges }; // === IMPORTS === -extern void gKernelBase; -extern void gKernelEnd; +extern char gKernelBase[]; +extern char gKernelEnd[]; // === PROTOTYPES === void MM_InitPhys_Multiboot(tMBoot_Info *MBoot); @@ -31,10 +31,10 @@ void MM_DerefPhys(tPAddr PAddr); int MM_int_GetRangeID( tPAddr Addr ); // === GLOBALS === -tSpinlock glPhysicalPages; -Uint64 *gaSuperBitmap; // 1 bit = 64 Pages, 16 MiB Per Word -Uint64 *gaMainBitmap; // 1 bit = 1 Page, 256 KiB per Word -Uint64 *gaMultiBitmap; // Each bit means that the page is being used multiple times +tMutex glPhysicalPages; +Uint64 *gaSuperBitmap = (void*)MM_PAGE_SUPBMP; // 1 bit = 64 Pages, 16 MiB Per Word +Uint64 *gaMainBitmap = (void*)MM_PAGE_BITMAP; // 1 bit = 1 Page, 256 KiB per Word +Uint64 *gaMultiBitmap = (void*)MM_PAGE_DBLBMP; // Each bit means that the page is being used multiple times Uint32 *gaiPageReferences = (void*)MM_PAGE_COUNTS; // Reference Counts tPAddr giFirstFreePage; // First possibly free page Uint64 giPhysRangeFree[NUM_MM_PHYS_RANGES]; // Number of free pages in each range @@ -208,9 +208,10 @@ void MM_InitPhys_Multiboot(tMBoot_Info *MBoot) LOG("Clearing multi bitmap"); // Fill the bitmaps - memset(gaMultiBitmap, 0, numPages<<12); + memset(gaMultiBitmap, 0, (numPages<<12)/8); // - initialise to one, then clear the avaliable areas - memset(gaMainBitmap, -1, numPages<<12); + memset(gaMainBitmap, -1, (numPages<<12)/8); + memset(gaSuperBitmap, -1, (numPages<<12)/(8*64)); LOG("Setting main bitmap"); // - Clear all Type=1 areas LOG("Clearing valid regions"); @@ -228,14 +229,14 @@ void MM_InitPhys_Multiboot(tMBoot_Info *MBoot) size = ent->Size >> 12; if(base & 63) { - Uint64 val = -1 << (base & 63); + Uint64 val = -1LL << (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 = -1 << (size & 7); + Uint64 val = -1LL << (size & 7); val <<= (size/8)&7; gaMainBitmap[base / 64] &= ~val; } @@ -246,7 +247,7 @@ void MM_InitPhys_Multiboot(tMBoot_Info *MBoot) size = (size + (base & 63) + 63) >> 6; base = base >> 6; if(base & 63) { - Uint64 val = -1 << (base & 63); + Uint64 val = -1LL << (base & 63); gaSuperBitmap[base / 64] &= ~val; size -= (base & 63); base += 64 - (base & 63); @@ -258,7 +259,7 @@ void MM_InitPhys_Multiboot(tMBoot_Info *MBoot) size = firstFreePage >> 12; memset( &gaMainBitmap[base / 64], -1, size/8 ); if( size & 7 ) { - Uint64 val = -1 << (size & 7); + Uint64 val = -1LL << (size & 7); val <<= (size/8)&7; gaMainBitmap[base / 64] |= val; } @@ -268,7 +269,7 @@ void MM_InitPhys_Multiboot(tMBoot_Info *MBoot) if(gaiStaticAllocPages[i] != 0) continue; gaMainBitmap[ gaiStaticAllocPages[i] >> (12+6) ] - &= ~(1 << ((gaiStaticAllocPages[i]>>12)&63)); + &= ~(1LL << ((gaiStaticAllocPages[i]>>12)&63)); } // Fill the super bitmap @@ -276,8 +277,8 @@ void MM_InitPhys_Multiboot(tMBoot_Info *MBoot) memset(gaSuperBitmap, 0, superPages<<12); for( base = 0; base < (size+63)/64; base ++) { - if( gaMainBitmap[ base ] == -1 ) - gaSuperBitmap[ base/64 ] |= 1 << (base&63); + if( gaMainBitmap[ base ] + 1 == 0 ) + gaSuperBitmap[ base/64 ] |= 1LL << (base&63); } // Set free page counts @@ -285,7 +286,7 @@ void MM_InitPhys_Multiboot(tMBoot_Info *MBoot) { int rangeID; // Skip allocated - if( gaMainBitmap[ base >> 6 ] & (1 << (base&63)) ) continue; + if( gaMainBitmap[ base >> 6 ] & (1LL << (base&63)) ) continue; // Get range ID rangeID = MM_int_GetRangeID( base << 12 ); @@ -306,28 +307,28 @@ void MM_InitPhys_Multiboot(tMBoot_Info *MBoot) /** * \brief Allocate a contiguous range of physical pages with a maximum - * bit size of \a Bits - * \param Num Number of pages to allocate - * \param Bits Maximum size of the physical address - * \note If \a Bits is <= 0, any sized address is used (with preference + * bit size of \a MaxBits + * \param Pages Number of pages to allocate + * \param MaxBits Maximum size of the physical address + * \note If \a MaxBits is <= 0, any sized address is used (with preference * to higher addresses) */ -tPAddr MM_AllocPhysRange(int Num, int Bits) +tPAddr MM_AllocPhysRange(int Pages, int MaxBits) { - tPAddr addr; + tPAddr addr, ret; int rangeID; int nFree = 0, i; - ENTER("iNum iBits", Num, Bits); + ENTER("iPages iBits", Pages, MaxBits); - if( Bits <= 0 || Bits >= 64 ) // Speedup for the common case + if( MaxBits <= 0 || MaxBits >= 64 ) // Speedup for the common case rangeID = MM_PHYS_MAX; else - rangeID = MM_int_GetRangeID( (1 << Bits) -1 ); + rangeID = MM_int_GetRangeID( (1LL << MaxBits) - 1 ); LOG("rangeID = %i", rangeID); - LOCK(&glPhysicalPages); + Mutex_Acquire(&glPhysicalPages); // Check if the range actually has any free pages while(giPhysRangeFree[rangeID] == 0 && rangeID) @@ -337,20 +338,20 @@ tPAddr MM_AllocPhysRange(int Num, int Bits) // What the? Oh, man. No free pages if(giPhysRangeFree[rangeID] == 0) { - RELEASE(&glPhysicalPages); + Mutex_Release(&glPhysicalPages); // TODO: Page out // ATM. Just Warning Warning(" MM_AllocPhysRange: Out of free pages"); Log_Warning("Arch", "Out of memory (unable to fulfil request for %i pages), zero remaining", - Num + Pages ); LEAVE('i', 0); return 0; } // Check if there is enough in the range - if(giPhysRangeFree[rangeID] >= Num) + if(giPhysRangeFree[rangeID] >= Pages) { LOG("{%i,0x%x -> 0x%x}", giPhysRangeFree[rangeID], @@ -364,23 +365,23 @@ tPAddr MM_AllocPhysRange(int Num, int Bits) { //Log(" MM_AllocPhysRange: addr = 0x%x", addr); // Check the super bitmap - if( gaSuperBitmap[addr >> (6+6)] == -1 ) { + if( gaSuperBitmap[addr >> (6+6)] + 1 == 0 ) { LOG("nFree = %i = 0 (super) (0x%x)", nFree, addr); nFree = 0; - addr += 1 << (6+6); - addr &= (1 << (6+6)) - 1; + addr += 1LL << (6+6); + addr &= ~0xFFF; // (1LL << 6+6) - 1 continue; } // Check page block (64 pages) - if( gaSuperBitmap[addr >> (6+6)] & (1 << (addr>>6)&63)) { + if( gaMainBitmap[addr >> 6] + 1 == 0) { LOG("nFree = %i = 0 (main) (0x%x)", nFree, addr); nFree = 0; - addr += 1 << (12+6); - addr &= (1 << (12+6)) - 1; + addr += 1LL << (6); + addr &= ~0x3F; continue; } // Check individual page - if( gaMainBitmap[addr >> 6] & (1 << (addr & 63)) ) { + if( gaMainBitmap[addr >> 6] & (1LL << (addr & 63)) ) { LOG("nFree = %i = 0 (page) (0x%x)", nFree, addr); nFree = 0; addr ++; @@ -388,14 +389,14 @@ tPAddr MM_AllocPhysRange(int Num, int Bits) } nFree ++; addr ++; - LOG("nFree(%i) == %i (0x%x)", nFree, Num, addr); + LOG("nFree(%i) == %i (0x%x)", nFree, Pages, addr); if(nFree == Num) break; } LOG("nFree = %i", nFree); // If we don't find a contiguous block, nFree will not be equal // to Num, so we set it to zero and do the expensive lookup. - if(nFree != Num) nFree = 0; + if(nFree != Pages) nFree = 0; } if( !nFree ) @@ -405,7 +406,7 @@ tPAddr MM_AllocPhysRange(int Num, int Bits) nFree = 1; addr = giPhysRangeLast[ rangeID ]; // TODO - RELEASE(&glPhysicalPages); + Mutex_Release(&glPhysicalPages); // TODO: Page out // ATM. Just Warning Warning(" MM_AllocPhysRange: Out of memory (unable to fulfil request for %i pages)", Num); @@ -419,27 +420,31 @@ tPAddr MM_AllocPhysRange(int Num, int Bits) LOG("nFree = %i, addr = 0x%08x", nFree, addr); // Mark pages as allocated - addr -= Num; - for( i = 0; i < Num; i++ ) + addr -= Pages; + for( i = 0; i < Pages; i++, addr++ ) { - gaMainBitmap[addr >> 6] |= 1 << (addr & 63); - rangeID = MM_int_GetRangeID(addr); + gaMainBitmap[addr >> 6] |= 1LL << (addr & 63); + rangeID = MM_int_GetRangeID(addr << 12); giPhysRangeFree[ rangeID ] --; + LOG("%x == %x", addr, giPhysRangeFirst[ rangeID ]); + if(addr == giPhysRangeFirst[ rangeID ]) + giPhysRangeFirst[ rangeID ] += 1; } + ret = addr; // Save the return address // Update super bitmap - Num += addr & (64-1); + Pages += addr & (64-1); addr &= ~(64-1); - Num = (Num + (64-1)) & ~(64-1); - for( i = 0; i < Num/64; i++ ) + Pages = (Pages + (64-1)) & ~(64-1); + for( i = 0; i < Pages/64; i++ ) { - if( gaMainBitmap[ addr >> 6 ] == -1 ) - gaSuperBitmap[addr>>12] |= 1 << ((addr >> 6) & 64); + if( gaMainBitmap[ addr >> 6 ] + 1 == 0 ) + gaSuperBitmap[addr>>12] |= 1LL << ((addr >> 6) & 63); } - RELEASE(&glPhysicalPages); - LEAVE('x', addr << 12); - return addr << 12; + Mutex_Release(&glPhysicalPages); + LEAVE('x', ret << 12); + return ret << 12; } /** @@ -472,18 +477,18 @@ void MM_RefPhys(tPAddr PAddr) if( PAddr >> 12 > giMaxPhysPage ) return ; - if( gaMainBitmap[ page >> 6 ] & (1 << (page&63)) ) + if( gaMainBitmap[ page >> 6 ] & (1LL << (page&63)) ) { // Reference again - gaMultiBitmap[ page >> 6 ] |= 1 << (page&63); + gaMultiBitmap[ page >> 6 ] |= 1LL << (page&63); gaiPageReferences[ page ] ++; } else { // Allocate - gaMainBitmap[page >> 6] |= 1 << (page&63); - if( gaMainBitmap[page >> 6 ] == -1 ) - gaSuperBitmap[page>> 12] |= 1 << ((page >> 6) & 63); + gaMainBitmap[page >> 6] |= 1LL << (page&63); + if( gaMainBitmap[page >> 6 ] + 1 == 0 ) + gaSuperBitmap[page>> 12] |= 1LL << ((page >> 6) & 63); } } @@ -496,26 +501,31 @@ void MM_DerefPhys(tPAddr PAddr) if( PAddr >> 12 > giMaxPhysPage ) return ; - if( gaMultiBitmap[ page >> 6 ] & (1 << (page&63)) ) { + if( gaMultiBitmap[ page >> 6 ] & (1LL << (page&63)) ) { gaiPageReferences[ page ] --; if( gaiPageReferences[ page ] == 1 ) - gaMultiBitmap[ page >> 6 ] &= ~(1 << (page&63)); + gaMultiBitmap[ page >> 6 ] &= ~(1LL << (page&63)); if( gaiPageReferences[ page ] == 0 ) - gaMainBitmap[ page >> 6 ] &= ~(1 << (page&63)); + gaMainBitmap[ page >> 6 ] &= ~(1LL << (page&63)); } else - gaMainBitmap[ page >> 6 ] &= ~(1 << (page&63)); + gaMainBitmap[ page >> 6 ] &= ~(1LL << (page&63)); - // TODO: Update free counts - if( !(gaMainBitmap[ page >> 6 ] & (1 << (page&63))) ) + // Update the free counts if the page was freed + if( !(gaMainBitmap[ page >> 6 ] & (1LL << (page&63))) ) { int rangeID; rangeID = MM_int_GetRangeID( PAddr ); giPhysRangeFree[ rangeID ] ++; + if( giPhysRangeFirst[rangeID] > page ) + giPhysRangeFirst[rangeID] = page; + if( giPhysRangeLast[rangeID] < page ) + giPhysRangeLast[rangeID] = page; } - if(gaMainBitmap[ page >> 6 ] == 0) { - gaSuperBitmap[page >> 12] &= ~(1 << ((page >> 6) & 63)); + // If the bitmap entry is not -1, unset the bit in the super bitmap + if(gaMainBitmap[ page >> 6 ] + 1 != 0 ) { + gaSuperBitmap[page >> 12] &= ~(1LL << ((page >> 6) & 63)); } }