Update contrib.
1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\memmodel\epoc\multiple\mmu.cpp
21 _LIT(KLitGlobalDollarCode,"GLOBAL$CODE");
23 /*******************************************************************************
24 * "Independent" MMU code
25 *******************************************************************************/
27 void Mmu::Panic(TPanic aPanic)
29 Kern::Fault("MMU",aPanic);
32 TPde* Mmu::LocalPageDir(TInt aOsAsid)
34 __ASSERT_DEBUG(TUint32(aOsAsid)<TUint32(iNumOsAsids),Panic(ELocalPageDirBadAsid));
35 return (TPde*)(iPdeBase+(aOsAsid<<iGlobalPdShift));
38 TPde* Mmu::GlobalPageDir(TInt aOsAsid)
40 __ASSERT_DEBUG(TUint32(aOsAsid)<TUint32(iNumOsAsids),Panic(EGlobalPageDirBadAsid));
41 if (iAsidInfo[aOsAsid]&1)
42 return (TPde*)(iPdeBase+(aOsAsid<<iGlobalPdShift));
43 return (TPde*)iPdeBase;
46 TPde& Mmu::PDE(TLinAddr aAddr, TInt aOsAsid)
48 __ASSERT_DEBUG(TUint32(aOsAsid)<TUint32(iNumOsAsids),Panic(EPDEBadAsid));
49 TPde* p=(TPde*)(iPdeBase+(aOsAsid<<iGlobalPdShift));
50 if (aAddr>=iUserSharedEnd && (iAsidInfo[aOsAsid]&1))
52 p+=(aAddr>>iChunkShift);
53 __KTRACE_OPT(KMMU,Kern::Printf("PDE(%08x,%d) at %08x",aAddr,aOsAsid,p));
57 TInt Mmu::NewOsAsid(TBool aSeparateGlobal)
59 // Allocate a new OS ASID and page directory.
60 // Map the page directory at the expected linear address and initialise it.
63 __KTRACE_OPT(KMMU,Kern::Printf("Mmu::NewOsAsid(%x)",aSeparateGlobal));
64 TInt os_asid=iOsAsidAllocator->Alloc();
69 TInt r=NewPageDirectory(os_asid,aSeparateGlobal,pdPhys,pdPages);
70 __KTRACE_OPT(KMMU,Kern::Printf("NewPageDirectory: %d %08x %d",r,pdPhys,pdPages));
73 iOsAsidAllocator->Free(os_asid);
76 TBool global=(pdPages<<iPageShift==iGlobalPdSize)?1:0;
77 TLinAddr pdLin=iPdeBase+(os_asid<<iGlobalPdShift);
78 if (((os_asid & iAsidGroupMask)==0) && (!iOsAsidAllocator->NotFree(os_asid+1,iAsidGroupMask)) )
80 // expand page directory mapping
81 TInt xptid=AllocPageTable();
84 iRamPageAllocator->FreePhysicalRam(pdPhys,pdPages<<iPageShift);
85 iOsAsidAllocator->Free(os_asid);
88 AssignPageTable(xptid, SPageTableInfo::EGlobal, NULL, pdLin, iPdPdePerm); // map XPT
91 for (i=0; i<pdPages; ++i)
92 MapRamPage(pdLin+(i<<iPageShift), pdPhys+(i<<iPageShift), iPdPtePerm);
93 InitPageDirectory(os_asid, global);
94 iNumGlobalPageDirs+=global;
95 iAsidInfo[os_asid]=global;
96 __KTRACE_OPT(KMMU,Kern::Printf("Mmu::NewOsAsid returns %d (%d)",os_asid,global));
100 void Mmu::FreeOsAsid(TInt aOsAsid)
102 // Free an OS ASID and the corresponding page directory.
103 // Assumes any local PDEs have already been unmapped.
106 __KTRACE_OPT(KMMU,Kern::Printf("Mmu::FreeOsAsid(%d)",aOsAsid));
107 __ASSERT_DEBUG(TUint32(aOsAsid)<TUint32(iNumOsAsids),Panic(EFreeOsAsidBadAsid));
108 TBool global=iAsidInfo[aOsAsid]&1;
109 iAsidInfo[aOsAsid]=0;
110 iOsAsidAllocator->Free(aOsAsid);
111 iNumGlobalPageDirs-=global;
112 TLinAddr pdLin=iPdeBase+(aOsAsid<<iGlobalPdShift);
113 TUint32 size=global?iGlobalPdSize:iLocalPdSize;
114 UnmapAndFree(pdLin,size>>iPageShift);
115 #ifdef BTRACE_KERNEL_MEMORY
116 BTrace4(BTrace::EKernelMemory, BTrace::EKernelMemoryMiscFree, size);
117 Epoc::KernelMiscPages -= size>>iPageShift;
119 TInt asid_group=aOsAsid&~iAsidGroupMask;
120 if (!iOsAsidAllocator->NotFree(asid_group,iAsidGroupSize))
122 // shrink page directory mapping
123 TInt xptid=PageTableId(pdLin,0);
124 DoUnassignPageTable(pdLin, GLOBAL_MAPPING);
125 FreePageTable(xptid);
129 TPhysAddr Mmu::LinearToPhysical(TLinAddr aLinAddr)
131 // Find the physical address corresponding to a given linear address
132 // Call with system locked
135 DMemModelProcess* pP=(DMemModelProcess*)TheCurrentThread->iOwningProcess;
136 return LinearToPhysical(aLinAddr, pP->iOsAsid);
139 TInt Mmu::LinearToPhysical(TLinAddr aLinAddr, TInt aSize, TPhysAddr& aPhysicalAddress, TPhysAddr* aPhysicalPageList)
141 DMemModelProcess* pP=(DMemModelProcess*)TheCurrentThread->iOwningProcess;
142 return LinearToPhysical(aLinAddr, aSize, aPhysicalAddress, aPhysicalPageList, pP->iOsAsid);
145 TInt Mmu::PageTableId(TLinAddr aAddr)
147 DMemModelProcess* pP=(DMemModelProcess*)TheCurrentThread->iOwningProcess;
148 return PageTableId(aAddr, pP->iOsAsid);
153 __KTRACE_OPT2(KBOOT,KMMU,Kern::Printf("Mmu::Init1"));
154 iMaxPageTables=65535; // possibly reduced when RAM size known
155 memclr(iAsidInfo, iNumOsAsids*sizeof(TUint32));
159 void Mmu::CreateUserGlobalSection(TLinAddr aBase, TLinAddr aEnd)
161 iUserGlobalSection=TLinearSection::New(aBase, aEnd);
162 __ASSERT_ALWAYS(iUserGlobalSection,Panic(ECreateUserGlobalSectionFailed));
167 __KTRACE_OPT2(KBOOT,KMMU,Kern::Printf("Mmu::DoInit2"));
168 iSharedSection=TLinearSection::New(iUserSharedBase, iUserSharedEnd);
169 __ASSERT_ALWAYS(iSharedSection,Panic(ECreateSharedSectionFailed));
170 iOsAsidAllocator=TBitMapAllocator::New(iNumOsAsids,ETrue);
171 __ASSERT_ALWAYS(iOsAsidAllocator,Panic(EOsAsidAllocCreateFailed));
172 iOsAsidAllocator->Alloc(0,1); // 0=kernel process
173 DMemModelProcess* pP=(DMemModelProcess*)K::TheKernelProcess;
175 pP->iLocalPageDir=LinearToPhysical(TLinAddr(LocalPageDir(0)));
176 pP->iGlobalPageDir=LinearToPhysical(TLinAddr(GlobalPageDir(0)));
177 __KTRACE_OPT(KMMU,Kern::Printf("Kernel process: LPD=%08x GPD=%08x",pP->iLocalPageDir,pP->iGlobalPageDir));
178 MM::UserCodeAllocator=TBitMapAllocator::New(iMaxUserCodeSize>>iAliasShift, ETrue); // code is aligned to alias size
179 __ASSERT_ALWAYS(MM::UserCodeAllocator,Panic(EUserCodeAllocatorCreateFailed));
180 MM::DllDataAllocator=TBitMapAllocator::New(iMaxDllDataSize>>iPageShift, ETrue);
181 __ASSERT_ALWAYS(MM::DllDataAllocator,Panic(EDllDataAllocatorCreateFailed));
182 __ASSERT_ALWAYS(TheRomHeader().iUserDataAddress==iDllDataBase+iMaxDllDataSize,Panic(ERomUserDataAddressInvalid));
183 __ASSERT_ALWAYS((TheRomHeader().iTotalUserDataSize&iPageMask)==0,Panic(ERomUserDataSizeInvalid));
184 TInt rom_dll_pages=TheRomHeader().iTotalUserDataSize>>iPageShift;
185 __KTRACE_OPT2(KBOOT,KMMU,Kern::Printf("UserCodeAllocator @ %08x DllDataAllocator @ %08x, %d ROM DLL Data Pages",
186 MM::UserCodeAllocator, MM::DllDataAllocator, rom_dll_pages));
188 MM::DllDataAllocator->Alloc(0, rom_dll_pages); // low bit numbers represent high addresses
191 void Mmu::SetupInitialPageInfo(SPageInfo* aPageInfo, TLinAddr aChunkAddr, TInt aPdeIndex)
193 __ASSERT_ALWAYS(aChunkAddr>=iUserSharedEnd,Panic(EBadInitialPageAddr));
194 TLinAddr addr=aChunkAddr+(aPdeIndex<<iPageShift);
195 if (aPageInfo->Type()!=SPageInfo::EUnused)
196 return; // already set (page table)
197 if (addr==(TLinAddr)iPtInfo)
199 aPageInfo->SetPtInfo(0);
202 else if (addr>=iPdeBase && addr<iPdeBase+iGlobalPdSize)
204 aPageInfo->SetPageDir(0,aPdeIndex);
208 aPageInfo->SetFixed();
211 void Mmu::SetupInitialPageTableInfo(TInt aId, TLinAddr aChunkAddr, TInt aNumPtes)
213 __ASSERT_ALWAYS(aChunkAddr>=iUserSharedEnd || aChunkAddr==0,Panic(EBadInitialPageAddr));
214 SPageTableInfo& pti=PtInfo(aId);
216 pti.SetGlobal(aChunkAddr>>iChunkShift);
219 void Mmu::AssignPageTable(TInt aId, TInt aUsage, TAny* aObject, TLinAddr aAddr, TPde aPdePerm)
221 __KTRACE_OPT(KMMU,Kern::Printf("Mmu::AssignPageTable id=%d, u=%08x, obj=%08x, addr=%08x, perm=%08x",
222 aId, aUsage, aObject, aAddr, aPdePerm));
223 const TAny* asids=GLOBAL_MAPPING;
224 SPageTableInfo& pti=PtInfo(aId);
227 case SPageTableInfo::EChunk:
229 DMemModelChunk* pC=(DMemModelChunk*)aObject;
230 TUint32 ccp=K::CompressKHeapPtr(pC);
231 TUint32 offset=(aAddr-TLinAddr(pC->iBase))>>iChunkShift;
232 pti.SetChunk(ccp,offset);
235 else if (pC->iOwningProcess)
236 asids=(const TAny*)((DMemModelProcess*)pC->iOwningProcess)->iOsAsid;
239 // case SPageTableInfo::EHwChunk:
241 case SPageTableInfo::EGlobal:
242 pti.SetGlobal(aAddr>>iChunkShift);
245 Panic(EAssignPageTableInvalidUsage);
247 DoAssignPageTable(aId, aAddr, aPdePerm, asids);
250 TInt Mmu::UnassignPageTable(TLinAddr aAddr)
252 __KTRACE_OPT(KMMU,Kern::Printf("Mmu::UnassignPageTable addr=%08x", aAddr));
253 TInt id=PageTableId(aAddr, 0);
255 DoUnassignPageTable(aAddr, GLOBAL_MAPPING);
259 TInt Mmu::CreateGlobalCodeChunk()
261 // Enter and return with neither system lock nor MMU mutex held
264 __KTRACE_OPT(KDLL,Kern::Printf("Mmu::CreateGlobalCodeChunk"));
265 TInt maxsize=Min(TheSuperPage().iTotalRamSize/2, 0x01000000);
268 c.iAtt=TChunkCreate::EDisconnected;
269 c.iForceFixed=EFalse;
270 c.iOperations=SChunkCreateInfo::EAdjust|SChunkCreateInfo::EAdd;
275 c.iName.Set(KLitGlobalDollarCode);
280 return K::TheKernelProcess->NewChunk((DChunk*&)iGlobalCode,c,runAddr);