author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Wed, 13 Oct 2010 16:04:24 +0300 | |
branch | RCL_3 |
changeset 294 | 039a3e647356 |
parent 257 | 3e88ff8f41d5 |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
2 |
// All rights reserved. |
|
3 |
// This component and the accompanying materials are made available |
|
4 |
// under the terms of the License "Eclipse Public License v1.0" |
|
5 |
// which accompanies this distribution, and is available |
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 |
// |
|
8 |
// Initial Contributors: |
|
9 |
// Nokia Corporation - initial contribution. |
|
10 |
// |
|
11 |
// Contributors: |
|
12 |
// |
|
13 |
// Description: |
|
14 |
// |
|
15 |
||
16 |
#include <plat_priv.h> |
|
17 |
#include "cache_maintenance.h" |
|
18 |
#include "mm.h" |
|
19 |
#include "mmu.h" |
|
20 |
#include "mmanager.h" |
|
21 |
#include "mobject.h" |
|
22 |
#include "mpager.h" |
|
23 |
#include "mcodepaging.h" |
|
24 |
||
25 |
/** |
|
26 |
Manager for memory objects containing demand paged executable code. |
|
27 |
This is the memory used by DCodeSegMemory object to store the contents of RAM loaded |
|
28 |
EXEs and DLLs which are to be demand paged. |
|
29 |
||
30 |
This memory has associated information, supplied by the Loader, which enables |
|
31 |
the executable's code to be located in the file system and its contents |
|
32 |
relocated and fixed-up when demand loaded. |
|
33 |
||
34 |
@see DPagedCodeInfo |
|
35 |
@see MM::PagedCodeNew |
|
36 |
*/ |
|
37 |
class DCodePagedMemoryManager : public DPagedMemoryManager |
|
38 |
{ |
|
39 |
private: |
|
40 |
// from DMemoryManager... |
|
41 |
virtual TInt New(DMemoryObject*& aMemory, TUint aSizeInPages, TMemoryAttributes aAttributes, TMemoryCreateFlags aCreateFlags); |
|
42 |
virtual void Destruct(DMemoryObject* aMemory); |
|
43 |
virtual void Free(DMemoryObject* aMemory, TUint aIndex, TUint aCount); |
|
44 |
virtual TInt CleanPage(DMemoryObject* aMemory, SPageInfo* aPageInfo, TPhysAddr*& aPageArrayEntry); |
|
45 |
||
46 |
// from DPagedMemoryManager... |
|
47 |
virtual void Init3(); |
|
48 |
virtual TInt InstallPagingDevice(DPagingDevice* aDevice); |
|
49 |
virtual TInt AcquirePageReadRequest(DPageReadRequest*& aRequest, DMemoryObject* aMemory, TUint aIndex, TUint aCount); |
|
50 |
virtual TInt ReadPages(DMemoryObject* aMemory, TUint aIndex, TUint aCount, TPhysAddr* aPages, DPageReadRequest* aRequest); |
|
51 |
virtual TBool IsAllocated(DMemoryObject* aMemory, TUint aIndex, TUint aCount); |
|
52 |
||
53 |
private: |
|
54 |
/** |
|
55 |
Array of paging devices used for each media drive. |
|
56 |
This is a initialised by #InstallPagingDevice. |
|
57 |
Drives without paging devices have the null pointer in their entry. |
|
58 |
*/ |
|
59 |
DPagingDevice* iDevice[KMaxLocalDrives]; |
|
60 |
||
61 |
public: |
|
62 |
/** |
|
63 |
The single instance of this manager class. |
|
64 |
*/ |
|
65 |
static DCodePagedMemoryManager TheManager; |
|
66 |
||
87
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
67 |
friend DPagingDevice* CodePagingDevice(TInt aDriveNum); |
0 | 68 |
}; |
69 |
||
70 |
||
71 |
/** |
|
72 |
Reference counted object containing a #TPagedCodeInfo. |
|
73 |
This is a structure containing the information about a demand paged code segment |
|
74 |
which is required to load and fixup its code section. |
|
75 |
||
76 |
An instance of this object is created for each memory object being managed by |
|
77 |
#DCodePagedMemoryManager, and a pointer to it is stored in the memory object's |
|
78 |
DMemoryObject::iManagerData member. |
|
79 |
||
80 |
@see TPagedCodeInfo |
|
81 |
@see MM::PagedCodeLoaded |
|
82 |
*/ |
|
83 |
class DPagedCodeInfo : public DReferenceCountedObject |
|
84 |
{ |
|
85 |
public: |
|
86 |
/** |
|
87 |
Return a reference to the embedded #TPagedCodeInfo. |
|
88 |
*/ |
|
89 |
inline TPagedCodeInfo& Info() |
|
90 |
{ return iInfo; } |
|
91 |
private: |
|
92 |
/** |
|
93 |
@copybrief TPagedCodeInfo |
|
94 |
*/ |
|
95 |
TPagedCodeInfo iInfo; |
|
96 |
}; |
|
97 |
||
98 |
||
99 |
DCodePagedMemoryManager DCodePagedMemoryManager::TheManager; |
|
100 |
DPagedMemoryManager* TheCodePagedMemoryManager = &DCodePagedMemoryManager::TheManager; |
|
101 |
||
102 |
||
103 |
DPagingDevice* CodePagingDevice(TInt aDriveNum) |
|
104 |
{ |
|
105 |
__NK_ASSERT_DEBUG(aDriveNum<KMaxLocalDrives); |
|
106 |
return DCodePagedMemoryManager::TheManager.iDevice[aDriveNum]; |
|
107 |
} |
|
108 |
||
109 |
||
110 |
void DCodePagedMemoryManager::Init3() |
|
111 |
{ |
|
112 |
TRACEB(("DCodePagedMemoryManager::Init3()")); |
|
113 |
} |
|
114 |
||
115 |
||
116 |
TInt DCodePagedMemoryManager::InstallPagingDevice(DPagingDevice* aDevice) |
|
117 |
{ |
|
118 |
TRACEB(("DCodePagedMemoryManager::InstallPagingDevice(0x%08x)",aDevice)); |
|
119 |
||
120 |
TUint codePolicy = TheSuperPage().KernelConfigFlags() & EKernelConfigCodePagingPolicyMask; |
|
121 |
TRACEB(("Code Paging Policy = %d", codePolicy >> EKernelConfigCodePagingPolicyShift)); |
|
122 |
if(codePolicy == EKernelConfigCodePagingPolicyNoPaging) |
|
123 |
{ |
|
124 |
// no paging allowed so end now... |
|
125 |
return KErrNone; |
|
126 |
} |
|
127 |
||
128 |
TInt i; |
|
129 |
for(i=0; i<KMaxLocalDrives; ++i) |
|
130 |
if(aDevice->iDrivesSupported&(1<<i)) |
|
131 |
{ |
|
132 |
TRACEB(("DCodePagedMemoryManager::InstallPagingDevice drive=%d",i)); |
|
133 |
TAny* null = 0; |
|
134 |
if(!__e32_atomic_cas_ord_ptr(&iDevice[i], &null, aDevice)) // set iDevice[i]=aDevice if it was originally 0 |
|
135 |
{ |
|
136 |
// paging device already registered... |
|
137 |
TRACEB(("DCodePagedMemoryManager::InstallPagingDevice returns ALREADY EXISTS!")); |
|
138 |
return KErrAlreadyExists; |
|
139 |
} |
|
140 |
// flag code paging is supported... |
|
141 |
__e32_atomic_ior_ord32(&K::MemModelAttributes, (TUint32)EMemModelAttrCodePaging); |
|
142 |
} |
|
143 |
||
144 |
return KErrNone; |
|
145 |
} |
|
146 |
||
147 |
||
148 |
TInt DCodePagedMemoryManager::AcquirePageReadRequest(DPageReadRequest*& aRequest, DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
149 |
{ |
|
150 |
DPagingDevice* device = 0; |
|
151 |
MmuLock::Lock(); |
|
152 |
DPagedCodeInfo* pagedCodeInfo = (DPagedCodeInfo*)aMemory->iManagerData; |
|
153 |
if(pagedCodeInfo) |
|
154 |
{ |
|
155 |
TPagedCodeInfo& info = pagedCodeInfo->Info(); |
|
156 |
device = iDevice[info.iCodeLocalDrive]; |
|
157 |
} |
|
158 |
MmuLock::Unlock(); |
|
159 |
||
160 |
if(!device) |
|
161 |
{ |
|
162 |
aRequest = 0; |
|
163 |
return KErrNotFound; |
|
164 |
} |
|
165 |
||
166 |
aRequest = device->iRequestPool->AcquirePageReadRequest(aMemory,aIndex,aCount); |
|
167 |
return KErrNone; |
|
168 |
} |
|
169 |
||
170 |
||
171 |
TInt DCodePagedMemoryManager::New(DMemoryObject*& aMemory, TUint aSizeInPages, TMemoryAttributes aAttributes, TMemoryCreateFlags aCreateFlags) |
|
172 |
{ |
|
173 |
DPagedCodeInfo* pagedCodeInfo = new DPagedCodeInfo; |
|
174 |
if(!pagedCodeInfo) |
|
175 |
return KErrNoMemory; |
|
176 |
||
177 |
TInt r = DPagedMemoryManager::New(aMemory, aSizeInPages, aAttributes, aCreateFlags); |
|
178 |
if(r!=KErrNone) |
|
179 |
pagedCodeInfo->Close(); |
|
180 |
else |
|
181 |
aMemory->iManagerData = pagedCodeInfo; |
|
182 |
||
183 |
return r; |
|
184 |
} |
|
185 |
||
186 |
||
187 |
void DCodePagedMemoryManager::Destruct(DMemoryObject* aMemory) |
|
188 |
{ |
|
189 |
MmuLock::Lock(); |
|
190 |
DPagedCodeInfo* pagedCodeInfo = (DPagedCodeInfo*)aMemory->iManagerData; |
|
191 |
aMemory->iManagerData = 0; |
|
192 |
MmuLock::Unlock(); |
|
193 |
||
194 |
if(pagedCodeInfo) |
|
195 |
pagedCodeInfo->Close(); |
|
196 |
||
197 |
// base call to free memory and close object... |
|
198 |
DPagedMemoryManager::Destruct(aMemory); |
|
199 |
} |
|
200 |
||
201 |
||
202 |
void DCodePagedMemoryManager::Free(DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
203 |
{ |
|
204 |
DoFree(aMemory,aIndex,aCount); |
|
205 |
} |
|
206 |
||
207 |
||
208 |
TInt DCodePagedMemoryManager::CleanPage(DMemoryObject* aMemory, SPageInfo* aPageInfo, TPhysAddr*& aPageArrayEntry) |
|
209 |
{ |
|
87
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
210 |
if(!aPageInfo->IsDirty()) |
0 | 211 |
return KErrNone; |
212 |
||
213 |
// shouldn't be asked to clean a page which is writable... |
|
87
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
214 |
__NK_ASSERT_DEBUG(!aPageInfo->IsWritable()); |
0 | 215 |
|
216 |
// Note, memory may have been modified by the CodeModifier class. |
|
217 |
||
218 |
// just mark page as clean as we don't try and preserve code modifications... |
|
219 |
ThePager.SetClean(*aPageInfo); |
|
220 |
||
221 |
return KErrNone; |
|
222 |
} |
|
223 |
||
224 |
||
225 |
TInt ReadFunc(TAny* aArg1, TAny* aArg2, TLinAddr aBuffer, TInt aBlockNumber, TInt aBlockCount) |
|
226 |
{ |
|
227 |
START_PAGING_BENCHMARK; |
|
228 |
TInt drive = (TInt)aArg1; |
|
229 |
TThreadMessage* msg = (TThreadMessage*)aArg2; |
|
230 |
DPagingDevice* device = CodePagingDevice(drive); |
|
231 |
TInt r = device->Read(msg, aBuffer, aBlockNumber, aBlockCount, drive); |
|
232 |
__NK_ASSERT_DEBUG(r!=KErrNoMemory); // not allowed to allocated memory, therefore can't fail with KErrNoMemory |
|
233 |
END_PAGING_BENCHMARK(EPagingBmReadMedia); |
|
234 |
return r; |
|
235 |
} |
|
236 |
||
237 |
||
238 |
TInt DCodePagedMemoryManager::ReadPages(DMemoryObject* aMemory, TUint aIndex, TUint aCount, TPhysAddr* aPages, DPageReadRequest* aRequest) |
|
239 |
{ |
|
240 |
TRACE2(("DCodePagedMemoryManager::ReadPage(0x%08x,0x%08x,0x%08x,?,?)",aMemory,aIndex,aCount)); |
|
241 |
||
110
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
242 |
__NK_ASSERT_DEBUG(aRequest->CheckUseContiguous(aMemory,aIndex,aCount)); |
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
243 |
__ASSERT_CRITICAL; |
0 | 244 |
|
245 |
START_PAGING_BENCHMARK; |
|
246 |
||
247 |
MmuLock::Lock(); |
|
248 |
DPagedCodeInfo* pagedCodeInfo = (DPagedCodeInfo*)aMemory->iManagerData; |
|
249 |
if(pagedCodeInfo) |
|
250 |
pagedCodeInfo->Open(); |
|
251 |
MmuLock::Unlock(); |
|
252 |
if(!pagedCodeInfo) |
|
253 |
return KErrNotFound; |
|
254 |
||
255 |
TPagedCodeInfo& info = pagedCodeInfo->Info(); |
|
256 |
DPagingDevice& device = *iDevice[info.iCodeLocalDrive]; |
|
257 |
||
258 |
TLinAddr linAddr = aRequest->MapPages(aIndex,aCount,aPages); |
|
259 |
TInt r = KErrNone; |
|
110
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
260 |
TThreadMessage message; |
0 | 261 |
|
262 |
if(!info.iCodeSize) |
|
263 |
{ |
|
264 |
// no blockmap yet, use blank pages... |
|
265 |
memset((TAny*)linAddr, aCount*KPageSize, 0x03); |
|
266 |
CacheMaintenance::CodeChanged(linAddr, aCount*KPageSize); |
|
267 |
goto done; |
|
268 |
} |
|
269 |
||
270 |
for(; aCount; ++aIndex, --aCount, linAddr+=KPageSize) |
|
271 |
{ |
|
272 |
// work out which bit of the file to read |
|
273 |
TInt codeOffset = aIndex<<KPageShift; |
|
274 |
TInt dataOffset; |
|
275 |
TInt dataSize; |
|
276 |
TInt decompressedSize = Min(KPageSize, info.iCodeSize-codeOffset); |
|
277 |
if(info.iCompressionType) |
|
278 |
{ |
|
279 |
dataOffset = info.iCodePageOffsets[aIndex]; |
|
280 |
dataSize = info.iCodePageOffsets[aIndex+1] - dataOffset; |
|
281 |
__KTRACE_OPT(KPAGING,Kern::Printf(" compressed, file offset == %x, size == %d", dataOffset, dataSize)); |
|
282 |
} |
|
283 |
else |
|
284 |
{ |
|
285 |
dataOffset = codeOffset + info.iCodeStartInFile; |
|
286 |
dataSize = Min(KPageSize, info.iBlockMap.DataLength()-dataOffset); |
|
287 |
__NK_ASSERT_DEBUG(dataSize==decompressedSize); |
|
288 |
__KTRACE_OPT(KPAGING,Kern::Printf(" uncompressed, file offset == %x, size == %d", dataOffset, dataSize)); |
|
289 |
} |
|
290 |
||
117
5b5d147c7838
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
110
diff
changeset
|
291 |
TInt bufferStart = info.iBlockMap.Read(aRequest->Buffer(), |
0 | 292 |
dataOffset, |
293 |
dataSize, |
|
294 |
device.iReadUnitShift, |
|
295 |
ReadFunc, |
|
296 |
(TAny*)info.iCodeLocalDrive, |
|
110
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
297 |
(TAny*)&message); |
0 | 298 |
|
257 | 299 |
if(bufferStart<0) |
300 |
{ |
|
301 |
r = bufferStart; // return error |
|
302 |
__NK_ASSERT_DEBUG(0); |
|
0 | 303 |
break; |
257 | 304 |
} |
0 | 305 |
|
117
5b5d147c7838
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
110
diff
changeset
|
306 |
TLinAddr data = aRequest->Buffer() + bufferStart; |
0 | 307 |
r = Decompress(info.iCompressionType, linAddr, decompressedSize, data, dataSize); |
257 | 308 |
if(r>=0) |
309 |
{ |
|
310 |
if(r!=decompressedSize) |
|
311 |
{ |
|
312 |
__KTRACE_OPT(KPANIC, Kern::Printf("DCodePagedMemoryManager::ReadPage: error decompressing page at %08x + %x: %d", dataOffset, dataSize, r)); |
|
313 |
__NK_ASSERT_DEBUG(0); |
|
314 |
r = KErrCorrupt; |
|
315 |
} |
|
316 |
else |
|
317 |
r = KErrNone; |
|
318 |
} |
|
319 |
else |
|
320 |
{ |
|
321 |
__NK_ASSERT_DEBUG(0); |
|
322 |
} |
|
323 |
||
0 | 324 |
if(r!=KErrNone) |
325 |
break; |
|
326 |
||
327 |
if(decompressedSize<KPageSize) |
|
328 |
memset((TAny*)(linAddr+decompressedSize), KPageSize-decompressedSize, 0x03); |
|
329 |
if(info.iLoaded) |
|
330 |
info.ApplyFixups(linAddr, aIndex); |
|
331 |
} |
|
332 |
done: |
|
333 |
aRequest->UnmapPages(true); |
|
334 |
||
335 |
pagedCodeInfo->AsyncClose(); |
|
336 |
||
110
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
87
diff
changeset
|
337 |
END_PAGING_BENCHMARK_N(EPagingBmReadCodePage, aCount); |
0 | 338 |
return r; |
339 |
} |
|
340 |
||
341 |
||
342 |
TBool DCodePagedMemoryManager::IsAllocated(DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
343 |
{ |
|
344 |
// all pages allocated if memory not destroyed (iManagerData!=0)... |
|
345 |
return aMemory->iManagerData!=0; |
|
346 |
} |
|
347 |
||
348 |
||
349 |
TInt MM::PagedCodeNew(DMemoryObject*& aMemory, TUint aPageCount, TPagedCodeInfo*& aInfo) |
|
350 |
{ |
|
351 |
TRACE(("MM::PagedCodeNew(?,0x%08x,0x%08x)",aPageCount,aInfo)); |
|
352 |
TMemoryCreateFlags createFlags = (TMemoryCreateFlags)(EMemoryCreateNoWipe | EMemoryCreateAllowExecution); |
|
353 |
TInt r = TheCodePagedMemoryManager->New(aMemory,aPageCount,EMemoryAttributeStandard,createFlags); |
|
354 |
if(r==KErrNone) |
|
355 |
aInfo = &((DPagedCodeInfo*)aMemory->iManagerData)->Info(); |
|
356 |
TRACE(("MM::PagedCodeNew returns %d, aMemory=0x%08x",r,aMemory)); |
|
357 |
return r; |
|
358 |
} |
|
359 |
||
360 |
||
361 |
void MM::PagedCodeLoaded(DMemoryObject* aMemory, TLinAddr aLoadAddress) |
|
362 |
{ |
|
363 |
TRACE(("MM::PagedCodeLoaded(0x%08x,0x%08x)",aMemory,aLoadAddress)); |
|
364 |
||
365 |
TPagedCodeInfo& info = ((DPagedCodeInfo*)aMemory->iManagerData)->Info(); |
|
366 |
||
367 |
// we need to apply fixups for all memory already paged in. |
|
368 |
// Note, if this memory is subsequently discarded it should not be paged-in again |
|
369 |
// until after this function has completed, because the Loader won't touch the memory |
|
370 |
// and it has not yet been mapped into any other process. |
|
371 |
||
372 |
// make iterator for memory... |
|
373 |
RPageArray::TIter pageIter; |
|
374 |
aMemory->iPages.FindStart(0,aMemory->iSizeInPages,pageIter); |
|
375 |
||
376 |
for(;;) |
|
377 |
{ |
|
378 |
// find some pages... |
|
379 |
RPageArray::TIter pageList; |
|
380 |
TUint n = pageIter.Find(pageList); |
|
381 |
if(!n) |
|
382 |
break; |
|
383 |
||
384 |
// fix up each page found... |
|
385 |
UNLOCK_USER_MEMORY(); |
|
386 |
do |
|
387 |
{ |
|
388 |
TUint i = pageList.Index(); |
|
389 |
TLinAddr a = aLoadAddress+i*KPageSize; |
|
390 |
info.ApplyFixups(a,i); |
|
391 |
CacheMaintenance::CodeChanged(a, KPageSize); |
|
392 |
// now we've finished updating the page, mark it as read only and |
|
393 |
// clean as we don't need to save changes if it is stolen. |
|
394 |
MmuLock::Lock(); |
|
395 |
TPhysAddr* pages; |
|
396 |
if(pageList.Pages(pages,1)==1) |
|
397 |
if(RPageArray::IsPresent(*pages)) |
|
398 |
{// The loader page still has a writable mapping but it won't |
|
399 |
// touch the page again so this is safe. No use restricting the |
|
400 |
// page to be read only as if the loader did write to it again |
|
401 |
// it would just be rejuvenated as writeable and made dirty. |
|
402 |
SPageInfo& pageInfo = *SPageInfo::FromPhysAddr(*pages); |
|
403 |
pageInfo.SetReadOnly(); |
|
404 |
ThePager.SetClean(pageInfo); |
|
405 |
} |
|
406 |
MmuLock::Unlock(); |
|
407 |
||
408 |
pageList.Skip(1); |
|
409 |
} |
|
410 |
while(pageList.Count()); |
|
411 |
LOCK_USER_MEMORY(); |
|
412 |
||
413 |
// move on... |
|
414 |
pageIter.FindRelease(n); |
|
415 |
} |
|
416 |
||
417 |
// done... |
|
418 |
aMemory->iPages.FindEnd(0,aMemory->iSizeInPages); |
|
419 |
info.iLoaded = true; // allow ReadPage to start applying fixups when handling page faults |
|
420 |
} |