author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 11 May 2010 17:28:22 +0300 | |
branch | RCL_3 |
changeset 110 | c734af59ce98 |
parent 87 | 2f92ad2dc5db |
child 117 | 5b5d147c7838 |
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 "cache_maintenance.h" |
|
18 |
#include "decompress.h" // include for the generic BytePairDecompress(). |
|
19 |
#include "mm.h" |
|
20 |
#include "mmu.h" |
|
21 |
#include "mpager.h" |
|
22 |
#include "mmanager.h" |
|
23 |
#include "mmapping.h" |
|
24 |
#include "mobject.h" |
|
25 |
#include "mcleanup.h" |
|
26 |
||
27 |
||
28 |
// |
|
29 |
// DMemoryManager |
|
30 |
// |
|
31 |
||
32 |
TInt DMemoryManager::New(DMemoryObject*& aMemory, TUint aSizeInPages, TMemoryAttributes aAttributes, TMemoryCreateFlags aCreateFlags) |
|
33 |
{ |
|
34 |
DMemoryObject* memory; |
|
35 |
if(aSizeInPages&(KChunkMask>>KPageShift)) |
|
36 |
memory = DFineMemory::New(this,aSizeInPages,aAttributes,aCreateFlags); |
|
37 |
else |
|
38 |
memory = DCoarseMemory::New(this,aSizeInPages,aAttributes,aCreateFlags); |
|
39 |
aMemory = memory; |
|
40 |
if(!memory) |
|
41 |
return KErrNoMemory; |
|
42 |
return KErrNone; |
|
43 |
} |
|
44 |
||
45 |
||
46 |
TInt DMemoryManager::Alloc(DMemoryObject* /*aMemory*/, TUint /*aIndex*/, TUint /*aCount*/) |
|
47 |
{ |
|
48 |
return KErrNotSupported; |
|
49 |
} |
|
50 |
||
51 |
||
52 |
TInt DMemoryManager::AllocContiguous(DMemoryObject* /*aMemory*/, TUint /*aIndex*/, TUint /*aCount*/, TUint /*aAlign*/, TPhysAddr& /*aPhysAddr*/) |
|
53 |
{ |
|
54 |
return KErrNotSupported; |
|
55 |
} |
|
56 |
||
57 |
||
58 |
void DMemoryManager::Free(DMemoryObject* /*aMemory*/, TUint /*aIndex*/, TUint /*aCount*/) |
|
59 |
{ |
|
60 |
} |
|
61 |
||
62 |
||
63 |
TInt DMemoryManager::Wipe(DMemoryObject* /*aMemory*/) |
|
64 |
{ |
|
65 |
return KErrNotSupported; |
|
66 |
} |
|
67 |
||
68 |
||
87
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
69 |
TInt DMemoryManager::AddPages(DMemoryObject* /*aMemory*/, TUint /*aIndex*/, TUint /*aCount*/, const TPhysAddr* /*aPages*/) |
0 | 70 |
{ |
71 |
return KErrNotSupported; |
|
72 |
} |
|
73 |
||
74 |
||
75 |
TInt DMemoryManager::AddContiguous(DMemoryObject* /*aMemory*/, TUint /*aIndex*/, TUint /*aCount*/, TPhysAddr /*aPhysAddr*/) |
|
76 |
{ |
|
77 |
return KErrNotSupported; |
|
78 |
} |
|
79 |
||
80 |
||
81 |
TInt DMemoryManager::RemovePages(DMemoryObject* /*aMemory*/, TUint /*aIndex*/, TUint /*aCount*/, TPhysAddr* /*aPages*/) |
|
82 |
{ |
|
83 |
return KErrNotSupported; |
|
84 |
} |
|
85 |
||
86 |
||
87 |
TInt DMemoryManager::AllowDiscard(DMemoryObject* /*aMemory*/, TUint /*aIndex*/, TUint /*aCount*/) |
|
88 |
{ |
|
89 |
return KErrNotSupported; |
|
90 |
} |
|
91 |
||
92 |
||
93 |
TInt DMemoryManager::DisallowDiscard(DMemoryObject* /*aMemory*/, TUint /*aIndex*/, TUint /*aCount*/) |
|
94 |
{ |
|
95 |
return KErrNotSupported; |
|
96 |
} |
|
97 |
||
98 |
||
99 |
TInt DMemoryManager::StealPage(DMemoryObject* /*aMemory*/, SPageInfo* /*aPageInfo*/) |
|
100 |
{ |
|
101 |
return KErrNotSupported; |
|
102 |
} |
|
103 |
||
104 |
||
105 |
TInt DMemoryManager::RestrictPage(DMemoryObject* /*aMemory*/, SPageInfo* /*aPageInfo*/, TRestrictPagesType /*aRestriction*/) |
|
106 |
{ |
|
107 |
return KErrNotSupported; |
|
108 |
} |
|
109 |
||
110 |
||
110
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
111 |
void DMemoryManager::CleanPages(TUint aPageCount, SPageInfo** aPageInfos, TBool /*aBackground*/) |
0 | 112 |
{ |
110
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
113 |
for (TUint i = 0 ; i < aPageCount ; ++i) |
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
114 |
__NK_ASSERT_DEBUG(!aPageInfos[i]->IsDirty()); |
0 | 115 |
} |
116 |
||
117 |
||
118 |
TInt DMemoryManager::HandleFault( DMemoryObject* aMemory, TUint aIndex, DMemoryMapping* aMapping, |
|
119 |
TUint aMapInstanceCount, TUint aAccessPermissions) |
|
120 |
{ |
|
121 |
(void)aMemory; |
|
122 |
(void)aIndex; |
|
123 |
(void)aMapping; |
|
124 |
(void)aMapInstanceCount; |
|
125 |
(void)aAccessPermissions; |
|
126 |
// Kern::Printf("DMemoryManager::HandlePageFault(0x%08x,0x%x,0x%08x,%d)",aMemory,aIndex,aMapping,aAccessPermissions); |
|
127 |
return KErrAbort; |
|
128 |
} |
|
129 |
||
130 |
||
131 |
TInt DMemoryManager::MovePage( DMemoryObject* aMemory, SPageInfo* aOldPageInfo, |
|
132 |
TPhysAddr& aNewPage, TUint aBlockZoneId, TBool aBlockRest) |
|
133 |
{ |
|
134 |
return KErrNotSupported; |
|
135 |
} |
|
136 |
||
110
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
137 |
|
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
138 |
TInt DMemoryManager::MoveAndAllocPage(DMemoryObject*, SPageInfo*, TZonePageType) |
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
139 |
{ |
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
140 |
return KErrNotSupported; |
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
141 |
} |
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
142 |
|
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
143 |
|
0 | 144 |
TZonePageType DMemoryManager::PageType() |
145 |
{// This should not be invoked on memory managers that do not use the methods |
|
146 |
// AllocPages() and FreePages(). |
|
147 |
__NK_ASSERT_DEBUG(0); |
|
148 |
return EPageFixed; |
|
149 |
} |
|
150 |
||
151 |
static TMemoryCleanup Cleanup; |
|
152 |
||
153 |
DMemoryObject* DMemoryManager::iCleanupHead = 0; |
|
154 |
TSpinLock DMemoryManager::iCleanupLock(TSpinLock::EOrderGenericIrqHigh3); |
|
155 |
||
156 |
void DMemoryManager::CleanupFunction(TAny*) |
|
157 |
{ |
|
158 |
for(;;) |
|
159 |
{ |
|
160 |
__SPIN_LOCK_IRQ(iCleanupLock); |
|
161 |
||
162 |
// get an object from queue... |
|
163 |
DMemoryObject* memory = iCleanupHead; |
|
164 |
if(!memory) |
|
165 |
{ |
|
166 |
// none left, so end... |
|
167 |
__SPIN_UNLOCK_IRQ(iCleanupLock); |
|
168 |
return; |
|
169 |
} |
|
170 |
||
171 |
if(memory->iCleanupFlags&ECleanupDecommitted) |
|
172 |
{ |
|
173 |
// object requires cleanup of decommitted pages... |
|
174 |
memory->iCleanupFlags &= ~ECleanupDecommitted; |
|
175 |
__SPIN_UNLOCK_IRQ(iCleanupLock); |
|
176 |
memory->iManager->DoCleanupDecommitted(memory); |
|
177 |
} |
|
178 |
else |
|
179 |
{ |
|
180 |
// object has no more cleanup operations to perform, |
|
181 |
// so remove it from the cleanup queue... |
|
182 |
__NK_ASSERT_DEBUG(memory->iCleanupFlags==ECleanupIsQueued); // no operations left, just flag to say its in the cleanup queue |
|
183 |
memory->iCleanupFlags &= ~ECleanupIsQueued; |
|
184 |
iCleanupHead = memory->iCleanupNext; |
|
185 |
memory->iCleanupNext = NULL; |
|
186 |
__SPIN_UNLOCK_IRQ(iCleanupLock); |
|
187 |
||
188 |
// close reference which was added when object was queued... |
|
189 |
memory->Close(); |
|
190 |
} |
|
191 |
} |
|
192 |
} |
|
193 |
||
194 |
||
195 |
void DMemoryManager::QueueCleanup(DMemoryObject* aMemory, TCleanupOperationFlag aCleanupOp) |
|
196 |
{ |
|
197 |
// add new cleanup operation... |
|
198 |
__SPIN_LOCK_IRQ(iCleanupLock); |
|
199 |
TUint32 oldFlags = aMemory->iCleanupFlags; |
|
200 |
aMemory->iCleanupFlags = oldFlags|aCleanupOp|ECleanupIsQueued; |
|
201 |
__SPIN_UNLOCK_IRQ(iCleanupLock); |
|
202 |
||
203 |
// if cleanup was already requested... |
|
204 |
if(oldFlags) |
|
205 |
return; // nothing more to do |
|
206 |
||
207 |
// increase reference count... |
|
208 |
aMemory->Open(); |
|
209 |
||
210 |
// add object to cleanup queue... |
|
211 |
__SPIN_LOCK_IRQ(iCleanupLock); |
|
212 |
aMemory->iCleanupNext = iCleanupHead; |
|
213 |
iCleanupHead = aMemory; |
|
214 |
__SPIN_UNLOCK_IRQ(iCleanupLock); |
|
215 |
||
216 |
// queue cleanup function to run... |
|
217 |
Cleanup.Add((TMemoryCleanupCallback)CleanupFunction,0); |
|
218 |
} |
|
219 |
||
220 |
||
221 |
void DMemoryManager::DoCleanupDecommitted(DMemoryObject* aMemory) |
|
222 |
{ |
|
223 |
TRACE2(("DMemoryManager::DoCleanupDecommitted(0x%08x)",aMemory)); |
|
224 |
__NK_ASSERT_DEBUG(0); |
|
225 |
} |
|
226 |
||
227 |
||
228 |
void DMemoryManager::ReAllocDecommitted(DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
229 |
{ |
|
230 |
__NK_ASSERT_DEBUG(MemoryObjectLock::IsHeld(aMemory)); |
|
231 |
||
232 |
// make iterator for region... |
|
233 |
RPageArray::TIter pageIter; |
|
234 |
aMemory->iPages.FindStart(aIndex,aCount,pageIter); |
|
235 |
||
236 |
for(;;) |
|
237 |
{ |
|
238 |
// find some pages... |
|
239 |
RPageArray::TIter pageList; |
|
240 |
TUint n = pageIter.Find(pageList); |
|
241 |
if(!n) |
|
242 |
break; |
|
243 |
||
244 |
// check each existing page... |
|
245 |
RamAllocLock::Lock(); |
|
246 |
TPhysAddr* pages; |
|
247 |
while(pageList.Pages(pages)) |
|
248 |
{ |
|
249 |
TPhysAddr page = *pages; |
|
250 |
if(RPageArray::State(page)==RPageArray::EDecommitted) |
|
251 |
{ |
|
252 |
// decommitted pages need re-initialising... |
|
253 |
TPhysAddr pagePhys = page&~KPageMask; |
|
254 |
*pages = pagePhys|RPageArray::ECommitted; |
|
255 |
TheMmu.PagesAllocated(&pagePhys,1,aMemory->RamAllocFlags(),true); |
|
256 |
} |
|
257 |
pageList.Skip(1); |
|
258 |
} |
|
259 |
RamAllocLock::Unlock(); |
|
260 |
||
261 |
// move on... |
|
262 |
pageIter.FindRelease(n); |
|
263 |
} |
|
264 |
||
265 |
aMemory->iPages.FindEnd(aIndex,aCount); |
|
266 |
} |
|
267 |
||
268 |
||
269 |
void DMemoryManager::FreeDecommitted(DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
270 |
{ |
|
271 |
TRACE2(("DMemoryManager::FreeDecommitted(0x%08x,0x%x,0x%x)",aMemory, aIndex, aCount)); |
|
272 |
__NK_ASSERT_DEBUG(MemoryObjectLock::IsHeld(aMemory)); |
|
273 |
||
274 |
// make iterator for region... |
|
275 |
RPageArray::TIter pageIter; |
|
276 |
aMemory->iPages.FindStart(aIndex,aCount,pageIter); |
|
277 |
||
278 |
for(;;) |
|
279 |
{ |
|
280 |
// find some pages... |
|
281 |
RPageArray::TIter pageList; |
|
282 |
TUint findCount = pageIter.Find(pageList); |
|
283 |
if(!findCount) |
|
284 |
break; |
|
285 |
||
286 |
// search for decommitted pages... |
|
287 |
RamAllocLock::Lock(); |
|
288 |
TPhysAddr* pages; |
|
289 |
TUint numPages; |
|
290 |
while((numPages=pageList.Pages(pages))!=0) |
|
291 |
{ |
|
292 |
TUint n=0; |
|
293 |
if(RPageArray::State(pages[n])!=RPageArray::EDecommitted) |
|
294 |
{ |
|
295 |
// skip pages which aren't EDecommitted... |
|
296 |
while(++n<numPages && RPageArray::State(pages[n])!=RPageArray::EDecommitted) |
|
297 |
{} |
|
298 |
} |
|
299 |
else |
|
300 |
{ |
|
301 |
// find range of pages which are EDecommitted... |
|
302 |
while(++n<numPages && RPageArray::State(pages[n])==RPageArray::EDecommitted) |
|
303 |
{} |
|
304 |
RPageArray::TIter decommittedList(pageList.Left(n)); |
|
305 |
||
306 |
// free pages... |
|
307 |
TUint freedCount = FreePages(aMemory,decommittedList); |
|
308 |
(void)freedCount; |
|
309 |
TRACE2(("DMemoryManager::FreeDecommitted(0x%08x) freed %d in 0x%x..0x%x",aMemory,freedCount,decommittedList.Index(),decommittedList.IndexEnd())); |
|
310 |
} |
|
311 |
pageList.Skip(n); |
|
312 |
} |
|
313 |
RamAllocLock::Unlock(); |
|
314 |
||
315 |
// move on... |
|
316 |
pageIter.FindRelease(findCount); |
|
317 |
} |
|
318 |
||
319 |
aMemory->iPages.FindEnd(aIndex,aCount); |
|
320 |
} |
|
321 |
||
322 |
||
323 |
void DMemoryManager::DoFree(DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
324 |
{ |
|
325 |
TRACE2(("DMemoryManager::DoFree(0x%08x,0x%x,0x%x)",aMemory, aIndex, aCount)); |
|
326 |
__NK_ASSERT_DEBUG(MemoryObjectLock::IsHeld(aMemory)); |
|
327 |
||
328 |
RPageArray::TIter pageIter; |
|
329 |
aMemory->iPages.FindStart(aIndex,aCount,pageIter); |
|
330 |
||
331 |
for(;;) |
|
332 |
{ |
|
333 |
// find some pages... |
|
334 |
RPageArray::TIter pageList; |
|
335 |
TUint n = pageIter.RemoveFind(pageList); |
|
336 |
if(!n) |
|
337 |
break; |
|
338 |
||
339 |
// free pages... |
|
340 |
FreePages(aMemory,pageList); |
|
341 |
||
342 |
// move on... |
|
343 |
pageIter.FindRelease(n); |
|
344 |
} |
|
345 |
||
346 |
aMemory->iPages.FindEnd(aIndex,aCount); |
|
347 |
} |
|
348 |
||
349 |
||
350 |
TInt DMemoryManager::FreePages(DMemoryObject* aMemory, RPageArray::TIter aPageList) |
|
351 |
{ |
|
352 |
// unmap the pages... |
|
353 |
aMemory->UnmapPages(aPageList,true); |
|
354 |
||
355 |
RamAllocLock::Lock(); |
|
356 |
||
357 |
// remove and free pages... |
|
358 |
Mmu& m = TheMmu; |
|
359 |
TUint count = 0; |
|
360 |
TPhysAddr pages[KMaxPagesInOneGo]; |
|
361 |
TUint n; |
|
362 |
while((n=aPageList.Remove(KMaxPagesInOneGo,pages))!=0) |
|
363 |
{ |
|
364 |
count += n; |
|
365 |
m.FreeRam(pages, n, aMemory->iManager->PageType()); |
|
366 |
} |
|
367 |
||
368 |
RamAllocLock::Unlock(); |
|
369 |
||
370 |
return count; |
|
371 |
} |
|
372 |
||
373 |
||
374 |
||
375 |
/** |
|
376 |
Manager for memory objects containing normal unpaged program memory (RAM) which |
|
377 |
is allocated from a system wide pool. The physical pages allocated to this |
|
378 |
memory are fixed until explicitly freed. |
|
379 |
||
380 |
This is normally used for kernel memory and any other situation where it |
|
381 |
is not permissible for memory accesses to generate page faults of any kind. |
|
382 |
*/ |
|
383 |
class DUnpagedMemoryManager : public DMemoryManager |
|
384 |
{ |
|
385 |
public: |
|
386 |
// from DMemoryManager... |
|
387 |
virtual void Destruct(DMemoryObject* aMemory); |
|
388 |
virtual TInt Alloc(DMemoryObject* aMemory, TUint aIndex, TUint aCount); |
|
389 |
virtual TInt AllocContiguous(DMemoryObject* aMemory, TUint aIndex, TUint aCount, TUint aAlign, TPhysAddr& aPhysAddr); |
|
390 |
virtual void Free(DMemoryObject* aMemory, TUint aIndex, TUint aCount); |
|
391 |
virtual TInt Pin(DMemoryObject* aMemory, DMemoryMappingBase* aMapping, TPinArgs& aPinArgs); |
|
392 |
virtual void Unpin(DMemoryObject* aMemory, DMemoryMappingBase* aMapping, TPinArgs& aPinArgs); |
|
393 |
virtual TInt Wipe(DMemoryObject* aMemory); |
|
394 |
virtual TZonePageType PageType(); |
|
395 |
||
396 |
private: |
|
397 |
// from DMemoryManager... |
|
398 |
virtual void DoCleanupDecommitted(DMemoryObject* aMemory); |
|
399 |
||
400 |
/** |
|
401 |
Implementation factor for implementation of #Alloc. |
|
402 |
*/ |
|
403 |
static TInt AllocPages(DMemoryObject* aMemory, RPageArray::TIter aPageList); |
|
404 |
||
405 |
/** |
|
406 |
Implementation factor for implementation of #AllocContiguous. |
|
407 |
*/ |
|
408 |
static TInt AllocContiguousPages(DMemoryObject* aMemory, RPageArray::TIter aPageList, TUint aAlign, TPhysAddr& aPhysAddr); |
|
409 |
||
410 |
/** |
|
411 |
Implementation factor for implementation of #Wipe. |
|
412 |
*/ |
|
413 |
static void WipePages(DMemoryObject* aMemory, RPageArray::TIter aPageList); |
|
414 |
||
415 |
public: |
|
416 |
/** |
|
417 |
The single instance of this manager class. |
|
418 |
*/ |
|
419 |
static DUnpagedMemoryManager TheManager; |
|
420 |
}; |
|
421 |
||
422 |
||
423 |
DUnpagedMemoryManager DUnpagedMemoryManager::TheManager; |
|
424 |
DMemoryManager* TheUnpagedMemoryManager = &DUnpagedMemoryManager::TheManager; |
|
425 |
||
426 |
||
427 |
void DUnpagedMemoryManager::Destruct(DMemoryObject* aMemory) |
|
428 |
{ |
|
429 |
MemoryObjectLock::Lock(aMemory); |
|
430 |
Free(aMemory,0,aMemory->iSizeInPages); |
|
431 |
MemoryObjectLock::Unlock(aMemory); |
|
432 |
aMemory->Close(); |
|
433 |
} |
|
434 |
||
435 |
||
436 |
TInt DUnpagedMemoryManager::Alloc(DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
437 |
{ |
|
438 |
TRACE2(("DUnpagedMemoryManager::Alloc(0x%08x,0x%x,0x%x)",aMemory, aIndex, aCount)); |
|
439 |
__NK_ASSERT_DEBUG(MemoryObjectLock::IsHeld(aMemory)); |
|
440 |
||
441 |
// re-initialise any decommitted pages which we may still own because they were pinned... |
|
442 |
ReAllocDecommitted(aMemory,aIndex,aCount); |
|
443 |
||
444 |
// check and allocate page array entries... |
|
445 |
RPageArray::TIter pageList; |
|
446 |
TInt r = aMemory->iPages.AddStart(aIndex,aCount,pageList,true); |
|
447 |
if(r!=KErrNone) |
|
448 |
return r; |
|
449 |
||
450 |
// allocate RAM and add it to page array... |
|
451 |
r = AllocPages(aMemory,pageList); |
|
452 |
||
453 |
// map pages... |
|
454 |
if(r==KErrNone) |
|
455 |
r = aMemory->MapPages(pageList); |
|
456 |
||
457 |
// release page array entries... |
|
458 |
aMemory->iPages.AddEnd(aIndex,aCount); |
|
459 |
||
460 |
// revert if error... |
|
461 |
if(r!=KErrNone) |
|
462 |
Free(aMemory,aIndex,aCount); |
|
463 |
||
464 |
return r; |
|
465 |
} |
|
466 |
||
467 |
||
468 |
TInt DUnpagedMemoryManager::AllocContiguous(DMemoryObject* aMemory, TUint aIndex, TUint aCount, TUint aAlign, TPhysAddr& aPhysAddr) |
|
469 |
{ |
|
470 |
TRACE2(("DUnpagedMemoryManager::AllocContiguous(0x%08x,0x%x,0x%x,%d,?)",aMemory, aIndex, aCount, aAlign)); |
|
471 |
__NK_ASSERT_DEBUG(MemoryObjectLock::IsHeld(aMemory)); |
|
472 |
||
473 |
// set invalid memory in case of error... |
|
474 |
aPhysAddr = KPhysAddrInvalid; |
|
475 |
||
476 |
// check and allocate page array entries... |
|
477 |
RPageArray::TIter pageList; |
|
478 |
TInt r = aMemory->iPages.AddStart(aIndex,aCount,pageList); |
|
479 |
if(r!=KErrNone) |
|
480 |
return r; |
|
481 |
||
482 |
// allocate memory... |
|
483 |
TPhysAddr physAddr; |
|
484 |
r = AllocContiguousPages(aMemory, pageList, aAlign, physAddr); |
|
485 |
||
486 |
// map memory... |
|
487 |
if(r==KErrNone) |
|
488 |
{ |
|
489 |
r = aMemory->MapPages(pageList); |
|
490 |
if(r==KErrNone) |
|
491 |
aPhysAddr = physAddr; |
|
492 |
} |
|
493 |
||
494 |
// release page array entries... |
|
495 |
aMemory->iPages.AddEnd(aIndex,aCount); |
|
496 |
||
497 |
// revert if error... |
|
498 |
if(r!=KErrNone) |
|
499 |
Free(aMemory,aIndex,aCount); |
|
500 |
||
501 |
return r; |
|
502 |
} |
|
503 |
||
504 |
||
505 |
void DUnpagedMemoryManager::Free(DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
506 |
{ |
|
507 |
DoFree(aMemory,aIndex,aCount); |
|
508 |
} |
|
509 |
||
510 |
||
511 |
TInt DUnpagedMemoryManager::AllocPages(DMemoryObject* aMemory, RPageArray::TIter aPageList) |
|
512 |
{ |
|
513 |
TInt r = KErrNone; |
|
514 |
RamAllocLock::Lock(); |
|
515 |
||
516 |
Mmu& m = TheMmu; |
|
517 |
for(;;) |
|
518 |
{ |
|
519 |
// find entries in page array to allocate... |
|
520 |
RPageArray::TIter allocList; |
|
521 |
TUint n = aPageList.AddFind(allocList); |
|
522 |
if(!n) |
|
523 |
break; |
|
524 |
||
525 |
do |
|
526 |
{ |
|
527 |
// allocate ram... |
|
528 |
TPhysAddr pages[KMaxPagesInOneGo]; |
|
529 |
if(n>KMaxPagesInOneGo) |
|
530 |
n = KMaxPagesInOneGo; |
|
531 |
r = m.AllocRam(pages, n, aMemory->RamAllocFlags(), aMemory->iManager->PageType()); |
|
532 |
if(r!=KErrNone) |
|
533 |
goto done; |
|
534 |
||
535 |
// assign pages to memory object... |
|
536 |
{ |
|
537 |
TUint index = allocList.Index(); |
|
538 |
TUint flags = aMemory->PageInfoFlags(); |
|
539 |
TUint i=0; |
|
540 |
MmuLock::Lock(); |
|
541 |
do |
|
542 |
{ |
|
543 |
SPageInfo* pi = SPageInfo::FromPhysAddr(pages[i]); |
|
544 |
pi->SetManaged(aMemory,index+i,flags); |
|
545 |
} |
|
546 |
while(++i<n); |
|
547 |
MmuLock::Unlock(); |
|
548 |
} |
|
549 |
||
550 |
// add pages to page array... |
|
551 |
allocList.Add(n,pages); |
|
552 |
} |
|
553 |
while((n=allocList.Count())!=0); |
|
554 |
} |
|
555 |
done: |
|
556 |
RamAllocLock::Unlock(); |
|
557 |
return r; |
|
558 |
} |
|
559 |
||
560 |
||
561 |
TInt DUnpagedMemoryManager::AllocContiguousPages(DMemoryObject* aMemory, RPageArray::TIter aPageList, TUint aAlign, TPhysAddr& aPhysAddr) |
|
562 |
{ |
|
563 |
TUint size = aPageList.Count(); |
|
564 |
RamAllocLock::Lock(); |
|
565 |
||
566 |
// allocate memory... |
|
567 |
Mmu& m = TheMmu; |
|
568 |
TPhysAddr physAddr; |
|
569 |
TInt r = m.AllocContiguousRam(physAddr, size, aAlign, aMemory->RamAllocFlags()); |
|
570 |
if(r==KErrNone) |
|
571 |
{ |
|
572 |
// assign pages to memory object... |
|
573 |
TUint index = aPageList.Index(); |
|
574 |
TUint flags = aMemory->PageInfoFlags(); |
|
575 |
SPageInfo* pi = SPageInfo::FromPhysAddr(physAddr); |
|
576 |
SPageInfo* piEnd = pi+size; |
|
577 |
TUint flash = 0; |
|
578 |
MmuLock::Lock(); |
|
579 |
while(pi<piEnd) |
|
580 |
{ |
|
581 |
MmuLock::Flash(flash,KMaxPageInfoUpdatesInOneGo); |
|
582 |
pi->SetManaged(aMemory,index++,flags); |
|
583 |
++pi; |
|
584 |
} |
|
585 |
MmuLock::Unlock(); |
|
586 |
||
587 |
// add pages to page array... |
|
588 |
aPageList.AddContiguous(size,physAddr); |
|
589 |
||
590 |
// set result... |
|
591 |
aPhysAddr = physAddr; |
|
592 |
} |
|
593 |
||
594 |
RamAllocLock::Unlock(); |
|
595 |
return r; |
|
596 |
} |
|
597 |
||
598 |
||
599 |
TInt DUnpagedMemoryManager::Pin(DMemoryObject* aMemory, DMemoryMappingBase* aMapping, TPinArgs& aPinArgs) |
|
600 |
{ |
|
601 |
RPageArray::TIter pageList; |
|
602 |
aMemory->iPages.FindStart(aMapping->iStartIndex,aMapping->iSizeInPages,pageList); |
|
603 |
||
604 |
MmuLock::Lock(); |
|
605 |
||
606 |
TUint n; |
|
607 |
TPhysAddr* pages; |
|
608 |
TUint flash = 0; |
|
609 |
while((n=pageList.Pages(pages,KMaxPageInfoUpdatesInOneGo))!=0) |
|
610 |
{ |
|
611 |
TPhysAddr* p = pages; |
|
612 |
TPhysAddr* pEnd = p+n; |
|
613 |
do |
|
614 |
{ |
|
615 |
TPhysAddr page = *p++; |
|
616 |
if(RPageArray::TargetStateIsDecommitted(page)) |
|
617 |
goto stop; // page is being decommitted, so can't pin it |
|
618 |
} |
|
619 |
while(p!=pEnd); |
|
620 |
pageList.Skip(n); |
|
621 |
flash += n; |
|
622 |
MmuLock::Flash(flash,KMaxPageInfoUpdatesInOneGo); |
|
623 |
} |
|
624 |
stop: |
|
625 |
MmuLock::Unlock(); |
|
626 |
||
627 |
aMemory->iPages.FindEnd(aMapping->iStartIndex,aMapping->iSizeInPages); |
|
628 |
||
629 |
return pageList.Count() ? KErrNotFound : KErrNone; |
|
630 |
} |
|
631 |
||
632 |
||
633 |
void DUnpagedMemoryManager::Unpin(DMemoryObject* aMemory, DMemoryMappingBase* aMapping, TPinArgs& aPinArgs) |
|
634 |
{ |
|
635 |
} |
|
636 |
||
637 |
||
638 |
void DUnpagedMemoryManager::DoCleanupDecommitted(DMemoryObject* aMemory) |
|
639 |
{ |
|
640 |
MemoryObjectLock::Lock(aMemory); |
|
641 |
FreeDecommitted(aMemory,0,aMemory->iSizeInPages); |
|
642 |
MemoryObjectLock::Unlock(aMemory); |
|
643 |
} |
|
644 |
||
645 |
||
646 |
TInt DUnpagedMemoryManager::Wipe(DMemoryObject* aMemory) |
|
647 |
{ |
|
648 |
__NK_ASSERT_DEBUG(MemoryObjectLock::IsHeld(aMemory)); |
|
649 |
||
650 |
// make iterator for region... |
|
651 |
RPageArray::TIter pageIter; |
|
652 |
aMemory->iPages.FindStart(0,aMemory->iSizeInPages,pageIter); |
|
653 |
||
654 |
for(;;) |
|
655 |
{ |
|
656 |
// find some pages... |
|
657 |
RPageArray::TIter pageList; |
|
658 |
TUint n = pageIter.Find(pageList); |
|
659 |
if(!n) |
|
660 |
break; |
|
661 |
||
662 |
// wipe some pages... |
|
663 |
WipePages(aMemory,pageList); |
|
664 |
||
665 |
// move on... |
|
666 |
pageIter.FindRelease(n); |
|
667 |
} |
|
668 |
||
669 |
aMemory->iPages.FindEnd(0,aMemory->iSizeInPages); |
|
670 |
||
671 |
return KErrNone; |
|
672 |
} |
|
673 |
||
674 |
||
675 |
void DUnpagedMemoryManager::WipePages(DMemoryObject* aMemory, RPageArray::TIter aPageList) |
|
676 |
{ |
|
677 |
TUint index = aPageList.Index(); |
|
678 |
TUint count = aPageList.Count(); |
|
679 |
TRACE(("DUnpagedMemoryManager::WipePages(0x%08x,0x%x,0x%x)",aMemory,index,count)); |
|
680 |
||
681 |
__NK_ASSERT_ALWAYS(!aMemory->IsReadOnly()); // trap wiping read-only memory |
|
682 |
||
683 |
RamAllocLock::Lock(); |
|
684 |
||
685 |
while(count) |
|
686 |
{ |
|
687 |
// get some physical page addresses... |
|
688 |
TPhysAddr pages[KMaxPagesInOneGo]; |
|
689 |
TPhysAddr physAddr; |
|
690 |
TUint n = count; |
|
691 |
if(n>KMaxPagesInOneGo) |
|
692 |
n = KMaxPagesInOneGo; |
|
693 |
TInt r = aMemory->iPages.PhysAddr(index,n,physAddr,pages); |
|
694 |
__NK_ASSERT_ALWAYS(r>=0); // caller should have ensured all pages are present |
|
695 |
||
696 |
// wipe some pages... |
|
697 |
TPhysAddr* pagesToWipe = r!=0 ? pages : (TPhysAddr*)((TLinAddr)physAddr|1); |
|
698 |
TheMmu.PagesAllocated(pagesToWipe,n,aMemory->RamAllocFlags(),true); |
|
699 |
||
700 |
// move on... |
|
701 |
index += n; |
|
702 |
count -= n; |
|
703 |
} |
|
704 |
||
705 |
RamAllocLock::Unlock(); |
|
706 |
} |
|
707 |
||
708 |
||
709 |
TZonePageType DUnpagedMemoryManager::PageType() |
|
710 |
{// Unpaged memory cannot be moved or discarded therefore it is fixed. |
|
711 |
return EPageFixed; |
|
712 |
} |
|
713 |
||
714 |
||
715 |
/** |
|
716 |
Manager for memory objects containing normal unpaged RAM, as |
|
717 |
#DUnpagedMemoryManager, but which may be 'moved' by RAM |
|
718 |
defragmentation. I.e. have the physical pages used to store its content |
|
719 |
substituted for others. |
|
720 |
||
721 |
Such memory may cause transient page faults if it is accessed whilst its |
|
722 |
contents are being moved, this makes it unsuitable for most kernel-side |
|
723 |
usage. This is the memory management scheme normally used for unpaged user |
|
724 |
memory. |
|
725 |
*/ |
|
726 |
class DMovableMemoryManager : public DUnpagedMemoryManager |
|
727 |
{ |
|
728 |
public: |
|
729 |
// from DMemoryManager... |
|
730 |
virtual TInt MovePage(DMemoryObject* aMemory, SPageInfo* aOldPageInfo, TPhysAddr& aNewPage, TUint aBlockZoneId, TBool aBlockRest); |
|
110
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
731 |
virtual TInt MoveAndAllocPage(DMemoryObject* aMemory, SPageInfo* aPageInfo, TZonePageType aPageType); |
0 | 732 |
virtual TInt HandleFault( DMemoryObject* aMemory, TUint aIndex, DMemoryMapping* aMapping, |
733 |
TUint aMapInstanceCount, TUint aAccessPermissions); |
|
734 |
virtual TZonePageType PageType(); |
|
735 |
public: |
|
736 |
/** |
|
737 |
The single instance of this manager class. |
|
738 |
*/ |
|
739 |
static DMovableMemoryManager TheManager; |
|
740 |
}; |
|
741 |
||
742 |
||
743 |
DMovableMemoryManager DMovableMemoryManager::TheManager; |
|
744 |
DMemoryManager* TheMovableMemoryManager = &DMovableMemoryManager::TheManager; |
|
745 |
||
746 |
||
747 |
TInt DMovableMemoryManager::MovePage( DMemoryObject* aMemory, SPageInfo* aOldPageInfo, |
|
748 |
TPhysAddr& aNewPage, TUint aBlockZoneId, TBool aBlockRest) |
|
749 |
{ |
|
750 |
__NK_ASSERT_DEBUG(RamAllocLock::IsHeld()); |
|
751 |
||
752 |
// Allocate the new page to move to, ensuring that we use the page type of the |
|
753 |
// manager assigned to this page. |
|
754 |
TPhysAddr newPage; |
|
755 |
Mmu& m = TheMmu; |
|
756 |
TInt r = m.AllocRam(&newPage, 1, aMemory->RamAllocFlags(), aMemory->iManager->PageType(), |
|
757 |
aBlockZoneId, aBlockRest); |
|
758 |
if (r != KErrNone) |
|
759 |
{// Failed to allocate a new page to move the page to so can't continue. |
|
760 |
return r; |
|
761 |
} |
|
762 |
||
763 |
r = KErrInUse; |
|
764 |
MmuLock::Lock(); |
|
765 |
||
766 |
TUint index = aOldPageInfo->Index(); |
|
767 |
TRACE( ("DMovableMemoryManager::MovePage(0x%08x,0x%08x,?,0x%08x,%d) index=0x%x", |
|
768 |
aMemory,aOldPageInfo,aBlockZoneId,aBlockRest,index)); |
|
769 |
__NK_ASSERT_DEBUG(aMemory==aOldPageInfo->Owner()); |
|
770 |
||
771 |
// Mark the page as being moved and get a pointer to the page array entry. |
|
772 |
RPageArray::TIter pageIter; |
|
773 |
TPhysAddr* const movingPageArrayPtr = aMemory->iPages.MovePageStart(index, pageIter); |
|
774 |
if (!movingPageArrayPtr) |
|
775 |
{// Can't move the page another operation is being performed on it. |
|
776 |
MmuLock::Unlock(); |
|
777 |
TheMmu.FreeRam(&newPage, 1, aMemory->iManager->PageType()); |
|
778 |
return r; |
|
779 |
} |
|
780 |
__NK_ASSERT_DEBUG(RPageArray::IsPresent(*movingPageArrayPtr)); |
|
781 |
TPhysAddr oldPageEntry = *movingPageArrayPtr; |
|
782 |
TPhysAddr oldPage = oldPageEntry & ~KPageMask; |
|
783 |
#ifdef _DEBUG |
|
784 |
if (oldPage != aOldPageInfo->PhysAddr()) |
|
785 |
{// The address of page array entry and the page info should match except |
|
786 |
// when the page is being shadowed. |
|
787 |
__NK_ASSERT_DEBUG(SPageInfo::FromPhysAddr(oldPage)->Type() == SPageInfo::EShadow); |
|
788 |
} |
|
789 |
#endif |
|
790 |
__NK_ASSERT_DEBUG((newPage & KPageMask) == 0); |
|
791 |
__NK_ASSERT_DEBUG(newPage != oldPage); |
|
792 |
||
793 |
// Set the modifier so we can detect if the page state is updated. |
|
794 |
aOldPageInfo->SetModifier(&pageIter); |
|
795 |
||
796 |
// Restrict the page ready for moving. |
|
797 |
// Read only memory objects don't need to be restricted but we still need |
|
798 |
// to discover any physically pinned mappings. |
|
799 |
TBool pageRestrictedNA = !aMemory->IsReadOnly(); |
|
800 |
TRestrictPagesType restrictType = pageRestrictedNA ? |
|
801 |
ERestrictPagesNoAccessForMoving : |
|
802 |
ERestrictPagesForMovingFlag; |
|
803 |
||
804 |
// This page's contents may be changed so restrict the page to no access |
|
805 |
// so we can detect any access to it while we are moving it. |
|
806 |
MmuLock::Unlock(); |
|
807 |
// This will clear the memory objects mapping added flag so we can detect any new mappings. |
|
808 |
aMemory->RestrictPages(pageIter, restrictType); |
|
809 |
||
810 |
const TUint KOldMappingSlot = 0; |
|
811 |
const TUint KNewMappingSlot = 1; |
|
812 |
const TAny* tmpPtrOld = NULL; |
|
813 |
TAny* tmpPtrNew; |
|
814 |
// Verify that page restricting wasn't interrupted, if it was then the page |
|
815 |
// can't be moved so remap it. |
|
816 |
// If the page array entry (*movingPageArrayPtr) has been modified then a pinning |
|
817 |
// veto'd the preparation. |
|
818 |
MmuLock::Lock(); |
|
819 |
if (aOldPageInfo->CheckModified(&pageIter) || oldPageEntry != *movingPageArrayPtr) |
|
820 |
{// Page is pinned or has been modified by another operation. |
|
821 |
MmuLock::Unlock(); |
|
822 |
TheMmu.FreeRam(&newPage, 1, aMemory->iManager->PageType()); |
|
823 |
goto remap; |
|
824 |
} |
|
825 |
||
826 |
MmuLock::Unlock(); |
|
827 |
// Copy the contents of the page using some temporary mappings. |
|
828 |
tmpPtrOld = (TAny*)TheMmu.MapTemp(oldPage, index, KOldMappingSlot); |
|
829 |
tmpPtrNew = (TAny*)TheMmu.MapTemp(newPage, index, KNewMappingSlot); |
|
830 |
pagecpy(tmpPtrNew, tmpPtrOld); |
|
831 |
||
832 |
// Unmap and perform cache maintenance if the memory object is executable. |
|
833 |
// Must do cache maintenance before we add any new mappings to the new page |
|
834 |
// to ensure that any old instruction cache entries for the new page aren't |
|
835 |
// picked up by any remapped executable mappings. |
|
836 |
if (aMemory->IsExecutable()) |
|
837 |
CacheMaintenance::CodeChanged((TLinAddr)tmpPtrNew, KPageSize); |
|
838 |
TheMmu.UnmapTemp(KNewMappingSlot); |
|
839 |
#ifndef _DEBUG |
|
840 |
TheMmu.UnmapTemp(KOldMappingSlot); |
|
841 |
#endif |
|
842 |
||
843 |
MmuLock::Lock(); |
|
844 |
if (!aOldPageInfo->CheckModified(&pageIter) && oldPageEntry == *movingPageArrayPtr && |
|
845 |
!aMemory->MappingAddedFlag()) |
|
846 |
{ |
|
847 |
// The page has been copied without anyone modifying it so set the page |
|
848 |
// array entry to new physical address and map the page. |
|
849 |
RPageArray::PageMoveNewAddr(*movingPageArrayPtr, newPage); |
|
850 |
||
851 |
// Copy across the page info data from the old page to the new. |
|
852 |
SPageInfo& newPageInfo = *SPageInfo::FromPhysAddr(newPage); |
|
853 |
newPageInfo = *aOldPageInfo; |
|
854 |
if (aMemory->IsDemandPaged()) |
|
855 |
{// Let the pager deal with the live list links for this page if required. |
|
856 |
ThePager.ReplacePage(*aOldPageInfo, newPageInfo); |
|
857 |
} |
|
858 |
||
859 |
MmuLock::Unlock(); |
|
860 |
r = KErrNone; |
|
861 |
aNewPage = newPage; |
|
862 |
} |
|
863 |
else |
|
864 |
{ |
|
865 |
MmuLock::Unlock(); |
|
866 |
TheMmu.FreeRam(&newPage, 1, aMemory->iManager->PageType()); |
|
867 |
} |
|
868 |
remap: |
|
869 |
// Remap all mappings to the new physical address if the move was successful or |
|
870 |
// back to the old page if the move failed. |
|
871 |
// Invalidate the TLB for the page if old mappings still exist or new |
|
872 |
// mappings were added but will be removed as the page can't be moved. |
|
873 |
TBool invalidateTLB = !pageRestrictedNA || r != KErrNone; |
|
874 |
aMemory->RemapPage(*movingPageArrayPtr, index, invalidateTLB); |
|
875 |
||
876 |
if (r == KErrNone) |
|
877 |
{// Must wait until here as read only memory objects' mappings aren't |
|
878 |
// all guaranteed to point to the new page until after RemapPage(). |
|
879 |
TheMmu.FreeRam(&oldPage, 1, aMemory->iManager->PageType()); |
|
880 |
#ifdef _DEBUG |
|
881 |
// For testing purposes clear the old page to help detect any |
|
882 |
// erroneous mappings to the old page. |
|
883 |
memclr((TAny*)tmpPtrOld, KPageSize); |
|
884 |
} |
|
885 |
TheMmu.UnmapTemp(KOldMappingSlot); // Will invalidate the TLB entry for the mapping. |
|
886 |
#else |
|
887 |
} |
|
888 |
#endif |
|
889 |
// indicate we've stopped moving memory now... |
|
890 |
MmuLock::Lock(); |
|
891 |
RPageArray::MovePageEnd(*movingPageArrayPtr); |
|
892 |
MmuLock::Unlock(); |
|
893 |
||
894 |
return r; |
|
895 |
} |
|
896 |
||
897 |
||
110
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
898 |
TInt DMovableMemoryManager::MoveAndAllocPage(DMemoryObject* aMemory, SPageInfo* aPageInfo, TZonePageType aPageType) |
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
899 |
{ |
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
900 |
TPhysAddr newPage; |
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
901 |
TInt r = MovePage(aMemory, aPageInfo, newPage, KRamZoneInvalidId, EFalse); |
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
902 |
if (r == KErrNone) |
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
903 |
{ |
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
904 |
TheMmu.MarkPageAllocated(aPageInfo->PhysAddr(), aPageType); |
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
905 |
} |
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
906 |
return r; |
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
907 |
} |
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
908 |
|
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
909 |
|
0 | 910 |
TInt DMovableMemoryManager::HandleFault(DMemoryObject* aMemory, TUint aIndex, DMemoryMapping* aMapping, |
911 |
TUint aMapInstanceCount, TUint aAccessPermissions) |
|
912 |
{ |
|
913 |
TInt r = KErrNotFound; |
|
914 |
SPageInfo* pageInfo; |
|
915 |
MmuLock::Lock(); |
|
916 |
__UNLOCK_GUARD_START(MmuLock); |
|
917 |
TPhysAddr* const pageEntry = aMemory->iPages.PageEntry(aIndex); |
|
918 |
if (!pageEntry || !RPageArray::IsPresent(*pageEntry) || |
|
919 |
aMapInstanceCount != aMapping->MapInstanceCount() || aMapping->BeingDetached()) |
|
920 |
{// The page isn't present or has been unmapped so invalid access. |
|
921 |
goto exit; |
|
922 |
} |
|
923 |
||
924 |
if (aMapping->MovingPageIn(*pageEntry, aIndex)) |
|
925 |
{// The page was has been paged in as it was still mapped. |
|
926 |
pageInfo = SPageInfo::FromPhysAddr(*pageEntry & ~KPageMask); |
|
927 |
pageInfo->SetModifier(0); // Signal to MovePage() that the page has been paged in. |
|
928 |
r = KErrNone; |
|
929 |
} |
|
930 |
||
931 |
exit: |
|
932 |
__UNLOCK_GUARD_END(MmuLock); |
|
933 |
MmuLock::Unlock(); |
|
934 |
return r; |
|
935 |
} |
|
936 |
||
937 |
||
938 |
TZonePageType DMovableMemoryManager::PageType() |
|
939 |
{// Movable memory object pages are movable. |
|
940 |
return EPageMovable; |
|
941 |
} |
|
942 |
||
943 |
||
944 |
/** |
|
945 |
Manager for memory objects containing normal unpaged RAM, which |
|
946 |
as well as being 'movable', like #DMovableMemoryManager, |
|
947 |
may also have regions marked as 'discardable'. Discardable pages may be |
|
948 |
reclaimed (removed) by the system at any time; this state is controlled using |
|
949 |
the functions #AllowDiscard and #DisallowDiscard. |
|
950 |
<P> |
|
951 |
This is used for the memory containing file system caches. Discardable memory |
|
952 |
is managed using similar |
|
953 |
*/ |
|
954 |
class DDiscardableMemoryManager : public DMovableMemoryManager |
|
955 |
{ |
|
956 |
public: |
|
957 |
// from DMemoryManager... |
|
958 |
virtual TInt AllowDiscard(DMemoryObject* aMemory, TUint aIndex, TUint aCount); |
|
959 |
virtual TInt DisallowDiscard(DMemoryObject* aMemory, TUint aIndex, TUint aCount); |
|
960 |
virtual TInt StealPage(DMemoryObject* aMemory, SPageInfo* aPageInfo); |
|
961 |
virtual TInt RestrictPage(DMemoryObject* aMemory, SPageInfo* aPageInfo, TRestrictPagesType aRestriction); |
|
962 |
virtual TZonePageType PageType(); |
|
963 |
public: |
|
964 |
/** |
|
965 |
The single instance of this manager class. |
|
966 |
*/ |
|
967 |
static DDiscardableMemoryManager TheManager; |
|
968 |
}; |
|
969 |
||
970 |
||
971 |
DDiscardableMemoryManager DDiscardableMemoryManager::TheManager; |
|
972 |
DMemoryManager* TheDiscardableMemoryManager = &DDiscardableMemoryManager::TheManager; |
|
973 |
||
974 |
||
975 |
TInt DDiscardableMemoryManager::AllowDiscard(DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
976 |
{ |
|
977 |
TRACE2(("DDiscardableMemoryManager::AllowDiscard(0x%08x,0x%x,0x%x)",aMemory, aIndex, aCount)); |
|
978 |
__NK_ASSERT_DEBUG(MemoryObjectLock::IsHeld(aMemory)); |
|
979 |
||
980 |
// make iterator for region... |
|
981 |
RPageArray::TIter pageIter; |
|
982 |
aMemory->iPages.FindStart(aIndex,aCount,pageIter); |
|
983 |
||
984 |
for(;;) |
|
985 |
{ |
|
986 |
// find some pages... |
|
987 |
RPageArray::TIter pageList; |
|
988 |
TUint nFound = pageIter.Find(pageList); |
|
989 |
if(!nFound) |
|
990 |
break; |
|
991 |
||
992 |
// donate pages... |
|
993 |
TUint n; |
|
994 |
TPhysAddr* pages; |
|
995 |
while((n=pageList.Pages(pages,KMaxPagesInOneGo))!=0) |
|
996 |
{ |
|
997 |
pageList.Skip(n); |
|
998 |
ThePager.DonatePages(n,pages); |
|
999 |
} |
|
1000 |
||
1001 |
// move on... |
|
1002 |
pageIter.FindRelease(nFound); |
|
1003 |
} |
|
1004 |
||
1005 |
// done... |
|
1006 |
aMemory->iPages.FindEnd(aIndex,aCount); |
|
1007 |
||
1008 |
return KErrNone; |
|
1009 |
} |
|
1010 |
||
1011 |
||
1012 |
TInt DDiscardableMemoryManager::DisallowDiscard(DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
1013 |
{ |
|
1014 |
TRACE2(("DDiscardableMemoryManager::DisallowDiscard(0x%08x,0x%x,0x%x)",aMemory, aIndex, aCount)); |
|
1015 |
__NK_ASSERT_DEBUG(MemoryObjectLock::IsHeld(aMemory)); |
|
1016 |
||
1017 |
TInt r = KErrNone; |
|
1018 |
||
1019 |
// get pages... |
|
1020 |
RPageArray::TIter pageIter; |
|
1021 |
aMemory->iPages.FindStart(aIndex,aCount,pageIter); |
|
1022 |
||
1023 |
RPageArray::TIter pageList; |
|
1024 |
TUint numPages = pageIter.Find(pageList); |
|
1025 |
||
1026 |
if(numPages!=aCount) |
|
1027 |
{ |
|
1028 |
// not all pages are present... |
|
1029 |
r = KErrNotFound; |
|
1030 |
} |
|
1031 |
else |
|
1032 |
{ |
|
1033 |
TUint n; |
|
1034 |
TPhysAddr* pages; |
|
1035 |
while((n=pageList.Pages(pages,KMaxPagesInOneGo))!=0) |
|
1036 |
{ |
|
1037 |
pageList.Skip(n); |
|
1038 |
r = ThePager.ReclaimPages(n,pages); |
|
1039 |
if(r!=KErrNone) |
|
1040 |
break; |
|
1041 |
} |
|
1042 |
} |
|
1043 |
||
1044 |
// done with pages... |
|
1045 |
if(numPages) |
|
1046 |
pageIter.FindRelease(numPages); |
|
1047 |
aMemory->iPages.FindEnd(aIndex,aCount); |
|
1048 |
||
1049 |
return r; |
|
1050 |
} |
|
1051 |
||
1052 |
||
1053 |
TInt DDiscardableMemoryManager::StealPage(DMemoryObject* aMemory, SPageInfo* aPageInfo) |
|
1054 |
{ |
|
1055 |
TRACE2(("DDiscardableMemoryManager::StealPage(0x%08x,0x%08x)",aMemory,aPageInfo)); |
|
1056 |
__NK_ASSERT_DEBUG(RamAllocLock::IsHeld()); |
|
1057 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
1058 |
__UNLOCK_GUARD_START(MmuLock); |
|
1059 |
||
110
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
1060 |
// must always hold the PageCleaningLock if the page needs to be cleaned |
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
1061 |
__NK_ASSERT_DEBUG(!aPageInfo->IsDirty() || PageCleaningLock::IsHeld()); |
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
1062 |
|
0 | 1063 |
TUint index = aPageInfo->Index(); |
1064 |
TInt r; |
|
1065 |
||
1066 |
RPageArray::TIter pageList; |
|
1067 |
TPhysAddr* p = aMemory->iPages.StealPageStart(index,pageList); |
|
1068 |
__NK_ASSERT_DEBUG((*p&~KPageMask)==aPageInfo->PhysAddr()); // object should have our page |
|
1069 |
||
1070 |
aPageInfo->SetModifier(&pageList); |
|
1071 |
||
1072 |
__UNLOCK_GUARD_END(MmuLock); |
|
1073 |
MmuLock::Unlock(); |
|
1074 |
||
1075 |
// unmap the page... |
|
1076 |
aMemory->UnmapPages(pageList,false); |
|
1077 |
||
1078 |
MmuLock::Lock(); |
|
1079 |
||
1080 |
__NK_ASSERT_DEBUG((*p&~KPageMask)==aPageInfo->PhysAddr()); // object should still have our page because freeing a page requires the RamAllocLock, which we hold |
|
1081 |
||
1082 |
if(aPageInfo->CheckModified(&pageList)) |
|
1083 |
{ |
|
1084 |
// page state was changed, this can only happen if a page fault put this page |
|
1085 |
// back into the committed state or if the page was pinned. |
|
1086 |
// From either of these states it's possible to subsequently change |
|
1087 |
// to any other state or use (so we can't assert anything here). |
|
1088 |
r = KErrInUse; |
|
1089 |
} |
|
1090 |
else |
|
1091 |
{ |
|
1092 |
// nobody else has modified page state, so we can... |
|
1093 |
TPhysAddr page = *p; |
|
1094 |
__NK_ASSERT_DEBUG(RPageArray::TargetStateIsDecommitted(page)); |
|
1095 |
if(page&RPageArray::EUnmapVetoed) |
|
1096 |
{ |
|
1097 |
// operation was vetoed, which means page had a pinned mapping but the pin |
|
1098 |
// operation hadn't got around to removing the page from the live list, |
|
1099 |
// we need to restore correct state... |
|
1100 |
if(RPageArray::State(page)==RPageArray::EStealing) |
|
1101 |
*p = (page&~(RPageArray::EStateMask|RPageArray::EUnmapVetoed))|RPageArray::ECommitted; |
|
1102 |
// else |
|
1103 |
// leave page in state it was before we attempted to steal it |
|
1104 |
||
1105 |
// put page back on live list so it doesn't get lost. |
|
1106 |
// We put it at the start as if it were recently accessed because being pinned |
|
1107 |
// counts as an access and we can't put it anywhere else otherwise when |
|
1108 |
// page stealing retries it may get this same page again, potentially causing |
|
1109 |
// deadlock. |
|
1110 |
__NK_ASSERT_DEBUG(aPageInfo->PagedState()==SPageInfo::EUnpaged); // no one else has changed page since we removed it in DPager::StealPage |
|
1111 |
ThePager.PagedIn(aPageInfo); |
|
1112 |
||
1113 |
r = KErrInUse; |
|
1114 |
} |
|
1115 |
else |
|
1116 |
{ |
|
1117 |
// page successfully unmapped... |
|
1118 |
aPageInfo->SetReadOnly(); // page not mapped, so must be read-only |
|
1119 |
||
110
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
1120 |
// attempt to clean the page if it is dirty... |
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
1121 |
if (aPageInfo->IsDirty()) |
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
1122 |
aMemory->iManager->CleanPages(1, &aPageInfo, EFalse); |
0 | 1123 |
|
110
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
1124 |
if(aPageInfo) |
0 | 1125 |
{ |
1126 |
// page successfully stolen... |
|
1127 |
__NK_ASSERT_DEBUG((*p^page)<(TUint)KPageSize); // sanity check, page should still be allocated to us |
|
1128 |
__NK_ASSERT_DEBUG(aPageInfo->IsDirty()==false); |
|
1129 |
__NK_ASSERT_DEBUG(aPageInfo->IsWritable()==false); |
|
1130 |
||
1131 |
TPhysAddr pagerInfo = aPageInfo->PagingManagerData(); |
|
1132 |
*p = pagerInfo; |
|
1133 |
__NK_ASSERT_ALWAYS((pagerInfo&(RPageArray::EFlagsMask|RPageArray::EStateMask)) == RPageArray::ENotPresent); |
|
1134 |
||
1135 |
TheMmu.PageFreed(aPageInfo); |
|
110
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
1136 |
r = KErrNone; |
0 | 1137 |
} |
1138 |
else |
|
110
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
1139 |
r = KErrInUse; |
0 | 1140 |
} |
1141 |
} |
|
1142 |
||
1143 |
aMemory->iPages.StealPageEnd(index,r==KErrNone ? 1 : 0); |
|
1144 |
||
1145 |
#ifdef _DEBUG |
|
1146 |
if(r!=KErrNone) |
|
1147 |
TRACE2(("DDiscardableMemoryManager::StealPage fail because preempted")); |
|
1148 |
#endif |
|
1149 |
||
1150 |
TRACE2(("DDiscardableMemoryManager::StealPage returns %d",r)); |
|
1151 |
return r; |
|
1152 |
} |
|
1153 |
||
1154 |
||
1155 |
TInt DDiscardableMemoryManager::RestrictPage(DMemoryObject* aMemory, SPageInfo* aPageInfo, TRestrictPagesType aRestriction) |
|
1156 |
{ |
|
1157 |
if(aRestriction==ERestrictPagesNoAccessForOldPage) |
|
1158 |
{ |
|
1159 |
// Lie to pager when it sets an old page inaccessible as we don't want to rejunvanate |
|
1160 |
// the page if it is accessed as RChunk::Lock() should be used to remove the page from |
|
1161 |
// the live list before accessing the page. |
|
1162 |
return KErrNone; |
|
1163 |
} |
|
1164 |
return DMovableMemoryManager::RestrictPage(aMemory, aPageInfo, aRestriction); |
|
1165 |
} |
|
1166 |
||
1167 |
||
1168 |
TZonePageType DDiscardableMemoryManager::PageType() |
|
1169 |
{// Discardable memory objects page are movable unless they are donated to the pager. |
|
1170 |
return EPageMovable; |
|
1171 |
} |
|
1172 |
||
1173 |
||
1174 |
||
1175 |
/** |
|
1176 |
Manager for memory objects containing memory mapped hardware devices or special |
|
1177 |
purpose memory for which the physical addresses are fixed. |
|
1178 |
*/ |
|
1179 |
class DHardwareMemoryManager : public DMemoryManager |
|
1180 |
{ |
|
1181 |
public: |
|
1182 |
// from DMemoryManager... |
|
1183 |
virtual void Destruct(DMemoryObject* aMemory); |
|
87
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1184 |
virtual TInt AddPages(DMemoryObject* aMemory, TUint aIndex, TUint aCount, const TPhysAddr* aPages); |
0 | 1185 |
virtual TInt AddContiguous(DMemoryObject* aMemory, TUint aIndex, TUint aCount, TPhysAddr aPhysAddr); |
1186 |
virtual TInt RemovePages(DMemoryObject* aMemory, TUint aIndex, TUint aCount, TPhysAddr* aPages); |
|
1187 |
virtual TInt Pin(DMemoryObject* aMemory, DMemoryMappingBase* aMapping, TPinArgs& aPinArgs); |
|
1188 |
virtual void Unpin(DMemoryObject* aMemory, DMemoryMappingBase* aMapping, TPinArgs& aPinArgs); |
|
1189 |
||
1190 |
private: |
|
1191 |
/** |
|
1192 |
Update the page information structure for RAM added with #AddPages and #AddContiguous. |
|
1193 |
||
1194 |
This performs debug checks to ensure that any physical memory which is added to more than |
|
1195 |
one memory object meets with the restriction imposed by the MMU and cache hardware. |
|
1196 |
It also verifies that the RAM pages are of type SPageInfo::EPhysAlloc, |
|
1197 |
i.e. were allocated with Epoc::AllocPhysicalRam or similar. |
|
1198 |
||
1199 |
This is only used when the physical addresses of the page being added to a memory |
|
1200 |
object corresponds to RAM being managed by the kernel, i.e. physical addresses |
|
1201 |
with an associated #SPageInfo structure. |
|
1202 |
||
1203 |
@param aMemory A memory object associated with this manager. |
|
1204 |
@param aIndex Page index, within the memory, for the page. |
|
1205 |
@param aPageInfo The page information structure of the RAM page. |
|
1206 |
||
1207 |
@pre #MmuLock held. |
|
1208 |
@post #MmuLock held. |
|
1209 |
*/ |
|
1210 |
static void AssignPage(DMemoryObject* aMemory, TUint aIndex, SPageInfo* aPageInfo); |
|
1211 |
||
1212 |
/** |
|
1213 |
Update the page information structure for RAM removed with #RemovePages. |
|
1214 |
||
1215 |
This is only used when the physical addresses of the page being removed from a memory |
|
1216 |
object corresponds to RAM being managed by the kernel, i.e. physical addresses |
|
1217 |
with an associated #SPageInfo structure. |
|
1218 |
||
1219 |
@param aMemory A memory object associated with this manager. |
|
1220 |
@param aIndex Page index, within the memory, for the page. |
|
1221 |
@param aPageInfo The page information structure of the RAM page. |
|
1222 |
||
1223 |
@pre #MmuLock held. |
|
1224 |
@post #MmuLock held. |
|
1225 |
*/ |
|
1226 |
static void UnassignPage(DMemoryObject* aMemory, TUint aIndex, SPageInfo* aPageInfo); |
|
1227 |
||
1228 |
public: |
|
1229 |
/** |
|
1230 |
The single instance of this manager class. |
|
1231 |
*/ |
|
1232 |
static DHardwareMemoryManager TheManager; |
|
1233 |
}; |
|
1234 |
||
1235 |
||
1236 |
DHardwareMemoryManager DHardwareMemoryManager::TheManager; |
|
1237 |
DMemoryManager* TheHardwareMemoryManager = &DHardwareMemoryManager::TheManager; |
|
1238 |
||
1239 |
||
1240 |
void DHardwareMemoryManager::Destruct(DMemoryObject* aMemory) |
|
1241 |
{ |
|
1242 |
MemoryObjectLock::Lock(aMemory); |
|
1243 |
RemovePages(aMemory,0,aMemory->iSizeInPages,0); |
|
1244 |
MemoryObjectLock::Unlock(aMemory); |
|
1245 |
aMemory->Close(); |
|
1246 |
} |
|
1247 |
||
1248 |
||
87
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1249 |
TInt DHardwareMemoryManager::AddPages(DMemoryObject* aMemory, TUint aIndex, TUint aCount, const TPhysAddr* aPages) |
0 | 1250 |
{ |
1251 |
TRACE2(("DHardwareMemoryManager::AddPages(0x%08x,0x%x,0x%x,?)",aMemory, aIndex, aCount)); |
|
1252 |
__NK_ASSERT_DEBUG(MemoryObjectLock::IsHeld(aMemory)); |
|
1253 |
||
1254 |
// validate arguments... |
|
87
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1255 |
const TPhysAddr* pages = aPages; |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1256 |
const TPhysAddr* pagesEnd = aPages+aCount; |
0 | 1257 |
TPhysAddr checkMask = 0; |
1258 |
do checkMask |= *pages++; |
|
1259 |
while(pages<pagesEnd); |
|
1260 |
if(checkMask&KPageMask) |
|
1261 |
return KErrArgument; |
|
1262 |
||
1263 |
// check and allocate page array entries... |
|
1264 |
RPageArray::TIter pageIter; |
|
1265 |
TInt r = aMemory->iPages.AddStart(aIndex,aCount,pageIter); |
|
1266 |
if(r!=KErrNone) |
|
1267 |
return r; |
|
1268 |
||
1269 |
// assign pages... |
|
1270 |
pages = aPages; |
|
1271 |
TUint index = aIndex; |
|
1272 |
TUint flash = 0; |
|
1273 |
MmuLock::Lock(); |
|
1274 |
do |
|
1275 |
{ |
|
1276 |
MmuLock::Flash(flash,KMaxPageInfoUpdatesInOneGo/2); // flash twice as often because we're doing about twice the work as a simple page info update |
|
1277 |
TPhysAddr pagePhys = *pages++; |
|
1278 |
SPageInfo* pi = SPageInfo::SafeFromPhysAddr(pagePhys); |
|
1279 |
if(pi) |
|
1280 |
AssignPage(aMemory,index,pi); |
|
1281 |
++index; |
|
1282 |
} |
|
1283 |
while(pages<pagesEnd); |
|
1284 |
MmuLock::Unlock(); |
|
1285 |
||
1286 |
// map the pages... |
|
1287 |
RPageArray::TIter pageList = pageIter; |
|
1288 |
pageIter.Add(aCount,aPages); |
|
1289 |
r = aMemory->MapPages(pageList); |
|
1290 |
||
1291 |
// release page array entries... |
|
1292 |
aMemory->iPages.AddEnd(aIndex,aCount); |
|
1293 |
||
1294 |
// revert if error... |
|
1295 |
if(r!=KErrNone) |
|
1296 |
RemovePages(aMemory,aIndex,aCount,0); |
|
1297 |
||
1298 |
return r; |
|
1299 |
} |
|
1300 |
||
1301 |
||
1302 |
TInt DHardwareMemoryManager::AddContiguous(DMemoryObject* aMemory, TUint aIndex, TUint aCount, TPhysAddr aPhysAddr) |
|
1303 |
{ |
|
1304 |
TRACE2(("DHardwareMemoryManager::AddContiguous(0x%08x,0x%x,0x%x,0x%08x)",aMemory, aIndex, aCount, aPhysAddr)); |
|
1305 |
__NK_ASSERT_DEBUG(MemoryObjectLock::IsHeld(aMemory)); |
|
1306 |
||
1307 |
// validate arguments... |
|
1308 |
if(aPhysAddr&KPageMask) |
|
1309 |
return KErrArgument; |
|
1310 |
||
1311 |
// check and allocate page array entries... |
|
1312 |
RPageArray::TIter pageIter; |
|
1313 |
TInt r = aMemory->iPages.AddStart(aIndex,aCount,pageIter); |
|
1314 |
if(r!=KErrNone) |
|
1315 |
return r; |
|
1316 |
||
1317 |
RPageArray::TIter pageList = pageIter; |
|
1318 |
||
1319 |
// assign pages... |
|
1320 |
SPageInfo* piStart = SPageInfo::SafeFromPhysAddr(aPhysAddr); |
|
1321 |
SPageInfo* piEnd = piStart+aCount; |
|
1322 |
if(piStart) |
|
1323 |
{ |
|
1324 |
SPageInfo* pi = piStart; |
|
1325 |
TUint index = aIndex; |
|
1326 |
TUint flash = 0; |
|
1327 |
MmuLock::Lock(); |
|
1328 |
while(pi<piEnd) |
|
1329 |
{ |
|
1330 |
MmuLock::Flash(flash,KMaxPageInfoUpdatesInOneGo/2); // flash twice as often because we're doing about twice the work as a simple page info update |
|
1331 |
AssignPage(aMemory,index,pi); |
|
1332 |
++index; |
|
1333 |
++pi; |
|
1334 |
} |
|
1335 |
MmuLock::Unlock(); |
|
1336 |
} |
|
1337 |
||
1338 |
// map the pages... |
|
1339 |
pageIter.AddContiguous(aCount,aPhysAddr); |
|
1340 |
r = aMemory->MapPages(pageList); |
|
1341 |
||
1342 |
// release page array entries... |
|
1343 |
aMemory->iPages.AddEnd(aIndex,aCount); |
|
1344 |
||
1345 |
// revert if error... |
|
1346 |
if(r!=KErrNone) |
|
1347 |
RemovePages(aMemory,aIndex,aCount,0); |
|
1348 |
||
1349 |
return r; |
|
1350 |
} |
|
1351 |
||
1352 |
||
1353 |
TInt DHardwareMemoryManager::RemovePages(DMemoryObject* aMemory, TUint aIndex, TUint aCount, TPhysAddr* aPages) |
|
1354 |
{ |
|
1355 |
TRACE2(("DHardwareMemoryManager::RemovePages(0x%08x,0x%x,0x%x,?)",aMemory, aIndex, aCount)); |
|
1356 |
__NK_ASSERT_DEBUG(MemoryObjectLock::IsHeld(aMemory)); |
|
1357 |
||
1358 |
RPageArray::TIter pageIter; |
|
1359 |
aMemory->iPages.FindStart(aIndex,aCount,pageIter); |
|
1360 |
||
1361 |
TUint numPages = 0; |
|
1362 |
for(;;) |
|
1363 |
{ |
|
1364 |
// find some pages... |
|
1365 |
RPageArray::TIter pageList; |
|
1366 |
TUint n = pageIter.RemoveFind(pageList); |
|
1367 |
if(!n) |
|
1368 |
break; |
|
1369 |
||
1370 |
// unmap some pages... |
|
1371 |
aMemory->UnmapPages(pageList,true); |
|
1372 |
||
1373 |
// free pages... |
|
1374 |
TPhysAddr pagePhys; |
|
1375 |
while(pageList.Remove(1,&pagePhys)) |
|
1376 |
{ |
|
1377 |
if(aPages) |
|
1378 |
*aPages++ = pagePhys; |
|
1379 |
++numPages; |
|
1380 |
||
1381 |
__NK_ASSERT_DEBUG((pagePhys&KPageMask)==0); |
|
1382 |
||
1383 |
TUint index = pageList.Index()-1; |
|
1384 |
SPageInfo* pi = SPageInfo::SafeFromPhysAddr(pagePhys); |
|
1385 |
if(!pi) |
|
1386 |
TheMmu.CleanAndInvalidatePages(&pagePhys,1,aMemory->Attributes(),index); |
|
1387 |
else |
|
1388 |
{ |
|
1389 |
MmuLock::Lock(); |
|
1390 |
UnassignPage(aMemory,index,pi); |
|
1391 |
MmuLock::Unlock(); |
|
1392 |
} |
|
1393 |
} |
|
1394 |
||
1395 |
// move on... |
|
1396 |
pageIter.FindRelease(n); |
|
1397 |
} |
|
1398 |
||
1399 |
aMemory->iPages.FindEnd(aIndex,aCount); |
|
1400 |
||
1401 |
return numPages; |
|
1402 |
} |
|
1403 |
||
1404 |
||
1405 |
void DHardwareMemoryManager::AssignPage(DMemoryObject* aMemory, TUint aIndex, SPageInfo* aPageInfo) |
|
1406 |
{ |
|
1407 |
TRACE2(("DHardwareMemoryManager::AssignPage(0x%08x,0x%x,phys=0x%08x)",aMemory, aIndex, aPageInfo->PhysAddr())); |
|
1408 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
1409 |
__NK_ASSERT_DEBUG(aPageInfo->Type()==SPageInfo::EPhysAlloc); |
|
1410 |
TUint flags = aMemory->PageInfoFlags(); |
|
1411 |
if(aPageInfo->UseCount()==0) |
|
1412 |
{ |
|
1413 |
// not mapped yet... |
|
1414 |
aPageInfo->SetMapped(aIndex,flags); |
|
1415 |
} |
|
1416 |
else |
|
1417 |
{ |
|
1418 |
// already mapped somewhere... |
|
1419 |
TMemoryType type = (TMemoryType)(flags&KMemoryTypeMask); |
|
1420 |
if(CacheMaintenance::IsCached(type)) |
|
1421 |
{ |
|
1422 |
// memory is cached at L1, check colour matches existing mapping... |
|
1423 |
if( (aPageInfo->Index()^aIndex) & KPageColourMask ) |
|
1424 |
{ |
|
1425 |
#ifdef _DEBUG |
|
1426 |
Kern::Printf("DHardwareMemoryManager::AssignPage BAD COLOUR"); |
|
1427 |
aPageInfo->Dump(); |
|
1428 |
#endif |
|
1429 |
__NK_ASSERT_ALWAYS(0); |
|
1430 |
} |
|
1431 |
} |
|
1432 |
// check memory type matches existing mapping... |
|
1433 |
if( (aPageInfo->Flags()^flags) & EMemoryAttributeMask ) |
|
1434 |
{ |
|
1435 |
#ifdef _DEBUG |
|
1436 |
Kern::Printf("DHardwareMemoryManager::AssignPage BAD MEMORY TYPE"); |
|
1437 |
aPageInfo->Dump(); |
|
1438 |
#endif |
|
1439 |
__NK_ASSERT_ALWAYS(0); |
|
1440 |
} |
|
1441 |
} |
|
1442 |
aPageInfo->IncUseCount(); |
|
1443 |
TRACE2(("DHardwareMemoryManager::AssignPage iUseCount=%d",aPageInfo->UseCount())); |
|
1444 |
} |
|
1445 |
||
1446 |
||
1447 |
void DHardwareMemoryManager::UnassignPage(DMemoryObject* aMemory, TUint aIndex, SPageInfo* aPageInfo) |
|
1448 |
{ |
|
1449 |
TRACE2(("DHardwareMemoryManager::UnassignPage(0x%08x,0x%x,phys=0x%08x)",aMemory, aIndex, aPageInfo->PhysAddr())); |
|
1450 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
1451 |
TRACE2(("DHardwareMemoryManager::UnassignPage iUseCount=%d",aPageInfo->UseCount())); |
|
1452 |
__NK_ASSERT_DEBUG(aPageInfo->UseCount()); |
|
1453 |
if(!aPageInfo->DecUseCount()) |
|
1454 |
{ |
|
1455 |
// page no longer being used by any memory object, make sure it's contents |
|
1456 |
// are purged from the cache... |
|
1457 |
TPhysAddr pagePhys = aPageInfo->PhysAddr(); |
|
1458 |
aPageInfo->SetModifier(&pagePhys); |
|
1459 |
MmuLock::Unlock(); |
|
1460 |
TheMmu.CleanAndInvalidatePages(&pagePhys,1,aMemory->Attributes(),aIndex); |
|
1461 |
MmuLock::Lock(); |
|
1462 |
if(!aPageInfo->CheckModified(&pagePhys)) // if page has not been reused... |
|
1463 |
aPageInfo->SetUncached(); // we know the memory is not in the cache |
|
1464 |
} |
|
1465 |
} |
|
1466 |
||
1467 |
||
1468 |
TInt DHardwareMemoryManager::Pin(DMemoryObject* aMemory, DMemoryMappingBase* aMapping, TPinArgs& aPinArgs) |
|
1469 |
{ |
|
1470 |
return ((DUnpagedMemoryManager*)this)->DUnpagedMemoryManager::Pin(aMemory,aMapping,aPinArgs); |
|
1471 |
} |
|
1472 |
||
1473 |
||
1474 |
void DHardwareMemoryManager::Unpin(DMemoryObject* aMemory, DMemoryMappingBase* aMapping, TPinArgs& aPinArgs) |
|
1475 |
{ |
|
1476 |
((DUnpagedMemoryManager*)this)->DUnpagedMemoryManager::Unpin(aMemory,aMapping,aPinArgs); |
|
1477 |
} |
|
1478 |
||
1479 |
||
1480 |
||
1481 |
// |
|
1482 |
// DPagedMemoryManager |
|
1483 |
// |
|
1484 |
||
1485 |
TInt DPagedMemoryManager::New(DMemoryObject*& aMemory, TUint aSizeInPages, TMemoryAttributes aAttributes, TMemoryCreateFlags aCreateFlags) |
|
1486 |
{ |
|
1487 |
return DMemoryManager::New(aMemory, aSizeInPages, aAttributes, (TMemoryCreateFlags)(aCreateFlags | EMemoryCreateDemandPaged)); |
|
1488 |
} |
|
1489 |
||
1490 |
||
1491 |
void DPagedMemoryManager::Destruct(DMemoryObject* aMemory) |
|
1492 |
{ |
|
1493 |
((DUnpagedMemoryManager*)this)->DUnpagedMemoryManager::Destruct(aMemory); |
|
1494 |
} |
|
1495 |
||
1496 |
||
1497 |
TInt DPagedMemoryManager::StealPage(DMemoryObject* aMemory, SPageInfo* aPageInfo) |
|
1498 |
{ |
|
1499 |
return ((DDiscardableMemoryManager*)this)->DDiscardableMemoryManager::StealPage(aMemory,aPageInfo); |
|
1500 |
} |
|
1501 |
||
1502 |
||
1503 |
TInt DPagedMemoryManager::MovePage( DMemoryObject* aMemory, SPageInfo* aOldPageInfo, |
|
1504 |
TPhysAddr& aNewPage, TUint aBlockZoneId, TBool aBlockRest) |
|
1505 |
{ |
|
1506 |
return TheMovableMemoryManager->MovePage(aMemory, aOldPageInfo, aNewPage, aBlockZoneId, aBlockRest); |
|
1507 |
} |
|
1508 |
||
1509 |
||
1510 |
TInt DPagedMemoryManager::RestrictPage(DMemoryObject* aMemory, SPageInfo* aPageInfo, TRestrictPagesType aRestriction) |
|
1511 |
{ |
|
1512 |
TRACE2(("DPagedMemoryManager::RestrictPage(0x%08x,0x%08x,%d)",aMemory,aPageInfo,aRestriction)); |
|
1513 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
1514 |
||
1515 |
TUint index = aPageInfo->Index(); |
|
1516 |
TInt r = KErrNotFound; |
|
1517 |
||
1518 |
TPhysAddr page; |
|
1519 |
TPhysAddr originalPage; |
|
1520 |
RPageArray::TIter pageList; |
|
1521 |
TPhysAddr* p = aMemory->iPages.RestrictPageNAStart(index,pageList); |
|
1522 |
if(!p) |
|
1523 |
goto fail; |
|
1524 |
originalPage = *p; |
|
1525 |
__NK_ASSERT_DEBUG((originalPage&~KPageMask)==aPageInfo->PhysAddr()); |
|
1526 |
||
1527 |
aPageInfo->SetModifier(&pageList); |
|
1528 |
||
1529 |
MmuLock::Unlock(); |
|
1530 |
||
1531 |
// restrict page... |
|
1532 |
aMemory->RestrictPages(pageList,aRestriction); |
|
1533 |
||
1534 |
MmuLock::Lock(); |
|
1535 |
||
1536 |
page = *p; |
|
1537 |
if(aPageInfo->CheckModified(&pageList) || page!=originalPage/*page state changed*/) |
|
1538 |
{ |
|
1539 |
// page state was changed by someone else... |
|
1540 |
r = KErrInUse; |
|
1541 |
} |
|
1542 |
else |
|
1543 |
{ |
|
1544 |
// nobody else has modified page state, so restrictions successfully applied... |
|
1545 |
*p = (page&~RPageArray::EStateMask)|RPageArray::ECommitted; // restore state |
|
1546 |
aPageInfo->SetReadOnly(); |
|
1547 |
r = KErrNone; |
|
1548 |
} |
|
1549 |
||
1550 |
aMemory->iPages.RestrictPageNAEnd(index); |
|
1551 |
||
1552 |
#ifdef _DEBUG |
|
1553 |
if(r!=KErrNone) |
|
1554 |
TRACE2(("DPagedMemoryManager::RestrictPage fail because preempted or vetoed")); |
|
1555 |
#endif |
|
1556 |
||
1557 |
fail: |
|
1558 |
TRACE2(("DPagedMemoryManager::RestrictPage returns %d",r)); |
|
1559 |
return r; |
|
1560 |
} |
|
1561 |
||
1562 |
||
1563 |
TInt DPagedMemoryManager::HandleFault( DMemoryObject* aMemory, TUint aIndex, DMemoryMapping* aMapping, |
|
1564 |
TUint aMapInstanceCount, TUint aAccessPermissions) |
|
1565 |
{ |
|
1566 |
TPinArgs pinArgs; |
|
1567 |
pinArgs.iReadOnly = !(aAccessPermissions&EReadWrite); |
|
1568 |
||
1569 |
TUint usedNew = 0; |
|
1570 |
||
1571 |
RPageArray::TIter pageList; |
|
1572 |
TPhysAddr* p = aMemory->iPages.AddPageStart(aIndex,pageList); |
|
1573 |
__NK_ASSERT_ALWAYS(p); // we should never run out of memory handling a paging fault |
|
1574 |
||
1575 |
TInt r = 1; // positive value to indicate nothing done |
|
1576 |
||
1577 |
// if memory object already has page, then we can use it... |
|
1578 |
MmuLock::Lock(); |
|
1579 |
if(RPageArray::IsPresent(*p)) |
|
1580 |
{ |
|
1581 |
r = PageInDone(aMemory,aIndex,0,p); |
|
1582 |
__NK_ASSERT_DEBUG(r<=0); // can't return >0 as we didn't supply a new page |
|
1583 |
} |
|
1584 |
MmuLock::Unlock(); |
|
1585 |
||
1586 |
if(r>0) |
|
1587 |
{ |
|
1588 |
// need to read page from backing store... |
|
1589 |
||
1590 |
// get paging request object... |
|
1591 |
DPageReadRequest* req; |
|
1592 |
do |
|
1593 |
{ |
|
1594 |
r = AcquirePageReadRequest(req,aMemory,aIndex,1); |
|
1595 |
__NK_ASSERT_DEBUG(r!=KErrNoMemory); // not allowed to allocated memory, therefore can't fail with KErrNoMemory |
|
1596 |
if(r==KErrNone) |
|
1597 |
{ |
|
1598 |
// if someone else has since read our page, then we can use it... |
|
1599 |
MmuLock::Lock(); |
|
1600 |
r = 1; |
|
1601 |
if(RPageArray::IsPresent(*p)) |
|
1602 |
{ |
|
1603 |
r = PageInDone(aMemory,aIndex,0,p); |
|
1604 |
__NK_ASSERT_DEBUG(r<=0); // can't return >0 as we didn't supply a new page |
|
1605 |
} |
|
1606 |
MmuLock::Unlock(); |
|
1607 |
} |
|
1608 |
} |
|
1609 |
while(r>0 && !req); // while not paged in && don't have a request object |
|
1610 |
||
1611 |
if(r>0) |
|
1612 |
{ |
|
1613 |
// still need to read page from backing store... |
|
1614 |
||
1615 |
// get RAM page... |
|
1616 |
TPhysAddr pagePhys; |
|
1617 |
r = ThePager.PageInAllocPages(&pagePhys,1,aMemory->RamAllocFlags()); |
|
1618 |
__NK_ASSERT_DEBUG(r!=KErrNoMemory); |
|
1619 |
if(r==KErrNone) |
|
1620 |
{ |
|
1621 |
// read data for page... |
|
1622 |
r = ReadPages(aMemory,aIndex,1,&pagePhys,req); |
|
1623 |
__NK_ASSERT_DEBUG(r!=KErrNoMemory); // not allowed to allocated memory, therefore can't fail with KErrNoMemory |
|
1624 |
if(r!=KErrNone) |
|
1625 |
{ |
|
1626 |
// error, so free unused pages... |
|
1627 |
ThePager.PageInFreePages(&pagePhys,1); |
|
1628 |
} |
|
1629 |
else |
|
1630 |
{ |
|
1631 |
// use new page... |
|
1632 |
MmuLock::Lock(); |
|
1633 |
r = PageInDone(aMemory,aIndex,SPageInfo::FromPhysAddr(pagePhys),p); |
|
1634 |
MmuLock::Unlock(); |
|
1635 |
if(r>0) |
|
1636 |
{ |
|
1637 |
// new page actually used... |
|
1638 |
r = KErrNone; |
|
1639 |
usedNew = 1; |
|
1640 |
} |
|
1641 |
} |
|
1642 |
} |
|
1643 |
} |
|
1644 |
||
1645 |
// done with paging request object... |
|
1646 |
if(req) |
|
1647 |
req->Release(); |
|
1648 |
} |
|
1649 |
||
1650 |
// map page... |
|
1651 |
if(r==KErrNone && aMapping) |
|
1652 |
{ |
|
1653 |
r = aMapping->PageIn(pageList, pinArgs, aMapInstanceCount); |
|
1654 |
__NK_ASSERT_ALWAYS(r!=KErrNoMemory); // we should never run out of memory handling a paging fault |
|
1655 |
#ifdef COARSE_GRAINED_TLB_MAINTENANCE |
|
1656 |
InvalidateTLB(); |
|
1657 |
#endif |
|
1658 |
} |
|
1659 |
||
1660 |
// finished with this page... |
|
1661 |
aMemory->iPages.AddPageEnd(aIndex,usedNew); |
|
1662 |
||
1663 |
__NK_ASSERT_ALWAYS(r!=KErrNoMemory); // we should never run out of memory handling a paging fault |
|
1664 |
return r; |
|
1665 |
} |
|
1666 |
||
1667 |
||
1668 |
TInt DPagedMemoryManager::Pin(DMemoryObject* aMemory, DMemoryMappingBase* aMapping, TPinArgs& aPinArgs) |
|
1669 |
{ |
|
1670 |
__ASSERT_CRITICAL; |
|
1671 |
return DoPin(aMemory,aMapping->iStartIndex,aMapping->iSizeInPages,aMapping,aPinArgs); |
|
1672 |
} |
|
1673 |
||
1674 |
||
1675 |
TInt DPagedMemoryManager::DoPin(DMemoryObject* aMemory, TUint aIndex, TUint aCount, DMemoryMappingBase* aMapping, TPinArgs& aPinArgs) |
|
1676 |
{ |
|
1677 |
TRACE(("DPagedMemoryManager::DoPin(0x%08x,0x%08x,0x%08x,0x%08x)",aMemory, aIndex, aCount, aMapping)); |
|
1678 |
__ASSERT_CRITICAL; |
|
1679 |
__NK_ASSERT_DEBUG(aPinArgs.HaveSufficientPages(aCount)); |
|
1680 |
||
1681 |
// check and allocate page array entries... |
|
1682 |
RPageArray::TIter pageList; |
|
1683 |
TInt r = aMemory->iPages.AddStart(aIndex,aCount,pageList,true); |
|
1684 |
if(r!=KErrNone) |
|
1685 |
return r; |
|
1686 |
||
1687 |
RPageArray::TIter pageIter = pageList; |
|
1688 |
TUint n; |
|
1689 |
TPhysAddr* pages; |
|
1690 |
while((n=pageIter.Pages(pages,DPageReadRequest::EMaxPages))!=0) |
|
1691 |
{ |
|
1692 |
MmuLock::Lock(); |
|
1693 |
||
1694 |
if(RPageArray::IsPresent(*pages)) |
|
1695 |
{ |
|
1696 |
// pin page which is already committed to memory object... |
|
1697 |
r = PageInPinnedDone(aMemory,pageIter.Index(),0,pages,aPinArgs); |
|
1698 |
__NK_ASSERT_DEBUG(r<=0); // can't return >0 as we didn't supply a new page |
|
1699 |
} |
|
1700 |
else |
|
1701 |
{ |
|
1702 |
// count consecutive pages which need to be read... |
|
1703 |
TUint i; |
|
1704 |
for(i=1; i<n; ++i) |
|
1705 |
if(RPageArray::IsPresent(pages[i])) |
|
1706 |
break; |
|
1707 |
n = i; |
|
1708 |
r = 1; // positive value to indicate nothing done |
|
1709 |
} |
|
1710 |
||
1711 |
MmuLock::Unlock(); |
|
1712 |
||
1713 |
if(r==KErrNone) |
|
1714 |
{ |
|
1715 |
// successfully pinned one page, so move on to next one... |
|
1716 |
pageIter.Skip(1); |
|
1717 |
continue; |
|
1718 |
} |
|
1719 |
else if(r<0) |
|
1720 |
{ |
|
1721 |
// error, so end... |
|
1722 |
break; |
|
1723 |
} |
|
1724 |
||
1725 |
// need to read pages from backing store... |
|
1726 |
||
1727 |
// get paging request object... |
|
1728 |
DPageReadRequest* req; |
|
1729 |
TUint i; |
|
1730 |
do |
|
1731 |
{ |
|
1732 |
i = 0; |
|
1733 |
r = AcquirePageReadRequest(req,aMemory,pageIter.Index(),n); |
|
1734 |
if(r==KErrNone) |
|
1735 |
{ |
|
1736 |
// see if someone else has since read any of our pages... |
|
1737 |
MmuLock::Lock(); |
|
1738 |
for(; i<n; ++i) |
|
1739 |
if(RPageArray::IsPresent(pages[i])) |
|
1740 |
break; |
|
1741 |
MmuLock::Unlock(); |
|
1742 |
} |
|
1743 |
} |
|
1744 |
while(i==n && !req); // while still need all pages && don't have a request object |
|
1745 |
||
1746 |
// if don't need all pages any more... |
|
1747 |
if(i!=n) |
|
1748 |
{ |
|
1749 |
// retry loop... |
|
1750 |
if(req) |
|
1751 |
req->Release(); |
|
1752 |
continue; |
|
1753 |
} |
|
1754 |
||
1755 |
// keep count of number of pages actually added to memory object... |
|
1756 |
TUint usedNew = 0; |
|
1757 |
||
1758 |
// get RAM pages... |
|
1759 |
TPhysAddr newPages[DPageReadRequest::EMaxPages]; |
|
1760 |
__NK_ASSERT_DEBUG(n<=DPageReadRequest::EMaxPages); |
|
1761 |
r = ThePager.PageInAllocPages(newPages,n,aMemory->RamAllocFlags()); |
|
1762 |
if(r==KErrNone) |
|
1763 |
{ |
|
1764 |
// read data for pages... |
|
1765 |
r = ReadPages(aMemory,pageIter.Index(),n,newPages,req); |
|
1766 |
if(r!=KErrNone) |
|
1767 |
{ |
|
1768 |
// error, so free unused pages... |
|
1769 |
ThePager.PageInFreePages(newPages,n); |
|
1770 |
} |
|
1771 |
else |
|
1772 |
{ |
|
1773 |
// use new pages... |
|
1774 |
for(i=0; i<n; ++i) |
|
1775 |
{ |
|
1776 |
MmuLock::Lock(); |
|
1777 |
r = PageInPinnedDone(aMemory, |
|
1778 |
pageIter.Index()+i, |
|
1779 |
SPageInfo::FromPhysAddr(newPages[i]), |
|
1780 |
pages+i, |
|
1781 |
aPinArgs |
|
1782 |
); |
|
1783 |
MmuLock::Unlock(); |
|
1784 |
if(r>0) |
|
1785 |
{ |
|
1786 |
// new page actually used... |
|
1787 |
r = KErrNone; |
|
1788 |
++usedNew; |
|
1789 |
} |
|
1790 |
if(r!=KErrNone) |
|
1791 |
{ |
|
1792 |
// error, so free remaining unused pages... |
|
1793 |
ThePager.PageInFreePages(newPages+(i+1),n-(i+1)); |
|
1794 |
// and update array for any pages already added... |
|
1795 |
if(i) |
|
1796 |
pageIter.Added(i,usedNew); |
|
1797 |
break; |
|
1798 |
} |
|
1799 |
} |
|
1800 |
} |
|
1801 |
} |
|
1802 |
||
1803 |
// done with paging request object... |
|
1804 |
if(req) |
|
1805 |
req->Release(); |
|
1806 |
||
1807 |
if(r!=KErrNone) |
|
1808 |
break; // error, so give up |
|
1809 |
||
1810 |
// move on to next set of pages... |
|
1811 |
pageIter.Added(n,usedNew); |
|
1812 |
} |
|
1813 |
||
1814 |
// map pages... |
|
1815 |
if(r==KErrNone) |
|
1816 |
{// Page in the page with the pinning mapping, OK to get the instance count here |
|
1817 |
// without any locking as the pinned mapping can't be reused for another purpose |
|
1818 |
// during this method. |
|
1819 |
r = aMapping->PageIn(pageList, aPinArgs, aMapping->MapInstanceCount()); |
|
1820 |
#ifdef COARSE_GRAINED_TLB_MAINTENANCE |
|
1821 |
InvalidateTLB(); |
|
1822 |
#endif |
|
1823 |
} |
|
1824 |
||
1825 |
// release page array entries... |
|
1826 |
aMemory->iPages.AddEnd(aIndex,aCount); |
|
1827 |
||
1828 |
if(r==KErrNone) |
|
1829 |
{ |
|
1830 |
// set EPagesPinned flag to indicate success... |
|
1831 |
__NK_ASSERT_DEBUG((aMapping->Flags()&DMemoryMapping::EPagesPinned)==0); |
|
1832 |
__e32_atomic_ior_ord8(&aMapping->Flags(), (TUint8)DMemoryMapping::EPagesPinned); |
|
1833 |
} |
|
1834 |
else |
|
1835 |
{ |
|
1836 |
// cleanup on error... |
|
1837 |
TUint pinnedCount = pageIter.Index()-aIndex; // number of pages actually pinned |
|
1838 |
DoUnpin(aMemory,aIndex,pinnedCount,aMapping,aPinArgs); |
|
1839 |
} |
|
1840 |
||
1841 |
return r; |
|
1842 |
} |
|
1843 |
||
1844 |
||
1845 |
void DPagedMemoryManager::Unpin(DMemoryObject* aMemory, DMemoryMappingBase* aMapping, TPinArgs& aPinArgs) |
|
1846 |
{ |
|
1847 |
__ASSERT_CRITICAL; |
|
1848 |
// if mapping successfully pinned... |
|
1849 |
if(aMapping->Flags()&DMemoryMapping::EPagesPinned) |
|
1850 |
{ |
|
1851 |
// then undo pinning... |
|
1852 |
DoUnpin(aMemory,aMapping->iStartIndex,aMapping->iSizeInPages,aMapping,aPinArgs); |
|
1853 |
} |
|
1854 |
} |
|
1855 |
||
1856 |
||
1857 |
void DPagedMemoryManager::DoUnpin(DMemoryObject* aMemory, TUint aIndex, TUint aCount, DMemoryMappingBase* aMapping, TPinArgs& aPinArgs) |
|
1858 |
{ |
|
1859 |
TRACE(("DPagedMemoryManager::DoUnpin(0x%08x,0x%08x,0x%08x,0x%08x,?)",aMemory, aIndex, aCount, aMapping)); |
|
1860 |
__ASSERT_CRITICAL; |
|
1861 |
||
1862 |
MmuLock::Lock(); |
|
1863 |
TUint endIndex = aIndex+aCount; |
|
1864 |
for(TUint i=aIndex; i<endIndex; ++i) |
|
1865 |
{ |
|
1866 |
TPhysAddr page = aMemory->iPages.Page(i); |
|
1867 |
__NK_ASSERT_DEBUG(RPageArray::IsPresent(page)); |
|
1868 |
__NK_ASSERT_DEBUG(SPageInfo::SafeFromPhysAddr(page&~KPageMask)); |
|
1869 |
ThePager.Unpin(SPageInfo::FromPhysAddr(page),aPinArgs); |
|
1870 |
MmuLock::Flash(); |
|
1871 |
} |
|
1872 |
MmuLock::Unlock(); |
|
1873 |
||
1874 |
// clear EPagesPinned flag... |
|
1875 |
__e32_atomic_and_ord8(&aMapping->Flags(), TUint8(~DMemoryMapping::EPagesPinned)); |
|
1876 |
} |
|
1877 |
||
1878 |
||
1879 |
void DPagedMemoryManager::DoCleanupDecommitted(DMemoryObject* aMemory) |
|
1880 |
{ |
|
1881 |
MemoryObjectLock::Lock(aMemory); |
|
1882 |
FreeDecommitted(aMemory,0,aMemory->iSizeInPages); |
|
1883 |
MemoryObjectLock::Unlock(aMemory); |
|
1884 |
} |
|
1885 |
||
1886 |
||
1887 |
TInt DPagedMemoryManager::PageInDone(DMemoryObject* aMemory, TUint aIndex, SPageInfo* aPageInfo, TPhysAddr* aPageArrayEntry) |
|
1888 |
{ |
|
1889 |
TInt r = DoPageInDone(aMemory,aIndex,aPageInfo,aPageArrayEntry,false); |
|
1890 |
||
1891 |
if(r>=0) |
|
1892 |
ThePager.PagedIn(aPageInfo); |
|
1893 |
||
1894 |
// check page assigned correctly... |
|
1895 |
#ifdef _DEBUG |
|
1896 |
if(RPageArray::IsPresent(*aPageArrayEntry)) |
|
1897 |
{ |
|
1898 |
SPageInfo* pi = SPageInfo::FromPhysAddr(*aPageArrayEntry); |
|
1899 |
__NK_ASSERT_DEBUG(pi->Owner()==aMemory); |
|
1900 |
__NK_ASSERT_DEBUG(pi->Index()==aIndex); |
|
1901 |
} |
|
1902 |
#endif |
|
1903 |
||
1904 |
return r; |
|
1905 |
} |
|
1906 |
||
1907 |
||
1908 |
TInt DPagedMemoryManager::PageInPinnedDone(DMemoryObject* aMemory, TUint aIndex, SPageInfo* aPageInfo, TPhysAddr* aPageArrayEntry, TPinArgs& aPinArgs) |
|
1909 |
{ |
|
1910 |
TInt r = DoPageInDone(aMemory,aIndex,aPageInfo,aPageArrayEntry,true); |
|
1911 |
||
1912 |
if(r>=0) |
|
1913 |
ThePager.PagedInPinned(aPageInfo,aPinArgs); |
|
1914 |
||
1915 |
// check page assigned correctly... |
|
1916 |
#ifdef _DEBUG |
|
1917 |
if(RPageArray::IsPresent(*aPageArrayEntry)) |
|
1918 |
{ |
|
1919 |
SPageInfo* pi = SPageInfo::FromPhysAddr(*aPageArrayEntry); |
|
1920 |
__NK_ASSERT_DEBUG(pi->Owner()==aMemory); |
|
1921 |
__NK_ASSERT_DEBUG(pi->Index()==aIndex); |
|
1922 |
if(r>=0) |
|
1923 |
__NK_ASSERT_DEBUG(pi->PagedState()==SPageInfo::EPagedPinned); |
|
1924 |
} |
|
1925 |
#endif |
|
1926 |
||
1927 |
return r; |
|
1928 |
} |
|
1929 |
||
1930 |
||
1931 |
TInt DPagedMemoryManager::DoPageInDone(DMemoryObject* aMemory, TUint aIndex, SPageInfo*& aPageInfo, TPhysAddr* aPageArrayEntry, TBool aPinning) |
|
1932 |
{ |
|
1933 |
TRACE(("DPagedMemoryManager::DoPageInDone(0x%08x,0x%08x,0x%08x,?,%d)",aMemory,aIndex,aPageInfo,aPinning)); |
|
1934 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
1935 |
||
1936 |
__UNLOCK_GUARD_START(MmuLock); |
|
1937 |
||
1938 |
SPageInfo* pi = aPageInfo; |
|
1939 |
||
1940 |
if(!IsAllocated(aMemory,aIndex,1)) |
|
1941 |
{ |
|
1942 |
// memory has been decommitted from memory object... |
|
1943 |
if(pi) |
|
1944 |
ThePager.PagedInUnneeded(pi); |
|
1945 |
__UNLOCK_GUARD_END(MmuLock); |
|
1946 |
aPageInfo = 0; |
|
1947 |
return KErrNotFound; |
|
1948 |
} |
|
1949 |
||
1950 |
TPhysAddr oldPage = *aPageArrayEntry; |
|
1951 |
TBool useNew = (bool)!RPageArray::IsPresent(oldPage); |
|
1952 |
if(useNew) |
|
1953 |
{ |
|
1954 |
if(!pi) |
|
1955 |
{ |
|
1956 |
__UNLOCK_GUARD_END(MmuLock); |
|
1957 |
// aPageInfo = 0; // this is already set to zero |
|
1958 |
return KErrNotFound; // no new page to use |
|
1959 |
} |
|
1960 |
||
1961 |
// assign page to memory object... |
|
1962 |
pi->SetManaged(aMemory,aIndex,aMemory->PageInfoFlags()); |
|
1963 |
||
1964 |
ThePager.Event(DPager::EEventPageInNew,pi); |
|
1965 |
||
1966 |
// save any paging manager data stored in page array before we overwrite it... |
|
1967 |
pi->SetPagingManagerData(*aPageArrayEntry); |
|
1968 |
} |
|
1969 |
else |
|
1970 |
{ |
|
1971 |
__NK_ASSERT_DEBUG(!pi); // should only have read new page if none present |
|
1972 |
||
1973 |
// discard new page... |
|
1974 |
if(pi) |
|
1975 |
ThePager.PagedInUnneeded(pi); |
|
1976 |
||
1977 |
// check existing page can be committed... |
|
1978 |
if(RPageArray::State(oldPage)<=RPageArray::EDecommitting) |
|
1979 |
{ |
|
1980 |
__UNLOCK_GUARD_END(MmuLock); |
|
1981 |
aPageInfo = 0; |
|
1982 |
return KErrNotFound; |
|
1983 |
} |
|
1984 |
||
1985 |
// and use one we already have... |
|
1986 |
SPageInfo* newPage = SPageInfo::FromPhysAddr(oldPage); |
|
1987 |
||
1988 |
if(!pi && !aPinning) |
|
1989 |
ThePager.Event(DPager::EEventPageInAgain,newPage); |
|
1990 |
||
1991 |
pi = newPage; |
|
1992 |
pi->SetModifier(0); // so observers see page state has changed |
|
1993 |
} |
|
1994 |
||
1995 |
// set page array entry... |
|
1996 |
TPhysAddr pagePhys = pi->PhysAddr(); |
|
1997 |
*aPageArrayEntry = pagePhys|RPageArray::ECommitted; |
|
1998 |
||
1999 |
// return the page we actually used... |
|
2000 |
aPageInfo = pi; |
|
2001 |
||
2002 |
__UNLOCK_GUARD_END(MmuLock); |
|
2003 |
return useNew; |
|
2004 |
} |
|
2005 |
||
2006 |
||
2007 |
TInt DPagedMemoryManager::Decompress(TUint32 aCompressionType, TLinAddr aDst, TUint aDstBytes, TLinAddr aSrc, TUint aSrcBytes) |
|
2008 |
{ |
|
2009 |
#ifdef BTRACE_PAGING_VERBOSE |
|
2010 |
BTraceContext4(BTrace::EPaging, BTrace::EPagingDecompressStart, aCompressionType); |
|
2011 |
#endif |
|
2012 |
TInt r; |
|
2013 |
switch(aCompressionType) |
|
2014 |
{ |
|
2015 |
case 0: |
|
2016 |
__NK_ASSERT_DEBUG(aSrcBytes == aDstBytes); |
|
2017 |
memcpy((void*)aDst, (void*)aSrc, aSrcBytes); |
|
2018 |
r = aSrcBytes; |
|
2019 |
break; |
|
2020 |
||
2021 |
case SRomPageInfo::EBytePair: |
|
2022 |
case KUidCompressionBytePair: |
|
2023 |
{ |
|
2024 |
TUint8* srcNext = 0; |
|
2025 |
START_PAGING_BENCHMARK; |
|
2026 |
r = BytePairDecompress((TUint8*)aDst, aDstBytes, (TUint8*)aSrc, aSrcBytes, srcNext); |
|
2027 |
END_PAGING_BENCHMARK(EPagingBmDecompress); |
|
2028 |
if (r > 0) |
|
2029 |
{ |
|
2030 |
// decompression successful so check srcNext points to the end of the compressed data... |
|
2031 |
__NK_ASSERT_ALWAYS((TLinAddr)srcNext == aSrc + aSrcBytes); |
|
2032 |
} |
|
2033 |
} |
|
2034 |
break; |
|
2035 |
||
2036 |
default: |
|
2037 |
r = KErrNotSupported; |
|
2038 |
break; |
|
2039 |
} |
|
2040 |
#ifdef BTRACE_PAGING_VERBOSE |
|
2041 |
BTraceContext0(BTrace::EPaging, BTrace::EPagingDecompressEnd); |
|
2042 |
#endif |
|
2043 |
return r; |
|
2044 |
} |
|
2045 |
||
2046 |
||
2047 |
TInt DPagedMemoryManager::AcquirePageWriteRequest(DPageWriteRequest*& aRequest, DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
2048 |
{ |
|
2049 |
__NK_ASSERT_ALWAYS(0); |
|
2050 |
return KErrNotSupported; |
|
2051 |
} |
|
2052 |
TZonePageType DPagedMemoryManager::PageType() |
|
2053 |
{// Paged manager's pages should be discardable and will actaully be freed by |
|
2054 |
// the pager so this value won't be used. |
|
2055 |
return EPageDiscard; |
|
2056 |
} |
|
2057 |