sl@0
|
1 |
// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
//
|
sl@0
|
15 |
|
sl@0
|
16 |
#include <x86_mem.h>
|
sl@0
|
17 |
#include "cache_maintenance.inl"
|
sl@0
|
18 |
#include "execs.h"
|
sl@0
|
19 |
#include "mm.h"
|
sl@0
|
20 |
#include "mmu.h"
|
sl@0
|
21 |
#include "mpager.h"
|
sl@0
|
22 |
#include "mpdalloc.h"
|
sl@0
|
23 |
|
sl@0
|
24 |
|
sl@0
|
25 |
TPte PteGlobal; // =0x100 on processors which support global pages, 0 on processors which don't
|
sl@0
|
26 |
|
sl@0
|
27 |
#if defined(KMMU)
|
sl@0
|
28 |
extern "C" void __DebugMsgFlushTLB()
|
sl@0
|
29 |
{
|
sl@0
|
30 |
__KTRACE_OPT(KMMU,Kern::Printf("FlushTLB"));
|
sl@0
|
31 |
}
|
sl@0
|
32 |
|
sl@0
|
33 |
extern "C" void __DebugMsgLocalFlushTLB()
|
sl@0
|
34 |
{
|
sl@0
|
35 |
__KTRACE_OPT(KMMU,Kern::Printf("FlushTLB"));
|
sl@0
|
36 |
}
|
sl@0
|
37 |
|
sl@0
|
38 |
extern "C" void __DebugMsgINVLPG(int a)
|
sl@0
|
39 |
{
|
sl@0
|
40 |
__KTRACE_OPT(KMMU,Kern::Printf("INVLPG(%08x)",a));
|
sl@0
|
41 |
}
|
sl@0
|
42 |
#endif
|
sl@0
|
43 |
|
sl@0
|
44 |
|
sl@0
|
45 |
|
sl@0
|
46 |
extern void DoLocalInvalidateTLB();
|
sl@0
|
47 |
|
sl@0
|
48 |
|
sl@0
|
49 |
#ifndef __SMP__
|
sl@0
|
50 |
|
sl@0
|
51 |
|
sl@0
|
52 |
FORCE_INLINE void LocalInvalidateTLB()
|
sl@0
|
53 |
{
|
sl@0
|
54 |
DoLocalInvalidateTLB();
|
sl@0
|
55 |
}
|
sl@0
|
56 |
|
sl@0
|
57 |
|
sl@0
|
58 |
#else // __SMP__
|
sl@0
|
59 |
|
sl@0
|
60 |
|
sl@0
|
61 |
const TInt KMaxPages = 1;
|
sl@0
|
62 |
|
sl@0
|
63 |
class TTLBIPI : public TGenericIPI
|
sl@0
|
64 |
{
|
sl@0
|
65 |
public:
|
sl@0
|
66 |
TTLBIPI();
|
sl@0
|
67 |
static void InvalidateForPagesIsr(TGenericIPI*);
|
sl@0
|
68 |
static void LocalInvalidateIsr(TGenericIPI*);
|
sl@0
|
69 |
static void InvalidateIsr(TGenericIPI*);
|
sl@0
|
70 |
static void WaitAndInvalidateIsr(TGenericIPI*);
|
sl@0
|
71 |
void AddAddress(TLinAddr aAddr);
|
sl@0
|
72 |
void InvalidateList();
|
sl@0
|
73 |
public:
|
sl@0
|
74 |
volatile TInt iFlag;
|
sl@0
|
75 |
TInt iCount;
|
sl@0
|
76 |
TLinAddr iAddr[KMaxPages];
|
sl@0
|
77 |
};
|
sl@0
|
78 |
|
sl@0
|
79 |
TTLBIPI::TTLBIPI()
|
sl@0
|
80 |
: iFlag(0), iCount(0)
|
sl@0
|
81 |
{
|
sl@0
|
82 |
}
|
sl@0
|
83 |
|
sl@0
|
84 |
void TTLBIPI::LocalInvalidateIsr(TGenericIPI*)
|
sl@0
|
85 |
{
|
sl@0
|
86 |
TRACE2(("TLBLocInv"));
|
sl@0
|
87 |
DoLocalInvalidateTLB();
|
sl@0
|
88 |
}
|
sl@0
|
89 |
|
sl@0
|
90 |
void TTLBIPI::InvalidateIsr(TGenericIPI*)
|
sl@0
|
91 |
{
|
sl@0
|
92 |
TRACE2(("TLBInv"));
|
sl@0
|
93 |
DoInvalidateTLB();
|
sl@0
|
94 |
}
|
sl@0
|
95 |
|
sl@0
|
96 |
void TTLBIPI::WaitAndInvalidateIsr(TGenericIPI* aTLBIPI)
|
sl@0
|
97 |
{
|
sl@0
|
98 |
TRACE2(("TLBWtInv"));
|
sl@0
|
99 |
TTLBIPI& a = *(TTLBIPI*)aTLBIPI;
|
sl@0
|
100 |
while (!a.iFlag)
|
sl@0
|
101 |
{}
|
sl@0
|
102 |
if (a.iCount == 1)
|
sl@0
|
103 |
DoInvalidateTLBForPage(a.iAddr[0]);
|
sl@0
|
104 |
else
|
sl@0
|
105 |
DoInvalidateTLB();
|
sl@0
|
106 |
}
|
sl@0
|
107 |
|
sl@0
|
108 |
void TTLBIPI::InvalidateForPagesIsr(TGenericIPI* aTLBIPI)
|
sl@0
|
109 |
{
|
sl@0
|
110 |
TTLBIPI& a = *(TTLBIPI*)aTLBIPI;
|
sl@0
|
111 |
TInt i;
|
sl@0
|
112 |
for (i=0; i<a.iCount; ++i)
|
sl@0
|
113 |
{
|
sl@0
|
114 |
TRACE2(("TLBInv %08x", a.iAddr[i]));
|
sl@0
|
115 |
DoInvalidateTLBForPage(a.iAddr[i]);
|
sl@0
|
116 |
}
|
sl@0
|
117 |
}
|
sl@0
|
118 |
|
sl@0
|
119 |
void TTLBIPI::AddAddress(TLinAddr aAddr)
|
sl@0
|
120 |
{
|
sl@0
|
121 |
iAddr[iCount] = aAddr;
|
sl@0
|
122 |
if (++iCount == KMaxPages)
|
sl@0
|
123 |
InvalidateList();
|
sl@0
|
124 |
}
|
sl@0
|
125 |
|
sl@0
|
126 |
void TTLBIPI::InvalidateList()
|
sl@0
|
127 |
{
|
sl@0
|
128 |
NKern::Lock();
|
sl@0
|
129 |
InvalidateForPagesIsr(this);
|
sl@0
|
130 |
QueueAllOther(&InvalidateForPagesIsr);
|
sl@0
|
131 |
NKern::Unlock();
|
sl@0
|
132 |
WaitCompletion();
|
sl@0
|
133 |
iCount = 0;
|
sl@0
|
134 |
}
|
sl@0
|
135 |
|
sl@0
|
136 |
void LocalInvalidateTLB()
|
sl@0
|
137 |
{
|
sl@0
|
138 |
TTLBIPI ipi;
|
sl@0
|
139 |
NKern::Lock();
|
sl@0
|
140 |
DoLocalInvalidateTLB();
|
sl@0
|
141 |
ipi.QueueAllOther(&TTLBIPI::LocalInvalidateIsr);
|
sl@0
|
142 |
NKern::Unlock();
|
sl@0
|
143 |
ipi.WaitCompletion();
|
sl@0
|
144 |
}
|
sl@0
|
145 |
|
sl@0
|
146 |
void InvalidateTLB()
|
sl@0
|
147 |
{
|
sl@0
|
148 |
TTLBIPI ipi;
|
sl@0
|
149 |
NKern::Lock();
|
sl@0
|
150 |
DoInvalidateTLB();
|
sl@0
|
151 |
ipi.QueueAllOther(&TTLBIPI::InvalidateIsr);
|
sl@0
|
152 |
NKern::Unlock();
|
sl@0
|
153 |
ipi.WaitCompletion();
|
sl@0
|
154 |
}
|
sl@0
|
155 |
|
sl@0
|
156 |
void InvalidateTLBForPage(TLinAddr aAddr)
|
sl@0
|
157 |
{
|
sl@0
|
158 |
TTLBIPI ipi;
|
sl@0
|
159 |
ipi.AddAddress(aAddr);
|
sl@0
|
160 |
ipi.InvalidateList();
|
sl@0
|
161 |
}
|
sl@0
|
162 |
|
sl@0
|
163 |
|
sl@0
|
164 |
#endif // __SMP__
|
sl@0
|
165 |
|
sl@0
|
166 |
|
sl@0
|
167 |
void InvalidateTLBForAsid(TUint aAsid)
|
sl@0
|
168 |
{
|
sl@0
|
169 |
if(aAsid==KKernelOsAsid)
|
sl@0
|
170 |
InvalidateTLB();
|
sl@0
|
171 |
else
|
sl@0
|
172 |
LocalInvalidateTLB();
|
sl@0
|
173 |
}
|
sl@0
|
174 |
|
sl@0
|
175 |
|
sl@0
|
176 |
void SinglePdeUpdated(TPde* aPde)
|
sl@0
|
177 |
{
|
sl@0
|
178 |
CacheMaintenance::SinglePdeUpdated((TLinAddr)aPde);
|
sl@0
|
179 |
PageDirectories.GlobalPdeChanged(aPde);
|
sl@0
|
180 |
}
|
sl@0
|
181 |
|
sl@0
|
182 |
|
sl@0
|
183 |
//
|
sl@0
|
184 |
// Functions for class Mmu
|
sl@0
|
185 |
//
|
sl@0
|
186 |
|
sl@0
|
187 |
TPhysAddr Mmu::PtePhysAddr(TPte aPte, TUint /*aPteIndex*/)
|
sl@0
|
188 |
{
|
sl@0
|
189 |
if(aPte&KPdePtePresent)
|
sl@0
|
190 |
return aPte & KPdePtePhysAddrMask;
|
sl@0
|
191 |
return KPhysAddrInvalid;
|
sl@0
|
192 |
}
|
sl@0
|
193 |
|
sl@0
|
194 |
|
sl@0
|
195 |
TPte* Mmu::PageTableFromPde(TPde aPde)
|
sl@0
|
196 |
{
|
sl@0
|
197 |
if((aPde&(KPdeLargePage|KPdePtePresent)) == KPdePtePresent)
|
sl@0
|
198 |
{
|
sl@0
|
199 |
SPageInfo* pi = SPageInfo::FromPhysAddr(aPde);
|
sl@0
|
200 |
TInt id = (pi->Index()<<KPtClusterShift) | ((aPde>>KPageTableShift)&KPtClusterMask);
|
sl@0
|
201 |
return (TPte*)(KPageTableBase+(id<<KPageTableShift));
|
sl@0
|
202 |
}
|
sl@0
|
203 |
return 0;
|
sl@0
|
204 |
}
|
sl@0
|
205 |
|
sl@0
|
206 |
|
sl@0
|
207 |
TPte* Mmu::SafePageTableFromPde(TPde aPde)
|
sl@0
|
208 |
{
|
sl@0
|
209 |
if((aPde&(KPdeLargePage|KPdePtePresent)) == KPdePtePresent)
|
sl@0
|
210 |
{
|
sl@0
|
211 |
SPageInfo* pi = SPageInfo::SafeFromPhysAddr(aPde&~KPageMask);
|
sl@0
|
212 |
if(pi)
|
sl@0
|
213 |
{
|
sl@0
|
214 |
TInt id = (pi->Index()<<KPtClusterShift) | ((aPde>>KPageTableShift)&KPtClusterMask);
|
sl@0
|
215 |
return (TPte*)(KPageTableBase+(id<<KPageTableShift));
|
sl@0
|
216 |
}
|
sl@0
|
217 |
}
|
sl@0
|
218 |
return 0;
|
sl@0
|
219 |
}
|
sl@0
|
220 |
|
sl@0
|
221 |
|
sl@0
|
222 |
/**
|
sl@0
|
223 |
Return the base phsical address of the section table referenced by the given
|
sl@0
|
224 |
Page Directory Entry (PDE) \a aPde. If the PDE doesn't refer to a
|
sl@0
|
225 |
section then KPhysAddrInvalid is returned.
|
sl@0
|
226 |
|
sl@0
|
227 |
@pre #MmuLock held.
|
sl@0
|
228 |
*/
|
sl@0
|
229 |
TPhysAddr Mmu::SectionBaseFromPde(TPde aPde)
|
sl@0
|
230 |
{
|
sl@0
|
231 |
if(PdeMapsSection(aPde))
|
sl@0
|
232 |
return aPde&KPdeLargePagePhysAddrMask;
|
sl@0
|
233 |
return KPhysAddrInvalid;
|
sl@0
|
234 |
}
|
sl@0
|
235 |
|
sl@0
|
236 |
|
sl@0
|
237 |
TPte* Mmu::PtePtrFromLinAddr(TLinAddr aAddress, TInt aOsAsid)
|
sl@0
|
238 |
{
|
sl@0
|
239 |
TPde pde = PageDirectory(aOsAsid)[aAddress>>KChunkShift];
|
sl@0
|
240 |
SPageInfo* pi = SPageInfo::FromPhysAddr(pde);
|
sl@0
|
241 |
TPte* pt = (TPte*)(KPageTableBase+(pi->Index()<<KPageShift)+(pde&(KPageMask&~KPageTableMask)));
|
sl@0
|
242 |
pt += (aAddress>>KPageShift)&(KChunkMask>>KPageShift);
|
sl@0
|
243 |
return pt;
|
sl@0
|
244 |
}
|
sl@0
|
245 |
|
sl@0
|
246 |
|
sl@0
|
247 |
TPte* Mmu::SafePtePtrFromLinAddr(TLinAddr aAddress, TInt aOsAsid)
|
sl@0
|
248 |
{
|
sl@0
|
249 |
TPde pde = PageDirectory(aOsAsid)[aAddress>>KChunkShift];
|
sl@0
|
250 |
TPte* pt = SafePageTableFromPde(pde);
|
sl@0
|
251 |
if(pt)
|
sl@0
|
252 |
pt += (aAddress>>KPageShift)&(KChunkMask>>KPageShift);
|
sl@0
|
253 |
return pt;
|
sl@0
|
254 |
}
|
sl@0
|
255 |
|
sl@0
|
256 |
|
sl@0
|
257 |
TPhysAddr Mmu::PageTablePhysAddr(TPte* aPt)
|
sl@0
|
258 |
{
|
sl@0
|
259 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld() || PageTablesLockIsHeld());
|
sl@0
|
260 |
|
sl@0
|
261 |
TInt pdeIndex = ((TLinAddr)aPt)>>KChunkShift;
|
sl@0
|
262 |
TPde pde = PageDirectory(KKernelOsAsid)[pdeIndex];
|
sl@0
|
263 |
__NK_ASSERT_DEBUG((pde&(KPdePtePresent|KPdeLargePage))==KPdePtePresent);
|
sl@0
|
264 |
|
sl@0
|
265 |
SPageInfo* pi = SPageInfo::FromPhysAddr(pde);
|
sl@0
|
266 |
TPte* pPte = (TPte*)(KPageTableBase+(pi->Index(true)<<KPageShift));
|
sl@0
|
267 |
TPte pte = pPte[(((TLinAddr)aPt)&KChunkMask)>>KPageShift];
|
sl@0
|
268 |
__NK_ASSERT_DEBUG(pte & KPdePtePresent);
|
sl@0
|
269 |
|
sl@0
|
270 |
return pte&KPdePtePhysAddrMask;
|
sl@0
|
271 |
}
|
sl@0
|
272 |
|
sl@0
|
273 |
|
sl@0
|
274 |
TPhysAddr Mmu::UncheckedLinearToPhysical(TLinAddr aLinAddr, TInt aOsAsid)
|
sl@0
|
275 |
{
|
sl@0
|
276 |
TRACE2(("Mmu::UncheckedLinearToPhysical(%08x,%d)",aLinAddr,aOsAsid));
|
sl@0
|
277 |
TInt pdeIndex = aLinAddr>>KChunkShift;
|
sl@0
|
278 |
TPde pde = PageDirectory(aOsAsid)[pdeIndex];
|
sl@0
|
279 |
TPhysAddr pa=KPhysAddrInvalid;
|
sl@0
|
280 |
if (pde & KPdePtePresent)
|
sl@0
|
281 |
{
|
sl@0
|
282 |
if(pde&KPdeLargePage)
|
sl@0
|
283 |
{
|
sl@0
|
284 |
pa=(pde&KPdeLargePagePhysAddrMask)+(aLinAddr&~KPdeLargePagePhysAddrMask);
|
sl@0
|
285 |
__KTRACE_OPT(KMMU,Kern::Printf("Mapped with large table - returning %08x",pa));
|
sl@0
|
286 |
}
|
sl@0
|
287 |
else
|
sl@0
|
288 |
{
|
sl@0
|
289 |
SPageInfo* pi = SPageInfo::FromPhysAddr(pde);
|
sl@0
|
290 |
TInt id = (pi->Index(true)<<KPtClusterShift) | ((pde>>KPageTableShift)&KPtClusterMask);
|
sl@0
|
291 |
TPte* pPte = (TPte*)(KPageTableBase+(id<<KPageTableShift));
|
sl@0
|
292 |
TPte pte = pPte[(aLinAddr&KChunkMask)>>KPageShift];
|
sl@0
|
293 |
if (pte & KPdePtePresent)
|
sl@0
|
294 |
{
|
sl@0
|
295 |
pa=(pte&KPdePtePhysAddrMask)+(aLinAddr&KPageMask);
|
sl@0
|
296 |
__KTRACE_OPT(KMMU,Kern::Printf("Mapped with page table - returning %08x",pa));
|
sl@0
|
297 |
}
|
sl@0
|
298 |
}
|
sl@0
|
299 |
}
|
sl@0
|
300 |
return pa;
|
sl@0
|
301 |
}
|
sl@0
|
302 |
|
sl@0
|
303 |
|
sl@0
|
304 |
void Mmu::Init1()
|
sl@0
|
305 |
{
|
sl@0
|
306 |
TRACEB(("Mmu::Init1"));
|
sl@0
|
307 |
|
sl@0
|
308 |
TUint pge = TheSuperPage().iCpuId & EX86Feat_PGE;
|
sl@0
|
309 |
PteGlobal = pge ? KPdePteGlobal : 0;
|
sl@0
|
310 |
X86_UseGlobalPTEs = pge!=0;
|
sl@0
|
311 |
|
sl@0
|
312 |
#ifdef __SMP__
|
sl@0
|
313 |
ApTrampolinePage = KApTrampolinePageLin;
|
sl@0
|
314 |
|
sl@0
|
315 |
TInt i;
|
sl@0
|
316 |
for (i=0; i<KMaxCpus; ++i)
|
sl@0
|
317 |
{
|
sl@0
|
318 |
TSubScheduler& ss = TheSubSchedulers[i];
|
sl@0
|
319 |
TLinAddr a = KIPCAlias + (i<<KChunkShift);
|
sl@0
|
320 |
ss.i_AliasLinAddr = (TAny*)a;
|
sl@0
|
321 |
ss.i_AliasPdePtr = (TAny*)(KPageDirectoryBase + (a>>KChunkShift)*sizeof(TPde));
|
sl@0
|
322 |
}
|
sl@0
|
323 |
#endif
|
sl@0
|
324 |
|
sl@0
|
325 |
Init1Common();
|
sl@0
|
326 |
}
|
sl@0
|
327 |
|
sl@0
|
328 |
void Mmu::Init2()
|
sl@0
|
329 |
{
|
sl@0
|
330 |
TRACEB(("Mmu::Init2"));
|
sl@0
|
331 |
|
sl@0
|
332 |
Init2Common();
|
sl@0
|
333 |
}
|
sl@0
|
334 |
|
sl@0
|
335 |
void Mmu::Init2Final()
|
sl@0
|
336 |
{
|
sl@0
|
337 |
TRACEB(("Mmu::Init2Final"));
|
sl@0
|
338 |
|
sl@0
|
339 |
Init2FinalCommon();
|
sl@0
|
340 |
}
|
sl@0
|
341 |
|
sl@0
|
342 |
|
sl@0
|
343 |
const TPde KPdeForBlankPageTable = KPdePtePresent|KPdePteWrite|KPdePteUser;
|
sl@0
|
344 |
|
sl@0
|
345 |
TPde Mmu::BlankPde(TMemoryAttributes aAttributes)
|
sl@0
|
346 |
{
|
sl@0
|
347 |
(void)aAttributes;
|
sl@0
|
348 |
TPde pde = KPdeForBlankPageTable;
|
sl@0
|
349 |
TRACE2(("Mmu::BlankPde(%x) returns 0x%x",aAttributes,pde));
|
sl@0
|
350 |
return pde;
|
sl@0
|
351 |
}
|
sl@0
|
352 |
|
sl@0
|
353 |
|
sl@0
|
354 |
TPde Mmu::BlankSectionPde(TMemoryAttributes aAttributes, TUint aPteType)
|
sl@0
|
355 |
{
|
sl@0
|
356 |
return PageToSectionEntry(BlankPte(aAttributes, aPteType), KPdeForBlankPageTable);
|
sl@0
|
357 |
}
|
sl@0
|
358 |
|
sl@0
|
359 |
|
sl@0
|
360 |
TPte Mmu::BlankPte(TMemoryAttributes aAttributes, TUint aPteType)
|
sl@0
|
361 |
{
|
sl@0
|
362 |
TPte pte = KPdePtePresent;
|
sl@0
|
363 |
if(aPteType&EPteTypeUserAccess)
|
sl@0
|
364 |
pte |= KPdePteUser;
|
sl@0
|
365 |
if(aPteType&EPteTypeWritable)
|
sl@0
|
366 |
pte |= KPdePteWrite;
|
sl@0
|
367 |
if(aPteType&EPteTypeGlobal)
|
sl@0
|
368 |
pte |= PteGlobal;
|
sl@0
|
369 |
|
sl@0
|
370 |
switch((TMemoryType)(aAttributes&EMemoryAttributeTypeMask))
|
sl@0
|
371 |
{
|
sl@0
|
372 |
case EMemAttStronglyOrdered:
|
sl@0
|
373 |
case EMemAttDevice:
|
sl@0
|
374 |
case EMemAttNormalUncached:
|
sl@0
|
375 |
pte |= KPdePteUncached;
|
sl@0
|
376 |
break;
|
sl@0
|
377 |
case EMemAttNormalCached:
|
sl@0
|
378 |
break;
|
sl@0
|
379 |
default:
|
sl@0
|
380 |
__NK_ASSERT_ALWAYS(0);
|
sl@0
|
381 |
break;
|
sl@0
|
382 |
}
|
sl@0
|
383 |
|
sl@0
|
384 |
TRACE2(("Mmu::BlankPte(%x,%x) returns 0x%x",aAttributes,aPteType,pte));
|
sl@0
|
385 |
return pte;
|
sl@0
|
386 |
}
|
sl@0
|
387 |
|
sl@0
|
388 |
|
sl@0
|
389 |
TPte Mmu::SectionToPageEntry(TPde& aPde)
|
sl@0
|
390 |
{
|
sl@0
|
391 |
TPte pte = aPde&~(KPdePtePhysAddrMask|KPdeLargePage);
|
sl@0
|
392 |
aPde = KPdeForBlankPageTable;
|
sl@0
|
393 |
return pte;
|
sl@0
|
394 |
}
|
sl@0
|
395 |
|
sl@0
|
396 |
|
sl@0
|
397 |
TPde Mmu::PageToSectionEntry(TPte aPte, TPde /*aPde*/)
|
sl@0
|
398 |
{
|
sl@0
|
399 |
TPte pde = aPte&~KPdeLargePagePhysAddrMask;
|
sl@0
|
400 |
pde |= KPdeLargePage;
|
sl@0
|
401 |
return pde;
|
sl@0
|
402 |
}
|
sl@0
|
403 |
|
sl@0
|
404 |
|
sl@0
|
405 |
TMemoryAttributes Mmu::CanonicalMemoryAttributes(TMemoryAttributes aAttr)
|
sl@0
|
406 |
{
|
sl@0
|
407 |
TUint attr = aAttr;
|
sl@0
|
408 |
if(attr&EMemoryAttributeDefaultShareable)
|
sl@0
|
409 |
{
|
sl@0
|
410 |
// sharing not specified, use default...
|
sl@0
|
411 |
#if defined (__CPU_USE_SHARED_MEMORY)
|
sl@0
|
412 |
attr |= EMemoryAttributeShareable;
|
sl@0
|
413 |
#else
|
sl@0
|
414 |
attr &= ~EMemoryAttributeShareable;
|
sl@0
|
415 |
#endif
|
sl@0
|
416 |
}
|
sl@0
|
417 |
|
sl@0
|
418 |
// remove invalid attributes...
|
sl@0
|
419 |
attr &= ~(EMemoryAttributeUseECC);
|
sl@0
|
420 |
|
sl@0
|
421 |
return (TMemoryAttributes)(attr&EMemoryAttributeMask);
|
sl@0
|
422 |
}
|
sl@0
|
423 |
|
sl@0
|
424 |
|
sl@0
|
425 |
void Mmu::PagesAllocated(TPhysAddr* aPageList, TUint aCount, TRamAllocFlags aFlags, TBool aReallocate)
|
sl@0
|
426 |
{
|
sl@0
|
427 |
TRACE2(("Mmu::PagesAllocated(0x%08x,%d,0x%x,%d)",aPageList, aCount, aFlags, (bool)aReallocate));
|
sl@0
|
428 |
__NK_ASSERT_DEBUG(RamAllocLock::IsHeld());
|
sl@0
|
429 |
|
sl@0
|
430 |
TBool wipe = !(aFlags&EAllocNoWipe); // do we need to wipe page contents?
|
sl@0
|
431 |
TMemoryType newType = (TMemoryType)(aFlags&KMemoryTypeMask); // memory type that pages will be used for
|
sl@0
|
432 |
TUint8 wipeByte = (aFlags&EAllocUseCustomWipeByte) ? (aFlags>>EAllocWipeByteShift)&0xff : 0x03; // value to wipe memory with
|
sl@0
|
433 |
|
sl@0
|
434 |
// process each page in turn...
|
sl@0
|
435 |
while(aCount--)
|
sl@0
|
436 |
{
|
sl@0
|
437 |
// get physical address of next page...
|
sl@0
|
438 |
TPhysAddr pagePhys;
|
sl@0
|
439 |
if((TPhysAddr)aPageList&1)
|
sl@0
|
440 |
{
|
sl@0
|
441 |
// aPageList is actually the physical address to use...
|
sl@0
|
442 |
pagePhys = (TPhysAddr)aPageList&~1;
|
sl@0
|
443 |
*(TPhysAddr*)&aPageList += KPageSize;
|
sl@0
|
444 |
}
|
sl@0
|
445 |
else
|
sl@0
|
446 |
pagePhys = *aPageList++;
|
sl@0
|
447 |
__NK_ASSERT_DEBUG((pagePhys&KPageMask)==0);
|
sl@0
|
448 |
|
sl@0
|
449 |
// get info about page...
|
sl@0
|
450 |
SPageInfo* pi = SPageInfo::FromPhysAddr(pagePhys);
|
sl@0
|
451 |
TMemoryType oldType = (TMemoryType)(pi->Flags(true)&KMemoryTypeMask);
|
sl@0
|
452 |
|
sl@0
|
453 |
TRACE2(("Mmu::PagesAllocated page=0x%08x, oldType=%d, wipe=%d",pagePhys,oldType,wipe));
|
sl@0
|
454 |
if(wipe)
|
sl@0
|
455 |
{
|
sl@0
|
456 |
// work out temporary mapping values...
|
sl@0
|
457 |
TLinAddr tempLinAddr = iTempMap[0].iLinAddr;
|
sl@0
|
458 |
TPte* tempPte = iTempMap[0].iPtePtr;
|
sl@0
|
459 |
|
sl@0
|
460 |
// temporarily map page...
|
sl@0
|
461 |
*tempPte = pagePhys | iTempPteCached;
|
sl@0
|
462 |
CacheMaintenance::SinglePteUpdated((TLinAddr)tempPte);
|
sl@0
|
463 |
InvalidateTLBForPage(tempLinAddr|KKernelOsAsid);
|
sl@0
|
464 |
|
sl@0
|
465 |
// wipe contents of memory...
|
sl@0
|
466 |
memset((TAny*)tempLinAddr, wipeByte, KPageSize);
|
sl@0
|
467 |
__e32_io_completion_barrier();
|
sl@0
|
468 |
|
sl@0
|
469 |
// invalidate temporary mapping...
|
sl@0
|
470 |
*tempPte = KPteUnallocatedEntry;
|
sl@0
|
471 |
CacheMaintenance::SinglePteUpdated((TLinAddr)tempPte);
|
sl@0
|
472 |
InvalidateTLBForPage(tempLinAddr|KKernelOsAsid);
|
sl@0
|
473 |
}
|
sl@0
|
474 |
|
sl@0
|
475 |
// indicate page has been allocated...
|
sl@0
|
476 |
if(aReallocate==false)
|
sl@0
|
477 |
pi->SetAllocated();
|
sl@0
|
478 |
}
|
sl@0
|
479 |
}
|
sl@0
|
480 |
|
sl@0
|
481 |
|
sl@0
|
482 |
void Mmu::PageFreed(SPageInfo* aPageInfo)
|
sl@0
|
483 |
{
|
sl@0
|
484 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld());
|
sl@0
|
485 |
|
sl@0
|
486 |
if(aPageInfo->Type()==SPageInfo::EUnused)
|
sl@0
|
487 |
return;
|
sl@0
|
488 |
|
sl@0
|
489 |
aPageInfo->SetUnused();
|
sl@0
|
490 |
|
sl@0
|
491 |
TRACE2(("Mmu::PageFreed page=0x%08x type=%d colour=%d",aPageInfo->PhysAddr(),aPageInfo->Flags()&KMemoryTypeMask,aPageInfo->Index()&KPageColourMask));
|
sl@0
|
492 |
}
|
sl@0
|
493 |
|
sl@0
|
494 |
|
sl@0
|
495 |
void Mmu::CleanAndInvalidatePages(TPhysAddr* aPages, TUint aCount, TMemoryAttributes aAttributes, TUint aColour)
|
sl@0
|
496 |
{
|
sl@0
|
497 |
TMemoryType type = (TMemoryType)(aAttributes&EMemoryAttributeTypeMask);
|
sl@0
|
498 |
if(!CacheMaintenance::IsCached(type))
|
sl@0
|
499 |
{
|
sl@0
|
500 |
TRACE2(("Mmu::CleanAndInvalidatePages - nothing to do"));
|
sl@0
|
501 |
return;
|
sl@0
|
502 |
}
|
sl@0
|
503 |
|
sl@0
|
504 |
RamAllocLock::Lock();
|
sl@0
|
505 |
|
sl@0
|
506 |
while(aCount--)
|
sl@0
|
507 |
{
|
sl@0
|
508 |
TPhysAddr pagePhys = *aPages++;
|
sl@0
|
509 |
TRACE2(("Mmu::CleanAndInvalidatePages 0x%08x",pagePhys));
|
sl@0
|
510 |
|
sl@0
|
511 |
// work out temporary mapping values...
|
sl@0
|
512 |
TLinAddr tempLinAddr = iTempMap[0].iLinAddr;
|
sl@0
|
513 |
TPte* tempPte = iTempMap[0].iPtePtr;
|
sl@0
|
514 |
|
sl@0
|
515 |
// temporarily map page...
|
sl@0
|
516 |
*tempPte = pagePhys | iTempPteCached;
|
sl@0
|
517 |
CacheMaintenance::SinglePteUpdated((TLinAddr)tempPte);
|
sl@0
|
518 |
InvalidateTLBForPage(tempLinAddr|KKernelOsAsid);
|
sl@0
|
519 |
|
sl@0
|
520 |
// sort out cache for memory reuse...
|
sl@0
|
521 |
CacheMaintenance::PageToPreserveAndReuse(tempLinAddr, type, KPageSize);
|
sl@0
|
522 |
|
sl@0
|
523 |
// invalidate temporary mapping...
|
sl@0
|
524 |
*tempPte = KPteUnallocatedEntry;
|
sl@0
|
525 |
CacheMaintenance::SinglePteUpdated((TLinAddr)tempPte);
|
sl@0
|
526 |
InvalidateTLBForPage(tempLinAddr|KKernelOsAsid);
|
sl@0
|
527 |
|
sl@0
|
528 |
RamAllocLock::Flash();
|
sl@0
|
529 |
}
|
sl@0
|
530 |
RamAllocLock::Unlock();
|
sl@0
|
531 |
}
|
sl@0
|
532 |
|
sl@0
|
533 |
|
sl@0
|
534 |
TInt DMemModelThread::Alias(TLinAddr aAddr, DMemModelProcess* aProcess, TInt aSize, TLinAddr& aAliasAddr, TUint& aAliasSize)
|
sl@0
|
535 |
//
|
sl@0
|
536 |
// Set up an alias mapping starting at address aAddr in specified process.
|
sl@0
|
537 |
// Note: Alias is removed if an exception is trapped by DThread::IpcExcHandler.
|
sl@0
|
538 |
//
|
sl@0
|
539 |
{
|
sl@0
|
540 |
TRACE2(("Thread %O Alias %08x+%x Process %O",this,aAddr,aSize,aProcess));
|
sl@0
|
541 |
__NK_ASSERT_DEBUG(this==TheCurrentThread); // many bad things can happen if false
|
sl@0
|
542 |
// If there is an existing alias it should be on the same process otherwise
|
sl@0
|
543 |
// the os asid reference may be leaked.
|
sl@0
|
544 |
__NK_ASSERT_DEBUG(!iAliasLinAddr || aProcess == iAliasProcess);
|
sl@0
|
545 |
|
sl@0
|
546 |
if(TUint(aAddr^KIPCAlias)<TUint(KIPCAliasAreaSize))
|
sl@0
|
547 |
return KErrBadDescriptor; // prevent access to alias region
|
sl@0
|
548 |
|
sl@0
|
549 |
// Grab the mmu lock before opening a reference on os asid so that this thread
|
sl@0
|
550 |
// is in an implicit critical section and therefore can't leak the reference by
|
sl@0
|
551 |
// dying before iAliasLinAddr is set.
|
sl@0
|
552 |
MmuLock::Lock();
|
sl@0
|
553 |
|
sl@0
|
554 |
TInt osAsid;
|
sl@0
|
555 |
if (!iAliasLinAddr)
|
sl@0
|
556 |
{// There isn't any existing alias.
|
sl@0
|
557 |
// Open a reference on the aProcess's os asid so that it is not freed and/or reused
|
sl@0
|
558 |
// while we are aliasing an address belonging to it.
|
sl@0
|
559 |
osAsid = aProcess->TryOpenOsAsid();
|
sl@0
|
560 |
if (osAsid < 0)
|
sl@0
|
561 |
{// Couldn't open os asid so aProcess is no longer running.
|
sl@0
|
562 |
MmuLock::Unlock();
|
sl@0
|
563 |
return KErrBadDescriptor;
|
sl@0
|
564 |
}
|
sl@0
|
565 |
}
|
sl@0
|
566 |
else
|
sl@0
|
567 |
{
|
sl@0
|
568 |
// Just read the os asid of the process being aliased we already have a reference on it.
|
sl@0
|
569 |
osAsid = aProcess->OsAsid();
|
sl@0
|
570 |
}
|
sl@0
|
571 |
|
sl@0
|
572 |
// Now we have the os asid check access to kernel memory.
|
sl@0
|
573 |
if(aAddr >= KUserMemoryLimit && osAsid != (TUint)KKernelOsAsid)
|
sl@0
|
574 |
{
|
sl@0
|
575 |
NKern::ThreadEnterCS();
|
sl@0
|
576 |
MmuLock::Unlock();
|
sl@0
|
577 |
if (!iAliasLinAddr)
|
sl@0
|
578 |
{// Close the new reference as RemoveAlias won't do as iAliasLinAddr is not set.
|
sl@0
|
579 |
aProcess->AsyncCloseOsAsid(); // Asynchronous close as this method should be quick.
|
sl@0
|
580 |
}
|
sl@0
|
581 |
NKern::ThreadLeaveCS();
|
sl@0
|
582 |
return KErrBadDescriptor; // prevent access to supervisor only memory
|
sl@0
|
583 |
}
|
sl@0
|
584 |
|
sl@0
|
585 |
// Now we know all accesses to global memory are safe so check if aAddr is global.
|
sl@0
|
586 |
if(aAddr >= KGlobalMemoryBase)
|
sl@0
|
587 |
{
|
sl@0
|
588 |
// address is in global section, don't bother aliasing it...
|
sl@0
|
589 |
if (!iAliasLinAddr)
|
sl@0
|
590 |
{// Close the new reference as not required.
|
sl@0
|
591 |
NKern::ThreadEnterCS();
|
sl@0
|
592 |
MmuLock::Unlock();
|
sl@0
|
593 |
aProcess->AsyncCloseOsAsid(); // Asynchronous close as this method should be quick.
|
sl@0
|
594 |
NKern::ThreadLeaveCS();
|
sl@0
|
595 |
}
|
sl@0
|
596 |
else
|
sl@0
|
597 |
{// Remove the existing alias as it is not required.
|
sl@0
|
598 |
DoRemoveAlias(iAliasLinAddr); // Releases mmulock.
|
sl@0
|
599 |
}
|
sl@0
|
600 |
aAliasAddr = aAddr;
|
sl@0
|
601 |
TInt maxSize = KChunkSize-(aAddr&KChunkMask);
|
sl@0
|
602 |
aAliasSize = aSize<maxSize ? aSize : maxSize;
|
sl@0
|
603 |
TRACE2(("DMemModelThread::Alias() abandoned as memory is globally mapped"));
|
sl@0
|
604 |
return KErrNone;
|
sl@0
|
605 |
}
|
sl@0
|
606 |
|
sl@0
|
607 |
TPde* pd = Mmu::PageDirectory(osAsid);
|
sl@0
|
608 |
TInt pdeIndex = aAddr>>KChunkShift;
|
sl@0
|
609 |
TPde pde = pd[pdeIndex];
|
sl@0
|
610 |
#ifdef __SMP__
|
sl@0
|
611 |
TLinAddr aliasAddr;
|
sl@0
|
612 |
#else
|
sl@0
|
613 |
TLinAddr aliasAddr = KIPCAlias+(aAddr&(KChunkMask & ~KPageMask));
|
sl@0
|
614 |
#endif
|
sl@0
|
615 |
if(pde==iAliasPde && iAliasLinAddr)
|
sl@0
|
616 |
{
|
sl@0
|
617 |
// pde already aliased, so just update linear address...
|
sl@0
|
618 |
#ifdef __SMP__
|
sl@0
|
619 |
__NK_ASSERT_DEBUG(iCpuRestoreCookie>=0);
|
sl@0
|
620 |
aliasAddr = iAliasLinAddr & ~KChunkMask;
|
sl@0
|
621 |
aliasAddr |= (aAddr & (KChunkMask & ~KPageMask));
|
sl@0
|
622 |
#endif
|
sl@0
|
623 |
iAliasLinAddr = aliasAddr;
|
sl@0
|
624 |
}
|
sl@0
|
625 |
else
|
sl@0
|
626 |
{
|
sl@0
|
627 |
// alias PDE changed...
|
sl@0
|
628 |
if(!iAliasLinAddr)
|
sl@0
|
629 |
{
|
sl@0
|
630 |
TheMmu.iAliasList.Add(&iAliasLink); // add to list if not already aliased
|
sl@0
|
631 |
#ifdef __SMP__
|
sl@0
|
632 |
__NK_ASSERT_DEBUG(iCpuRestoreCookie==-1);
|
sl@0
|
633 |
iCpuRestoreCookie = NKern::FreezeCpu(); // temporarily lock current thread to this processor
|
sl@0
|
634 |
#endif
|
sl@0
|
635 |
}
|
sl@0
|
636 |
iAliasPde = pde;
|
sl@0
|
637 |
iAliasProcess = aProcess;
|
sl@0
|
638 |
#ifdef __SMP__
|
sl@0
|
639 |
TSubScheduler& ss = SubScheduler(); // OK since we are locked to this CPU
|
sl@0
|
640 |
aliasAddr = TLinAddr(ss.i_AliasLinAddr) + (aAddr & (KChunkMask & ~KPageMask));
|
sl@0
|
641 |
iAliasPdePtr = (TPde*)(TLinAddr(ss.i_AliasPdePtr) + (osAsid << KPageTableShift));
|
sl@0
|
642 |
#endif
|
sl@0
|
643 |
iAliasLinAddr = aliasAddr;
|
sl@0
|
644 |
*iAliasPdePtr = pde;
|
sl@0
|
645 |
}
|
sl@0
|
646 |
TRACE2(("DMemModelThread::Alias() PDEntry=%x, iAliasLinAddr=%x",pde, aliasAddr));
|
sl@0
|
647 |
LocalInvalidateTLBForPage(aliasAddr);
|
sl@0
|
648 |
TInt offset = aAddr&KPageMask;
|
sl@0
|
649 |
aAliasAddr = aliasAddr | offset;
|
sl@0
|
650 |
TInt maxSize = KPageSize - offset;
|
sl@0
|
651 |
aAliasSize = aSize<maxSize ? aSize : maxSize;
|
sl@0
|
652 |
iAliasTarget = aAddr & ~KPageMask;
|
sl@0
|
653 |
|
sl@0
|
654 |
MmuLock::Unlock();
|
sl@0
|
655 |
|
sl@0
|
656 |
return KErrNone;
|
sl@0
|
657 |
}
|
sl@0
|
658 |
|
sl@0
|
659 |
|
sl@0
|
660 |
void DMemModelThread::RemoveAlias()
|
sl@0
|
661 |
//
|
sl@0
|
662 |
// Remove alias mapping (if present)
|
sl@0
|
663 |
//
|
sl@0
|
664 |
{
|
sl@0
|
665 |
TRACE2(("Thread %O RemoveAlias", this));
|
sl@0
|
666 |
__NK_ASSERT_DEBUG(this==TheCurrentThread); // many bad things can happen if false
|
sl@0
|
667 |
|
sl@0
|
668 |
TLinAddr addr = iAliasLinAddr;
|
sl@0
|
669 |
if(addr)
|
sl@0
|
670 |
{
|
sl@0
|
671 |
MmuLock::Lock();
|
sl@0
|
672 |
|
sl@0
|
673 |
DoRemoveAlias(addr); // Unlocks mmulock.
|
sl@0
|
674 |
}
|
sl@0
|
675 |
}
|
sl@0
|
676 |
|
sl@0
|
677 |
|
sl@0
|
678 |
/**
|
sl@0
|
679 |
Remove the alias mapping.
|
sl@0
|
680 |
|
sl@0
|
681 |
@pre Mmulock held
|
sl@0
|
682 |
*/
|
sl@0
|
683 |
void DMemModelThread::DoRemoveAlias(TLinAddr aAddr)
|
sl@0
|
684 |
{
|
sl@0
|
685 |
iAliasLinAddr = 0;
|
sl@0
|
686 |
iAliasPde = KPdeUnallocatedEntry;
|
sl@0
|
687 |
*iAliasPdePtr = KPdeUnallocatedEntry;
|
sl@0
|
688 |
SinglePdeUpdated(iAliasPdePtr);
|
sl@0
|
689 |
__NK_ASSERT_DEBUG((aAddr&KPageMask)==0);
|
sl@0
|
690 |
LocalInvalidateTLBForPage(aAddr);
|
sl@0
|
691 |
iAliasLink.Deque();
|
sl@0
|
692 |
#ifdef __SMP__
|
sl@0
|
693 |
__NK_ASSERT_DEBUG(iCpuRestoreCookie>=0);
|
sl@0
|
694 |
NKern::EndFreezeCpu(iCpuRestoreCookie);
|
sl@0
|
695 |
iCpuRestoreCookie = -1;
|
sl@0
|
696 |
#endif
|
sl@0
|
697 |
|
sl@0
|
698 |
// Must close the os asid while in critical section to prevent it being
|
sl@0
|
699 |
// leaked. However, we can't hold the mmu lock so we have to enter an
|
sl@0
|
700 |
// explict crtical section. It is ok to release the mmu lock as the
|
sl@0
|
701 |
// iAliasLinAddr and iAliasProcess members are only ever updated by the
|
sl@0
|
702 |
// current thread.
|
sl@0
|
703 |
NKern::ThreadEnterCS();
|
sl@0
|
704 |
MmuLock::Unlock();
|
sl@0
|
705 |
iAliasProcess->AsyncCloseOsAsid(); // Asynchronous close as this method should be quick.
|
sl@0
|
706 |
NKern::ThreadLeaveCS();
|
sl@0
|
707 |
}
|
sl@0
|
708 |
|
sl@0
|
709 |
|
sl@0
|
710 |
TInt M::DemandPagingFault(TAny* aExceptionInfo)
|
sl@0
|
711 |
{
|
sl@0
|
712 |
TX86ExcInfo& exc=*(TX86ExcInfo*)aExceptionInfo;
|
sl@0
|
713 |
if(exc.iExcId!=EX86VectorPageFault)
|
sl@0
|
714 |
return KErrAbort; // not a page fault
|
sl@0
|
715 |
|
sl@0
|
716 |
/*
|
sl@0
|
717 |
Meanings of exc.iExcErrorCode when exception type is EX86VectorPageFault...
|
sl@0
|
718 |
|
sl@0
|
719 |
Bit 0 0 The fault was caused by a non-present page.
|
sl@0
|
720 |
1 The fault was caused by a page-level protection violation.
|
sl@0
|
721 |
Bit 1 0 The access causing the fault was a read.
|
sl@0
|
722 |
1 The access causing the fault was a write.
|
sl@0
|
723 |
Bit 2 0 The access causing the fault originated when the processor was executing in supervisor mode.
|
sl@0
|
724 |
1 The access causing the fault originated when the processor was executing in user mode.
|
sl@0
|
725 |
Bit 3 0 The fault was not caused by reserved bit violation.
|
sl@0
|
726 |
1 The fault was caused by reserved bits set to 1 in a page directory.
|
sl@0
|
727 |
Bit 4 0 The fault was not caused by an instruction fetch.
|
sl@0
|
728 |
1 The fault was caused by an instruction fetch.
|
sl@0
|
729 |
*/
|
sl@0
|
730 |
|
sl@0
|
731 |
// check access type...
|
sl@0
|
732 |
TUint accessPermissions = EUser; // we only allow paging of user memory
|
sl@0
|
733 |
if(exc.iExcErrorCode&(1<<1))
|
sl@0
|
734 |
accessPermissions |= EReadWrite;
|
sl@0
|
735 |
|
sl@0
|
736 |
// let TheMmu handle the fault...
|
sl@0
|
737 |
return TheMmu.HandlePageFault(exc.iEip, exc.iFaultAddress, accessPermissions, aExceptionInfo);
|
sl@0
|
738 |
}
|
sl@0
|
739 |
|
sl@0
|
740 |
|