author | hgs |
Mon, 10 May 2010 11:40:53 +0100 | |
changeset 132 | e4a7b1cbe40c |
parent 33 | 0173bcd7697c |
child 176 | af6ec97d9189 |
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 "mpager.h" |
|
20 |
||
21 |
#include "mmanager.h" |
|
22 |
#include "mmapping.h" |
|
23 |
#include "mobject.h" |
|
24 |
||
25 |
#include "mptalloc.h" |
|
26 |
#include "cache_maintenance.inl" |
|
27 |
||
28 |
/** |
|
29 |
@class PageTableAllocator |
|
30 |
@details |
|
31 |
||
32 |
NOTES |
|
33 |
||
34 |
Page tables are mapped into a sparse array in the virtual address range |
|
35 |
#KPageTableBase..#KPageTableEnd. For each present page table there is a |
|
36 |
corresponding #SPageTableInfo object mapped from #KPageTableInfoBase upwards. |
|
37 |
||
38 |
Page tables for demand paged content are kept separate from other page tables, |
|
39 |
this enables the memory for these to be freed when the page tables no longer map |
|
40 |
any memory i.e. when it has all been paged-out. Pages with these 'paged' page |
|
41 |
tables are stored in the demand paging live list, so it participates in the page |
|
42 |
aging process. |
|
43 |
||
44 |
The 'unpaged' page tables are allocated from the bottom of the array upwards, |
|
45 |
via TPtPageAllocator::iLowerAllocator; the 'paged' page tables are allocated |
|
46 |
from the top of the array downwards, via TPtPageAllocator::iUpperAllocator. |
|
47 |
These two regions are prevented from overlapping, or from coming close enough |
|
48 |
together so that the #SPageTableInfo struct for paged and unpaged page tables |
|
49 |
lie in the same page. This means that the SPageTableInfo memory for paged page |
|
50 |
tables can be discarded when it's page tables are discarded. |
|
51 |
||
52 |
Memory for page tables and page table info objects is managed by |
|
53 |
#ThePageTableMemoryManager. When allocating memory for demand paged use, this |
|
54 |
uses memory from #ThePager which will reclaim paged memory if necessary. |
|
55 |
Providing the live list always has #DPager::iMinYoungPages, this guarantees that |
|
56 |
handling page faults can never fail by running out of memory. |
|
57 |
*/ |
|
58 |
||
59 |
||
60 |
PageTableAllocator PageTables; |
|
61 |
||
62 |
||
63 |
||
64 |
TBool PageTablesLockIsHeld() |
|
65 |
{ |
|
66 |
return ::PageTables.LockIsHeld(); |
|
67 |
} |
|
68 |
||
69 |
||
70 |
/** |
|
71 |
Minimum number of page tables to keep in reserve. |
|
72 |
*/ |
|
73 |
const TUint KNumReservedPageTables = 0; // none needed - page tables for mapping page tables and infos are permanently allocated |
|
74 |
||
75 |
||
76 |
/** |
|
77 |
Manager for the memory object used to store all the MMU page tables. |
|
78 |
*/ |
|
79 |
class DPageTableMemoryManager : public DMemoryManager |
|
80 |
{ |
|
81 |
public: |
|
82 |
/** |
|
83 |
Not implemented - page table memory is never destroyed. |
|
84 |
*/ |
|
85 |
virtual void Destruct(DMemoryObject* aMemory) |
|
86 |
{} |
|
87 |
||
88 |
virtual TInt StealPage(DMemoryObject* aMemory, SPageInfo* aPageInfo) |
|
89 |
{ return PageTables.StealPage(aPageInfo); } |
|
90 |
||
91 |
/** |
|
92 |
Does nothing, returns KErrNone. |
|
93 |
The RAM containing page tables does not need access restrictions applied for demand paging |
|
94 |
purposes. Page table life-time is implicitly managed through the pages it maps. |
|
95 |
*/ |
|
96 |
virtual TInt RestrictPage(DMemoryObject* aMemory, SPageInfo* aPageInfo, TRestrictPagesType aRestriction) |
|
97 |
{ return KErrNone; } |
|
98 |
||
99 |
/** |
|
100 |
Does nothing, returns KErrNone. |
|
101 |
The contents of page tables never need saving as their contents are dynamically generated. |
|
102 |
*/ |
|
103 |
virtual TInt CleanPage(DMemoryObject* aMemory, SPageInfo* aPageInfo, TPhysAddr*& aPageArrayEntry) |
|
104 |
{ return KErrNone; } |
|
105 |
||
106 |
/** |
|
107 |
Not implemented, returns KErrNotSupported. |
|
108 |
*/ |
|
109 |
virtual TInt Pin(DMemoryObject* aMemory, DMemoryMappingBase* aMapping, TPinArgs& aPinArgs) |
|
110 |
{ return KErrNotSupported; } |
|
111 |
||
112 |
/** |
|
113 |
Not implemented. |
|
114 |
*/ |
|
115 |
virtual void Unpin(DMemoryObject* aMemory, DMemoryMappingBase* aMapping, TPinArgs& aPinArgs) |
|
116 |
{ } |
|
117 |
||
118 |
||
119 |
virtual TInt MovePage( DMemoryObject* aMemory, SPageInfo* aOldPageInfo, |
|
120 |
TPhysAddr& aNewPage, TUint aBlockZoneId, TBool aBlockRest); |
|
121 |
public: |
|
122 |
/** |
|
123 |
Allocate a page of RAM for storing page tables in. |
|
124 |
||
125 |
@param aMemory A memory object associated with this manager. |
|
126 |
@param aIndex Page index, within the memory, to allocate the page at. |
|
127 |
@param aDemandPaged True if the memory is to be used for page tables mapping |
|
128 |
demand paged content. |
|
129 |
||
130 |
@return KErrNone if successful, otherwise one of the system wide error codes. |
|
131 |
*/ |
|
132 |
TInt Alloc(DMemoryObject* aMemory, TUint aIndex, TBool aDemandPaged); |
|
133 |
||
134 |
/** |
|
135 |
Allocate a page of RAM being used for storing page tables in. |
|
136 |
||
137 |
@param aMemory A memory object associated with this manager. |
|
138 |
@param aIndex Page index, within the memory, to free the page from. |
|
139 |
@param aDemandPaged True if the memory is being used for page tables mapping |
|
140 |
demand paged content. |
|
141 |
||
142 |
@return KErrNone if successful, otherwise one of the system wide error codes. |
|
143 |
*/ |
|
144 |
TInt Free(DMemoryObject* aMemory, TUint aIndex, TBool aDemandPaged); |
|
145 |
}; |
|
146 |
||
147 |
/** |
|
148 |
The single instance of the #DPageTableMemoryManager class. |
|
149 |
*/ |
|
150 |
DPageTableMemoryManager ThePageTableMemoryManager; |
|
151 |
||
152 |
||
153 |
TInt DPageTableMemoryManager::Alloc(DMemoryObject* aMemory, TUint aIndex, TBool aDemandPaged) |
|
154 |
{ |
|
155 |
TRACE2(("DPageTableMemoryManager::Alloc(0x%08x,0x%x,%d)",aMemory, aIndex, aDemandPaged)); |
|
156 |
__NK_ASSERT_DEBUG(MemoryObjectLock::IsHeld(aMemory)); |
|
157 |
||
158 |
// allocate page array entry... |
|
159 |
RPageArray::TIter pageList; |
|
160 |
TPhysAddr* p = aMemory->iPages.AddPageStart(aIndex,pageList); |
|
161 |
if(!p) |
|
162 |
return KErrNoMemory; |
|
163 |
||
164 |
// allocate RAM... |
|
165 |
RamAllocLock::Lock(); |
|
166 |
TPhysAddr pagePhys; |
|
167 |
TInt r; |
|
168 |
if(aDemandPaged) |
|
169 |
{ |
|
170 |
r = ThePager.PageInAllocPages(&pagePhys,1,aMemory->RamAllocFlags()); |
|
171 |
__NK_ASSERT_DEBUG(r!=KErrNoMemory); |
|
172 |
} |
|
173 |
else |
|
174 |
{// Allocate fixed paged as page tables aren't movable. |
|
175 |
r = TheMmu.AllocRam(&pagePhys, 1, aMemory->RamAllocFlags(), EPageFixed); |
|
132 | 176 |
|
177 |
#ifdef BTRACE_KERNEL_MEMORY |
|
178 |
if (r == KErrNone) |
|
179 |
{ |
|
180 |
BTrace4(BTrace::EKernelMemory, BTrace::EKernelMemoryMiscAlloc, KPageSize); |
|
181 |
++Epoc::KernelMiscPages; |
|
182 |
} |
|
183 |
#endif |
|
0 | 184 |
} |
185 |
RamAllocLock::Unlock(); |
|
186 |
||
187 |
TUint usedNew = 0; |
|
188 |
if(r==KErrNone) |
|
189 |
{ |
|
190 |
// add RAM to page array... |
|
191 |
MmuLock::Lock(); |
|
192 |
if(aDemandPaged) |
|
193 |
ThePager.Event(DPager::EEventPagePageTableAlloc,SPageInfo::FromPhysAddr(pagePhys)); |
|
194 |
SPageInfo* pi = SPageInfo::FromPhysAddr(pagePhys); |
|
195 |
pi->SetManaged(aMemory,aIndex,aMemory->PageInfoFlags()); |
|
196 |
RPageArray::AddPage(p,pagePhys); |
|
197 |
MmuLock::Unlock(); |
|
198 |
usedNew = 1; |
|
199 |
||
200 |
// map page... |
|
201 |
r = aMemory->MapPages(pageList); |
|
202 |
} |
|
203 |
||
204 |
// release page array entry... |
|
205 |
aMemory->iPages.AddPageEnd(aIndex,usedNew); |
|
206 |
||
207 |
// revert if error... |
|
208 |
if(r!=KErrNone) |
|
209 |
Free(aMemory,aIndex,aDemandPaged); |
|
210 |
||
211 |
return r; |
|
212 |
} |
|
213 |
||
214 |
||
215 |
TInt DPageTableMemoryManager::Free(DMemoryObject* aMemory, TUint aIndex, TBool aDemandPaged) |
|
216 |
{ |
|
217 |
TRACE2(("DPageTableMemoryManager::Free(0x%08x,0x%x,%d)",aMemory, aIndex, aDemandPaged)); |
|
218 |
__NK_ASSERT_DEBUG(MemoryObjectLock::IsHeld(aMemory)); |
|
219 |
||
220 |
// find page array entry... |
|
221 |
RPageArray::TIter pageList; |
|
222 |
TPhysAddr* p = aMemory->iPages.RemovePageStart(aIndex,pageList); |
|
223 |
if(!p) |
|
224 |
return KErrNoMemory; |
|
225 |
||
226 |
// unmap page... |
|
227 |
aMemory->UnmapPages(pageList,true); |
|
228 |
||
229 |
RamAllocLock::Lock(); |
|
230 |
||
231 |
// remove page... |
|
232 |
MmuLock::Lock(); |
|
233 |
TPhysAddr pagePhys = RPageArray::RemovePage(p); |
|
234 |
MmuLock::Unlock(); |
|
235 |
||
236 |
TInt r; |
|
237 |
if(pagePhys==KPhysAddrInvalid) |
|
238 |
{ |
|
239 |
// no page removed... |
|
240 |
r = 0; |
|
241 |
} |
|
242 |
else |
|
243 |
{ |
|
244 |
// free the removed page... |
|
245 |
if(aDemandPaged) |
|
246 |
ThePager.PageInFreePages(&pagePhys,1); |
|
247 |
else |
|
132 | 248 |
{ |
0 | 249 |
TheMmu.FreeRam(&pagePhys, 1, EPageFixed); |
132 | 250 |
|
251 |
#ifdef BTRACE_KERNEL_MEMORY |
|
252 |
BTrace4(BTrace::EKernelMemory, BTrace::EKernelMemoryMiscFree, KPageSize); |
|
253 |
--Epoc::KernelMiscPages; |
|
254 |
#endif |
|
255 |
} |
|
0 | 256 |
r = 1; |
257 |
} |
|
258 |
||
259 |
RamAllocLock::Unlock(); |
|
260 |
||
261 |
// cleanup... |
|
262 |
aMemory->iPages.RemovePageEnd(aIndex,r); |
|
263 |
return r; |
|
264 |
} |
|
265 |
||
266 |
TInt DPageTableMemoryManager::MovePage( DMemoryObject* aMemory, SPageInfo* aOldPageInfo, |
|
267 |
TPhysAddr& aNewPage, TUint aBlockZoneId, TBool aBlockRest) |
|
268 |
{ |
|
269 |
// This could be a demand paged page table info which can be discarded |
|
270 |
// but let the PageTableAllocator handle that. |
|
271 |
return ::PageTables.MovePage(aMemory, aOldPageInfo, aBlockZoneId, aBlockRest); |
|
272 |
} |
|
273 |
||
274 |
||
275 |
// |
|
276 |
// PageTableAllocator |
|
277 |
// |
|
278 |
||
279 |
void PageTableAllocator::Init2(DMutex* aLock) |
|
280 |
{ |
|
281 |
TRACEB(("PageTableAllocator::Init2(0x%x)",aLock)); |
|
282 |
iLock = aLock; |
|
283 |
||
284 |
__NK_ASSERT_DEBUG(iUnpagedAllocator.CheckFreeList()); |
|
285 |
||
286 |
// scan for already allocated page tables |
|
287 |
// (assumes the first page table is used to map page tables)... |
|
288 |
SPageTableInfo* pti = (SPageTableInfo*)KPageTableInfoBase; |
|
289 |
TUint pages = 0; |
|
290 |
for(;;) |
|
291 |
{ |
|
292 |
TPte pte = ((TPte*)KPageTableBase)[pages]; |
|
293 |
if(pte==KPteUnallocatedEntry) |
|
294 |
break; // end (assumes no gaps in page table allocation) |
|
295 |
||
296 |
// process free page tables in this page... |
|
297 |
TUint freeCount = 0; |
|
298 |
do |
|
299 |
{ |
|
300 |
if(pti->IsUnused()) |
|
301 |
{ |
|
302 |
pti->PtClusterAlloc(); |
|
303 |
iUnpagedAllocator.iFreeList.Add(&pti->FreeLink()); |
|
304 |
++freeCount; |
|
305 |
} |
|
306 |
#ifdef _DEBUG |
|
307 |
else |
|
308 |
__NK_ASSERT_DEBUG(pti->IsPtClusterAllocated()); |
|
309 |
#endif |
|
310 |
} |
|
311 |
while(!(++pti)->IsFirstInPage()); |
|
312 |
iUnpagedAllocator.iFreeCount += freeCount; |
|
313 |
__NK_ASSERT_DEBUG(iUnpagedAllocator.CheckFreeList()); |
|
314 |
TRACE2(("PT page 0x%08x has %d free tables",pti[-KPtClusterSize].PageTable(),freeCount)); |
|
315 |
||
316 |
// count page, and move on to next one... |
|
317 |
++pages; |
|
318 |
__NK_ASSERT_DEBUG(pages<KChunkSize/KPageSize); // we've assumed less than one page table of page tables |
|
319 |
} |
|
320 |
||
321 |
// construct allocator for page table pages... |
|
322 |
iPtPageAllocator.Init2(pages); |
|
323 |
||
324 |
// initialise allocator page table infos... |
|
325 |
iPageTableGroupCounts[0] = pages; |
|
326 |
__NK_ASSERT_DEBUG(pages/KPageTableGroupSize==0); // we've assumed less than 1 page of page table infos |
|
327 |
||
328 |
// FOLLOWING CODE WILL USE THIS OBJECT TO ALLOCATE SOME PAGE TABLES, |
|
329 |
// SO ALLOCATOR MUST BE INITIALISED TO A FIT STATE BEFORE THIS POINT! |
|
330 |
||
331 |
// construct memory object for page tables... |
|
332 |
TMappingCreateFlags mapFlags = (TMappingCreateFlags)(EMappingCreateFixedVirtual|EMappingCreateReserveAllResources); |
|
333 |
#if defined(__CPU_PAGE_TABLES_FULLY_CACHED) |
|
334 |
TMemoryAttributes memAttr = EMemoryAttributeStandard; |
|
335 |
#else |
|
336 |
TMemoryAttributes memAttr = (TMemoryAttributes)(EMemoryAttributeNormalUncached|EMemoryAttributeDefaultShareable); |
|
337 |
#endif |
|
338 |
TMemoryCreateFlags createFlags = (TMemoryCreateFlags)(EMemoryCreateNoWipe|EMemoryCreateCustomManager); |
|
339 |
TInt r = MM::InitFixedKernelMemory(iPageTableMemory, KPageTableBase, KPageTableEnd, pages<<KPageShift, (TMemoryObjectType)(T_UintPtr)&ThePageTableMemoryManager, createFlags, memAttr, mapFlags); |
|
340 |
__NK_ASSERT_ALWAYS(r==KErrNone); |
|
341 |
MM::MemorySetLock(iPageTableMemory,aLock); |
|
342 |
||
343 |
// construct memory object for page table infos... |
|
344 |
memAttr = EMemoryAttributeStandard; |
|
345 |
TUint size = pages*KPtClusterSize*sizeof(SPageTableInfo); |
|
346 |
size = (size+KPageMask)&~KPageMask; |
|
347 |
r = MM::InitFixedKernelMemory(iPageTableInfoMemory, KPageTableInfoBase, KPageTableInfoEnd, size, (TMemoryObjectType)(T_UintPtr)&ThePageTableMemoryManager, createFlags, memAttr, mapFlags); |
|
348 |
__NK_ASSERT_ALWAYS(r==KErrNone); |
|
349 |
MM::MemorySetLock(iPageTableInfoMemory,aLock); |
|
350 |
||
351 |
// make sure we have enough reserve page tables... |
|
352 |
Lock(); |
|
353 |
iUnpagedAllocator.Init2(this,KNumReservedPageTables,false); |
|
354 |
iPagedAllocator.Init2(this,0,true); |
|
355 |
Unlock(); |
|
356 |
||
357 |
TRACEB(("PageTableAllocator::Init2 done")); |
|
358 |
} |
|
359 |
||
360 |
||
361 |
void PageTableAllocator::Init2B() |
|
362 |
{ |
|
363 |
TRACEB(("PageTableAllocator::Init2B()")); |
|
364 |
TInt r = iPageTableMemory->iPages.PreallocateMemory(); |
|
365 |
__NK_ASSERT_ALWAYS(r==KErrNone); |
|
366 |
r = iPageTableInfoMemory->iPages.PreallocateMemory(); |
|
367 |
__NK_ASSERT_ALWAYS(r==KErrNone); |
|
368 |
TRACEB(("PageTableAllocator::Init2B done")); |
|
369 |
} |
|
370 |
||
371 |
||
372 |
void PageTableAllocator::TSubAllocator::Init2(PageTableAllocator* aAllocator, TUint aReserveCount, TBool aDemandPaged) |
|
373 |
{ |
|
374 |
iReserveCount = aReserveCount; |
|
375 |
iDemandPaged = aDemandPaged; |
|
132 | 376 |
while(iFreeCount < aReserveCount) |
377 |
__NK_ASSERT_ALWAYS(aAllocator->AllocReserve(*this)); |
|
0 | 378 |
} |
379 |
||
380 |
||
381 |
void PageTableAllocator::TPtPageAllocator::Init2(TUint aNumInitPages) |
|
382 |
{ |
|
383 |
iLowerAllocator = TBitMapAllocator::New(KMaxPageTablePages,ETrue); |
|
384 |
__NK_ASSERT_ALWAYS(iLowerAllocator); |
|
385 |
iLowerAllocator->Alloc(0,aNumInitPages); |
|
386 |
iLowerWaterMark = aNumInitPages-1; |
|
387 |
||
388 |
iUpperAllocator = TBitMapAllocator::New(KMaxPageTablePages,ETrue); |
|
389 |
__NK_ASSERT_ALWAYS(iUpperAllocator); |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
390 |
|
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
391 |
__ASSERT_COMPILE(KMaxPageTablePages > (TUint)KMinUnpinnedPagedPtPages); // Unlikely to be untrue. |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
392 |
iUpperWaterMark = KMaxPageTablePages - KMinUnpinnedPagedPtPages; |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
393 |
iPinnedPageTablePages = 0; // OK to clear this without MmuLock as only one thread running so far. |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
394 |
} |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
395 |
|
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
396 |
|
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
397 |
static TUint32 RandomSeed = 33333; |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
398 |
|
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
399 |
TUint PageTableAllocator::TPtPageAllocator::RandomPagedPtPage() |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
400 |
{ |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
401 |
__NK_ASSERT_DEBUG(PageTablesLockIsHeld()); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
402 |
|
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
403 |
// Pick an allocated page at random, from iUpperWaterMark - KMaxPageTablePages. |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
404 |
RandomSeed = RandomSeed * 69069 + 1; // next 'random' number |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
405 |
TUint allocRange = KMaxPageTablePages - iUpperWaterMark - 1; |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
406 |
TUint bit = (TUint64(RandomSeed) * TUint64(allocRange)) >> 32; |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
407 |
|
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
408 |
// All page table pages should be allocated or we shouldn't be stealing one at random. |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
409 |
__NK_ASSERT_DEBUG(iUpperAllocator->NotFree(bit, 1)); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
410 |
|
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
411 |
return KMaxPageTablePages - 1 - bit; |
0 | 412 |
} |
413 |
||
414 |
||
415 |
TInt PageTableAllocator::TPtPageAllocator::Alloc(TBool aDemandPaged) |
|
416 |
{ |
|
417 |
__NK_ASSERT_DEBUG(PageTablesLockIsHeld()); |
|
418 |
TUint pageIndex; |
|
419 |
if(aDemandPaged) |
|
420 |
{ |
|
421 |
TInt bit = iUpperAllocator->Alloc(); |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
422 |
// There are always unpaged page tables so iUpperAllocator will always have |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
423 |
// at least one free bit. |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
424 |
__NK_ASSERT_DEBUG(bit >= 0); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
425 |
|
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
426 |
pageIndex = KMaxPageTablePages - 1 - bit; |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
427 |
|
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
428 |
if(pageIndex < iUpperWaterMark) |
0 | 429 |
{ |
430 |
// new upper watermark... |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
431 |
if((pageIndex & ~(KPageTableGroupSize - 1)) <= iLowerWaterMark) |
0 | 432 |
{ |
433 |
// clashes with other bitmap allocator, so fail.. |
|
434 |
iUpperAllocator->Free(bit); |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
435 |
TRACE(("TPtPageAllocator::Alloc too low iUpperWaterMark %d ",iUpperWaterMark)); |
0 | 436 |
return -1; |
437 |
} |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
438 |
// Hold mmu lock so iUpperWaterMark isn't read by pinning before we've updated it. |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
439 |
MmuLock::Lock(); |
0 | 440 |
iUpperWaterMark = pageIndex; |
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
441 |
MmuLock::Unlock(); |
0 | 442 |
TRACE(("TPtPageAllocator::Alloc new iUpperWaterMark=%d",pageIndex)); |
443 |
} |
|
444 |
} |
|
445 |
else |
|
446 |
{ |
|
447 |
TInt bit = iLowerAllocator->Alloc(); |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
448 |
if(bit < 0) |
0 | 449 |
return bit; |
450 |
pageIndex = bit; |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
451 |
if(pageIndex > iLowerWaterMark) |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
452 |
{// iLowerAllocator->Alloc() should only pick the next bit after iLowerWaterMark. |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
453 |
__NK_ASSERT_DEBUG(pageIndex == iLowerWaterMark + 1); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
454 |
MmuLock::Lock(); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
455 |
// new lower watermark... |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
456 |
if( pageIndex >= (iUpperWaterMark & ~(KPageTableGroupSize - 1)) || |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
457 |
AtPinnedPagedPtsLimit(iUpperWaterMark, pageIndex, iPinnedPageTablePages)) |
0 | 458 |
{ |
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
459 |
// clashes with other bitmap allocator or it would reduce the amount |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
460 |
// of available unpinned paged page tables too far, so fail.. |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
461 |
MmuLock::Unlock(); |
0 | 462 |
iLowerAllocator->Free(bit); |
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
463 |
TRACE(("TPtPageAllocator::Alloc iLowerWaterMark=%d",iLowerWaterMark)); |
0 | 464 |
return -1; |
465 |
} |
|
466 |
iLowerWaterMark = pageIndex; |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
467 |
MmuLock::Unlock(); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
468 |
TRACE(("TPtPageAllocator::Alloc new iLowerWaterMark=%d", iLowerWaterMark)); |
0 | 469 |
} |
470 |
} |
|
471 |
return pageIndex; |
|
472 |
} |
|
473 |
||
474 |
||
475 |
void PageTableAllocator::TPtPageAllocator::Free(TUint aPageIndex, TBool aDemandPaged) |
|
476 |
{ |
|
477 |
__NK_ASSERT_DEBUG(PageTablesLockIsHeld()); |
|
478 |
if(aDemandPaged) |
|
479 |
iUpperAllocator->Free(KMaxPageTablePages-1-aPageIndex); |
|
480 |
else |
|
481 |
iLowerAllocator->Free(aPageIndex); |
|
482 |
} |
|
483 |
||
484 |
||
485 |
void PageTableAllocator::Lock() |
|
486 |
{ |
|
487 |
Kern::MutexWait(*iLock); |
|
488 |
} |
|
489 |
||
490 |
||
491 |
void PageTableAllocator::Unlock() |
|
492 |
{ |
|
493 |
Kern::MutexSignal(*iLock); |
|
494 |
} |
|
495 |
||
496 |
||
497 |
TBool PageTableAllocator::LockIsHeld() |
|
498 |
{ |
|
499 |
return iLock->iCleanup.iThread == &Kern::CurrentThread(); |
|
500 |
} |
|
501 |
||
502 |
||
503 |
TBool PageTableAllocator::AllocReserve(TSubAllocator& aSubAllocator) |
|
504 |
{ |
|
505 |
__NK_ASSERT_DEBUG(LockIsHeld()); |
|
506 |
||
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
507 |
TBool demandPaged = aSubAllocator.iDemandPaged; |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
508 |
|
0 | 509 |
// allocate page... |
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
510 |
TInt ptPageIndex = iPtPageAllocator.Alloc(demandPaged); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
511 |
if (ptPageIndex < 0) |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
512 |
{ |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
513 |
if (demandPaged) |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
514 |
{ |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
515 |
TInt r; |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
516 |
do |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
517 |
{ |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
518 |
// Can't fail to find a demand paged page table, otherwise a page fault |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
519 |
// could fail with KErrNoMemory. Therefore, keep attempting to steal a |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
520 |
// demand paged page table page until successful. |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
521 |
TUint index = iPtPageAllocator.RandomPagedPtPage(); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
522 |
MmuLock::Lock(); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
523 |
TLinAddr pageTableLin = KPageTableBase + (index << (KPtClusterShift + KPageTableShift)); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
524 |
TPhysAddr ptPhysAddr = Mmu::LinearToPhysical(pageTableLin); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
525 |
// Page tables must be allocated otherwise we shouldn't be stealing the page. |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
526 |
__NK_ASSERT_DEBUG(ptPhysAddr != KPhysAddrInvalid); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
527 |
SPageInfo* ptSPageInfo = SPageInfo::FromPhysAddr(ptPhysAddr); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
528 |
r = StealPage(ptSPageInfo); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
529 |
MmuLock::Unlock(); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
530 |
} |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
531 |
while(r != KErrCompletion); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
532 |
// Retry the allocation now that we've stolen a page table page. |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
533 |
ptPageIndex = iPtPageAllocator.Alloc(demandPaged); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
534 |
__NK_ASSERT_DEBUG(ptPageIndex >= 0); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
535 |
} |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
536 |
else |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
537 |
{ |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
538 |
return EFalse; |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
539 |
} |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
540 |
} |
0 | 541 |
|
542 |
// commit memory for page... |
|
543 |
__NK_ASSERT_DEBUG(iPageTableMemory); // check we've initialised iPageTableMemory |
|
544 |
TInt r = ThePageTableMemoryManager.Alloc(iPageTableMemory,ptPageIndex,aSubAllocator.iDemandPaged); |
|
545 |
if(r==KErrNoMemory) |
|
546 |
{ |
|
547 |
iPtPageAllocator.Free(ptPageIndex,aSubAllocator.iDemandPaged); |
|
548 |
return false; |
|
549 |
} |
|
550 |
__NK_ASSERT_DEBUG(r==KErrNone); |
|
551 |
||
552 |
// allocate page table info... |
|
553 |
TUint ptgIndex = ptPageIndex/KPageTableGroupSize; |
|
554 |
if(!iPageTableGroupCounts[ptgIndex]) |
|
555 |
{ |
|
556 |
__NK_ASSERT_DEBUG(iPageTableInfoMemory); // check we've initialised iPageTableInfoMemory |
|
557 |
r = ThePageTableMemoryManager.Alloc(iPageTableInfoMemory,ptgIndex,aSubAllocator.iDemandPaged); |
|
558 |
||
559 |
if(r==KErrNoMemory) |
|
560 |
{ |
|
561 |
r = ThePageTableMemoryManager.Free(iPageTableMemory,ptPageIndex,aSubAllocator.iDemandPaged); |
|
562 |
__NK_ASSERT_DEBUG(r==1); |
|
563 |
iPtPageAllocator.Free(ptPageIndex,aSubAllocator.iDemandPaged); |
|
564 |
return false; |
|
565 |
} |
|
566 |
__NK_ASSERT_DEBUG(r==KErrNone); |
|
567 |
// For paged page tables set all the page table infos in this page as unused |
|
568 |
// and their page table clusters as not allocated. |
|
569 |
if (aSubAllocator.iDemandPaged) |
|
570 |
{ |
|
571 |
SPageTableInfo* ptiBase = (SPageTableInfo*)KPageTableInfoBase + (ptgIndex*KPageTableInfosPerPage); |
|
572 |
memclr(ptiBase, KPageSize); |
|
573 |
} |
|
574 |
} |
|
575 |
++iPageTableGroupCounts[ptgIndex]; |
|
576 |
||
577 |
SPageTableInfo* pti = (SPageTableInfo*)KPageTableInfoBase+ptPageIndex*KPtClusterSize; |
|
578 |
aSubAllocator.AllocPage(pti); |
|
579 |
return true; |
|
580 |
} |
|
581 |
||
582 |
||
583 |
void PageTableAllocator::TSubAllocator::AllocPage(SPageTableInfo* aPageTableInfo) |
|
584 |
{ |
|
585 |
SPageTableInfo* pti = aPageTableInfo; |
|
586 |
__NK_ASSERT_DEBUG(pti->IsFirstInPage()); |
|
587 |
||
588 |
TRACE2(("Alloc PT page (%d) 0x%08x",iDemandPaged,pti->PageTable())); |
|
589 |
||
590 |
// initialise page table infos... |
|
591 |
do pti->New(iDemandPaged); |
|
592 |
while(!(++pti)->IsFirstInPage()); |
|
593 |
pti -= KPtClusterSize; |
|
594 |
||
595 |
// all page tables initially unused, so start them off on iCleanupList... |
|
596 |
pti->AddToCleanupList(iCleanupList); |
|
597 |
iFreeCount += KPtClusterSize; |
|
598 |
__NK_ASSERT_DEBUG(CheckFreeList()); |
|
599 |
} |
|
600 |
||
601 |
||
602 |
SPageTableInfo* PageTableAllocator::TSubAllocator::FreePage() |
|
603 |
{ |
|
604 |
if(!IsCleanupRequired()) |
|
605 |
return 0; |
|
606 |
||
607 |
// get a completely free page... |
|
608 |
SDblQueLink* link = iCleanupList.Last(); |
|
609 |
__NK_ASSERT_DEBUG(link); |
|
610 |
SPageTableInfo* pti = SPageTableInfo::FromFreeLink(link); |
|
611 |
__NK_ASSERT_DEBUG(pti->IsFirstInPage()); |
|
612 |
pti->RemoveFromCleanupList(); |
|
613 |
iFreeCount -= KPtClusterSize; |
|
614 |
__NK_ASSERT_DEBUG(CheckFreeList()); |
|
615 |
||
616 |
TRACE2(("Free PT page (%d) 0x%08x",iDemandPaged,pti->PageTable())); |
|
617 |
||
618 |
// Mark each page table info as no longer having its page table cluster allocated. |
|
619 |
do |
|
620 |
{// make sure all page tables in page are unused... |
|
621 |
__NK_ASSERT_DEBUG(pti->IsUnused()); |
|
622 |
pti->PtClusterFreed(); |
|
623 |
} |
|
624 |
while(!(++pti)->IsFirstInPage()); |
|
625 |
pti -= KPtClusterSize; |
|
626 |
||
627 |
return pti; |
|
628 |
} |
|
629 |
||
630 |
||
631 |
TBool PageTableAllocator::FreeReserve(TSubAllocator& aSubAllocator) |
|
632 |
{ |
|
633 |
__NK_ASSERT_DEBUG(LockIsHeld()); |
|
634 |
||
635 |
// get a page which needs freeing... |
|
636 |
SPageTableInfo* pti = aSubAllocator.FreePage(); |
|
637 |
if(!pti) |
|
638 |
return false; |
|
639 |
||
640 |
// free the page... |
|
641 |
TUint ptPageIndex = ((TLinAddr)pti-KPageTableInfoBase)>>(KPageTableInfoShift+KPtClusterShift); |
|
642 |
iPtPageAllocator.Free(ptPageIndex,aSubAllocator.iDemandPaged); |
|
643 |
TInt r = ThePageTableMemoryManager.Free(iPageTableMemory,ptPageIndex,aSubAllocator.iDemandPaged); |
|
644 |
(void)r; |
|
645 |
__NK_ASSERT_DEBUG(r==1); |
|
646 |
||
647 |
// free page table info... |
|
648 |
TUint ptgIndex = ptPageIndex/KPageTableGroupSize; |
|
649 |
TUint groupCount = iPageTableGroupCounts[ptgIndex]; // compiler handles half-word values stupidly, so give it a hand |
|
650 |
--groupCount; |
|
651 |
iPageTableGroupCounts[ptgIndex] = groupCount; |
|
652 |
if(!groupCount) |
|
653 |
r = ThePageTableMemoryManager.Free(iPageTableInfoMemory,ptgIndex,aSubAllocator.iDemandPaged); |
|
654 |
||
655 |
return true; |
|
656 |
} |
|
657 |
||
658 |
||
659 |
TPte* PageTableAllocator::Alloc(TBool aDemandPaged) |
|
660 |
{ |
|
661 |
TRACE(("PageTableAllocator::Alloc(%d)",(bool)aDemandPaged)); |
|
662 |
TPte* pt = DoAlloc(aDemandPaged); |
|
663 |
TRACE(("PageTableAllocator::Alloc() returns 0x%08x phys=0x%08x",pt,pt?Mmu::PageTablePhysAddr(pt):KPhysAddrInvalid)); |
|
664 |
return pt; |
|
665 |
} |
|
666 |
||
667 |
||
668 |
TPte* PageTableAllocator::DoAlloc(TBool aDemandPaged) |
|
669 |
{ |
|
670 |
__NK_ASSERT_DEBUG(LockIsHeld()); |
|
671 |
||
672 |
#ifdef _DEBUG |
|
673 |
// simulated OOM, but not if demand paged as this can't fail under normal circumstances... |
|
674 |
if(!aDemandPaged) |
|
675 |
{ |
|
676 |
RamAllocLock::Lock(); |
|
677 |
TBool fail = K::CheckForSimulatedAllocFail(); |
|
678 |
RamAllocLock::Unlock(); |
|
679 |
if(fail) |
|
680 |
return 0; |
|
681 |
} |
|
682 |
#endif |
|
683 |
||
684 |
TSubAllocator& allocator = aDemandPaged ? iPagedAllocator : iUnpagedAllocator; |
|
685 |
||
686 |
__NK_ASSERT_DEBUG(!iAllocating || !aDemandPaged); // can't recursively allocate demand paged tables |
|
687 |
||
688 |
__NK_ASSERT_DEBUG(iAllocating<=allocator.iReserveCount); // can't recursively allocate more than the reserve |
|
689 |
||
690 |
// keep up enough spare page tables... |
|
691 |
if(!iAllocating++) // if we haven't gone recursive... |
|
692 |
{ |
|
693 |
// make sure we have a page table to allocate... |
|
694 |
while(allocator.iFreeCount<=allocator.iReserveCount) |
|
695 |
if(!AllocReserve(allocator)) |
|
696 |
{ |
|
697 |
--iAllocating; |
|
698 |
return 0; // out of memory |
|
699 |
} |
|
700 |
} |
|
701 |
else |
|
702 |
{ |
|
703 |
TRACE(("PageTableAllocator::DoAlloc recurse=%d",iAllocating)); |
|
704 |
} |
|
705 |
||
706 |
// allocate a page table... |
|
707 |
SPageTableInfo* pti = allocator.Alloc(); |
|
708 |
||
709 |
// initialise page table info... |
|
710 |
pti->Init(); |
|
711 |
||
712 |
// initialise page table... |
|
713 |
TPte* pt = pti->PageTable(); |
|
714 |
memclr(pt,KPageTableSize); |
|
715 |
CacheMaintenance::MultiplePtesUpdated((TLinAddr)pt,KPageTableSize); |
|
716 |
||
717 |
// done... |
|
718 |
--iAllocating; |
|
719 |
return pt; |
|
720 |
} |
|
721 |
||
722 |
||
723 |
SPageTableInfo* PageTableAllocator::TSubAllocator::Alloc() |
|
724 |
{ |
|
725 |
__NK_ASSERT_DEBUG(PageTablesLockIsHeld()); |
|
726 |
__NK_ASSERT_DEBUG(iFreeCount); |
|
727 |
__NK_ASSERT_DEBUG(CheckFreeList()); |
|
728 |
||
729 |
// get next free page table... |
|
730 |
SDblQueLink* link = iFreeList.GetFirst(); |
|
731 |
SPageTableInfo* pti; |
|
732 |
if(link) |
|
733 |
pti = SPageTableInfo::FromFreeLink(link); |
|
734 |
else |
|
735 |
{ |
|
736 |
// need to get one back from the cleanup list... |
|
737 |
link = iCleanupList.First(); |
|
738 |
__NK_ASSERT_DEBUG(link); // we can't be out of page tables |
|
739 |
pti = SPageTableInfo::FromFreeLink(link); |
|
740 |
__NK_ASSERT_DEBUG(pti->IsFirstInPage()); |
|
741 |
pti->RemoveFromCleanupList(); |
|
742 |
||
743 |
// add other page tables in the page to the free list... |
|
744 |
SPageTableInfo* free = pti+1; |
|
745 |
while(!free->IsFirstInPage()) |
|
746 |
{ |
|
747 |
__NK_ASSERT_DEBUG(free->IsUnused()); |
|
748 |
iFreeList.Add(&free->FreeLink()); |
|
749 |
++free; |
|
750 |
} |
|
751 |
} |
|
752 |
||
753 |
// count page as allocated... |
|
754 |
--iFreeCount; |
|
755 |
__NK_ASSERT_DEBUG(pti->IsUnused()); |
|
756 |
__NK_ASSERT_DEBUG(CheckFreeList()); |
|
757 |
||
758 |
return pti; |
|
759 |
} |
|
760 |
||
761 |
||
762 |
void PageTableAllocator::Free(TPte* aPageTable) |
|
763 |
{ |
|
764 |
TRACE(("PageTableAllocator::Free(0x%08x)",aPageTable)); |
|
765 |
DoFree(aPageTable); |
|
766 |
} |
|
767 |
||
768 |
||
769 |
void PageTableAllocator::DoFree(TPte* aPageTable) |
|
770 |
{ |
|
771 |
__NK_ASSERT_DEBUG(LockIsHeld()); |
|
772 |
||
773 |
// make sure page table isn't being aliased... |
|
774 |
TPhysAddr pagePhys = Mmu::PageTablePhysAddr(aPageTable); |
|
775 |
__NK_ASSERT_DEBUG(pagePhys!=KPhysAddrInvalid); |
|
776 |
TheMmu.RemoveAliasesForPageTable(pagePhys); |
|
777 |
||
778 |
// free page table... |
|
779 |
SPageTableInfo* pti = SPageTableInfo::FromPtPtr(aPageTable); |
|
780 |
TSubAllocator& allocator = pti->IsDemandPaged() ? iPagedAllocator : iUnpagedAllocator; |
|
781 |
allocator.Free(pti); |
|
782 |
||
783 |
// check for surplus pages... |
|
784 |
if(allocator.IsCleanupRequired()) |
|
785 |
{ |
|
786 |
iCleanup.Add(CleanupTrampoline,this); |
|
787 |
} |
|
788 |
} |
|
789 |
||
790 |
||
791 |
void PageTableAllocator::TSubAllocator::Free(SPageTableInfo* aPageTableInfo) |
|
792 |
{ |
|
793 |
__NK_ASSERT_DEBUG(PageTablesLockIsHeld()); |
|
794 |
__NK_ASSERT_DEBUG(CheckFreeList()); |
|
795 |
||
796 |
SPageTableInfo* pti = aPageTableInfo; |
|
797 |
||
798 |
// clear the page table info... |
|
799 |
MmuLock::Lock(); |
|
800 |
__NK_ASSERT_DEBUG(!pti->PermanenceCount()); |
|
801 |
pti->SetUnused(); |
|
802 |
MmuLock::Unlock(); |
|
803 |
||
804 |
// scan other page tables in same page... |
|
805 |
SPageTableInfo* first = pti->FirstInPage(); |
|
806 |
SPageTableInfo* last = pti->LastInPage(); |
|
807 |
SPageTableInfo* prev; |
|
808 |
SPageTableInfo* next; |
|
809 |
||
810 |
// try insert page table after previous free page table in same page... |
|
811 |
prev = pti; |
|
812 |
while(prev>first) |
|
813 |
{ |
|
814 |
--prev; |
|
815 |
if(prev->IsUnused()) |
|
816 |
{ |
|
817 |
pti->FreeLink().InsertAfter(&prev->FreeLink()); |
|
818 |
goto inserted; |
|
819 |
} |
|
820 |
} |
|
821 |
||
822 |
// try insert page table before next free page table in same page... |
|
823 |
next = pti; |
|
824 |
while(next<last) |
|
825 |
{ |
|
826 |
++next; |
|
827 |
if(next->IsUnused()) |
|
828 |
{ |
|
829 |
pti->FreeLink().InsertBefore(&next->FreeLink()); |
|
830 |
goto inserted; |
|
831 |
} |
|
832 |
} |
|
833 |
||
834 |
// only free page table in page, so link into start of free list... |
|
835 |
pti->FreeLink().InsertAfter(&iFreeList.iA); |
|
836 |
||
837 |
inserted: |
|
838 |
++iFreeCount; |
|
839 |
__NK_ASSERT_DEBUG(CheckFreeList()); |
|
840 |
||
841 |
// see if all page tables in page are empty... |
|
842 |
pti = first; |
|
843 |
do |
|
844 |
{ |
|
845 |
if(!pti->IsUnused()) |
|
846 |
return; // some page tables still in use, so end |
|
847 |
} |
|
848 |
while(!(++pti)->IsFirstInPage()); |
|
849 |
pti -= KPtClusterSize; |
|
850 |
||
851 |
// check if page with page table in is pinned... |
|
852 |
MmuLock::Lock(); |
|
853 |
TPte* pt = pti->PageTable(); |
|
854 |
TPhysAddr pagePhys = Mmu::PageTablePhysAddr(pt); |
|
855 |
SPageInfo* pi = SPageInfo::FromPhysAddr(pagePhys); |
|
856 |
TBool pinned = pi->PagedState()==SPageInfo::EPagedPinned; |
|
857 |
MmuLock::Unlock(); |
|
858 |
// Note, the pinned state can't change even though we've now released the MmuLock. |
|
859 |
// This is because all page tables in the page are unused and we don't pin unused |
|
860 |
// page tables. Also, the page table(s) can't become used again whilst this function |
|
861 |
// executes as we hold the page table allocator lock. |
|
862 |
if(pinned) |
|
863 |
{ |
|
864 |
// return now and leave page table(s) in free list if their page is pinned... |
|
865 |
// Note, when page is unpinned it will end up in the paging live list and |
|
866 |
// eventually be reclaimed for other use (if the page tables in the page |
|
867 |
// don't get reallocated before then). |
|
868 |
__NK_ASSERT_DEBUG(pti->IsDemandPaged()); // only paged page tables should have been pinned |
|
869 |
return; |
|
870 |
} |
|
871 |
||
872 |
// the page with our page table in it is no longer in use... |
|
873 |
MoveToCleanup(pti); |
|
874 |
} |
|
875 |
||
876 |
||
877 |
void PageTableAllocator::TSubAllocator::MoveToCleanup(SPageTableInfo* aPageTableInfo) |
|
878 |
{ |
|
879 |
__NK_ASSERT_DEBUG(PageTablesLockIsHeld()); |
|
880 |
__NK_ASSERT_DEBUG(CheckFreeList()); |
|
881 |
||
882 |
SPageTableInfo* pti = aPageTableInfo; |
|
883 |
__NK_ASSERT_DEBUG(pti->IsFirstInPage()); |
|
884 |
||
885 |
TRACE2(("Cleanup PT page (%d) 0x%08x",iDemandPaged,pti->PageTable())); |
|
886 |
||
887 |
// make sure all page tables in page are unused... |
|
888 |
#ifdef _DEBUG |
|
889 |
do __NK_ASSERT_DEBUG(pti->IsUnused()); |
|
890 |
while(!(++pti)->IsFirstInPage()); |
|
891 |
pti -= KPtClusterSize; |
|
892 |
#endif |
|
893 |
||
894 |
// unlink all page tables in page... |
|
895 |
SDblQueLink* prev = pti->FreeLink().iPrev; |
|
896 |
SDblQueLink* next = pti->LastInPage()->FreeLink().iNext; |
|
897 |
prev->iNext = next; |
|
898 |
next->iPrev = prev; |
|
899 |
||
900 |
// add page tables to cleanup list... |
|
901 |
__NK_ASSERT_DEBUG(!pti->IsOnCleanupList()); |
|
902 |
pti->AddToCleanupList(iCleanupList); |
|
903 |
__NK_ASSERT_DEBUG(CheckFreeList()); |
|
904 |
} |
|
905 |
||
906 |
||
907 |
||
908 |
TBool PageTableAllocator::TSubAllocator::IsCleanupRequired() |
|
909 |
{ |
|
910 |
return iFreeCount>=iReserveCount+KPtClusterSize && !iCleanupList.IsEmpty(); |
|
911 |
} |
|
912 |
||
913 |
||
914 |
#ifdef _DEBUG |
|
915 |
||
916 |
TBool PageTableAllocator::TSubAllocator::CheckFreeList() |
|
917 |
{ |
|
918 |
TUint count = iFreeCount; |
|
919 |
||
920 |
// count page tables in iCleanupList... |
|
921 |
SDblQueLink* head = &iCleanupList.iA; |
|
922 |
SDblQueLink* link = head; |
|
923 |
for(;;) |
|
924 |
{ |
|
925 |
link = link->iNext; |
|
926 |
if(link==head) |
|
927 |
break; |
|
928 |
SPageTableInfo* pti = SPageTableInfo::FromFreeLink(link); |
|
929 |
__NK_ASSERT_DEBUG(pti->IsFirstInPage()); |
|
930 |
__NK_ASSERT_DEBUG(pti->IsOnCleanupList()); |
|
931 |
if(count<(TUint)KPtClusterSize) |
|
932 |
return false; |
|
933 |
count -= KPtClusterSize; |
|
934 |
} |
|
935 |
||
936 |
// count page tables in iFreeList... |
|
937 |
head = &iFreeList.iA; |
|
938 |
link = head; |
|
939 |
while(count) |
|
940 |
{ |
|
941 |
link = link->iNext; |
|
942 |
if(link==head) |
|
943 |
return false; |
|
944 |
||
945 |
// check next free page table in page is linked in correct order... |
|
946 |
SPageTableInfo* pti = SPageTableInfo::FromFreeLink(link); |
|
947 |
SPageTableInfo* last = pti->LastInPage(); |
|
948 |
SPageTableInfo* next = pti; |
|
949 |
while(next<last) |
|
950 |
{ |
|
951 |
++next; |
|
952 |
if(next->IsUnused()) |
|
953 |
{ |
|
954 |
__NK_ASSERT_DEBUG(pti->FreeLink().iNext==&next->FreeLink()); |
|
955 |
__NK_ASSERT_DEBUG(next->FreeLink().iPrev==&pti->FreeLink()); |
|
956 |
break; |
|
957 |
} |
|
958 |
} |
|
959 |
||
960 |
--count; |
|
961 |
} |
|
962 |
||
963 |
return link->iNext==head; |
|
964 |
} |
|
965 |
||
966 |
#endif |
|
967 |
||
968 |
||
969 |
||
970 |
// |
|
971 |
// Paged page table handling |
|
972 |
// |
|
973 |
||
974 |
TInt SPageTableInfo::ForcedFree() |
|
975 |
{ |
|
976 |
__NK_ASSERT_DEBUG(PageTablesLockIsHeld()); |
|
977 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
978 |
__NK_ASSERT_DEBUG(IsDemandPaged()); |
|
979 |
||
980 |
TUint type = iType; |
|
981 |
||
982 |
if(type==EUnused) |
|
983 |
return KErrNone; |
|
984 |
||
985 |
__NK_ASSERT_DEBUG(iPermanenceCount==0); |
|
986 |
||
987 |
// clear all PTEs in page table... |
|
988 |
TPte* pt = PageTable(); |
|
989 |
memclr(pt,KPageTableSize); |
|
990 |
__e32_memory_barrier(); // make sure all CPUs read zeros from pt so forcing a page-in (rather than a rejuvenate) if accessed |
|
991 |
iPageCount = 0; |
|
992 |
||
993 |
if(type==ECoarseMapping) |
|
994 |
{ |
|
995 |
TRACE2(("SPageTableInfo::ForcedFree() coarse 0x%08x 0x%x %d",iCoarse.iMemoryObject,iCoarse.iChunkIndex,iCoarse.iPteType)); |
|
996 |
// mustn't release MmuLock between clearing page table and calling this |
|
997 |
// (otherwise page table may get updated before its actually removed from |
|
998 |
// the memory object)... |
|
999 |
iCoarse.iMemoryObject->StealPageTable(iCoarse.iChunkIndex,iCoarse.iPteType); |
|
1000 |
} |
|
1001 |
else if(type==EFineMapping) |
|
1002 |
{ |
|
1003 |
// need to remove page table from address spaces's page directory... |
|
1004 |
TLinAddr addr = iFine.iLinAddrAndOsAsid; |
|
1005 |
TUint osAsid = addr&KPageMask; |
|
1006 |
TPde* pPde = Mmu::PageDirectoryEntry(osAsid,addr); |
|
1007 |
||
1008 |
TRACE2(("SPageTableInfo::ForcedFree() fine %d 0x%08x",osAsid,addr&~KPageMask)); |
|
1009 |
||
1010 |
TPde pde = KPdeUnallocatedEntry; |
|
1011 |
TRACE2(("!PDE %x=%x",pPde,pde)); |
|
1012 |
*pPde = pde; |
|
1013 |
SinglePdeUpdated(pPde); |
|
1014 |
} |
|
1015 |
else |
|
1016 |
{ |
|
1017 |
// invalid type... |
|
1018 |
__NK_ASSERT_DEBUG(0); |
|
1019 |
return KErrNotSupported; |
|
1020 |
} |
|
1021 |
||
1022 |
MmuLock::Unlock(); |
|
1023 |
||
1024 |
// make sure page table updates visible to MMU... |
|
1025 |
CacheMaintenance::MultiplePtesUpdated((TLinAddr)pt,KPageTableSize); |
|
1026 |
InvalidateTLB(); |
|
1027 |
||
1028 |
// free the page table back to the allocator, |
|
1029 |
// this will also remove any IPC alias using it... |
|
1030 |
__NK_ASSERT_DEBUG(iPageCount==0); // should still be unused |
|
1031 |
::PageTables.Free(pt); |
|
1032 |
||
1033 |
MmuLock::Lock(); |
|
1034 |
||
1035 |
return KErrNone; |
|
1036 |
} |
|
1037 |
||
1038 |
||
1039 |
TInt PageTableAllocator::StealPage(SPageInfo* aPageInfo) |
|
1040 |
{ |
|
1041 |
TRACE2(("PageTableAllocator::StealPage(0x%08x)",aPageInfo)); |
|
1042 |
__NK_ASSERT_DEBUG(LockIsHeld()); // only works if PageTableAllocator lock is the RamAllocLock |
|
1043 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
1044 |
||
1045 |
if (aPageInfo->Owner() == iPageTableInfoMemory) |
|
1046 |
return StealPageTableInfo(aPageInfo); |
|
1047 |
||
1048 |
__UNLOCK_GUARD_START(MmuLock); |
|
1049 |
||
1050 |
// This must be a page table page so steal it. |
|
1051 |
__NK_ASSERT_ALWAYS(aPageInfo->Owner()==iPageTableMemory); |
|
1052 |
TUint ptPageIndex = aPageInfo->Index(); |
|
1053 |
SPageTableInfo* pti = (SPageTableInfo*)KPageTableInfoBase+ptPageIndex*KPtClusterSize; |
|
1054 |
||
1055 |
aPageInfo->SetModifier(&pti); |
|
1056 |
__UNLOCK_GUARD_END(MmuLock); |
|
1057 |
||
1058 |
// forcibly free each page table in the page... |
|
1059 |
TInt r; |
|
1060 |
do |
|
1061 |
{// Check for pinning, ForcedFree() releases MmuLock so must check for |
|
1062 |
// each page table. |
|
1063 |
if (aPageInfo->PagedState() == SPageInfo::EPagedPinned) |
|
1064 |
{// The page table page is pinned so can't steal it. |
|
1065 |
r = KErrInUse; |
|
1066 |
break; |
|
1067 |
} |
|
1068 |
r = pti->ForcedFree(); |
|
1069 |
if(r!=KErrNone) |
|
1070 |
break; |
|
1071 |
if(aPageInfo->CheckModified(&pti)) |
|
1072 |
{ |
|
1073 |
r = KErrInUse; |
|
1074 |
break; |
|
1075 |
} |
|
1076 |
} |
|
1077 |
while(!(++pti)->IsFirstInPage()); |
|
1078 |
pti -= KPtClusterSize; // restore pti back to first page table |
|
1079 |
||
1080 |
if(r==KErrNone) |
|
1081 |
{ |
|
1082 |
MmuLock::Unlock(); |
|
1083 |
if(!pti->IsOnCleanupList()) |
|
1084 |
{ |
|
1085 |
// the page might not already be on the cleanup list in the case where |
|
1086 |
// it was previously freed whilst it was pinned. |
|
1087 |
// In this case, a later unpinning would have put it back into the paging live |
|
1088 |
// list from where it is now subsequently being stolen... |
|
1089 |
iPagedAllocator.MoveToCleanup(pti); |
|
1090 |
} |
|
1091 |
// free the page from allocator so it ends up back in the paging pool as a free page... |
|
1092 |
while(FreeReserve(iPagedAllocator)) |
|
1093 |
{} |
|
1094 |
// return an 'error' to indicate page has not been stolen. |
|
1095 |
// We have however achieved the main aim of making the page 'free' and |
|
1096 |
// it will be available if page stealing attempts to steal the page again... |
|
1097 |
r = KErrCompletion; |
|
1098 |
MmuLock::Lock(); |
|
1099 |
} |
|
1100 |
||
1101 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
1102 |
TRACE2(("PageTableAllocator::StealPage returns %d",r)); |
|
1103 |
return r; |
|
1104 |
} |
|
1105 |
||
1106 |
||
1107 |
TInt PageTableAllocator::StealPageTableInfo(SPageInfo* aPageInfo) |
|
1108 |
{ |
|
1109 |
// Need to steal every page table for every page table info in this page. |
|
1110 |
// This page can't be modified or removed as we hold the lock, however |
|
1111 |
// the page table pages being freed may be rejuvenated and therefore their |
|
1112 |
// SPageInfos may be marked as modified. |
|
1113 |
TInt r = KErrNone; |
|
1114 |
TUint ptiOffset = aPageInfo->Index() * KPageTableInfosPerPage; |
|
1115 |
SPageTableInfo* ptiBase = (SPageTableInfo*)KPageTableInfoBase + ptiOffset; |
|
1116 |
SPageTableInfo* ptiEnd = ptiBase + KPageTableInfosPerPage; |
|
1117 |
TUint flash = 0; |
|
1118 |
for (SPageTableInfo* pti = ptiBase; pti < ptiEnd;) |
|
1119 |
{// Free each page table cluster that is allocated. |
|
1120 |
if (pti->IsPtClusterAllocated()) |
|
1121 |
{ |
|
1122 |
TPhysAddr ptPhysAddr = Mmu::LinearToPhysical((TLinAddr)pti->PageTable()); |
|
1123 |
SPageInfo* ptSPageInfo = SPageInfo::FromPhysAddr(ptPhysAddr); |
|
1124 |
ptSPageInfo->SetModifier(&flash); |
|
1125 |
do |
|
1126 |
{ |
|
1127 |
__NK_ASSERT_DEBUG(pti->IsPtClusterAllocated()); |
|
1128 |
if (aPageInfo->PagedState() == SPageInfo::EPagedPinned || |
|
1129 |
ptSPageInfo->PagedState() == SPageInfo::EPagedPinned) |
|
1130 |
{// The page table or page table info is pinned so can't steal info page. |
|
1131 |
r = KErrInUse; |
|
1132 |
break; |
|
1133 |
} |
|
1134 |
r = pti->ForcedFree(); |
|
1135 |
if(r!=KErrNone) |
|
1136 |
break; |
|
1137 |
if(ptSPageInfo->CheckModified(&flash)) |
|
1138 |
{// The page table page has been rejunvenated so can't steal it. |
|
1139 |
r = KErrInUse; |
|
1140 |
break; |
|
1141 |
} |
|
1142 |
} |
|
1143 |
while (!(++pti)->IsFirstInPage()); |
|
1144 |
if (r != KErrNone) |
|
1145 |
break; |
|
1146 |
SPageTableInfo* ptiTmp = pti - KPtClusterSize; |
|
1147 |
MmuLock::Unlock(); |
|
1148 |
if(!ptiTmp->IsOnCleanupList()) |
|
1149 |
{ |
|
1150 |
// the page might not already be on the cleanup list in the case where |
|
1151 |
// it was previously freed whilst it was pinned. |
|
1152 |
// In this case, a later unpinning would have put it back into the paging live |
|
1153 |
// list from where it is now subsequently being stolen... |
|
1154 |
iPagedAllocator.MoveToCleanup(ptiTmp); |
|
1155 |
} |
|
1156 |
MmuLock::Lock(); |
|
1157 |
flash = 0; // The MmuLock has been flashed at least once. |
|
1158 |
} |
|
1159 |
else |
|
1160 |
{// Move onto the next cluster this page of page table infos refers to. |
|
1161 |
__NK_ASSERT_DEBUG(pti->IsFirstInPage()); |
|
1162 |
pti += KPtClusterSize; |
|
1163 |
MmuLock::Flash(flash,KMaxPageInfoUpdatesInOneGo); |
|
1164 |
} |
|
1165 |
} |
|
1166 |
// free the pages discarded from allocator so they end up back in the paging pool as free pages... |
|
1167 |
MmuLock::Unlock(); |
|
1168 |
while(FreeReserve(iPagedAllocator)) |
|
1169 |
{} |
|
1170 |
if (r == KErrNone) |
|
1171 |
r = KErrCompletion; // The pager needs to remove the page from the live list. |
|
1172 |
MmuLock::Lock(); |
|
1173 |
return r; |
|
1174 |
} |
|
1175 |
||
1176 |
||
1177 |
TInt PageTableAllocator::MovePage(DMemoryObject* aMemory, SPageInfo* aOldPageInfo, |
|
1178 |
TUint aBlockZoneId, TBool aBlockRest) |
|
1179 |
{ |
|
1180 |
// We don't move page table or page table info pages, however, if this page |
|
1181 |
// is demand paged then we may be able to discard it. |
|
1182 |
MmuLock::Lock(); |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1183 |
if (aOldPageInfo->Owner() == iPageTableInfoMemory) |
0 | 1184 |
{ |
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1185 |
if (!(iPtPageAllocator.IsDemandPagedPtInfo(aOldPageInfo))) |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1186 |
{ |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1187 |
MmuLock::Unlock(); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1188 |
return KErrNotSupported; |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1189 |
} |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1190 |
} |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1191 |
else |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1192 |
{ |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1193 |
__NK_ASSERT_DEBUG(aOldPageInfo->Owner() == iPageTableMemory); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1194 |
if (!(iPtPageAllocator.IsDemandPagedPt(aOldPageInfo))) |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1195 |
{ |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1196 |
MmuLock::Unlock(); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1197 |
return KErrNotSupported; |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1198 |
} |
0 | 1199 |
} |
1200 |
if (aOldPageInfo->PagedState() == SPageInfo::EPagedPinned) |
|
1201 |
{// The page is pinned so don't attempt to discard it as pinned pages |
|
1202 |
// can't be discarded. Also, the pager will invoke this method again. |
|
1203 |
MmuLock::Unlock(); |
|
1204 |
return KErrInUse; |
|
1205 |
} |
|
1206 |
// Let the pager discard the page as it controls the size of the live list. |
|
1207 |
// If the size of the live list allows then eventually |
|
1208 |
// PageTableAllocator::StealPage() will be invoked on this page. |
|
1209 |
return ThePager.DiscardPage(aOldPageInfo, aBlockZoneId, aBlockRest); |
|
1210 |
} |
|
1211 |
||
1212 |
||
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1213 |
TInt PageTableAllocator::PinPageTable(TPte* aPageTable, TPinArgs& aPinArgs) |
0 | 1214 |
{ |
1215 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
1216 |
__NK_ASSERT_DEBUG(SPageTableInfo::FromPtPtr(aPageTable)->IsDemandPaged()); |
|
1217 |
__NK_ASSERT_DEBUG(!SPageTableInfo::FromPtPtr(aPageTable)->IsUnused()); |
|
1218 |
__NK_ASSERT_DEBUG(aPinArgs.HaveSufficientPages(KNumPagesToPinOnePageTable)); |
|
1219 |
||
1220 |
// pin page with page table in... |
|
1221 |
TPhysAddr pagePhys = Mmu::PageTablePhysAddr(aPageTable); |
|
1222 |
SPageInfo* pi = SPageInfo::FromPhysAddr(pagePhys); |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1223 |
if (!pi->PinCount()) |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1224 |
{// Page is being pinned having previously been unpinned. |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1225 |
TInt r = iPtPageAllocator.PtPagePinCountInc(); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1226 |
if (r != KErrNone) |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1227 |
return r; |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1228 |
} |
0 | 1229 |
ThePager.Pin(pi,aPinArgs); |
1230 |
||
1231 |
// pin page with page table info in... |
|
1232 |
SPageTableInfo* pti = SPageTableInfo::FromPtPtr(aPageTable); |
|
1233 |
pagePhys = Mmu::UncheckedLinearToPhysical((TLinAddr)pti,KKernelOsAsid); |
|
1234 |
pi = SPageInfo::FromPhysAddr(pagePhys); |
|
1235 |
ThePager.Pin(pi,aPinArgs); |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1236 |
return KErrNone; |
0 | 1237 |
} |
1238 |
||
1239 |
||
1240 |
void PageTableAllocator::UnpinPageTable(TPte* aPageTable, TPinArgs& aPinArgs) |
|
1241 |
{ |
|
1242 |
// unpin page with page table info in... |
|
1243 |
SPageTableInfo* pti = SPageTableInfo::FromPtPtr(aPageTable); |
|
1244 |
TPhysAddr pagePhys = Mmu::UncheckedLinearToPhysical((TLinAddr)pti,KKernelOsAsid); |
|
1245 |
SPageInfo* pi = SPageInfo::FromPhysAddr(pagePhys); |
|
1246 |
ThePager.Unpin(pi,aPinArgs); |
|
1247 |
||
1248 |
// unpin page with page table in... |
|
1249 |
pagePhys = Mmu::PageTablePhysAddr(aPageTable); |
|
1250 |
pi = SPageInfo::FromPhysAddr(pagePhys); |
|
1251 |
ThePager.Unpin(pi,aPinArgs); |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1252 |
|
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1253 |
if (!pi->PinCount()) |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1254 |
{// This page table page is no longer pinned. |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1255 |
iPtPageAllocator.PtPagePinCountDec(); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1256 |
} |
0 | 1257 |
} |
1258 |
||
1259 |
||
1260 |
#ifdef _DEBUG |
|
1261 |
TBool IsPageTableUnpagedRemoveAllowed(SPageInfo* aPageInfo) |
|
1262 |
{ return ::PageTables.IsPageTableUnpagedRemoveAllowed(aPageInfo); } |
|
1263 |
||
1264 |
TBool PageTableAllocator::IsPageTableUnpagedRemoveAllowed(SPageInfo* aPageInfo) |
|
1265 |
{ |
|
1266 |
if (aPageInfo->Owner() == iPageTableInfoMemory) |
|
1267 |
{// Page table info pages are never added to the live list but can be |
|
1268 |
// stolen via DPager::StealPage() |
|
1269 |
return ETrue; |
|
1270 |
} |
|
1271 |
||
1272 |
if (aPageInfo->Owner() == iPageTableMemory) |
|
1273 |
{// Page table pages are added to the live list but only after the page they |
|
1274 |
// map has been paged in. Therefore, a pde can reference a pte before it has been |
|
1275 |
// added to the live list so allow this but for uninitialised page table pages only. |
|
1276 |
TUint ptPageIndex = aPageInfo->Index(); |
|
1277 |
SPageTableInfo* pti = (SPageTableInfo*)KPageTableInfoBase+ptPageIndex*KPtClusterSize; |
|
1278 |
do |
|
1279 |
{ |
|
1280 |
if (!pti->IsUnused()) |
|
1281 |
{ |
|
1282 |
TPte* pte = pti->PageTable(); |
|
1283 |
TPte* pteEnd = pte + (KPageTableSize/sizeof(TPte)); |
|
1284 |
while (pte < pteEnd) |
|
1285 |
if (*pte++ != KPteUnallocatedEntry) |
|
1286 |
return EFalse; |
|
1287 |
} |
|
1288 |
} |
|
1289 |
while(!(++pti)->IsFirstInPage()); |
|
1290 |
return ETrue; |
|
1291 |
} |
|
1292 |
return EFalse; |
|
1293 |
} |
|
1294 |
#endif |
|
1295 |
||
1296 |
||
1297 |
// |
|
1298 |
// Cleanup |
|
1299 |
// |
|
1300 |
||
1301 |
void PageTableAllocator::CleanupTrampoline(TAny* aSelf) |
|
1302 |
{ |
|
1303 |
((PageTableAllocator*)aSelf)->Cleanup(); |
|
1304 |
} |
|
1305 |
||
1306 |
||
1307 |
void PageTableAllocator::Cleanup() |
|
1308 |
{ |
|
1309 |
// free any surplus pages... |
|
1310 |
Lock(); |
|
1311 |
while(FreeReserve(iPagedAllocator) || FreeReserve(iUnpagedAllocator)) |
|
1312 |
{} |
|
1313 |
Unlock(); |
|
1314 |
} |