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