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