author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 18 Jan 2010 21:31:10 +0200 | |
changeset 36 | 538db54a451d |
parent 33 | 0173bcd7697c |
child 39 | 5d2844f35677 |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright (c) 2007-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". |
|
7 |
// |
|
8 |
// Initial Contributors: |
|
9 |
// Nokia Corporation - initial contribution. |
|
10 |
// |
|
11 |
// Contributors: |
|
12 |
// |
|
13 |
// Description: |
|
14 |
// |
|
15 |
||
16 |
#include <plat_priv.h> |
|
17 |
#include "mm.h" |
|
18 |
#include "mmu.h" |
|
19 |
#include "mmapping.h" |
|
20 |
#include "mobject.h" |
|
21 |
#include "maddressspace.h" |
|
22 |
#include "mptalloc.h" |
|
23 |
#include "mmanager.h" // needed for DMemoryManager::Pin/Unpin, not nice, but no obvious way to break dependency |
|
24 |
#include "cache_maintenance.inl" |
|
25 |
||
26 |
// |
|
27 |
// DMemoryMapping |
|
28 |
// |
|
29 |
||
30 |
DMemoryMapping::DMemoryMapping(TUint aType) |
|
31 |
: DMemoryMappingBase(aType) |
|
32 |
{ |
|
33 |
} |
|
34 |
||
35 |
||
36 |
TInt DMemoryMapping::Construct(TMemoryAttributes aAttributes, TMappingCreateFlags aFlags, TInt aOsAsid, TLinAddr aAddr, TUint aSize, TLinAddr aColourOffset) |
|
37 |
{ |
|
38 |
TRACE(("DMemoryMapping[0x%08x]::Construct(0x%x,0x%x,%d,0x%08x,0x%08x,0x%08x)",this,(TUint32&)aAttributes,aFlags,aOsAsid,aAddr,aSize,aColourOffset)); |
|
39 |
||
40 |
// setup PDE values... |
|
41 |
iBlankPde = Mmu::BlankPde(aAttributes); |
|
42 |
||
43 |
// setup flags... |
|
44 |
if(aFlags&EMappingCreateReserveAllResources) |
|
45 |
Flags() |= EPermanentPageTables; |
|
46 |
||
47 |
// allocate virtual memory... |
|
48 |
TInt r = AllocateVirtualMemory(aFlags,aOsAsid,aAddr,aSize,aColourOffset); |
|
49 |
if(r==KErrNone) |
|
50 |
{ |
|
51 |
// add to address space... |
|
52 |
TLinAddr addr = iAllocatedLinAddrAndOsAsid&~KPageMask; |
|
53 |
TInt osAsid = iAllocatedLinAddrAndOsAsid&KPageMask; |
|
54 |
r = AddressSpace[osAsid]->AddMapping(addr,this); |
|
55 |
if(r!=KErrNone) |
|
56 |
FreeVirtualMemory(); |
|
57 |
} |
|
58 |
||
59 |
return r; |
|
60 |
} |
|
61 |
||
62 |
||
63 |
DMemoryMapping::~DMemoryMapping() |
|
64 |
{ |
|
65 |
TRACE(("DMemoryMapping[0x%08x]::~DMemoryMapping()",this)); |
|
66 |
__NK_ASSERT_DEBUG(!IsAttached()); |
|
67 |
||
68 |
// remove from address space... |
|
69 |
TLinAddr addr = iAllocatedLinAddrAndOsAsid&~KPageMask; |
|
70 |
TInt osAsid = iAllocatedLinAddrAndOsAsid&KPageMask; |
|
71 |
TAny* removed = AddressSpace[osAsid]->RemoveMapping(addr); |
|
72 |
if(removed) |
|
73 |
__NK_ASSERT_DEBUG(removed==this); |
|
74 |
||
75 |
FreeVirtualMemory(); |
|
76 |
} |
|
77 |
||
78 |
||
79 |
void DMemoryMapping::BTraceCreate() |
|
80 |
{ |
|
81 |
MmuLock::Lock(); |
|
82 |
TUint32 data[4] = { iStartIndex, iSizeInPages, OsAsid(), Base() }; |
|
83 |
BTraceContextN(BTrace::EFlexibleMemModel,BTrace::EMemoryMappingCreate,this,Memory(),data,sizeof(data)); |
|
84 |
MmuLock::Unlock(); |
|
85 |
} |
|
86 |
||
87 |
||
88 |
TInt DMemoryMapping::Map(DMemoryObject* aMemory, TUint aIndex, TUint aCount, TMappingPermissions aPermissions) |
|
89 |
{ |
|
90 |
TRACE(("DMemoryMapping[0x%08x]::Map(0x%08x,0x%x,0x%x,0x%08x)",this,aMemory,aIndex,aCount,aPermissions)); |
|
91 |
__NK_ASSERT_DEBUG(!IsAttached()); |
|
92 |
||
93 |
// check reserved resources are compatible (memory objects with reserved resources |
|
94 |
// don't expect to have to allocate memory when mapping new pages),,, |
|
95 |
if(aMemory->iFlags&DMemoryObject::EReserveResources && !(Flags()&EPermanentPageTables)) |
|
96 |
return KErrArgument; |
|
97 |
||
98 |
// check arguments for coarse mappings... |
|
99 |
if(IsCoarse()) |
|
100 |
{ |
|
101 |
if(!aMemory->IsCoarse()) |
|
102 |
return KErrArgument; |
|
103 |
if((aCount|aIndex)&(KChunkMask>>KPageShift)) |
|
104 |
return KErrArgument; |
|
105 |
} |
|
106 |
||
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
107 |
TLinAddr base = iAllocatedLinAddrAndOsAsid & ~KPageMask; |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
108 |
TLinAddr top = base + (aCount << KPageShift); |
0 | 109 |
|
110 |
// check user/supervisor memory partitioning... |
|
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
111 |
if (aPermissions & EUser) |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
112 |
{ |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
113 |
if (base > KUserMemoryLimit || top > KUserMemoryLimit) |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
114 |
return KErrAccessDenied; |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
115 |
} |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
116 |
else |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
117 |
{ |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
118 |
if (base < KUserMemoryLimit || top < KUserMemoryLimit) |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
119 |
return KErrAccessDenied; |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
120 |
} |
0 | 121 |
|
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
122 |
// check that mapping doesn't straddle KUserMemoryLimit or KGlobalMemoryBase ... |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
123 |
__NK_ASSERT_DEBUG((base < KUserMemoryLimit) == (top <= KUserMemoryLimit)); |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
124 |
__NK_ASSERT_DEBUG((base < KGlobalMemoryBase) == (top <= KGlobalMemoryBase)); |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
125 |
|
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
126 |
// check that only global memory is mapped into the kernel process |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
127 |
TBool global = base >= KGlobalMemoryBase; |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
128 |
__NK_ASSERT_DEBUG(global || (iAllocatedLinAddrAndOsAsid & KPageMask) != KKernelOsAsid); |
0 | 129 |
|
130 |
// setup attributes... |
|
131 |
PteType() = Mmu::PteType(aPermissions,global); |
|
132 |
iBlankPte = Mmu::BlankPte(aMemory->Attributes(),PteType()); |
|
133 |
||
134 |
// setup base address... |
|
135 |
TUint colourOffset = ((aIndex&KPageColourMask)<<KPageShift); |
|
136 |
if(colourOffset+aCount*KPageSize > iAllocatedSize) |
|
137 |
return KErrTooBig; |
|
138 |
__NK_ASSERT_DEBUG(!iLinAddrAndOsAsid || ((iLinAddrAndOsAsid^iAllocatedLinAddrAndOsAsid)&~(KPageColourMask<<KPageShift))==0); // new, OR, only differ in page colour |
|
139 |
iLinAddrAndOsAsid = iAllocatedLinAddrAndOsAsid+colourOffset; |
|
140 |
||
141 |
// attach to memory object... |
|
142 |
TInt r = Attach(aMemory,aIndex,aCount); |
|
143 |
||
144 |
// cleanup if error... |
|
145 |
if(r!=KErrNone) |
|
146 |
iLinAddrAndOsAsid = 0; |
|
147 |
||
148 |
return r; |
|
149 |
} |
|
150 |
||
151 |
||
152 |
void DMemoryMapping::Unmap() |
|
153 |
{ |
|
154 |
Detach(); |
|
155 |
// we can't clear iLinAddrAndOsAsid here because this may be needed by other code, |
|
156 |
// e.g. DFineMapping::MapPages/UnmapPages/RestrictPages/PageIn |
|
157 |
} |
|
158 |
||
159 |
||
160 |
TInt DMemoryMapping::AllocateVirtualMemory(TMappingCreateFlags aFlags, TInt aOsAsid, TLinAddr aAddr, TUint aSize, TLinAddr aColourOffset) |
|
161 |
{ |
|
162 |
TRACE(("DMemoryMapping[0x%08x]::AllocateVirtualMemory(0x%x,%d,0x%08x,0x%08x,0x%08x)",this,aFlags,aOsAsid,aAddr,aSize,aColourOffset)); |
|
163 |
__NK_ASSERT_DEBUG((aAddr&KPageMask)==0); |
|
164 |
__NK_ASSERT_DEBUG(!iAllocatedLinAddrAndOsAsid); |
|
165 |
__NK_ASSERT_DEBUG(!iAllocatedSize); |
|
166 |
||
167 |
// setup PDE type... |
|
168 |
TUint pdeType = 0; |
|
169 |
if(aFlags&EMappingCreateCommonVirtual) |
|
170 |
pdeType |= EVirtualSlabTypeCommonVirtual; |
|
171 |
if(aFlags&EMappingCreateDemandPaged) |
|
172 |
pdeType |= EVirtualSlabTypeDemandPaged; |
|
173 |
||
174 |
TInt r; |
|
175 |
TUint colourOffset = aColourOffset&(KPageColourMask<<KPageShift); |
|
176 |
TLinAddr addr; |
|
177 |
TUint size; |
|
178 |
if(aFlags&(EMappingCreateFixedVirtual|EMappingCreateAdoptVirtual)) |
|
179 |
{ |
|
180 |
// just use the supplied virtual address... |
|
181 |
__NK_ASSERT_ALWAYS(aAddr); |
|
182 |
__NK_ASSERT_ALWAYS(colourOffset==0); |
|
183 |
__NK_ASSERT_DEBUG((aFlags&EMappingCreateAdoptVirtual)==0 || AddressSpace[aOsAsid]->CheckPdeType(aAddr,aSize,pdeType)); |
|
184 |
addr = aAddr; |
|
185 |
size = aSize; |
|
186 |
r = KErrNone; |
|
187 |
} |
|
188 |
else |
|
189 |
{ |
|
190 |
if(aFlags&(EMappingCreateExactVirtual|EMappingCreateCommonVirtual)) |
|
191 |
{ |
|
192 |
__NK_ASSERT_ALWAYS(aAddr); // address must be specified |
|
193 |
} |
|
194 |
else |
|
195 |
{ |
|
196 |
__NK_ASSERT_ALWAYS(!aAddr); // address shouldn't have been specified |
|
197 |
} |
|
198 |
||
199 |
// adjust for colour... |
|
200 |
TUint allocSize = aSize+colourOffset; |
|
201 |
TUint allocAddr = aAddr; |
|
202 |
if(allocAddr) |
|
203 |
{ |
|
204 |
allocAddr -= colourOffset; |
|
205 |
if(allocAddr&(KPageColourMask<<KPageShift)) |
|
206 |
return KErrArgument; // wrong colour |
|
207 |
} |
|
208 |
||
209 |
// allocate virtual addresses... |
|
210 |
if(aFlags&EMappingCreateUserGlobalVirtual) |
|
211 |
{ |
|
212 |
if(aOsAsid!=(TInt)KKernelOsAsid) |
|
213 |
return KErrArgument; |
|
214 |
r = DAddressSpace::AllocateUserGlobalVirtualMemory(addr,size,allocAddr,allocSize,pdeType); |
|
215 |
} |
|
216 |
else |
|
217 |
r = AddressSpace[aOsAsid]->AllocateVirtualMemory(addr,size,allocAddr,allocSize,pdeType); |
|
218 |
} |
|
219 |
||
220 |
if(r==KErrNone) |
|
221 |
{ |
|
222 |
iAllocatedLinAddrAndOsAsid = addr|aOsAsid; |
|
223 |
iAllocatedSize = size; |
|
224 |
} |
|
225 |
||
226 |
TRACE(("DMemoryMapping[0x%08x]::AllocateVirtualMemory returns %d address=0x%08x",this,r,addr)); |
|
227 |
return r; |
|
228 |
} |
|
229 |
||
230 |
||
231 |
void DMemoryMapping::FreeVirtualMemory() |
|
232 |
{ |
|
233 |
if(!iAllocatedSize) |
|
234 |
return; // no virtual memory to free |
|
235 |
||
236 |
TRACE(("DMemoryMapping[0x%08x]::FreeVirtualMemory()",this)); |
|
237 |
||
238 |
iLinAddrAndOsAsid = 0; |
|
239 |
||
240 |
TLinAddr addr = iAllocatedLinAddrAndOsAsid&~KPageMask; |
|
241 |
TInt osAsid = iAllocatedLinAddrAndOsAsid&KPageMask; |
|
242 |
AddressSpace[osAsid]->FreeVirtualMemory(addr,iAllocatedSize); |
|
243 |
iAllocatedLinAddrAndOsAsid = 0; |
|
244 |
iAllocatedSize = 0; |
|
245 |
} |
|
246 |
||
247 |
||
248 |
||
249 |
// |
|
250 |
// DCoarseMapping |
|
251 |
// |
|
252 |
||
253 |
DCoarseMapping::DCoarseMapping() |
|
254 |
: DMemoryMapping(ECoarseMapping) |
|
255 |
{ |
|
256 |
} |
|
257 |
||
258 |
||
259 |
DCoarseMapping::DCoarseMapping(TUint aFlags) |
|
260 |
: DMemoryMapping(ECoarseMapping|aFlags) |
|
261 |
{ |
|
262 |
} |
|
263 |
||
264 |
||
265 |
DCoarseMapping::~DCoarseMapping() |
|
266 |
{ |
|
267 |
} |
|
268 |
||
269 |
||
270 |
TInt DCoarseMapping::DoMap() |
|
271 |
{ |
|
272 |
TRACE(("DCoarseMapping[0x%08x]::DoMap()", this)); |
|
273 |
__NK_ASSERT_DEBUG(((iStartIndex|iSizeInPages)&(KChunkMask>>KPageShift))==0); // be extra paranoid about alignment |
|
274 |
||
275 |
MmuLock::Lock(); |
|
276 |
TPde* pPde = Mmu::PageDirectoryEntry(OsAsid(),Base()); |
|
277 |
DCoarseMemory* memory = (DCoarseMemory*)Memory(true); // safe because we're called from code which has added mapping to memory |
|
278 |
||
279 |
TUint flash = 0; |
|
280 |
TUint chunk = iStartIndex >> KPagesInPDEShift; |
|
281 |
TUint endChunk = (iStartIndex + iSizeInPages) >> KPagesInPDEShift; |
|
282 |
TBool sectionMappingsBroken = EFalse; |
|
283 |
||
284 |
while(chunk < endChunk) |
|
285 |
{ |
|
286 |
MmuLock::Flash(flash,KMaxPdesInOneGo*2); |
|
287 |
TPte* pt = memory->GetPageTable(PteType(), chunk); |
|
288 |
if(!pt) |
|
289 |
{ |
|
290 |
TRACE2(("!PDE %x=%x (was %x)",pPde,KPdeUnallocatedEntry,*pPde)); |
|
291 |
__NK_ASSERT_DEBUG(*pPde==KPdeUnallocatedEntry); |
|
292 |
} |
|
293 |
else |
|
294 |
{ |
|
295 |
TPde pde = Mmu::PageTablePhysAddr(pt)|iBlankPde; |
|
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
296 |
#ifdef __USER_MEMORY_GUARDS_ENABLED__ |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
297 |
if (IsUserMapping()) |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
298 |
pde = PDE_IN_DOMAIN(pde, USER_MEMORY_DOMAIN); |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
299 |
#endif |
0 | 300 |
TRACE2(("!PDE %x=%x (was %x)",pPde,pde,*pPde)); |
301 |
if (Mmu::PdeMapsSection(*pPde)) |
|
302 |
{ |
|
303 |
// break previous section mapping... |
|
304 |
__NK_ASSERT_DEBUG(*pPde==Mmu::PageToSectionEntry(pt[0],iBlankPde)); |
|
305 |
sectionMappingsBroken = ETrue; |
|
306 |
} |
|
307 |
else |
|
308 |
__NK_ASSERT_DEBUG(*pPde==KPdeUnallocatedEntry || ((*pPde^pde)&~KPdeMatchMask)==0); |
|
309 |
*pPde = pde; |
|
310 |
SinglePdeUpdated(pPde); |
|
311 |
flash += 3; // increase flash rate because we've done quite a bit more work |
|
312 |
} |
|
313 |
++pPde; |
|
314 |
++chunk; |
|
315 |
} |
|
316 |
MmuLock::Unlock(); |
|
317 |
||
318 |
if (sectionMappingsBroken) |
|
319 |
{ |
|
320 |
// We must invalidate the TLB since we broke section mappings created by the bootstrap. |
|
321 |
// Since this will only ever happen on boot, we just invalidate the entire TLB for this |
|
322 |
// process. |
|
323 |
InvalidateTLBForAsid(OsAsid()); |
|
324 |
} |
|
325 |
||
326 |
return KErrNone; |
|
327 |
} |
|
328 |
||
329 |
||
330 |
void DCoarseMapping::DoUnmap() |
|
331 |
{ |
|
332 |
TRACE(("DCoarseMapping[0x%08x]::DoUnmap()", this)); |
|
333 |
MmuLock::Lock(); |
|
334 |
TPde* pPde = Mmu::PageDirectoryEntry(OsAsid(),Base()); |
|
335 |
TPde* pPdeEnd = pPde+(iSizeInPages>>(KChunkShift-KPageShift)); |
|
336 |
TUint flash = 0; |
|
337 |
do |
|
338 |
{ |
|
339 |
MmuLock::Flash(flash,KMaxPdesInOneGo); |
|
340 |
TPde pde = KPdeUnallocatedEntry; |
|
341 |
TRACE2(("!PDE %x=%x",pPde,pde)); |
|
342 |
*pPde = pde; |
|
343 |
SinglePdeUpdated(pPde); |
|
344 |
++pPde; |
|
345 |
} |
|
346 |
while(pPde<pPdeEnd); |
|
347 |
MmuLock::Unlock(); |
|
348 |
||
349 |
InvalidateTLBForAsid(OsAsid()); |
|
350 |
} |
|
351 |
||
352 |
||
353 |
TInt DCoarseMapping::MapPages(RPageArray::TIter aPages, TUint aMapInstanceCount) |
|
354 |
{ |
|
355 |
// shouldn't ever be called because coarse mappings don't have their own page tables... |
|
356 |
__NK_ASSERT_DEBUG(0); |
|
357 |
return KErrNotSupported; |
|
358 |
} |
|
359 |
||
360 |
||
361 |
void DCoarseMapping::UnmapPages(RPageArray::TIter aPages, TUint aMapInstanceCount) |
|
362 |
{ |
|
363 |
// shouldn't ever be called because coarse mappings don't have their own page tables... |
|
364 |
__NK_ASSERT_DEBUG(0); |
|
365 |
} |
|
366 |
||
367 |
void DCoarseMapping::RemapPage(TPhysAddr& aPageArray, TUint aIndex, TUint aMapInstanceCount, TBool aInvalidateTLB) |
|
368 |
{ |
|
369 |
// shouldn't ever be called because coarse mappings don't have their own page tables... |
|
370 |
__NK_ASSERT_DEBUG(0); |
|
371 |
} |
|
372 |
||
373 |
void DCoarseMapping::RestrictPagesNA(RPageArray::TIter aPages, TUint aMapInstanceCount) |
|
374 |
{ |
|
375 |
// shouldn't ever be called because coarse mappings don't have their own page tables... |
|
376 |
__NK_ASSERT_DEBUG(0); |
|
377 |
} |
|
378 |
||
379 |
||
380 |
TInt DCoarseMapping::PageIn(RPageArray::TIter aPages, TPinArgs& aPinArgs, TUint aMapInstanceCount) |
|
381 |
{ |
|
382 |
MmuLock::Lock(); |
|
383 |
||
384 |
if(!IsAttached()) |
|
385 |
{ |
|
386 |
MmuLock::Unlock(); |
|
387 |
return KErrNotFound; |
|
388 |
} |
|
389 |
||
390 |
DCoarseMemory* memory = (DCoarseMemory*)Memory(true); // safe because we've checked mapping IsAttached |
|
391 |
return memory->PageIn(this, aPages, aPinArgs, aMapInstanceCount); |
|
392 |
} |
|
393 |
||
394 |
||
395 |
TBool DCoarseMapping::MovingPageIn(TPhysAddr& aPageArrayPtr, TUint aIndex) |
|
396 |
{ |
|
397 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
398 |
__NK_ASSERT_DEBUG(IsAttached()); |
|
399 |
||
400 |
DCoarseMemory* memory = (DCoarseMemory*)Memory(true); // safe because we've checked mapping IsAttached |
|
401 |
TBool success = memory->MovingPageIn(this, aPageArrayPtr, aIndex); |
|
402 |
if (success) |
|
403 |
{ |
|
404 |
TLinAddr addr = Base() + (aIndex - iStartIndex) * KPageSize; |
|
405 |
InvalidateTLBForPage(addr); |
|
406 |
} |
|
407 |
return success; |
|
408 |
} |
|
409 |
||
410 |
||
411 |
TPte* DCoarseMapping::FindPageTable(TLinAddr aLinAddr, TUint aMemoryIndex) |
|
412 |
{ |
|
413 |
TRACE(("DCoarseMapping::FindPageTable(0x%x, %d)", aLinAddr, aMemoryIndex)); |
|
414 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
415 |
__NK_ASSERT_DEBUG(IsAttached()); |
|
416 |
DCoarseMemory* memory = (DCoarseMemory*)Memory(true); // safe because we've checked mapping IsAttached |
|
417 |
return memory->FindPageTable(this, aLinAddr, aMemoryIndex); |
|
418 |
} |
|
419 |
||
420 |
||
421 |
||
422 |
// |
|
423 |
// DFineMapping |
|
424 |
// |
|
425 |
||
426 |
DFineMapping::DFineMapping() |
|
427 |
: DMemoryMapping(0) |
|
428 |
{ |
|
429 |
} |
|
430 |
||
431 |
||
432 |
DFineMapping::~DFineMapping() |
|
433 |
{ |
|
434 |
TRACE(("DFineMapping[0x%08x]::~DFineMapping()",this)); |
|
435 |
FreePermanentPageTables(); |
|
436 |
} |
|
437 |
||
438 |
#ifdef _DEBUG |
|
439 |
void DFineMapping::ValidatePageTable(TPte* aPt, TLinAddr aAddr) |
|
440 |
{ |
|
441 |
if(aPt) |
|
442 |
{ |
|
443 |
// check page table is correct... |
|
444 |
SPageTableInfo* pti = SPageTableInfo::FromPtPtr(aPt); |
|
445 |
__NK_ASSERT_DEBUG(pti->CheckFine(aAddr&~KChunkMask,OsAsid())); |
|
446 |
DMemoryObject* memory = Memory(); |
|
447 |
if(memory) |
|
448 |
{ |
|
449 |
if(memory->IsDemandPaged() && !IsPinned() && !(Flags()&EPageTablesAllocated)) |
|
450 |
__NK_ASSERT_DEBUG(pti->IsDemandPaged()); |
|
451 |
else |
|
452 |
__NK_ASSERT_DEBUG(!pti->IsDemandPaged()); |
|
453 |
} |
|
454 |
} |
|
455 |
} |
|
456 |
#endif |
|
457 |
||
458 |
TPte* DFineMapping::GetPageTable(TLinAddr aAddr) |
|
459 |
{ |
|
460 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
461 |
||
462 |
// get address of PDE which refers to the page table... |
|
463 |
TPde* pPde = Mmu::PageDirectoryEntry(OsAsid(),aAddr); |
|
464 |
||
465 |
// get page table... |
|
466 |
TPte* pt = Mmu::PageTableFromPde(*pPde); |
|
467 |
#ifdef _DEBUG |
|
468 |
ValidatePageTable(pt, aAddr); |
|
469 |
#endif |
|
470 |
return pt; |
|
471 |
} |
|
472 |
||
473 |
||
474 |
TPte* DFineMapping::GetOrAllocatePageTable(TLinAddr aAddr) |
|
475 |
{ |
|
476 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
477 |
||
478 |
// get address of PDE which refers to the page table... |
|
479 |
TPde* pPde = Mmu::PageDirectoryEntry(OsAsid(),aAddr); |
|
480 |
||
481 |
// get page table... |
|
482 |
TPte* pt = Mmu::PageTableFromPde(*pPde); |
|
483 |
if(!pt) |
|
484 |
{ |
|
485 |
pt = AllocatePageTable(aAddr,pPde); |
|
486 |
#ifdef _DEBUG |
|
487 |
ValidatePageTable(pt, aAddr); |
|
488 |
#endif |
|
489 |
} |
|
490 |
||
491 |
return pt; |
|
492 |
} |
|
493 |
||
494 |
||
495 |
TPte* DFineMapping::GetOrAllocatePageTable(TLinAddr aAddr, TPinArgs& aPinArgs) |
|
496 |
{ |
|
497 |
__NK_ASSERT_DEBUG(aPinArgs.iPinnedPageTables); |
|
498 |
||
499 |
if(!aPinArgs.HaveSufficientPages(KNumPagesToPinOnePageTable)) |
|
500 |
return 0; |
|
501 |
||
502 |
TPte* pinnedPt = 0; |
|
503 |
for(;;) |
|
504 |
{ |
|
505 |
TPte* pt = GetOrAllocatePageTable(aAddr); |
|
506 |
||
507 |
if(pinnedPt && pinnedPt!=pt) |
|
508 |
{ |
|
509 |
// previously pinned page table not needed... |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
510 |
::PageTables.UnpinPageTable(pinnedPt,aPinArgs); |
0 | 511 |
|
512 |
// make sure we have memory for next pin attempt... |
|
513 |
MmuLock::Unlock(); |
|
514 |
aPinArgs.AllocReplacementPages(KNumPagesToPinOnePageTable); |
|
515 |
if(!aPinArgs.HaveSufficientPages(KNumPagesToPinOnePageTable)) // if out of memory... |
|
516 |
{ |
|
517 |
// make sure we free any unneeded page table we allocated... |
|
518 |
if(pt) |
|
519 |
FreePageTable(Mmu::PageDirectoryEntry(OsAsid(),aAddr)); |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
520 |
MmuLock::Lock(); |
0 | 521 |
return 0; |
522 |
} |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
523 |
MmuLock::Lock(); |
0 | 524 |
} |
525 |
||
526 |
if(!pt) |
|
527 |
return 0; // out of memory |
|
528 |
||
529 |
if(pt==pinnedPt) |
|
530 |
{ |
|
531 |
// we got a page table and it was pinned... |
|
532 |
*aPinArgs.iPinnedPageTables++ = pt; |
|
533 |
++aPinArgs.iNumPinnedPageTables; |
|
534 |
return pt; |
|
535 |
} |
|
536 |
||
537 |
// don't pin page table if it's not paged (e.g. unpaged part of ROM)... |
|
538 |
SPageTableInfo* pti = SPageTableInfo::FromPtPtr(pt); |
|
539 |
if(!pti->IsDemandPaged()) |
|
540 |
return pt; |
|
541 |
||
542 |
// pin the page table... |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
543 |
if (::PageTables.PinPageTable(pt,aPinArgs) != KErrNone) |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
544 |
{// Couldn't pin the page table... |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
545 |
MmuLock::Unlock(); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
546 |
// make sure we free any unneeded page table we allocated... |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
547 |
FreePageTable(Mmu::PageDirectoryEntry(OsAsid(),aAddr)); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
548 |
MmuLock::Lock(); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
549 |
return 0; |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
550 |
} |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
551 |
|
0 | 552 |
pinnedPt = pt; |
553 |
} |
|
554 |
} |
|
555 |
||
556 |
||
557 |
TInt DFineMapping::AllocateVirtualMemory(TMappingCreateFlags aFlags, TInt aOsAsid, TLinAddr aAddr, TUint aSize, TLinAddr aColourOffset) |
|
558 |
{ |
|
559 |
TInt r = DMemoryMapping::AllocateVirtualMemory(aFlags,aOsAsid,aAddr,aSize,aColourOffset); |
|
560 |
if(r==KErrNone && (Flags()&EPermanentPageTables)) |
|
561 |
{ |
|
562 |
r = AllocatePermanentPageTables(); |
|
563 |
if(r!=KErrNone) |
|
564 |
FreeVirtualMemory(); |
|
565 |
} |
|
566 |
return r; |
|
567 |
} |
|
568 |
||
569 |
||
570 |
void DFineMapping::FreeVirtualMemory() |
|
571 |
{ |
|
572 |
FreePermanentPageTables(); |
|
573 |
DMemoryMapping::FreeVirtualMemory(); |
|
574 |
} |
|
575 |
||
576 |
||
577 |
TPte* DFineMapping::AllocatePageTable(TLinAddr aAddr, TPde* aPdeAddress, TBool aPermanent) |
|
578 |
{ |
|
579 |
TRACE2(("DFineMapping[0x%08x]::AllocatePageTable(0x%08x,0x%08x,%d)",this,aAddr,aPdeAddress,aPermanent)); |
|
580 |
||
581 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
582 |
||
583 |
for(;;) |
|
584 |
{ |
|
585 |
// mapping is going, so we don't need a page table any more... |
|
586 |
if(BeingDetached()) |
|
587 |
return 0; |
|
588 |
||
589 |
// get paged state... |
|
590 |
TBool demandPaged = false; |
|
591 |
if(!aPermanent) |
|
592 |
{ |
|
593 |
DMemoryObject* memory = Memory(); |
|
594 |
__NK_ASSERT_DEBUG(memory); // can't be NULL because not BeingDetached() |
|
595 |
demandPaged = memory->IsDemandPaged(); |
|
596 |
} |
|
597 |
||
598 |
// get page table... |
|
599 |
TPte* pt = Mmu::PageTableFromPde(*aPdeAddress); |
|
600 |
if(pt!=0) |
|
601 |
{ |
|
602 |
// we have a page table... |
|
603 |
__NK_ASSERT_DEBUG(SPageTableInfo::FromPtPtr(pt)->CheckFine(aAddr&~KChunkMask,iAllocatedLinAddrAndOsAsid&KPageMask)); |
|
604 |
if(aPermanent) |
|
605 |
{ |
|
606 |
__NK_ASSERT_DEBUG(BeingDetached()==false); |
|
607 |
__NK_ASSERT_ALWAYS(!demandPaged); |
|
608 |
SPageTableInfo* pti = SPageTableInfo::FromPtPtr(pt); |
|
609 |
pti->IncPermanenceCount(); |
|
610 |
} |
|
611 |
return pt; |
|
612 |
} |
|
613 |
||
614 |
// allocate a new page table... |
|
615 |
MmuLock::Unlock(); |
|
616 |
::PageTables.Lock(); |
|
617 |
TPte* newPt = ::PageTables.Alloc(demandPaged); |
|
618 |
if(!newPt) |
|
619 |
{ |
|
620 |
// out of memory... |
|
621 |
::PageTables.Unlock(); |
|
622 |
MmuLock::Lock(); |
|
623 |
return 0; |
|
624 |
} |
|
625 |
||
626 |
// check if new page table is still needed... |
|
627 |
MmuLock::Lock(); |
|
628 |
pt = Mmu::PageTableFromPde(*aPdeAddress); |
|
629 |
if(pt) |
|
630 |
{ |
|
631 |
// someone else has already allocated a page table, |
|
632 |
// so free the one we just allocated and try again... |
|
633 |
MmuLock::Unlock(); |
|
634 |
::PageTables.Free(newPt); |
|
635 |
} |
|
636 |
else if(BeingDetached()) |
|
637 |
{ |
|
638 |
// mapping is going, so we don't need a page table any more... |
|
639 |
MmuLock::Unlock(); |
|
640 |
::PageTables.Free(newPt); |
|
641 |
::PageTables.Unlock(); |
|
642 |
MmuLock::Lock(); |
|
643 |
return 0; |
|
644 |
} |
|
645 |
else |
|
646 |
{ |
|
647 |
// setup new page table... |
|
648 |
SPageTableInfo* pti = SPageTableInfo::FromPtPtr(newPt); |
|
649 |
pti->SetFine(aAddr&~KChunkMask,iAllocatedLinAddrAndOsAsid&KPageMask); |
|
650 |
||
651 |
TPde pde = Mmu::PageTablePhysAddr(newPt)|iBlankPde; |
|
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
652 |
#ifdef __USER_MEMORY_GUARDS_ENABLED__ |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
653 |
if (IsUserMapping()) |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
654 |
pde = PDE_IN_DOMAIN(pde, USER_MEMORY_DOMAIN); |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
655 |
#endif |
0 | 656 |
TRACE2(("!PDE %x=%x",aPdeAddress,pde)); |
657 |
__NK_ASSERT_DEBUG(((*aPdeAddress^pde)&~KPdeMatchMask)==0 || *aPdeAddress==KPdeUnallocatedEntry); |
|
658 |
*aPdeAddress = pde; |
|
659 |
SinglePdeUpdated(aPdeAddress); |
|
660 |
||
661 |
MmuLock::Unlock(); |
|
662 |
} |
|
663 |
||
664 |
// loop back and recheck... |
|
665 |
::PageTables.Unlock(); |
|
666 |
MmuLock::Lock(); |
|
667 |
} |
|
668 |
} |
|
669 |
||
670 |
||
671 |
void DFineMapping::FreePageTable(TPde* aPdeAddress) |
|
672 |
{ |
|
673 |
TRACE2(("DFineMapping[0x%08x]::FreePageTable(0x%08x)",this,aPdeAddress)); |
|
674 |
||
675 |
// get page table lock... |
|
676 |
::PageTables.Lock(); |
|
677 |
MmuLock::Lock(); |
|
678 |
||
679 |
// find page table... |
|
680 |
TPte* pt = Mmu::PageTableFromPde(*aPdeAddress); |
|
681 |
if(pt) |
|
682 |
{ |
|
683 |
SPageTableInfo* pti = SPageTableInfo::FromPtPtr(pt); |
|
684 |
if(pti->PageCount() || pti->PermanenceCount()) |
|
685 |
{ |
|
686 |
// page table still in use, so don't free it... |
|
687 |
pt = 0; |
|
688 |
} |
|
689 |
else |
|
690 |
{ |
|
691 |
// page table not used, so unmap it... |
|
692 |
TPde pde = KPdeUnallocatedEntry; |
|
693 |
TRACE2(("!PDE %x=%x",aPdeAddress,pde)); |
|
694 |
*aPdeAddress = pde; |
|
695 |
SinglePdeUpdated(aPdeAddress); |
|
696 |
} |
|
697 |
} |
|
698 |
||
699 |
MmuLock::Unlock(); |
|
700 |
if(pt) |
|
701 |
::PageTables.Free(pt); |
|
702 |
::PageTables.Unlock(); |
|
703 |
} |
|
704 |
||
705 |
||
706 |
void DFineMapping::RemapPage(TPhysAddr& aPageArray, TUint aIndex, TUint aMapInstanceCount, TBool aInvalidateTLB) |
|
707 |
{ |
|
708 |
TRACE2(("DFineMemoryMapping[0x%08x]::RemapPage(0x%x,0x%x,%d,%d)",this,aPageArray,aIndex,aMapInstanceCount,aInvalidateTLB)); |
|
709 |
||
710 |
__NK_ASSERT_DEBUG(aIndex >= iStartIndex); |
|
711 |
__NK_ASSERT_DEBUG(aIndex < iStartIndex + iSizeInPages); |
|
712 |
||
713 |
TLinAddr addr = Base() + ((aIndex - iStartIndex) << KPageShift); |
|
714 |
TUint pteIndex = (addr >> KPageShift) & (KChunkMask >> KPageShift); |
|
715 |
||
716 |
// get address of page table... |
|
717 |
MmuLock::Lock(); |
|
718 |
TPte* pPte = GetPageTable(addr); |
|
719 |
||
720 |
// check the page is still mapped and mapping isn't being detached |
|
721 |
// or hasn't been reused for another purpose... |
|
722 |
if(!pPte || BeingDetached() || aMapInstanceCount != MapInstanceCount()) |
|
723 |
{ |
|
724 |
// can't map pages to this mapping any more so just exit. |
|
725 |
MmuLock::Unlock(); |
|
726 |
return; |
|
727 |
} |
|
728 |
||
729 |
// remap the page... |
|
730 |
pPte += pteIndex; |
|
731 |
Mmu::RemapPage(pPte, aPageArray, iBlankPte); |
|
732 |
MmuLock::Unlock(); |
|
733 |
||
734 |
#ifndef COARSE_GRAINED_TLB_MAINTENANCE |
|
735 |
// clean TLB... |
|
736 |
if (aInvalidateTLB) |
|
737 |
{ |
|
738 |
InvalidateTLBForPage(addr + OsAsid()); |
|
739 |
} |
|
740 |
#endif |
|
741 |
} |
|
742 |
||
743 |
||
744 |
TInt DFineMapping::MapPages(RPageArray::TIter aPages, TUint aMapInstanceCount) |
|
745 |
{ |
|
746 |
TRACE2(("DFineMapping[0x%08x]::MapPages(?,%d) index=0x%x count=0x%x",this,aMapInstanceCount,aPages.Index(),aPages.Count())); |
|
747 |
||
748 |
__NK_ASSERT_DEBUG(aPages.Count()); |
|
749 |
__NK_ASSERT_DEBUG(aPages.Index()>=iStartIndex); |
|
750 |
__NK_ASSERT_DEBUG(aPages.IndexEnd()-iStartIndex<=iSizeInPages); |
|
751 |
||
752 |
TLinAddr addr = Base()+(aPages.Index()-iStartIndex)*KPageSize; |
|
753 |
for(;;) |
|
754 |
{ |
|
755 |
TUint pteIndex = (addr>>KPageShift)&(KChunkMask>>KPageShift); |
|
756 |
||
757 |
// calculate max number of pages to do... |
|
758 |
TUint n = (KChunkSize>>KPageShift)-pteIndex; // pages left in page table |
|
759 |
if(n>KMaxPagesInOneGo) |
|
760 |
n = KMaxPagesInOneGo; |
|
761 |
||
762 |
// get some pages... |
|
763 |
TPhysAddr* pages; |
|
764 |
n = aPages.Pages(pages,n); |
|
765 |
if(!n) |
|
766 |
break; |
|
767 |
||
768 |
// get address of page table... |
|
769 |
MmuLock::Lock(); |
|
770 |
TPte* pPte = GetOrAllocatePageTable(addr); |
|
771 |
||
772 |
// check mapping isn't being unmapped, or been reused for another purpose... |
|
773 |
if(BeingDetached() || aMapInstanceCount!=MapInstanceCount()) |
|
774 |
{ |
|
775 |
// can't map pages to this mapping any more, so free any page table |
|
776 |
// we just got (if it's not used)... |
|
777 |
if(!pPte) |
|
778 |
MmuLock::Unlock(); |
|
779 |
else |
|
780 |
{ |
|
781 |
SPageTableInfo* pti = SPageTableInfo::FromPtPtr(pPte); |
|
782 |
TBool keepPt = pti->PermanenceCount() || pti->PageCount(); |
|
783 |
MmuLock::Unlock(); |
|
784 |
if(!keepPt) |
|
785 |
FreePageTable(Mmu::PageDirectoryEntry(OsAsid(),addr)); |
|
786 |
} |
|
787 |
// then end... |
|
788 |
return KErrNone; |
|
789 |
} |
|
790 |
||
791 |
// check for OOM... |
|
792 |
if(!pPte) |
|
793 |
{ |
|
794 |
MmuLock::Unlock(); |
|
795 |
return KErrNoMemory; |
|
796 |
} |
|
797 |
||
798 |
// map some pages... |
|
799 |
pPte += pteIndex; |
|
800 |
TBool keepPt = Mmu::MapPages(pPte, n, pages, iBlankPte); |
|
801 |
MmuLock::Unlock(); |
|
802 |
||
803 |
// free page table if no longer needed... |
|
804 |
if(!keepPt) |
|
805 |
FreePageTable(Mmu::PageDirectoryEntry(OsAsid(),addr)); |
|
806 |
||
807 |
// move on... |
|
808 |
aPages.Skip(n); |
|
809 |
addr += n*KPageSize; |
|
810 |
} |
|
811 |
||
812 |
return KErrNone; |
|
813 |
} |
|
814 |
||
815 |
||
816 |
void DFineMapping::UnmapPages(RPageArray::TIter aPages, TUint aMapInstanceCount) |
|
817 |
{ |
|
818 |
TRACE2(("DFineMapping[0x%08x]::UnmapPages(?,%d) index=0x%x count=0x%x",this,aMapInstanceCount,aPages.Index(),aPages.Count())); |
|
819 |
||
820 |
__NK_ASSERT_DEBUG(aPages.Count()); |
|
821 |
||
822 |
TLinAddr addr = Base()+(aPages.Index()-iStartIndex)*KPageSize; |
|
823 |
#ifndef COARSE_GRAINED_TLB_MAINTENANCE |
|
824 |
TLinAddr startAddr = addr; |
|
825 |
#endif |
|
826 |
for(;;) |
|
827 |
{ |
|
828 |
TUint pteIndex = (addr>>KPageShift)&(KChunkMask>>KPageShift); |
|
829 |
||
830 |
// calculate max number of pages to do... |
|
831 |
TUint n = (KChunkSize>>KPageShift)-pteIndex; // pages left in page table |
|
832 |
if(n>KMaxPagesInOneGo) |
|
833 |
n = KMaxPagesInOneGo; |
|
834 |
||
835 |
// get some pages... |
|
836 |
TPhysAddr* pages; |
|
837 |
n = aPages.Pages(pages,n); |
|
838 |
if(!n) |
|
839 |
break; |
|
840 |
||
841 |
MmuLock::Lock(); |
|
842 |
||
843 |
// check that mapping hasn't been reused for another purpose... |
|
844 |
if(aMapInstanceCount!=MapInstanceCount()) |
|
845 |
{ |
|
846 |
MmuLock::Unlock(); |
|
847 |
break; |
|
848 |
} |
|
849 |
||
850 |
// get address of PTE for pages... |
|
851 |
TPde* pPde = Mmu::PageDirectoryEntry(OsAsid(),addr); |
|
852 |
TPte* pPte = Mmu::PageTableFromPde(*pPde); |
|
853 |
if(pPte) |
|
854 |
{ |
|
855 |
// unmap some pages... |
|
856 |
pPte += pteIndex; |
|
857 |
TBool keepPt = Mmu::UnmapPages(pPte,n,pages); |
|
858 |
MmuLock::Unlock(); |
|
859 |
||
860 |
// free page table if no longer needed... |
|
861 |
if(!keepPt) |
|
862 |
FreePageTable(pPde); |
|
863 |
} |
|
864 |
else |
|
865 |
{ |
|
866 |
// no page table found... |
|
867 |
MmuLock::Unlock(); |
|
868 |
} |
|
869 |
||
870 |
// move on... |
|
871 |
aPages.Skip(n); |
|
872 |
addr += n*KPageSize; |
|
873 |
} |
|
874 |
||
875 |
#ifndef COARSE_GRAINED_TLB_MAINTENANCE |
|
876 |
// clean TLB... |
|
877 |
TLinAddr endAddr = addr; |
|
878 |
addr = startAddr+OsAsid(); |
|
879 |
do InvalidateTLBForPage(addr); |
|
880 |
while((addr+=KPageSize)<endAddr); |
|
881 |
#endif |
|
882 |
} |
|
883 |
||
884 |
||
885 |
void DFineMapping::RestrictPagesNA(RPageArray::TIter aPages, TUint aMapInstanceCount) |
|
886 |
{ |
|
887 |
TRACE2(("DFineMapping[0x%08x]::RestrictPages(?,%d) index=0x%x count=0x%x",this,aMapInstanceCount,aPages.Index(),aPages.Count())); |
|
888 |
||
889 |
__NK_ASSERT_DEBUG(aPages.Count()); |
|
890 |
||
891 |
TLinAddr addr = Base()+(aPages.Index()-iStartIndex)*KPageSize; |
|
892 |
#ifndef COARSE_GRAINED_TLB_MAINTENANCE |
|
893 |
TLinAddr startAddr = addr; |
|
894 |
#endif |
|
895 |
for(;;) |
|
896 |
{ |
|
897 |
TUint pteIndex = (addr>>KPageShift)&(KChunkMask>>KPageShift); |
|
898 |
||
899 |
// calculate max number of pages to do... |
|
900 |
TUint n = (KChunkSize>>KPageShift)-pteIndex; // pages left in page table |
|
901 |
if(n>KMaxPagesInOneGo) |
|
902 |
n = KMaxPagesInOneGo; |
|
903 |
||
904 |
// get some pages... |
|
905 |
TPhysAddr* pages; |
|
906 |
n = aPages.Pages(pages,n); |
|
907 |
if(!n) |
|
908 |
break; |
|
909 |
||
910 |
MmuLock::Lock(); |
|
911 |
||
912 |
// check that mapping hasn't been reused for another purpose... |
|
913 |
if(aMapInstanceCount!=MapInstanceCount()) |
|
914 |
{ |
|
915 |
MmuLock::Unlock(); |
|
916 |
break; |
|
917 |
} |
|
918 |
||
919 |
// get address of PTE for pages... |
|
920 |
TPde* pPde = Mmu::PageDirectoryEntry(OsAsid(),addr); |
|
921 |
TPte* pPte = Mmu::PageTableFromPde(*pPde); |
|
922 |
if(pPte) |
|
923 |
{ |
|
924 |
// restrict some pages... |
|
925 |
pPte += pteIndex; |
|
926 |
Mmu::RestrictPagesNA(pPte,n,pages); |
|
927 |
} |
|
928 |
MmuLock::Unlock(); |
|
929 |
||
930 |
// move on... |
|
931 |
aPages.Skip(n); |
|
932 |
addr += n*KPageSize; |
|
933 |
} |
|
934 |
||
935 |
#ifndef COARSE_GRAINED_TLB_MAINTENANCE |
|
936 |
// clean TLB... |
|
937 |
TLinAddr endAddr = addr; |
|
938 |
addr = startAddr+OsAsid(); |
|
939 |
do InvalidateTLBForPage(addr); |
|
940 |
while((addr+=KPageSize)<endAddr); |
|
941 |
#endif |
|
942 |
} |
|
943 |
||
944 |
||
945 |
TInt DFineMapping::PageIn(RPageArray::TIter aPages, TPinArgs& aPinArgs, TUint aMapInstanceCount) |
|
946 |
{ |
|
947 |
TRACE2(("DFineMapping[0x%08x]::PageIn(?,?,%d) index=0x%x count=0x%x",this,aMapInstanceCount,aPages.Index(),aPages.Count())); |
|
948 |
||
949 |
__NK_ASSERT_DEBUG(aPages.Count()); |
|
950 |
__NK_ASSERT_DEBUG(aPages.Index()>=iStartIndex); |
|
951 |
__NK_ASSERT_DEBUG(aPages.IndexEnd()-iStartIndex<=iSizeInPages); |
|
952 |
||
953 |
TInt r = KErrNone; |
|
954 |
||
955 |
TLinAddr addr = Base()+(aPages.Index()-iStartIndex)*KPageSize; |
|
956 |
#ifndef COARSE_GRAINED_TLB_MAINTENANCE |
|
957 |
TLinAddr startAddr = addr; |
|
958 |
#endif |
|
959 |
TBool pinPageTable = aPinArgs.iPinnedPageTables!=0; // check if we need to pin the first page table |
|
960 |
for(;;) |
|
961 |
{ |
|
962 |
TUint pteIndex = (addr>>KPageShift)&(KChunkMask>>KPageShift); |
|
963 |
if(pteIndex==0) |
|
964 |
pinPageTable = aPinArgs.iPinnedPageTables!=0; // started a new page table, check if we need to pin it |
|
965 |
||
966 |
// calculate max number of pages to do... |
|
967 |
TUint n = (KChunkSize>>KPageShift)-pteIndex; // pages left in page table |
|
968 |
if(n>KMaxPagesInOneGo) |
|
969 |
n = KMaxPagesInOneGo; |
|
970 |
||
971 |
// get some pages... |
|
972 |
TPhysAddr* pages; |
|
973 |
n = aPages.Pages(pages,n); |
|
974 |
if(!n) |
|
975 |
break; |
|
976 |
||
977 |
// make sure we have memory to pin the page table if required... |
|
978 |
if(pinPageTable) |
|
979 |
aPinArgs.AllocReplacementPages(KNumPagesToPinOnePageTable); |
|
980 |
||
981 |
// get address of page table... |
|
982 |
MmuLock::Lock(); |
|
983 |
TPte* pPte; |
|
984 |
if(pinPageTable) |
|
985 |
pPte = GetOrAllocatePageTable(addr,aPinArgs); |
|
986 |
else |
|
987 |
pPte = GetOrAllocatePageTable(addr); |
|
988 |
||
989 |
// check mapping isn't being unmapped or hasn't been reused... |
|
990 |
if(BeingDetached() || aMapInstanceCount != MapInstanceCount()) |
|
991 |
{ |
|
992 |
// can't map pages to this mapping any more, so free any page table |
|
993 |
// we just got (if it's not used)... |
|
994 |
if(!pPte) |
|
995 |
MmuLock::Unlock(); |
|
996 |
else |
|
997 |
{ |
|
998 |
SPageTableInfo* pti = SPageTableInfo::FromPtPtr(pPte); |
|
999 |
TBool keepPt = pti->PermanenceCount() || pti->PageCount(); |
|
1000 |
MmuLock::Unlock(); |
|
1001 |
if(!keepPt) |
|
1002 |
FreePageTable(Mmu::PageDirectoryEntry(OsAsid(),addr)); |
|
1003 |
} |
|
1004 |
// then end... |
|
1005 |
r = KErrNotFound; |
|
1006 |
break; |
|
1007 |
} |
|
1008 |
||
1009 |
// check for OOM... |
|
1010 |
if(!pPte) |
|
1011 |
{ |
|
1012 |
MmuLock::Unlock(); |
|
1013 |
r = KErrNoMemory; |
|
1014 |
break; |
|
1015 |
} |
|
1016 |
||
1017 |
// map some pages... |
|
1018 |
pPte += pteIndex; |
|
1019 |
TPte blankPte = iBlankPte; |
|
1020 |
if(aPinArgs.iReadOnly) |
|
1021 |
blankPte = Mmu::MakePteInaccessible(blankPte,true); |
|
1022 |
TBool keepPt = Mmu::PageInPages(pPte, n, pages, blankPte); |
|
1023 |
MmuLock::Unlock(); |
|
1024 |
||
1025 |
// free page table if no longer needed... |
|
1026 |
if(!keepPt) |
|
1027 |
FreePageTable(Mmu::PageDirectoryEntry(OsAsid(),addr)); |
|
1028 |
||
1029 |
// move on... |
|
1030 |
aPages.Skip(n); |
|
1031 |
addr += n*KPageSize; |
|
1032 |
pinPageTable = false; |
|
1033 |
} |
|
1034 |
||
1035 |
#ifndef COARSE_GRAINED_TLB_MAINTENANCE |
|
1036 |
// clean TLB... |
|
1037 |
TLinAddr endAddr = addr; |
|
1038 |
addr = startAddr+OsAsid(); |
|
1039 |
do InvalidateTLBForPage(addr); |
|
1040 |
while((addr+=KPageSize)<endAddr); |
|
1041 |
#endif |
|
1042 |
return r; |
|
1043 |
} |
|
1044 |
||
1045 |
||
1046 |
TBool DFineMapping::MovingPageIn(TPhysAddr& aPageArrayPtr, TUint aIndex) |
|
1047 |
{ |
|
1048 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
1049 |
__NK_ASSERT_DEBUG(IsAttached()); |
|
1050 |
__NK_ASSERT_DEBUG(!BeingDetached()); |
|
1051 |
||
1052 |
TLinAddr addr = Base() + (aIndex - iStartIndex) * KPageSize; |
|
1053 |
TUint pteIndex = (addr >> KPageShift) & (KChunkMask >> KPageShift); |
|
1054 |
||
1055 |
// get address of page table... |
|
1056 |
TPte* pPte = GetPageTable(addr); |
|
1057 |
||
1058 |
// Check the page is still mapped. |
|
1059 |
if (!pPte) |
|
1060 |
return EFalse; |
|
1061 |
||
1062 |
// map some pages... |
|
1063 |
pPte += pteIndex; |
|
1064 |
Mmu::RemapPage(pPte, aPageArrayPtr, iBlankPte); |
|
1065 |
InvalidateTLBForPage(addr); |
|
1066 |
return ETrue; |
|
1067 |
} |
|
1068 |
||
1069 |
||
1070 |
TInt DFineMapping::DoMap() |
|
1071 |
{ |
|
1072 |
TRACE(("DFineMapping[0x%08x]::DoMap()", this)); |
|
1073 |
DMemoryObject* memory = Memory(true); // safe because we're called from code which has added mapping to memory |
|
1074 |
if(memory->IsDemandPaged()) |
|
1075 |
{ |
|
1076 |
// do nothing, allow pages to be mapped on demand... |
|
1077 |
return KErrNone; |
|
1078 |
} |
|
1079 |
||
1080 |
RPageArray::TIter pageIter; |
|
1081 |
memory->iPages.FindStart(iStartIndex,iSizeInPages,pageIter); |
|
1082 |
||
1083 |
// map pages... |
|
1084 |
TInt r = KErrNone; |
|
1085 |
for(;;) |
|
1086 |
{ |
|
1087 |
// find some pages... |
|
1088 |
RPageArray::TIter pageList; |
|
1089 |
TUint n = pageIter.Find(pageList); |
|
1090 |
if(!n) |
|
1091 |
break; // done |
|
1092 |
||
1093 |
// map some pages... |
|
1094 |
r = MapPages(pageList,MapInstanceCount()); |
|
1095 |
||
1096 |
// done with pages... |
|
1097 |
pageIter.FindRelease(n); |
|
1098 |
||
1099 |
if(r!=KErrNone) |
|
1100 |
break; |
|
1101 |
} |
|
1102 |
||
1103 |
memory->iPages.FindEnd(iStartIndex,iSizeInPages); |
|
1104 |
return r; |
|
1105 |
} |
|
1106 |
||
1107 |
||
1108 |
void DFineMapping::DoUnmap() |
|
1109 |
{ |
|
1110 |
TRACE2(("DFineMapping[0x%08x]::DoUnmap()",this)); |
|
1111 |
||
1112 |
TLinAddr startAddr = Base(); |
|
1113 |
TUint count = iSizeInPages; |
|
1114 |
TLinAddr addr = startAddr; |
|
1115 |
TPde* pPde = Mmu::PageDirectoryEntry(OsAsid(),addr); |
|
1116 |
||
1117 |
for(;;) |
|
1118 |
{ |
|
1119 |
TUint pteIndex = (addr>>KPageShift)&(KChunkMask>>KPageShift); |
|
1120 |
||
1121 |
// calculate number of pages to do... |
|
1122 |
TUint n = (KChunkSize>>KPageShift)-pteIndex; // pages left in page table |
|
1123 |
if(n>count) |
|
1124 |
n = count; |
|
1125 |
||
1126 |
// get page table... |
|
1127 |
MmuLock::Lock(); |
|
1128 |
TPte* pPte = Mmu::PageTableFromPde(*pPde); |
|
1129 |
if(!pPte) |
|
1130 |
{ |
|
1131 |
// no page table found, so nothing to do... |
|
1132 |
MmuLock::Unlock(); |
|
1133 |
} |
|
1134 |
else |
|
1135 |
{ |
|
1136 |
// unmap some pages... |
|
1137 |
pPte += pteIndex; |
|
1138 |
if(n>KMaxPagesInOneGo) |
|
1139 |
n = KMaxPagesInOneGo; |
|
1140 |
TBool keepPt = Mmu::UnmapPages(pPte, n); |
|
1141 |
MmuLock::Unlock(); |
|
1142 |
||
1143 |
// free page table if no longer needed... |
|
1144 |
if(!keepPt) |
|
1145 |
FreePageTable(pPde); |
|
1146 |
} |
|
1147 |
||
1148 |
// move on... |
|
1149 |
addr += n*KPageSize; |
|
1150 |
count -= n; |
|
1151 |
if(!count) |
|
1152 |
break; |
|
1153 |
if(!(addr&KChunkMask)) |
|
1154 |
++pPde; |
|
1155 |
} |
|
1156 |
||
1157 |
#ifdef COARSE_GRAINED_TLB_MAINTENANCE |
|
1158 |
InvalidateTLBForAsid(OsAsid()); |
|
1159 |
#else |
|
1160 |
// clean TLB... |
|
1161 |
TLinAddr endAddr = addr; |
|
1162 |
addr = LinAddrAndOsAsid(); |
|
1163 |
do InvalidateTLBForPage(addr); |
|
1164 |
while((addr+=KPageSize)<endAddr); |
|
1165 |
#endif |
|
1166 |
} |
|
1167 |
||
1168 |
||
1169 |
TInt DFineMapping::AllocatePermanentPageTables() |
|
1170 |
{ |
|
1171 |
TRACE2(("DFineMapping[0x%08x]::AllocatePermanentPageTables()",this)); |
|
1172 |
__NK_ASSERT_DEBUG(((Flags()&EPageTablesAllocated)==0)); |
|
1173 |
__NK_ASSERT_DEBUG(iBlankPde); |
|
1174 |
||
1175 |
TLinAddr addr = iAllocatedLinAddrAndOsAsid&~KPageMask; |
|
1176 |
TInt osAsid = iAllocatedLinAddrAndOsAsid&KPageMask; |
|
1177 |
TPde* pStartPde = Mmu::PageDirectoryEntry(osAsid,addr); |
|
1178 |
TPde* pEndPde = Mmu::PageDirectoryEntry(osAsid,addr+iAllocatedSize-1); |
|
1179 |
TPde* pPde = pStartPde; |
|
1180 |
||
1181 |
while(pPde<=pEndPde) |
|
1182 |
{ |
|
1183 |
MmuLock::Lock(); |
|
1184 |
TPte* pPte = AllocatePageTable(addr,pPde,true); |
|
1185 |
if(!pPte) |
|
1186 |
{ |
|
1187 |
// out of memory... |
|
1188 |
MmuLock::Unlock(); |
|
1189 |
FreePermanentPageTables(pStartPde,pPde-1); |
|
1190 |
return KErrNoMemory; |
|
1191 |
} |
|
1192 |
MmuLock::Unlock(); |
|
1193 |
||
1194 |
addr += KChunkSize; |
|
1195 |
++pPde; |
|
1196 |
} |
|
1197 |
||
1198 |
TRACE2(("DFineMapping[0x%08x]::AllocatePermanentPageTables() done",this)); |
|
1199 |
Flags() |= DMemoryMapping::EPageTablesAllocated; |
|
1200 |
return KErrNone; |
|
1201 |
} |
|
1202 |
||
1203 |
||
1204 |
void DFineMapping::FreePermanentPageTables(TPde* aFirstPde, TPde* aLastPde) |
|
1205 |
{ |
|
1206 |
Flags() &= ~DMemoryMapping::EPageTablesAllocated; |
|
1207 |
||
1208 |
MmuLock::Lock(); |
|
1209 |
||
1210 |
TUint flash = 0; |
|
1211 |
TPde* pPde = aFirstPde; |
|
1212 |
while(pPde<=aLastPde) |
|
1213 |
{ |
|
1214 |
TPte* pPte = Mmu::PageTableFromPde(*pPde); |
|
1215 |
__NK_ASSERT_DEBUG(pPte); |
|
1216 |
SPageTableInfo* pti = SPageTableInfo::FromPtPtr(pPte); |
|
1217 |
if(pti->DecPermanenceCount() || pti->PageCount()) |
|
1218 |
{ |
|
1219 |
// still in use... |
|
1220 |
MmuLock::Flash(flash,KMaxPageInfoUpdatesInOneGo*2); |
|
1221 |
} |
|
1222 |
else |
|
1223 |
{ |
|
1224 |
// page table no longer used for anything... |
|
1225 |
MmuLock::Unlock(); |
|
1226 |
FreePageTable(pPde); |
|
1227 |
MmuLock::Lock(); |
|
1228 |
} |
|
1229 |
||
1230 |
++pPde; |
|
1231 |
} |
|
1232 |
||
1233 |
MmuLock::Unlock(); |
|
1234 |
} |
|
1235 |
||
1236 |
||
1237 |
void DFineMapping::FreePermanentPageTables() |
|
1238 |
{ |
|
1239 |
if((Flags()&EPageTablesAllocated)==0) |
|
1240 |
return; |
|
1241 |
||
1242 |
TRACE2(("DFineMapping[0x%08x]::FreePermanentPageTables()",this)); |
|
1243 |
||
1244 |
TLinAddr addr = iAllocatedLinAddrAndOsAsid&~KPageMask; |
|
1245 |
TInt osAsid = iAllocatedLinAddrAndOsAsid&KPageMask; |
|
1246 |
TPde* pPde = Mmu::PageDirectoryEntry(osAsid,addr); |
|
1247 |
TPde* pEndPde = Mmu::PageDirectoryEntry(osAsid,addr+iAllocatedSize-1); |
|
1248 |
FreePermanentPageTables(pPde,pEndPde); |
|
1249 |
} |
|
1250 |
||
1251 |
||
1252 |
TPte* DFineMapping::FindPageTable(TLinAddr aLinAddr, TUint aMemoryIndex) |
|
1253 |
{ |
|
1254 |
TRACE(("DFineMapping::FindPageTable(0x%x, %d)", aLinAddr, aMemoryIndex)); |
|
1255 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
1256 |
__NK_ASSERT_DEBUG(IsAttached()); |
|
1257 |
return GetPageTable(aLinAddr); |
|
1258 |
} |
|
1259 |
||
1260 |
||
1261 |
||
1262 |
// |
|
1263 |
// DPhysicalPinMapping |
|
1264 |
// |
|
1265 |
||
1266 |
DPhysicalPinMapping::DPhysicalPinMapping() |
|
1267 |
: DMemoryMappingBase(EPinned|EPhysicalPinningMapping) |
|
1268 |
{ |
|
1269 |
} |
|
1270 |
||
1271 |
||
1272 |
TInt DPhysicalPinMapping::PhysAddr(TUint aIndex, TUint aCount, TPhysAddr& aPhysicalAddress, TPhysAddr* aPhysicalPageList) |
|
1273 |
{ |
|
1274 |
__NK_ASSERT_ALWAYS(IsAttached()); |
|
1275 |
||
1276 |
__NK_ASSERT_ALWAYS(TUint(aIndex+aCount)>aIndex && TUint(aIndex+aCount)<=iSizeInPages); |
|
1277 |
aIndex += iStartIndex; |
|
1278 |
||
1279 |
DCoarseMemory* memory = (DCoarseMemory*)Memory(true); // safe because we should only be called whilst memory is Pinned |
|
1280 |
TInt r = memory->PhysAddr(aIndex,aCount,aPhysicalAddress,aPhysicalPageList); |
|
1281 |
if(r!=KErrNone) |
|
1282 |
return r; |
|
1283 |
||
1284 |
if(memory->IsDemandPaged() && !IsReadOnly()) |
|
1285 |
{ |
|
1286 |
// the memory is demand paged and writeable so we need to mark it as dirty |
|
1287 |
// as we have to assume that the memory will be modified via the physical |
|
1288 |
// addresses we return... |
|
1289 |
MmuLock::Lock(); |
|
1290 |
TPhysAddr* pages = aPhysicalPageList; |
|
1291 |
TUint count = aCount; |
|
1292 |
while(count) |
|
1293 |
{ |
|
1294 |
SPageInfo* pi = SPageInfo::FromPhysAddr(*(pages++)); |
|
1295 |
pi->SetDirty(); |
|
1296 |
if((count&(KMaxPageInfoUpdatesInOneGo-1))==0) |
|
1297 |
MmuLock::Flash(); // flash lock every KMaxPageInfoUpdatesInOneGo iterations of the loop |
|
1298 |
--count; |
|
1299 |
} |
|
1300 |
MmuLock::Unlock(); |
|
1301 |
} |
|
1302 |
||
1303 |
return KErrNone; |
|
1304 |
} |
|
1305 |
||
1306 |
||
1307 |
TInt DPhysicalPinMapping::Pin(DMemoryObject* aMemory, TUint aIndex, TUint aCount, TMappingPermissions aPermissions) |
|
1308 |
{ |
|
1309 |
PteType() = Mmu::PteType(aPermissions,true); |
|
1310 |
return Attach(aMemory,aIndex,aCount); |
|
1311 |
} |
|
1312 |
||
1313 |
||
1314 |
void DPhysicalPinMapping::Unpin() |
|
1315 |
{ |
|
1316 |
Detach(); |
|
1317 |
} |
|
1318 |
||
1319 |
||
1320 |
TInt DPhysicalPinMapping::MapPages(RPageArray::TIter /*aPages*/, TUint /*aMapInstanceCount*/) |
|
1321 |
{ |
|
1322 |
// shouldn't ever be called because these mappings are always pinned... |
|
1323 |
__NK_ASSERT_DEBUG(0); |
|
1324 |
return KErrNotSupported; |
|
1325 |
} |
|
1326 |
||
1327 |
||
1328 |
void DPhysicalPinMapping::UnmapPages(RPageArray::TIter /*aPages*/, TUint /*aMapInstanceCount*/) |
|
1329 |
{ |
|
1330 |
// nothing to do... |
|
1331 |
} |
|
1332 |
||
1333 |
||
1334 |
void DPhysicalPinMapping::RemapPage(TPhysAddr& /*aPageArrayPtr*/, TUint /*aIndex*/, TUint /*aMapInstanceCount*/, TBool /*aInvalidateTLB*/) |
|
1335 |
{ |
|
1336 |
// shouldn't ever be called because physically pinned mappings block page moving. |
|
1337 |
__NK_ASSERT_DEBUG(0); |
|
1338 |
} |
|
1339 |
||
1340 |
||
1341 |
void DPhysicalPinMapping::RestrictPagesNA(RPageArray::TIter /*aPages*/, TUint /*aMapInstanceCount*/) |
|
1342 |
{ |
|
1343 |
// nothing to do... |
|
1344 |
} |
|
1345 |
||
1346 |
||
1347 |
TInt DPhysicalPinMapping::PageIn(RPageArray::TIter /*aPages*/, TPinArgs& /*aPinArgs*/, TUint /*aMapInstanceCount*/) |
|
1348 |
{ |
|
1349 |
// nothing to do... |
|
1350 |
return KErrNone; |
|
1351 |
} |
|
1352 |
||
1353 |
||
1354 |
TInt DPhysicalPinMapping::MovingPageIn(TPhysAddr& /*aPageArrayPtr*/, TUint /*aIndex*/) |
|
1355 |
{ |
|
1356 |
// Should never be asked to page in a page that is being moved as physical |
|
1357 |
// pin mappings don't own any page tables. |
|
1358 |
__NK_ASSERT_DEBUG(0); |
|
1359 |
return KErrAbort; |
|
1360 |
} |
|
1361 |
||
1362 |
TInt DPhysicalPinMapping::DoMap() |
|
1363 |
{ |
|
1364 |
// nothing to do... |
|
1365 |
return KErrNone; |
|
1366 |
} |
|
1367 |
||
1368 |
||
1369 |
void DPhysicalPinMapping::DoUnmap() |
|
1370 |
{ |
|
1371 |
// nothing to do... |
|
1372 |
} |
|
1373 |
||
1374 |
||
1375 |
||
1376 |
// |
|
1377 |
// DVirtualPinMapping |
|
1378 |
// |
|
1379 |
||
1380 |
DVirtualPinMapping::DVirtualPinMapping() |
|
1381 |
: iMaxCount(0) |
|
1382 |
{ |
|
1383 |
// Clear flag so it is possible to distingish between virtual and physical pin mappings. |
|
1384 |
Flags() &= ~EPhysicalPinningMapping; |
|
1385 |
} |
|
1386 |
||
1387 |
||
1388 |
DVirtualPinMapping::~DVirtualPinMapping() |
|
1389 |
{ |
|
1390 |
TRACE(("DVirtualPinMapping[0x%08x]::~DVirtualPinMapping()",this)); |
|
1391 |
FreePageTableArray(); |
|
1392 |
} |
|
1393 |
||
1394 |
||
1395 |
DVirtualPinMapping* DVirtualPinMapping::New(TUint aMaxCount) |
|
1396 |
{ |
|
1397 |
TRACE(("DVirtualPinMapping::New(0x%x)",aMaxCount)); |
|
1398 |
DVirtualPinMapping* self = new DVirtualPinMapping; |
|
1399 |
if(aMaxCount) |
|
1400 |
{ |
|
1401 |
// pages have been reserved for our use. |
|
1402 |
||
1403 |
// Create the array for storing pinned paged tables now, so we |
|
1404 |
// don't risk out-of-memory errors trying to do so later... |
|
1405 |
if(self->AllocPageTableArray(aMaxCount)!=KErrNone) |
|
1406 |
{ |
|
1407 |
// failed, so cleanup... |
|
1408 |
self->Close(); |
|
1409 |
self = 0; |
|
1410 |
} |
|
1411 |
else |
|
1412 |
{ |
|
1413 |
// success, so remember the pages that have been reserved for us... |
|
1414 |
self->iMaxCount = aMaxCount; |
|
1415 |
self->Flags() |= EPinningPagesReserved; |
|
1416 |
} |
|
1417 |
} |
|
1418 |
TRACE(("DVirtualPinMapping::New(0x%x) returns 0x%08x",aMaxCount,self)); |
|
1419 |
return self; |
|
1420 |
} |
|
1421 |
||
1422 |
||
1423 |
TUint DVirtualPinMapping::MaxPageTables(TUint aPageCount) |
|
1424 |
{ |
|
1425 |
return (aPageCount+2*KChunkSize/KPageSize-2)>>(KChunkShift-KPageShift); |
|
1426 |
} |
|
1427 |
||
1428 |
||
1429 |
TInt DVirtualPinMapping::AllocPageTableArray(TUint aCount) |
|
1430 |
{ |
|
1431 |
__NK_ASSERT_ALWAYS(iAllocatedPinnedPageTables==0); |
|
1432 |
TUint maxPt = MaxPageTables(aCount); |
|
1433 |
if(maxPt>KSmallPinnedPageTableCount) |
|
1434 |
{ |
|
1435 |
iAllocatedPinnedPageTables = new TPte*[maxPt]; |
|
1436 |
if(!iAllocatedPinnedPageTables) |
|
1437 |
return KErrNoMemory; |
|
1438 |
} |
|
1439 |
return KErrNone; |
|
1440 |
} |
|
1441 |
||
1442 |
||
1443 |
void DVirtualPinMapping::FreePageTableArray() |
|
1444 |
{ |
|
1445 |
delete [] iAllocatedPinnedPageTables; |
|
1446 |
iAllocatedPinnedPageTables = 0; |
|
1447 |
} |
|
1448 |
||
1449 |
||
1450 |
TPte** DVirtualPinMapping::PageTableArray() |
|
1451 |
{ |
|
1452 |
return iAllocatedPinnedPageTables ? iAllocatedPinnedPageTables : iSmallPinnedPageTablesArray; |
|
1453 |
} |
|
1454 |
||
1455 |
||
1456 |
TInt DVirtualPinMapping::Pin( DMemoryObject* aMemory, TUint aIndex, TUint aCount, TMappingPermissions aPermissions, |
|
1457 |
DMemoryMappingBase* aMapping, TUint aMappingInstanceCount) |
|
1458 |
{ |
|
1459 |
// Virtual pinning ensures a page is always mapped to a particular virtual address |
|
1460 |
// and therefore require a non-pinning mapping of the virtual address to pin. |
|
1461 |
__NK_ASSERT_ALWAYS(aMapping && !aMapping->IsPinned()); |
|
1462 |
||
1463 |
if(iMaxCount) |
|
1464 |
{ |
|
1465 |
if(aCount>iMaxCount) |
|
1466 |
return KErrArgument; |
|
1467 |
} |
|
1468 |
else |
|
1469 |
{ |
|
1470 |
TInt r = AllocPageTableArray(aCount); |
|
1471 |
if(r!=KErrNone) |
|
1472 |
return r; |
|
1473 |
} |
|
1474 |
||
1475 |
iPinVirtualMapping = aMapping; |
|
1476 |
iPinVirtualMapInstanceCount = aMappingInstanceCount; |
|
1477 |
TInt r = DPhysicalPinMapping::Pin(aMemory,aIndex,aCount,aPermissions); |
|
1478 |
iPinVirtualMapping = 0; |
|
1479 |
||
1480 |
return r; |
|
1481 |
} |
|
1482 |
||
1483 |
||
1484 |
void DVirtualPinMapping::Unpin() |
|
1485 |
{ |
|
1486 |
Detach(); |
|
1487 |
} |
|
1488 |
||
1489 |
||
1490 |
void DVirtualPinMapping::UnpinPageTables(TPinArgs& aPinArgs) |
|
1491 |
{ |
|
1492 |
TPte** pPt = PageTableArray(); |
|
1493 |
TPte** pPtEnd = pPt+iNumPinnedPageTables; |
|
1494 |
||
1495 |
MmuLock::Lock(); |
|
1496 |
while(pPt<pPtEnd) |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
1497 |
::PageTables.UnpinPageTable(*pPt++,aPinArgs); |
0 | 1498 |
MmuLock::Unlock(); |
1499 |
iNumPinnedPageTables = 0; |
|
1500 |
||
1501 |
if(!iMaxCount) |
|
1502 |
FreePageTableArray(); |
|
1503 |
} |
|
1504 |
||
1505 |
||
1506 |
void DVirtualPinMapping::RemapPage(TPhysAddr& /*aPageArrayPtr*/, TUint /*aIndex*/, TUint /*aMapInstanceCount*/, TBool /*aInvalidateTLB*/) |
|
1507 |
{ |
|
1508 |
__NK_ASSERT_DEBUG(0); |
|
1509 |
} |
|
1510 |
||
1511 |
||
1512 |
TInt DVirtualPinMapping::PageIn(RPageArray::TIter aPages, TPinArgs& aPinArgs, TUint aMapInstanceCount) |
|
1513 |
{ |
|
1514 |
if(iPinVirtualMapping) |
|
1515 |
return iPinVirtualMapping->PageIn(aPages, aPinArgs, iPinVirtualMapInstanceCount); |
|
1516 |
return KErrNone; |
|
1517 |
} |
|
1518 |
||
1519 |
||
1520 |
TInt DVirtualPinMapping::MovingPageIn(TPhysAddr& /*aPageArrayPtr*/, TUint /*aIndex*/) |
|
1521 |
{ |
|
1522 |
// Should never be asked to page in a page that is being moved as virtual |
|
1523 |
// pin mappings don't own any page tables. |
|
1524 |
__NK_ASSERT_DEBUG(0); |
|
1525 |
return KErrAbort; |
|
1526 |
} |
|
1527 |
||
1528 |
||
1529 |
TInt DVirtualPinMapping::DoPin(TPinArgs& aPinArgs) |
|
1530 |
{ |
|
1531 |
// setup for page table pinning... |
|
1532 |
aPinArgs.iPinnedPageTables = PageTableArray(); |
|
1533 |
||
1534 |
// do pinning... |
|
1535 |
TInt r = DPhysicalPinMapping::DoPin(aPinArgs); |
|
1536 |
||
1537 |
// save results... |
|
1538 |
iNumPinnedPageTables = aPinArgs.iNumPinnedPageTables; |
|
1539 |
__NK_ASSERT_DEBUG(iNumPinnedPageTables<=MaxPageTables(iSizeInPages)); |
|
1540 |
||
1541 |
// cleanup if error... |
|
1542 |
if(r!=KErrNone) |
|
1543 |
UnpinPageTables(aPinArgs); |
|
1544 |
||
1545 |
return r; |
|
1546 |
} |
|
1547 |
||
1548 |
||
1549 |
void DVirtualPinMapping::DoUnpin(TPinArgs& aPinArgs) |
|
1550 |
{ |
|
1551 |
DPhysicalPinMapping::DoUnpin(aPinArgs); |
|
1552 |
UnpinPageTables(aPinArgs); |
|
1553 |
} |
|
1554 |
||
1555 |
||
1556 |
||
1557 |
// |
|
1558 |
// DMemoryMappingBase |
|
1559 |
// |
|
1560 |
||
1561 |
||
1562 |
DMemoryMappingBase::DMemoryMappingBase(TUint aType) |
|
1563 |
{ |
|
1564 |
Flags() = aType; // rest of members cleared by DBase |
|
1565 |
} |
|
1566 |
||
1567 |
||
1568 |
TInt DMemoryMappingBase::Attach(DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
1569 |
{ |
|
1570 |
TRACE(("DMemoryMappingBase[0x%08x]::Attach(0x%08x,0x%x,0x%x)",this,aMemory,aIndex,aCount)); |
|
1571 |
__NK_ASSERT_DEBUG(!IsAttached()); |
|
1572 |
TInt r; |
|
1573 |
||
1574 |
if(++iMapInstanceCount>1) |
|
1575 |
{// This mapping is being reused... |
|
1576 |
||
1577 |
// Non-pinned mappings can be reused however this is only exercised |
|
1578 |
// by aligned shared buffers whose memory is managed by the unpaged |
|
1579 |
// or hardware memory manager. Reusing mappings to paged or movable |
|
1580 |
// memory hasn't tested and may need reusing mappings and its |
|
1581 |
// interactions with the fault handler, pinning etc to be tested. |
|
1582 |
__NK_ASSERT_DEBUG( IsPinned() || |
|
1583 |
aMemory->iManager == TheUnpagedMemoryManager || |
|
1584 |
aMemory->iManager == TheHardwareMemoryManager); |
|
1585 |
||
1586 |
// make sure new instance count is seen by other threads which may be operating |
|
1587 |
// on old mapping instance (this will stop them changing the mapping any more)... |
|
1588 |
MmuLock::Lock(); |
|
1589 |
MmuLock::Unlock(); |
|
1590 |
// clear unmapping flag from previous use... |
|
1591 |
__e32_atomic_and_ord16(&Flags(), (TUint16)~(EDetaching|EPageUnmapVetoed)); |
|
1592 |
} |
|
1593 |
||
1594 |
__NK_ASSERT_DEBUG((Flags()&(EDetaching|EPageUnmapVetoed))==0); |
|
1595 |
||
1596 |
// set region being mapped... |
|
1597 |
iStartIndex = aIndex; |
|
1598 |
iSizeInPages = aCount; |
|
1599 |
||
1600 |
// reserve any pages required for pinning demand paged memory. |
|
1601 |
// We must do this before we add the mapping to the memory object |
|
1602 |
// because once that is done the pages we are mapping will be prevented |
|
1603 |
// from being paged out. That could leave the paging system without |
|
1604 |
// enough pages to correctly handle page faults... |
|
1605 |
TPinArgs pinArgs; |
|
1606 |
pinArgs.iReadOnly = IsReadOnly(); |
|
1607 |
if(IsPinned() && aMemory->IsDemandPaged()) |
|
1608 |
{ |
|
1609 |
pinArgs.iUseReserve = Flags()&EPinningPagesReserved; |
|
1610 |
r = pinArgs.AllocReplacementPages(aCount); |
|
1611 |
if(r!=KErrNone) |
|
1612 |
return r; |
|
1613 |
} |
|
1614 |
||
1615 |
// link into memory object... |
|
1616 |
r = aMemory->AddMapping(this); |
|
1617 |
if(r==KErrNone) |
|
1618 |
{ |
|
1619 |
// pin pages if needed... |
|
1620 |
if(IsPinned()) |
|
1621 |
r = DoPin(pinArgs); |
|
1622 |
||
1623 |
// add pages to this mapping... |
|
1624 |
if(r==KErrNone) |
|
1625 |
r = DoMap(); |
|
1626 |
||
1627 |
// revert if error... |
|
1628 |
if(r!=KErrNone) |
|
1629 |
Detach(); |
|
1630 |
} |
|
1631 |
||
1632 |
// free any left over pinning pages... |
|
1633 |
pinArgs.FreeReplacementPages(); |
|
1634 |
||
1635 |
return r; |
|
1636 |
} |
|
1637 |
||
1638 |
||
1639 |
void DMemoryMappingBase::Detach() |
|
1640 |
{ |
|
1641 |
TRACE(("DMemoryMappingBase[0x%08x]::Detach()",this)); |
|
1642 |
__NK_ASSERT_DEBUG(IsAttached()); |
|
1643 |
||
1644 |
// set EDetaching flag, which prevents anyone modifying pages in this |
|
1645 |
// mapping, except to remove them... |
|
1646 |
MmuLock::Lock(); |
|
1647 |
__e32_atomic_ior_ord16(&Flags(), (TUint16)EDetaching); |
|
1648 |
MmuLock::Unlock(); |
|
1649 |
||
1650 |
// remove all pages from this mapping... |
|
1651 |
DoUnmap(); |
|
1652 |
||
1653 |
// unpin pages if needed... |
|
1654 |
TPinArgs pinArgs; |
|
1655 |
if(IsPinned()) |
|
1656 |
DoUnpin(pinArgs); |
|
1657 |
||
1658 |
// unlink from memory object... |
|
1659 |
iMemory->RemoveMapping(this); |
|
1660 |
||
1661 |
// free any spare pages produced by unpinning... |
|
1662 |
pinArgs.FreeReplacementPages(); |
|
1663 |
} |
|
1664 |
||
1665 |
||
1666 |
TInt DMemoryMappingBase::DoPin(TPinArgs& aPinArgs) |
|
1667 |
{ |
|
1668 |
DMemoryObject* memory = Memory(true); // safe because we're called from code which has added mapping to memory |
|
1669 |
return memory->iManager->Pin(memory,this,aPinArgs); |
|
1670 |
} |
|
1671 |
||
1672 |
||
1673 |
void DMemoryMappingBase::DoUnpin(TPinArgs& aPinArgs) |
|
1674 |
{ |
|
1675 |
DMemoryObject* memory = Memory(true); // safe because we're called from code which will be removing this mapping from memory afterwards |
|
1676 |
memory->iManager->Unpin(memory,this,aPinArgs); |
|
1677 |
} |
|
1678 |
||
1679 |
||
1680 |
void DMemoryMappingBase::LinkToMemory(DMemoryObject* aMemory, TMappingList& aMappingList) |
|
1681 |
{ |
|
1682 |
TRACE(("DMemoryMappingBase[0x%08x]::LinkToMemory(0x%08x,?)",this,aMemory)); |
|
1683 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
1684 |
__NK_ASSERT_DEBUG(aMappingList.LockIsHeld()); |
|
1685 |
__NK_ASSERT_ALWAYS(!IsAttached()); |
|
1686 |
__NK_ASSERT_DEBUG(!BeingDetached()); |
|
1687 |
aMappingList.Add(this); |
|
1688 |
iMemory = aMemory; |
|
1689 |
iMemory->SetMappingAddedFlag(); |
|
1690 |
} |
|
1691 |
||
1692 |
||
1693 |
void DMemoryMappingBase::UnlinkFromMemory(TMappingList& aMappingList) |
|
1694 |
{ |
|
1695 |
TRACE(("DMemoryMappingBase[0x%08x]::UnlinkMapping(?)",this)); |
|
1696 |
||
1697 |
// unlink... |
|
1698 |
MmuLock::Lock(); |
|
1699 |
aMappingList.Lock(); |
|
1700 |
__NK_ASSERT_DEBUG(IsAttached()); |
|
1701 |
__NK_ASSERT_DEBUG(BeingDetached()); |
|
1702 |
aMappingList.Remove(this); |
|
1703 |
DMemoryObject* memory = iMemory; |
|
1704 |
iMemory = 0; |
|
1705 |
aMappingList.Unlock(); |
|
1706 |
MmuLock::Unlock(); |
|
1707 |
||
1708 |
// if mapping had vetoed any page decommits... |
|
1709 |
if(Flags()&DMemoryMapping::EPageUnmapVetoed) |
|
1710 |
{ |
|
1711 |
// then queue cleanup of decommitted pages... |
|
1712 |
memory->iManager->QueueCleanup(memory,DMemoryManager::ECleanupDecommitted); |
|
1713 |
} |
|
1714 |
} |
|
1715 |
||
1716 |
||
1717 |
||
1718 |
// |
|
1719 |
// Debug |
|
1720 |
// |
|
1721 |
||
1722 |
void DMemoryMappingBase::Dump() |
|
1723 |
{ |
|
1724 |
#ifdef _DEBUG |
|
1725 |
Kern::Printf("DMemoryMappingBase[0x%08x]::Dump()",this); |
|
1726 |
Kern::Printf(" IsAttached() = %d",(bool)IsAttached()); |
|
1727 |
Kern::Printf(" iMemory = 0x%08x",iMemory); |
|
1728 |
Kern::Printf(" iStartIndex = 0x%x",iStartIndex); |
|
1729 |
Kern::Printf(" iSizeInPages = 0x%x",iSizeInPages); |
|
1730 |
Kern::Printf(" Flags() = 0x%x",Flags()); |
|
1731 |
Kern::Printf(" PteType() = 0x%x",PteType()); |
|
1732 |
#endif // _DEBUG |
|
1733 |
} |
|
1734 |
||
1735 |
||
1736 |
void DMemoryMapping::Dump() |
|
1737 |
{ |
|
1738 |
#ifdef _DEBUG |
|
1739 |
Kern::Printf("DMemoryMapping[0x%08x]::Dump()",this); |
|
1740 |
Kern::Printf(" Base() = 0x08%x",iLinAddrAndOsAsid&~KPageMask); |
|
1741 |
Kern::Printf(" OsAsid() = %d",iLinAddrAndOsAsid&KPageMask); |
|
1742 |
Kern::Printf(" iBlankPde = 0x%08x",iBlankPde); |
|
1743 |
Kern::Printf(" iBlankPte = 0x%08x",iBlankPte); |
|
1744 |
Kern::Printf(" iAllocatedLinAddrAndOsAsid = 0x%08x",iAllocatedLinAddrAndOsAsid); |
|
1745 |
Kern::Printf(" iAllocatedSize = 0x%x",iAllocatedSize); |
|
1746 |
DMemoryMappingBase::Dump(); |
|
1747 |
#endif // _DEBUG |
|
1748 |
} |
|
1749 |
||
1750 |
||
1751 |
void DVirtualPinMapping::Dump() |
|
1752 |
{ |
|
1753 |
#ifdef _DEBUG |
|
1754 |
Kern::Printf("DVirtualPinMapping[0x%08x]::Dump()",this); |
|
1755 |
Kern::Printf(" iMaxCount = %d",iMaxCount); |
|
1756 |
Kern::Printf(" iNumPinnedPageTables = %d",iNumPinnedPageTables); |
|
1757 |
DMemoryMappingBase::Dump(); |
|
1758 |
#endif // _DEBUG |
|
1759 |
} |
|
1760 |