sl@0: // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32\memmodel\epoc\moving\mdefrag.cpp sl@0: // sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: #include "mmboot.h" sl@0: #include sl@0: #include sl@0: #include "cache_maintenance.h" sl@0: sl@0: /* sl@0: * Move a kernel page from aOld to aNew, updating the page table in aChunk. sl@0: * Enter with system locked, exit with system unlocked (!!) sl@0: * Must hold RAM alloc mutex. sl@0: */ sl@0: TInt Mmu::MoveKernelPage(DChunk* aChunk, TUint32 aOffset, TPhysAddr aOld, TPhysAddr& aNew, TUint aBlockZoneId, TBool aBlockRest) sl@0: { sl@0: __KTRACE_OPT(KMMU,Kern::Printf("Mmu::MoveKernelPage() off=%08x old=%08x",aOffset,aOld)); sl@0: Mmu& m=Mmu::Get(); sl@0: sl@0: // Release the system lock - the kernel chunks can't ever be freed sl@0: // and the ramalloc mutex protects us from decommit. sl@0: NKern::UnlockSystem(); sl@0: sl@0: DMemModelChunk* chunk = (DMemModelChunk*)aChunk; sl@0: sl@0: // Allocate new page, map it uncached but buffered, and find old mapping sl@0: TPhysAddr newPage; sl@0: if (m.AllocRamPages(&newPage, 1, EPageMovable, aBlockZoneId, aBlockRest) != KErrNone) sl@0: return KErrNoMemory; sl@0: TLinAddr vOld = (TLinAddr)chunk->iBase + aOffset; sl@0: TLinAddr vNew = m.MapTemp(newPage, EFalse); sl@0: sl@0: // Find page table for mapping sl@0: TInt ptid=m.GetPageTableId(vOld); sl@0: if(ptid<0) sl@0: Panic(EDefragKernelChunkNoPageTable); sl@0: sl@0: // With the system lock, ask Mmu to remap the page. sl@0: // This will copy and remap it with interrupts disabled, while sl@0: // avoiding touching any cache lines on the heap. sl@0: NKern::LockSystem(); sl@0: m.RemapKernelPage(ptid, vOld, vNew, newPage, chunk->iPtePermissions); sl@0: sl@0: // update new pageinfo, clear old, then done with system lock. sl@0: SPageInfo* oldpi = SPageInfo::FromPhysAddr(aOld); sl@0: SPageInfo* pi = SPageInfo::FromPhysAddr(newPage); sl@0: pi->Set(oldpi->Type(),oldpi->Owner(),oldpi->Offset()); sl@0: oldpi->SetUnused(); sl@0: NKern::UnlockSystem(); sl@0: sl@0: // Remove temporary new page mapping sl@0: m.UnmapTemp(); sl@0: sl@0: // Remove old page from external cache - RemapKernelPage has already removed it from internal cache. sl@0: CacheMaintenance::PageToReusePhysicalCache(aOld); sl@0: sl@0: // Free old page sl@0: #ifdef _DEBUG sl@0: m.ClearPages(1, (TPhysAddr*)(aOld|1)); sl@0: #endif sl@0: m.iRamPageAllocator->FreeRamPage(aOld, EPageMovable); sl@0: sl@0: aNew = newPage; sl@0: return KErrNone; sl@0: } sl@0: sl@0: /* sl@0: * These pages don't exist on moving memory model, no need to move them sl@0: * but this function must exist to make the kernel link. sl@0: */ sl@0: TInt Mmu::MoveCodeSegMemoryPage(DMemModelCodeSegMemory* /*aCodeSegMemory*/, TUint32 /*aOffset*/, TPhysAddr /*aOld*/, sl@0: TPhysAddr& /*aNew*/, TUint /*aBlockZoneId*/, TBool /*aBlockRest*/) sl@0: { sl@0: NKern::UnlockSystem(); sl@0: return KErrNotSupported; sl@0: } sl@0: sl@0: /* sl@0: * Move a code chunk page from aOld to aNew, updating the page table in aChunk. sl@0: * Enter with system locked, exit with system unlocked (!!) sl@0: * Must hold RAM alloc mutex. sl@0: */ sl@0: TInt Mmu::MoveCodeChunkPage(DChunk* aChunk, TUint32 aOffset, TPhysAddr aOld, TPhysAddr& aNew, TUint aBlockZoneId, TBool aBlockRest) sl@0: { sl@0: __KTRACE_OPT(KMMU,Kern::Printf("Mmu::MoveCodeChunkPage() off=%08x old=%08x",aOffset,aOld)); sl@0: Mmu& m=Mmu::Get(); sl@0: sl@0: // look up the code seg that corresponds to this page sl@0: TLinAddr aLinearAddress = (TLinAddr)(aChunk->Base() + aOffset); sl@0: DMemModelCodeSeg* codeseg = (DMemModelCodeSeg*)DCodeSeg::CodeSegsByAddress.Find(aLinearAddress); sl@0: sl@0: // if the code seg is not done loading yet, we can't move it the easy way sl@0: // also, if it's being unloaded the codeseg will have gone. sl@0: if (!codeseg || !(codeseg->iMark & DCodeSeg::EMarkLoaded)) sl@0: { sl@0: NKern::UnlockSystem(); sl@0: return KErrInUse; sl@0: } sl@0: sl@0: // Release system lock as page can't be decommitted while we hold ramalloc mutex sl@0: NKern::UnlockSystem(); sl@0: sl@0: // Allocate new page, map it uncached but buffered, and find old mapping sl@0: TPhysAddr newPage; sl@0: if (m.AllocRamPages(&newPage, 1, EPageMovable, aBlockZoneId, aBlockRest) != KErrNone) sl@0: return KErrNoMemory; sl@0: TLinAddr vOld = aLinearAddress; sl@0: TLinAddr vNew = m.MapTemp(newPage, EFalse); sl@0: sl@0: // Copy the page and remap it sl@0: pagecpy((TAny*)vNew, (TAny*)vOld); sl@0: NKern::LockSystem(); sl@0: // Substitute drains the write buffer for us during the remap. sl@0: aChunk->Substitute(aOffset, aOld, newPage); sl@0: NKern::UnlockSystem(); sl@0: sl@0: // Remove temporary new page mapping sl@0: m.UnmapTemp(); sl@0: sl@0: // Remove old page from physical cache sl@0: CacheMaintenance::PageToReusePhysicalCache(aOld); sl@0: sl@0: // Free old page sl@0: #ifdef _DEBUG sl@0: m.ClearPages(1, (TPhysAddr*)(aOld|1)); sl@0: #endif sl@0: m.iRamPageAllocator->FreeRamPage(aOld, EPageMovable); sl@0: sl@0: aNew = newPage; sl@0: return KErrNone; sl@0: } sl@0: sl@0: /* sl@0: * Move a data chunk page from aOld to aNew, updating the page table in aChunk. sl@0: * Enter with system locked, exit with system unlocked (!!) sl@0: * Must hold RAM alloc mutex. sl@0: */ sl@0: TInt Mmu::MoveDataChunkPage(DChunk* aChunk, TUint32 aOffset, TPhysAddr aOld, TPhysAddr& aNew, TUint aBlockZoneId, TBool aBlockRest) sl@0: { sl@0: __KTRACE_OPT(KMMU,Kern::Printf("Mmu::MoveDataChunkPage() off=%08x old=%08x",aOffset,aOld)); sl@0: Mmu& m=Mmu::Get(); sl@0: TInt r; sl@0: sl@0: // Release system lock as page can't be decommitted while we hold ramalloc mutex sl@0: NKern::UnlockSystem(); sl@0: sl@0: // Allocate new page, map it uncached but buffered sl@0: TPhysAddr newPage; sl@0: if (m.AllocRamPages(&newPage, 1, EPageMovable, aBlockZoneId, aBlockRest) != KErrNone) sl@0: return KErrNoMemory; sl@0: TLinAddr vNew = m.MapTemp(newPage, EFalse); sl@0: sl@0: // Mark the PTE as inaccessible to avoid the data being overwritten while we copy sl@0: // This also takes care of the cache requirements to alias the page elsewhere, sl@0: // since it can't be copied from an inaccessible PTE sl@0: DisablePageModification((DMemModelChunk*)aChunk, aOffset); sl@0: TLinAddr vOldAlias = m.MapSecondTemp(aOld, ETrue); sl@0: sl@0: // Copy the page's contents and remap its PTE sl@0: pagecpy((TAny*)vNew, (TAny*)vOldAlias); sl@0: NKern::LockSystem(); sl@0: if (iDisabledPte != NULL) sl@0: { sl@0: // Access wasn't reenabled, so we can continue sl@0: aChunk->Substitute(aOffset, aOld, newPage); sl@0: iDisabledAddr = 0; sl@0: iDisabledPte = NULL; sl@0: iDisabledOldVal = 0; sl@0: r = KErrNone; sl@0: } sl@0: else sl@0: r = KErrInUse; sl@0: NKern::UnlockSystem(); sl@0: sl@0: // Remove temporary page mappings sl@0: CacheMaintenance::PageToReuseVirtualCache(vOldAlias); sl@0: m.UnmapTemp(); sl@0: m.UnmapSecondTemp(); sl@0: sl@0: if (r == KErrNone) sl@0: { sl@0: // Remove old page from physical cache - DisablePageModification removed it from L1 already sl@0: CacheMaintenance::PageToReusePhysicalCache(aOld); sl@0: } sl@0: sl@0: if (r == KErrNone) sl@0: { sl@0: // Free old page sl@0: #ifdef _DEBUG sl@0: m.ClearPages(1, (TPhysAddr*)(aOld|1)); sl@0: #endif sl@0: m.iRamPageAllocator->FreeRamPage(aOld, EPageMovable); sl@0: aNew = newPage; sl@0: } sl@0: else sl@0: { sl@0: // Free new page sl@0: m.iRamPageAllocator->FreeRamPage(newPage, EPageMovable); sl@0: } sl@0: sl@0: return r; sl@0: }