author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Wed, 31 Mar 2010 23:38:45 +0300 | |
branch | RCL_3 |
changeset 87 | 2f92ad2dc5db |
parent 81 | e7d2d738d3c2 |
child 256 | c1f20ce4abcf |
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 "memmodel.h" |
|
17 |
#include "mm.h" |
|
18 |
#include "mmu.h" |
|
19 |
#include "mobject.h" |
|
20 |
#include "mmapping.h" |
|
21 |
#include "mmanager.h" |
|
22 |
#include "mpdalloc.h" |
|
23 |
#include "mptalloc.h" |
|
24 |
#include "mpager.h" |
|
25 |
#include "maddressspace.h" |
|
26 |
||
27 |
||
28 |
||
29 |
||
30 |
// |
|
31 |
// DMutexPool |
|
32 |
// |
|
33 |
||
34 |
DMutexPool::~DMutexPool() |
|
35 |
{ |
|
36 |
TUint i; |
|
37 |
for(i=0; i<iCount; ++i) |
|
38 |
{ |
|
39 |
DMutex* mutex = iMembers[i].iMutex; |
|
40 |
if(mutex) |
|
41 |
mutex->Close(0); |
|
42 |
} |
|
43 |
Kern::Free(iMembers); |
|
44 |
} |
|
45 |
||
46 |
||
47 |
TInt DMutexPool::Create(TUint aCount, const TDesC* aName, TUint aOrder) |
|
48 |
{ |
|
49 |
if(aCount>EMaxPoolSize) |
|
50 |
return KErrTooBig; |
|
51 |
||
52 |
iMembers = (SMember*)Kern::AllocZ(aCount*sizeof(SMember)); |
|
53 |
if(!iMembers) |
|
54 |
return KErrNoMemory; |
|
55 |
||
56 |
iCount = aCount; |
|
57 |
||
58 |
TInt r = KErrNone; |
|
59 |
TUint i; |
|
60 |
for(i=0; i<aCount; ++i) |
|
61 |
{ |
|
62 |
TKName name; |
|
63 |
if(aName) |
|
64 |
{ |
|
65 |
name = *aName; |
|
66 |
name.AppendNum(i); |
|
67 |
} |
|
68 |
K::MutexCreate(iMembers[i].iMutex, name, NULL, EFalse, aOrder); |
|
69 |
if(r!=KErrNone) |
|
70 |
break; |
|
71 |
} |
|
72 |
||
73 |
return r; |
|
74 |
} |
|
75 |
||
76 |
||
77 |
/** |
|
78 |
@class DMutexPool |
|
79 |
@details |
|
80 |
||
81 |
The cookie used for dynamically assigned mutexes is broken into three bit fields: |
|
82 |
- Bit 0, always set. (To distinguish the cookie from a proper DMutex*). |
|
83 |
- Bits 1 through #KMutexPoolIndexBits, these contain the index of the assigned |
|
84 |
mutex within DMutexPool::iMembers. |
|
85 |
- Bits (#KMutexPoolIndexBits+1) through 31, the count of the number of threads waiting |
|
86 |
for this particular mutex assignment. When this reaches zero, the mutex can |
|
87 |
be unassigned. |
|
88 |
*/ |
|
89 |
||
90 |
/** |
|
91 |
Number of bits used to contain the index value of a dynamically assigned pool mutex. |
|
92 |
*/ |
|
93 |
const TUint KMutexPoolIndexBits = 7; |
|
94 |
||
95 |
const TUint KMutexPoolIndexMask = ((1<<KMutexPoolIndexBits)-1)<<1; |
|
96 |
const TUint KMutexPoolWaitCountIncrement = 1<<(KMutexPoolIndexBits+1); |
|
97 |
||
98 |
__ASSERT_COMPILE(DMutexPool::EMaxPoolSize<=TUint(KMutexPoolIndexMask/2+1)); // required for algorithm correctness |
|
99 |
||
100 |
__ASSERT_COMPILE(DMutexPool::EMaxPoolSize<=64); // required to avoid excessive system lock hold time |
|
101 |
||
102 |
||
103 |
void DMutexPool::Wait(DMutex*& aMutexRef) |
|
104 |
{ |
|
105 |
NKern::LockSystem(); |
|
106 |
||
107 |
TUintPtr poolMutex = (TUintPtr)aMutexRef; |
|
108 |
if(!poolMutex) |
|
109 |
{ |
|
110 |
// try and find a free mutex, else use the next one... |
|
111 |
TUint next = iNext; |
|
112 |
do |
|
113 |
{ |
|
114 |
if(iMembers[next].iUseCount==0) |
|
115 |
break; |
|
116 |
if(++next>=iCount) |
|
117 |
next = 0; |
|
118 |
} |
|
119 |
while(next!=iNext); |
|
120 |
// use found mutex... |
|
121 |
++iMembers[next].iUseCount; |
|
122 |
poolMutex = (next*2)+1; // mutex index*2 | 1 |
|
123 |
// update next... |
|
124 |
if(++next>=iCount) |
|
125 |
next = 0; |
|
126 |
iNext = next; |
|
127 |
} |
|
128 |
||
129 |
DMutex* mutex = (DMutex*)poolMutex; |
|
130 |
if(poolMutex&1) |
|
131 |
{ |
|
132 |
// mutex is a pool mutex, get pointer, and update wait count... |
|
133 |
SMember* member = &iMembers[(poolMutex&KMutexPoolIndexMask)>>1]; |
|
134 |
mutex = member->iMutex; |
|
135 |
poolMutex += KMutexPoolWaitCountIncrement; |
|
136 |
__NK_ASSERT_ALWAYS(poolMutex>=KMutexPoolWaitCountIncrement); |
|
137 |
aMutexRef = (DMutex*)poolMutex; |
|
138 |
} |
|
139 |
||
140 |
mutex->Wait(); |
|
141 |
||
142 |
NKern::UnlockSystem(); |
|
143 |
} |
|
144 |
||
145 |
||
146 |
void DMutexPool::Signal(DMutex*& aMutexRef) |
|
147 |
{ |
|
148 |
NKern::LockSystem(); |
|
149 |
||
150 |
TUintPtr poolMutex = (TUintPtr)aMutexRef; |
|
151 |
__NK_ASSERT_ALWAYS(poolMutex); |
|
152 |
||
153 |
DMutex* mutex = (DMutex*)poolMutex; |
|
154 |
||
155 |
if(poolMutex&1) |
|
156 |
{ |
|
157 |
// mutex is a pool mutex, get pointer, and update wait count... |
|
158 |
SMember* member = &iMembers[(poolMutex&KMutexPoolIndexMask)>>1]; |
|
159 |
mutex = member->iMutex; |
|
160 |
__NK_ASSERT_ALWAYS(poolMutex>=KMutexPoolWaitCountIncrement); |
|
161 |
poolMutex -= KMutexPoolWaitCountIncrement; |
|
162 |
if(poolMutex<KMutexPoolWaitCountIncrement) |
|
163 |
{ |
|
164 |
--member->iUseCount; |
|
165 |
poolMutex = 0; |
|
166 |
} |
|
167 |
aMutexRef = (DMutex*)poolMutex; |
|
168 |
} |
|
169 |
||
170 |
mutex->Signal(); |
|
171 |
} |
|
172 |
||
173 |
||
174 |
TBool DMutexPool::IsHeld(DMutex*& aMutexRef) |
|
175 |
{ |
|
176 |
TBool held = false; |
|
177 |
NKern::LockSystem(); |
|
178 |
TUintPtr poolMutex = (TUintPtr)aMutexRef; |
|
179 |
if(poolMutex) |
|
180 |
{ |
|
181 |
DMutex* mutex = (DMutex*)poolMutex; |
|
182 |
if(poolMutex&1) |
|
183 |
{ |
|
184 |
SMember* member = &iMembers[(poolMutex&KMutexPoolIndexMask)>>1]; |
|
185 |
mutex = member->iMutex; |
|
186 |
} |
|
187 |
held = mutex->iCleanup.iThread==&Kern::CurrentThread(); |
|
188 |
} |
|
189 |
NKern::UnlockSystem(); |
|
190 |
return held; |
|
191 |
} |
|
192 |
||
193 |
||
194 |
||
195 |
// |
|
196 |
// DReferenceCountedObject |
|
197 |
// |
|
198 |
||
199 |
DReferenceCountedObject::~DReferenceCountedObject() |
|
200 |
{ |
|
201 |
__NK_ASSERT_DEBUG(iReferenceCount==0); |
|
202 |
} |
|
203 |
||
204 |
||
205 |
void DReferenceCountedObject::Open() |
|
206 |
{ |
|
81
e7d2d738d3c2
Revision: 201010
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
80
diff
changeset
|
207 |
CHECK_PRECONDITIONS(MASK_NO_KILL_OR_SUSPEND, "DReferenceCountedObject::Open"); |
e7d2d738d3c2
Revision: 201010
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
80
diff
changeset
|
208 |
TInt orig = __e32_atomic_tas_ord32(&iReferenceCount, 1, 1, 0); |
e7d2d738d3c2
Revision: 201010
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
80
diff
changeset
|
209 |
if (orig <= 0) |
e7d2d738d3c2
Revision: 201010
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
80
diff
changeset
|
210 |
__crash(); |
0 | 211 |
} |
212 |
||
213 |
||
214 |
TBool DReferenceCountedObject::TryOpen() |
|
215 |
{ |
|
81
e7d2d738d3c2
Revision: 201010
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
80
diff
changeset
|
216 |
CHECK_PRECONDITIONS(MASK_NO_KILL_OR_SUSPEND, "DReferenceCountedObject::Open"); |
e7d2d738d3c2
Revision: 201010
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
80
diff
changeset
|
217 |
TInt orig = __e32_atomic_tas_ord32(&iReferenceCount, 1, 1, 0); |
e7d2d738d3c2
Revision: 201010
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
80
diff
changeset
|
218 |
return (orig>0); |
0 | 219 |
} |
220 |
||
221 |
||
222 |
TBool DReferenceCountedObject::CheckCloseIsSafe() |
|
223 |
{ |
|
224 |
__ASSERT_CRITICAL |
|
225 |
#ifdef _DEBUG |
|
226 |
NFastMutex* fm = NKern::HeldFastMutex(); |
|
227 |
if(fm) |
|
228 |
{ |
|
229 |
Kern::Printf("DReferenceCountedObject[0x%08x]::Close() fast mutex violation %M",this,fm); |
|
230 |
return false; |
|
231 |
} |
|
232 |
SDblQue& ml = TheCurrentThread->iMutexList; |
|
233 |
if(!ml.IsEmpty()) |
|
234 |
{ |
|
235 |
DMutex* m = _LOFF(ml.First(), DMutex, iOrderLink); |
|
236 |
if(m->iOrder<KMutexOrdKernelHeap) |
|
237 |
{ |
|
238 |
Kern::Printf("DReferenceCountedObject[0x%08x]::Close() mutex order violation holding mutex %O",this,m); |
|
239 |
return false; |
|
240 |
} |
|
241 |
} |
|
242 |
#endif |
|
243 |
return true; |
|
244 |
} |
|
245 |
||
246 |
||
247 |
void DReferenceCountedObject::Close() |
|
248 |
{ |
|
249 |
__NK_ASSERT_DEBUG(CheckCloseIsSafe()); |
|
81
e7d2d738d3c2
Revision: 201010
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
80
diff
changeset
|
250 |
TInt orig = __e32_atomic_tas_ord32(&iReferenceCount, 1, -1, 0); |
e7d2d738d3c2
Revision: 201010
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
80
diff
changeset
|
251 |
if (orig == 1) |
0 | 252 |
delete this; |
81
e7d2d738d3c2
Revision: 201010
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
80
diff
changeset
|
253 |
else if (orig <= 0) |
e7d2d738d3c2
Revision: 201010
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
80
diff
changeset
|
254 |
__crash(); |
0 | 255 |
} |
256 |
||
257 |
||
258 |
void DReferenceCountedObject::AsyncClose() |
|
259 |
{ |
|
81
e7d2d738d3c2
Revision: 201010
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
80
diff
changeset
|
260 |
CHECK_PRECONDITIONS(MASK_NO_KILL_OR_SUSPEND, "DReferenceCountedObject::AsyncClose"); |
e7d2d738d3c2
Revision: 201010
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
80
diff
changeset
|
261 |
TInt orig = __e32_atomic_tas_ord32(&iReferenceCount, 1, -1, 0); |
e7d2d738d3c2
Revision: 201010
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
80
diff
changeset
|
262 |
if (orig == 1) |
0 | 263 |
AsyncDelete(); |
81
e7d2d738d3c2
Revision: 201010
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
80
diff
changeset
|
264 |
else if (orig <= 0) |
e7d2d738d3c2
Revision: 201010
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
80
diff
changeset
|
265 |
__crash(); |
0 | 266 |
} |
267 |
||
268 |
||
269 |
// |
|
270 |
// Memory object functions |
|
271 |
// |
|
272 |
||
273 |
TInt MM::MemoryNew(DMemoryObject*& aMemory, TMemoryObjectType aType, TUint aPageCount, TMemoryCreateFlags aCreateFlags, TMemoryAttributes aAttributes) |
|
274 |
{ |
|
275 |
TRACE(("MM::MemoryNew(?,0x%08x,0x%08x,0x%08x,0x%08x)",aType,aPageCount,aCreateFlags,*(TUint32*)&aAttributes)); |
|
276 |
||
277 |
DMemoryManager* manager; |
|
278 |
if(aCreateFlags&EMemoryCreateCustomManager) |
|
279 |
manager = (DMemoryManager*)aType; |
|
280 |
else |
|
281 |
{ |
|
282 |
switch(aType) |
|
283 |
{ |
|
284 |
case EMemoryObjectUnpaged: |
|
285 |
manager = TheUnpagedMemoryManager; |
|
286 |
break; |
|
287 |
case EMemoryObjectMovable: |
|
288 |
manager = TheMovableMemoryManager; |
|
289 |
break; |
|
290 |
case EMemoryObjectPaged: |
|
291 |
manager = TheDataPagedMemoryManager; |
|
292 |
break; |
|
293 |
case EMemoryObjectDiscardable: |
|
294 |
manager = TheDiscardableMemoryManager; |
|
295 |
break; |
|
296 |
case EMemoryObjectHardware: |
|
297 |
manager = TheHardwareMemoryManager; |
|
298 |
break; |
|
299 |
default: |
|
300 |
manager = 0; |
|
301 |
__NK_ASSERT_DEBUG(0); |
|
302 |
break; |
|
303 |
} |
|
304 |
} |
|
305 |
TMemoryCreateFlags flags = (TMemoryCreateFlags)(aCreateFlags&~(EMemoryCreateDemandPaged)); |
|
306 |
TInt r = manager->New(aMemory,aPageCount,aAttributes,flags); |
|
307 |
TRACE(("MM::MemoryNew returns %d, aMemory=0x%08x",r,aMemory)); |
|
308 |
#ifdef BTRACE_FLEXIBLE_MEM_MODEL |
|
309 |
if (r == KErrNone) |
|
310 |
aMemory->BTraceCreate(); |
|
311 |
#endif |
|
312 |
return r; |
|
313 |
} |
|
314 |
||
315 |
||
316 |
TInt MM::MemoryClaimInitialPages(DMemoryObject* aMemory, TLinAddr aBase, TUint aSize, TMappingPermissions aPermissions, TBool aAllowGaps, TBool aAllowNonRamPages) |
|
317 |
{ |
|
318 |
TRACE(("MM::MemoryClaimInitialPages(0x%08x,0x%08x,0x%08x,0x%08x,%d,%d)",aMemory,aBase,aPermissions,aSize,aAllowGaps!=0,aAllowNonRamPages!=0)); |
|
319 |
TInt r = aMemory->ClaimInitialPages(aBase,aSize,aPermissions,aAllowGaps,aAllowNonRamPages); |
|
320 |
TRACE(("MM::MemoryClaimInitialPages returns %d",r)); |
|
321 |
__NK_ASSERT_DEBUG(r==KErrNone); |
|
322 |
return r; |
|
323 |
} |
|
324 |
||
325 |
||
326 |
void MM::MemorySetLock(DMemoryObject* aMemory, DMutex* aLock) |
|
327 |
{ |
|
328 |
aMemory->SetLock(aLock); |
|
329 |
} |
|
330 |
||
331 |
||
332 |
void MM::MemoryLock(DMemoryObject* aMemory) |
|
333 |
{ |
|
334 |
MemoryObjectLock::Lock(aMemory); |
|
335 |
} |
|
336 |
||
337 |
||
338 |
void MM::MemoryUnlock(DMemoryObject* aMemory) |
|
339 |
{ |
|
340 |
MemoryObjectLock::Unlock(aMemory); |
|
341 |
} |
|
342 |
||
343 |
||
344 |
void MM::MemoryDestroy(DMemoryObject*& aMemory) |
|
345 |
{ |
|
346 |
DMemoryObject* memory = (DMemoryObject*)__e32_atomic_swp_ord_ptr(&aMemory, 0); |
|
347 |
if (!memory) |
|
348 |
return; |
|
349 |
TRACE(("MM::MemoryDestroy(0x%08x)",memory)); |
|
350 |
#ifdef BTRACE_FLEXIBLE_MEM_MODEL |
|
351 |
BTraceContext4(BTrace::EFlexibleMemModel,BTrace::EMemoryObjectDestroy,memory); |
|
352 |
#endif |
|
353 |
memory->iManager->Destruct(memory); |
|
354 |
} |
|
355 |
||
356 |
||
357 |
TInt MM::MemoryAlloc(DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
358 |
{ |
|
359 |
TRACE(("MM::MemoryAlloc(0x%08x,0x%08x,0x%08x)",aMemory,aIndex,aCount)); |
|
360 |
MemoryObjectLock::Lock(aMemory); |
|
361 |
TInt r; |
|
362 |
if(!aMemory->CheckRegion(aIndex,aCount)) |
|
363 |
r = KErrArgument; |
|
364 |
else |
|
365 |
r = aMemory->iManager->Alloc(aMemory,aIndex,aCount); |
|
366 |
MemoryObjectLock::Unlock(aMemory); |
|
367 |
TRACE(("MM::MemoryAlloc returns %d",r)); |
|
368 |
return r; |
|
369 |
} |
|
370 |
||
371 |
||
372 |
TInt MM::MemoryAllocContiguous(DMemoryObject* aMemory, TUint aIndex, TUint aCount, TUint aAlign, TPhysAddr& aPhysAddr) |
|
373 |
{ |
|
374 |
TRACE(("MM::MemoryAllocContiguous(0x%08x,0x%08x,0x%08x,%d,?)",aMemory,aIndex,aCount,aAlign)); |
|
375 |
MemoryObjectLock::Lock(aMemory); |
|
376 |
TInt r; |
|
377 |
if(!aMemory->CheckRegion(aIndex,aCount)) |
|
378 |
r = KErrArgument; |
|
379 |
else |
|
380 |
r = aMemory->iManager->AllocContiguous(aMemory,aIndex,aCount,MM::RoundToPageShift(aAlign),aPhysAddr); |
|
381 |
MemoryObjectLock::Unlock(aMemory); |
|
382 |
TRACE(("MM::MemoryAlloc returns %d (aPhysAddr=0x%08x)",r,aPhysAddr)); |
|
383 |
return r; |
|
384 |
} |
|
385 |
||
386 |
||
387 |
void MM::MemoryFree(DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
388 |
{ |
|
389 |
TRACE(("MM::MemoryFree(0x%08x,0x%08x,0x%08x)",aMemory,aIndex,aCount)); |
|
390 |
MemoryObjectLock::Lock(aMemory); |
|
391 |
aMemory->ClipRegion(aIndex,aCount); |
|
392 |
aMemory->iManager->Free(aMemory,aIndex,aCount); |
|
393 |
MemoryObjectLock::Unlock(aMemory); |
|
394 |
} |
|
395 |
||
396 |
||
87
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
81
diff
changeset
|
397 |
TInt MM::MemoryAddPages(DMemoryObject* aMemory, TUint aIndex, TUint aCount, const TPhysAddr* aPages) |
0 | 398 |
{ |
399 |
TRACE(("MM::MemoryAddPages(0x%08x,0x%08x,0x%08x,?)",aMemory,aIndex,aCount)); |
|
400 |
MemoryObjectLock::Lock(aMemory); |
|
401 |
TInt r; |
|
402 |
if(!aMemory->CheckRegion(aIndex,aCount)) |
|
403 |
r = KErrArgument; |
|
404 |
else |
|
405 |
r = aMemory->iManager->AddPages(aMemory,aIndex,aCount,aPages); |
|
406 |
MemoryObjectLock::Unlock(aMemory); |
|
407 |
TRACE(("MM::MemoryAddPages returns %d",r)); |
|
408 |
return r; |
|
409 |
} |
|
410 |
||
411 |
||
412 |
TInt MM::MemoryAddContiguous(DMemoryObject* aMemory, TUint aIndex, TUint aCount, TPhysAddr aPhysAddr) |
|
413 |
{ |
|
414 |
TRACE(("MM::MemoryAddContiguous(0x%08x,0x%08x,0x%08x,0x%08x)",aMemory,aIndex,aCount,aPhysAddr)); |
|
415 |
MemoryObjectLock::Lock(aMemory); |
|
416 |
TInt r; |
|
417 |
if(!aMemory->CheckRegion(aIndex,aCount)) |
|
418 |
r = KErrArgument; |
|
419 |
else |
|
420 |
r = aMemory->iManager->AddContiguous(aMemory,aIndex,aCount,aPhysAddr); |
|
421 |
MemoryObjectLock::Unlock(aMemory); |
|
422 |
TRACE(("MM::MemoryAddContiguous returns %d",r)); |
|
423 |
return r; |
|
424 |
} |
|
425 |
||
426 |
||
427 |
TUint MM::MemoryRemovePages(DMemoryObject* aMemory, TUint aIndex, TUint aCount, TPhysAddr* aPages) |
|
428 |
{ |
|
429 |
TRACE(("MM::MemoryRemovePages(0x%08x,0x%08x,0x%08x)",aMemory,aIndex,aCount)); |
|
430 |
MemoryObjectLock::Lock(aMemory); |
|
431 |
aMemory->ClipRegion(aIndex,aCount); |
|
432 |
TInt r = aMemory->iManager->RemovePages(aMemory,aIndex,aCount,aPages); |
|
433 |
if(r<0) |
|
434 |
r = 0; |
|
435 |
MemoryObjectLock::Unlock(aMemory); |
|
436 |
TRACE(("MM::MemoryRemovePages returns %d",r)); |
|
437 |
return r; |
|
438 |
} |
|
439 |
||
440 |
||
441 |
TInt MM::MemoryAllowDiscard(DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
442 |
{ |
|
443 |
TRACE(("MM::MemoryAllowDiscard(0x%08x,0x%08x,0x%08x)",aMemory,aIndex,aCount)); |
|
444 |
MemoryObjectLock::Lock(aMemory); |
|
445 |
TInt r; |
|
446 |
if(!aMemory->CheckRegion(aIndex,aCount)) |
|
447 |
r = KErrArgument; |
|
448 |
else |
|
449 |
r = aMemory->iManager->AllowDiscard(aMemory,aIndex,aCount); |
|
450 |
MemoryObjectLock::Unlock(aMemory); |
|
451 |
TRACE(("MM::MemoryAllowDiscard returns %d",r)); |
|
452 |
return r; |
|
453 |
} |
|
454 |
||
455 |
||
456 |
TInt MM::MemoryDisallowDiscard(DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
457 |
{ |
|
458 |
TRACE(("MM::MemoryDisallowDiscard(0x%08x,0x%08x,0x%08x)",aMemory,aIndex,aCount)); |
|
459 |
MemoryObjectLock::Lock(aMemory); |
|
460 |
TInt r; |
|
461 |
if(!aMemory->CheckRegion(aIndex,aCount)) |
|
462 |
r = KErrArgument; |
|
463 |
else |
|
464 |
r = aMemory->iManager->DisallowDiscard(aMemory,aIndex,aCount); |
|
465 |
MemoryObjectLock::Unlock(aMemory); |
|
466 |
TRACE(("MM::MemoryDisallowDiscard returns %d",r)); |
|
467 |
return r; |
|
468 |
} |
|
469 |
||
470 |
||
471 |
TInt MM::MemoryPhysAddr(DMemoryObject* aMemory, TUint aIndex, TUint aCount, TPhysAddr& aPhysicalAddress, TPhysAddr* aPhysicalPageList) |
|
472 |
{ |
|
473 |
TRACE(("MM::MemoryPhysAddr(0x%08x,0x%08x,0x%08x,?,?)",aMemory,aIndex,aCount)); |
|
474 |
TInt r = aMemory->PhysAddr(aIndex,aCount,aPhysicalAddress,aPhysicalPageList); |
|
475 |
TRACE(("MM::MemoryPhysAddr returns %d aPhysicalAddress=0x%08x",r,aPhysicalAddress)); |
|
476 |
return r; |
|
477 |
} |
|
478 |
||
479 |
||
480 |
void MM::MemoryBTracePrime(DMemoryObject* aMemory) |
|
481 |
{ |
|
482 |
aMemory->BTraceCreate(); |
|
483 |
aMemory->iMappings.Lock(); |
|
484 |
TMappingListIter iter; |
|
485 |
DMemoryMapping* mapping = (DMemoryMapping*)iter.Start(aMemory->iMappings); |
|
486 |
while(mapping) |
|
487 |
{ |
|
488 |
aMemory->iMappings.Unlock(); |
|
489 |
mapping->BTraceCreate(); |
|
490 |
aMemory->iMappings.Lock(); |
|
491 |
mapping = (DMemoryMapping*)iter.Next(); |
|
492 |
} |
|
493 |
iter.Finish(); |
|
494 |
aMemory->iMappings.Unlock(); |
|
495 |
} |
|
496 |
||
497 |
||
498 |
void MM::MemoryClose(DMemoryObject* aMemory) |
|
499 |
{ |
|
500 |
aMemory->Close(); |
|
501 |
} |
|
502 |
||
503 |
||
504 |
TBool MM::MemoryIsNotMapped(DMemoryObject* aMemory) |
|
505 |
{ |
|
506 |
TBool r = aMemory->iMappings.IsEmpty(); |
|
507 |
TRACE2(("MM::MemoryIsNotMapped(0x%08x) returns %d",aMemory,r)); |
|
508 |
return r; |
|
509 |
} |
|
510 |
||
511 |
// |
|
512 |
// Physical pinning |
|
513 |
// |
|
514 |
||
515 |
TInt MM::PinPhysicalMemory(DMemoryObject* aMemory, DPhysicalPinMapping* aPinObject, TUint aIndex, TUint aCount, TBool aReadOnly, TPhysAddr& aAddress, TPhysAddr* aPages, TUint32& aMapAttr, TUint& aColour) |
|
516 |
{ |
|
517 |
||
518 |
if (!aMemory->CheckRegion(aIndex,aCount)) |
|
519 |
return KErrArgument; |
|
520 |
||
521 |
TMappingPermissions permissions = aReadOnly ? ESupervisorReadOnly : ESupervisorReadWrite; |
|
522 |
TInt r = aPinObject->Pin(aMemory, aIndex, aCount, permissions); |
|
523 |
if (r == KErrNone) |
|
524 |
{ |
|
525 |
r = aPinObject->PhysAddr(aIndex, aCount, aAddress, aPages); |
|
526 |
if (r>=KErrNone) |
|
527 |
{ |
|
528 |
r = KErrNone; //Do not report discontigious memory in return value. |
|
529 |
const TMappingAttributes2& mapAttr2 = |
|
530 |
MM::LegacyMappingAttributes(aMemory->Attributes(), permissions); |
|
531 |
*(TMappingAttributes2*)&aMapAttr = mapAttr2; |
|
532 |
} |
|
533 |
else |
|
534 |
{ |
|
535 |
aPinObject->Unpin(); |
|
536 |
} |
|
537 |
} |
|
538 |
||
539 |
aColour = 0; |
|
540 |
return r; |
|
541 |
} |
|
542 |
||
543 |
||
544 |
TInt MM::MemoryWipe(DMemoryObject* aMemory) |
|
545 |
{ |
|
546 |
__NK_ASSERT_ALWAYS(aMemory->iMappings.IsEmpty()); // can't be mapped otherwise confidentiality can't be guaranteed |
|
547 |
TRACE2(("MM::MemoryWipe(0x%08x)",aMemory)); |
|
548 |
MemoryObjectLock::Lock(aMemory); |
|
549 |
TInt r = aMemory->iManager->Wipe(aMemory); |
|
550 |
MemoryObjectLock::Unlock(aMemory); |
|
551 |
return r; |
|
552 |
} |
|
553 |
||
554 |
||
555 |
TInt MM::MemorySetReadOnly(DMemoryObject* aMemory) |
|
556 |
{ |
|
557 |
TRACE2(("MM::MemorySetReadOnly(0x%08x)",aMemory)); |
|
558 |
MemoryObjectLock::Lock(aMemory); |
|
559 |
TInt r = aMemory->SetReadOnly(); |
|
560 |
MemoryObjectLock::Unlock(aMemory); |
|
561 |
return r; |
|
562 |
} |
|
563 |
||
564 |
// |
|
565 |
// Mapping functions |
|
566 |
// |
|
567 |
||
568 |
TInt MM::MappingNew(DMemoryMapping*& aMapping, DMemoryObject* aMemory, TMappingPermissions aPermissions, TInt aOsAsid, TMappingCreateFlags aFlags, TLinAddr aAddr, TUint aIndex, TUint aCount) |
|
569 |
{ |
|
570 |
TRACE(("MM::MappingNew(?,0x%08x,0x%08x,%d,0x%08x,0x%08x,0x%08x,0x%08x)",aMemory, aPermissions, aOsAsid, aFlags, aAddr, aIndex, aCount)); |
|
571 |
||
572 |
/** |
|
573 |
@todo Make mappings created with this function fail (panic?) if the are reused to map |
|
574 |
another object. |
|
575 |
*/ |
|
576 |
if(aCount==~0u) |
|
577 |
aCount = aMemory->iSizeInPages-aIndex; |
|
578 |
||
579 |
// if memory object reserves all resources, make mappings also do so... |
|
580 |
if(aMemory->iFlags&DMemoryObject::EReserveResources) |
|
581 |
FlagSet(aFlags,EMappingCreateReserveAllResources); |
|
582 |
||
583 |
// check if mapping is for global user data... |
|
584 |
if(aOsAsid==(TInt)KKernelOsAsid && aPermissions&EUser) |
|
585 |
FlagSet(aFlags,EMappingCreateUserGlobalVirtual); |
|
586 |
else |
|
587 |
FlagClear(aFlags,EMappingCreateUserGlobalVirtual); |
|
588 |
||
589 |
// set paged attribute for mapping... |
|
590 |
if(aMemory->IsDemandPaged()) |
|
591 |
FlagSet(aFlags,EMappingCreateDemandPaged); |
|
592 |
else |
|
593 |
FlagClear(aFlags,EMappingCreateDemandPaged); |
|
594 |
||
595 |
DMemoryMapping* mapping = 0; |
|
596 |
TInt r = KErrNone; |
|
597 |
if(!aMemory->CheckRegion(aIndex,aCount)) |
|
598 |
r = KErrArgument; |
|
599 |
else |
|
600 |
{ |
|
601 |
mapping = aMemory->CreateMapping(aIndex, aCount); |
|
602 |
if(!mapping) |
|
603 |
r = KErrNoMemory; |
|
604 |
} |
|
605 |
||
606 |
if(!mapping) |
|
607 |
{ |
|
608 |
// free any virtual address the mapping should have adopted... |
|
609 |
if(aFlags&EMappingCreateAdoptVirtual) |
|
610 |
MM::VirtualFree(aOsAsid, aAddr, aCount<<KPageShift); |
|
611 |
} |
|
612 |
else |
|
613 |
{ |
|
614 |
r = mapping->Construct(aMemory->Attributes(), aFlags, aOsAsid, aAddr, aCount<<KPageShift, aIndex<<KPageShift); |
|
615 |
if(r==KErrNone) |
|
616 |
r = mapping->Map(aMemory, aIndex, aCount, aPermissions); |
|
617 |
if(r!=KErrNone) |
|
618 |
{ |
|
619 |
mapping->Close(); |
|
620 |
mapping = 0; |
|
621 |
} |
|
622 |
} |
|
623 |
||
624 |
aMapping = mapping; |
|
625 |
TRACE(("MM::MappingNew returns %d (aMapping=0x%0x)",r,aMapping)); |
|
626 |
#ifdef BTRACE_FLEXIBLE_MEM_MODEL |
|
627 |
if (r == KErrNone) |
|
628 |
aMapping->BTraceCreate(); |
|
629 |
#endif |
|
630 |
return r; |
|
631 |
} |
|
632 |
||
633 |
||
634 |
TInt MM::MappingNew(DMemoryMapping*& aMapping, TUint aCount, TInt aOsAsid, TMappingCreateFlags aFlags, TLinAddr aAddr, TLinAddr aColourOffset) |
|
635 |
{ |
|
636 |
TRACE2(("MM::MappingNew(?,0x%08x,%d,0x%08x,0x%08x,0x%08x)",aCount, aOsAsid, aFlags, aAddr, aColourOffset)); |
|
637 |
||
638 |
FlagClear(aFlags,EMappingCreateDemandPaged); // mapping can't use demand paged page tables |
|
639 |
||
640 |
TInt r = KErrNone; |
|
641 |
DMemoryMapping* mapping = new DFineMapping(); |
|
642 |
if(!mapping) |
|
643 |
r = KErrNoMemory; |
|
644 |
||
645 |
if(!mapping) |
|
646 |
{ |
|
647 |
// free any virtual address the mapping should have adopted... |
|
648 |
if(aFlags&EMappingCreateAdoptVirtual) |
|
649 |
MM::VirtualFree(aOsAsid, aAddr, aCount<<KPageShift); |
|
650 |
} |
|
651 |
else |
|
652 |
{ |
|
653 |
r = mapping->Construct(EMemoryAttributeStandard, aFlags, aOsAsid, aAddr, aCount<<KPageShift, aColourOffset); |
|
654 |
if(r!=KErrNone) |
|
655 |
{ |
|
656 |
mapping->Close(); |
|
657 |
mapping = 0; |
|
658 |
} |
|
659 |
} |
|
660 |
||
661 |
aMapping = mapping; |
|
662 |
TRACE2(("MM::MappingNew returns %d (aMapping=0x%0x)",r,aMapping)); |
|
663 |
||
664 |
return r; |
|
665 |
} |
|
666 |
||
667 |
||
668 |
TInt MM::MappingMap(DMemoryMapping* aMapping, TMappingPermissions aPermissions, DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
669 |
{ |
|
670 |
TRACE2(("MM::MappingMap(0x%08x,0x%08x,0x%08x,0x%x,0x%x)",aMapping,aPermissions,aMemory,aIndex,aCount)); |
|
671 |
if(aCount==~0u) |
|
672 |
aCount = aMemory->iSizeInPages-aIndex; |
|
673 |
TInt r = aMapping->Map(aMemory, aIndex, aCount, aPermissions); |
|
674 |
TRACE2(("MM::MappingMap returns %d",r)); |
|
675 |
return r; |
|
676 |
} |
|
677 |
||
678 |
||
679 |
void MM::MappingUnmap(DMemoryMapping* aMapping) |
|
680 |
{ |
|
681 |
if(aMapping->IsAttached()) |
|
682 |
{ |
|
683 |
TRACE2(("MM::MappingUnmap(0x%08x)",aMapping)); |
|
684 |
aMapping->Unmap(); |
|
685 |
} |
|
686 |
} |
|
687 |
||
688 |
||
689 |
void MM::MappingDestroy(DMemoryMapping*& aMapping) |
|
690 |
{ |
|
691 |
DMemoryMapping* mapping = (DMemoryMapping*)__e32_atomic_swp_ord_ptr(&aMapping, 0); |
|
692 |
if (!mapping) |
|
693 |
return; |
|
694 |
TRACE(("MM::MappingDestroy(0x%08x)",mapping)); |
|
695 |
#ifdef BTRACE_FLEXIBLE_MEM_MODEL |
|
696 |
BTraceContext4(BTrace::EFlexibleMemModel,BTrace::EMemoryMappingDestroy,mapping); |
|
697 |
#endif |
|
698 |
if(mapping->IsAttached()) |
|
699 |
mapping->Unmap(); |
|
700 |
mapping->Close(); |
|
701 |
} |
|
702 |
||
703 |
||
704 |
void MM::MappingDestroy(TLinAddr aAddr, TInt aOsAsid) |
|
705 |
{ |
|
706 |
DMemoryMapping* mapping = AddressSpace[aOsAsid]->GetMapping(aAddr); |
|
707 |
MM::MappingDestroy(mapping); |
|
708 |
} |
|
709 |
||
710 |
||
711 |
void MM::MappingAndMemoryDestroy(DMemoryMapping*& aMapping) |
|
712 |
{ |
|
713 |
DMemoryMapping* mapping = (DMemoryMapping*)__e32_atomic_swp_ord_ptr(&aMapping, 0); |
|
714 |
TRACE(("MM::MappingAndMemoryDestroy(0x%08x)",mapping)); |
|
715 |
if (!mapping) |
|
716 |
return; |
|
717 |
DMemoryObject* memory = mapping->Memory(true); // safe because we assume owner hasn't unmapped mapping |
|
718 |
MM::MappingDestroy(mapping); |
|
719 |
MM::MemoryDestroy(memory); |
|
720 |
} |
|
721 |
||
722 |
||
723 |
void MM::MappingAndMemoryDestroy(TLinAddr aAddr, TInt aOsAsid) |
|
724 |
{ |
|
725 |
DMemoryMapping* mapping = AddressSpace[aOsAsid]->GetMapping(aAddr); |
|
726 |
MM::MappingAndMemoryDestroy(mapping); |
|
727 |
} |
|
728 |
||
729 |
||
730 |
TLinAddr MM::MappingBase(DMemoryMapping* aMapping) |
|
731 |
{ |
|
732 |
TLinAddr base = aMapping->Base(); |
|
733 |
TRACE2(("MM::MappingBase(0x%08x) returns 0x%08x",aMapping,base)); |
|
734 |
return base; |
|
735 |
} |
|
736 |
||
737 |
||
738 |
TInt MM::MappingOsAsid(DMemoryMapping* aMapping) |
|
739 |
{ |
|
740 |
return aMapping->OsAsid(); |
|
741 |
} |
|
742 |
||
743 |
||
744 |
DMemoryObject* MM::MappingGetAndOpenMemory(DMemoryMapping* aMapping) |
|
745 |
{ |
|
746 |
MmuLock::Lock(); |
|
747 |
DMemoryObject* memory = aMapping->Memory(); |
|
748 |
if (memory) |
|
749 |
memory->Open(); |
|
750 |
MmuLock::Unlock(); |
|
751 |
TRACE2(("MM::MappingGetAndOpenMemory(0x%08x) returns 0x%08x",aMapping,memory)); |
|
752 |
return memory; |
|
753 |
} |
|
754 |
||
755 |
||
756 |
void MM::MappingClose(DMemoryMapping* aMapping) |
|
757 |
{ |
|
758 |
TRACE2(("MM::MappingClose(0x%08x)",aMapping)); |
|
759 |
aMapping->Close(); |
|
760 |
} |
|
761 |
||
762 |
||
763 |
DMemoryMapping* MM::FindMappingInThread(DMemModelThread* aThread, TLinAddr aAddr, TUint aSize, |
|
764 |
TUint& aOffsetInMapping, TUint& aInstanceCount) |
|
765 |
{ |
|
766 |
if(aAddr>=KGlobalMemoryBase) |
|
767 |
{ |
|
768 |
// Address in global region, so look it up in kernel's address space... |
|
769 |
return FindMappingInAddressSpace(KKernelOsAsid, aAddr, aSize, aOffsetInMapping, aInstanceCount); |
|
770 |
} |
|
771 |
||
772 |
// Address in thread's process address space so open a reference to its os asid |
|
773 |
// so that it remains valid for FindMappingInAddressSpace() call. |
|
774 |
DMemModelProcess* process = (DMemModelProcess*)aThread->iOwningProcess; |
|
775 |
TInt osAsid = process->TryOpenOsAsid(); |
|
776 |
if (osAsid < 0) |
|
777 |
{// The process no longer owns an address space so can't have any mappings. |
|
778 |
return NULL; |
|
779 |
} |
|
780 |
||
781 |
DMemoryMapping* r = FindMappingInAddressSpace(osAsid, aAddr, aSize, aOffsetInMapping, aInstanceCount); |
|
782 |
||
783 |
process->CloseOsAsid(); |
|
784 |
return r; |
|
785 |
} |
|
786 |
||
787 |
||
788 |
DMemoryMapping* MM::FindMappingInAddressSpace( TUint aOsAsid, TLinAddr aAddr, TUint aSize, |
|
789 |
TUint& aOffsetInMapping, TUint& aInstanceCount) |
|
790 |
{ |
|
791 |
return AddressSpace[aOsAsid]->FindMapping(aAddr, aSize, aOffsetInMapping, aInstanceCount); |
|
792 |
} |
|
793 |
||
794 |
||
795 |
||
796 |
// |
|
797 |
// Address space |
|
798 |
// |
|
799 |
||
800 |
TInt MM::AddressSpaceAlloc(TPhysAddr& aPageDirectory) |
|
801 |
{ |
|
802 |
return DAddressSpace::New(aPageDirectory); |
|
803 |
} |
|
804 |
||
805 |
||
806 |
void MM::AddressSpaceFree(TUint aOsAsid) |
|
807 |
{ |
|
808 |
AddressSpace[aOsAsid]->Close(); |
|
809 |
} |
|
810 |
||
811 |
||
812 |
void MM::AsyncAddressSpaceFree(TUint aOsAsid) |
|
813 |
{ |
|
814 |
AddressSpace[aOsAsid]->AsyncClose(); |
|
815 |
} |
|
816 |
||
817 |
||
818 |
TInt MM::VirtualAllocCommon(TLinAddr& aLinAddr, TUint aSize, TBool aDemandPaged) |
|
819 |
{ |
|
820 |
TRACE(("MM::VirtualAllocCommon(?,0x%08x,%d)",aSize,aDemandPaged)); |
|
821 |
TUint pdeType = aDemandPaged ? EVirtualSlabTypeDemandPaged : 0; |
|
822 |
TInt r = DAddressSpace::AllocateUserCommonVirtualMemory(aLinAddr, aSize, 0, aSize, pdeType); |
|
823 |
TRACE(("MM::VirtualAllocCommon returns %d region=0x%08x+0x%08x",r,aLinAddr,aSize)); |
|
824 |
return r; |
|
825 |
} |
|
826 |
||
827 |
||
828 |
void MM::VirtualFreeCommon(TLinAddr aLinAddr, TUint aSize) |
|
829 |
{ |
|
830 |
TRACE(("MM::VirtualFreeCommon(0x%08x,0x%08x)",aLinAddr,aSize)); |
|
831 |
DAddressSpace::FreeUserCommonVirtualMemory(aLinAddr, aSize); |
|
832 |
} |
|
833 |
||
834 |
||
835 |
TInt MM::VirtualAlloc(TInt aOsAsid, TLinAddr& aLinAddr, TUint aSize, TBool aDemandPaged) |
|
836 |
{ |
|
837 |
TRACE(("MM::VirtualAlloc(?,%d,0x%08x,%d)",aOsAsid,aSize,aDemandPaged)); |
|
838 |
TUint pdeType = aDemandPaged ? EVirtualSlabTypeDemandPaged : 0; |
|
839 |
TInt r = AddressSpace[aOsAsid]->AllocateVirtualMemory(aLinAddr, aSize, 0, aSize, pdeType); |
|
840 |
TRACE(("MM::VirtualAlloc returns %d region=0x%08x+0x%08x",r,aLinAddr,aSize)); |
|
841 |
return r; |
|
842 |
} |
|
843 |
||
844 |
||
845 |
void MM::VirtualFree(TInt aOsAsid, TLinAddr aLinAddr, TUint aSize) |
|
846 |
{ |
|
847 |
TRACE(("MM::VirtualFree(%d,0x%08x,0x%08x)",aOsAsid,aLinAddr,aSize)); |
|
848 |
AddressSpace[aOsAsid]->FreeVirtualMemory(aLinAddr, aSize); |
|
849 |
} |
|
850 |
||
851 |
||
852 |
||
853 |
// |
|
854 |
// Init |
|
855 |
// |
|
856 |
||
857 |
void MM::Init1() |
|
858 |
{ |
|
859 |
TheMmu.Init1(); |
|
860 |
} |
|
861 |
||
862 |
||
863 |
extern DMutexPool MemoryObjectMutexPool; |
|
864 |
extern DMutexPool AddressSpaceMutexPool; |
|
865 |
||
866 |
void MM::Init2() |
|
867 |
{ |
|
868 |
TInt r; |
|
869 |
||
870 |
TheMmu.Init2(); |
|
871 |
||
872 |
// create mutex pools before calling any functions which require them... |
|
873 |
_LIT(KAddressSpaceMutexName,"AddressSpaceMutex"); |
|
874 |
r = AddressSpaceMutexPool.Create(4, &KAddressSpaceMutexName, KMutexOrdAddresSpace); |
|
875 |
__NK_ASSERT_ALWAYS(r==KErrNone); |
|
876 |
_LIT(KMemoryObjectMutexName,"MemoryObjectMutex"); |
|
877 |
r = MemoryObjectMutexPool.Create(8, &KMemoryObjectMutexName, KMutexOrdMemoryObject); |
|
878 |
__NK_ASSERT_ALWAYS(r==KErrNone); |
|
879 |
||
880 |
// use the Ram Allocator mutex for low-level memory functions... |
|
881 |
DMutex* mmuAllocMutex = TheMmu.iRamAllocatorMutex; |
|
882 |
||
883 |
// memory cleanup needs initialising before any memory is freed... |
|
884 |
TMemoryCleanup::Init2(); |
|
885 |
||
886 |
// initialise allocators used for MMU operations... |
|
887 |
RPageArray::Init2A(); |
|
888 |
PageTables.Init2(mmuAllocMutex); // must come before any other code which allocates memory objects |
|
889 |
RPageArray::Init2B(mmuAllocMutex); |
|
890 |
PageTables.Init2B(); |
|
891 |
PageDirectories.Init2(); |
|
892 |
||
893 |
// initialise address spaces... |
|
894 |
DAddressSpace::Init2(); |
|
895 |
||
896 |
TheMmu.Init2Final(); |
|
897 |
} |
|
898 |
||
899 |
||
900 |
/** HAL Function wrapper for the RAM allocator. |
|
901 |
*/ |
|
902 |
TInt RamHalFunction(TAny*, TInt aFunction, TAny* a1, TAny* a2) |
|
903 |
{ |
|
904 |
return TheMmu.RamHalFunction(aFunction, a1, a2); |
|
905 |
} |
|
906 |
||
907 |
||
908 |
void MM::Init3() |
|
909 |
{ |
|
910 |
__KTRACE_OPT2(KBOOT,KMMU,Kern::Printf("MM::Init3")); |
|
911 |
ThePager.Init3(); |
|
912 |
||
913 |
// Register a HAL Function for the Ram allocator. |
|
914 |
TInt r = Kern::AddHalEntry(EHalGroupRam, RamHalFunction, 0); |
|
915 |
__NK_ASSERT_ALWAYS(r==KErrNone); |
|
916 |
||
917 |
TheMmu.Init3(); |
|
918 |
} |
|
919 |
||
920 |
||
921 |
TInt MM::InitFixedKernelMemory(DMemoryObject*& aMemory, |
|
922 |
TLinAddr aStart, |
|
923 |
TLinAddr aEnd, |
|
924 |
TUint aInitSize, |
|
925 |
TMemoryObjectType aType, |
|
926 |
TMemoryCreateFlags aMemoryCreateFlags, |
|
927 |
TMemoryAttributes aMemoryAttributes, |
|
928 |
TMappingCreateFlags aMappingCreateFlags |
|
929 |
) |
|
930 |
{ |
|
931 |
TUint maxSize = aEnd-aStart; |
|
932 |
TInt r = MM::MemoryNew(aMemory, aType, MM::BytesToPages(maxSize), aMemoryCreateFlags, aMemoryAttributes); |
|
933 |
if(r==KErrNone) |
|
934 |
{ |
|
935 |
TBool allowGaps = aInitSize&1; // lower bit of size is set if region to be claimed contains gaps |
|
936 |
aInitSize &= ~1; |
|
937 |
r = MM::MemoryClaimInitialPages(aMemory,aStart,aInitSize,ESupervisorReadWrite,allowGaps); |
|
938 |
if(r==KErrNone) |
|
939 |
{ |
|
940 |
DMemoryMapping* mapping; |
|
941 |
r = MM::MappingNew(mapping,aMemory,ESupervisorReadWrite,KKernelOsAsid,aMappingCreateFlags,aStart); |
|
942 |
// prevent any further mappings of this memory, |
|
943 |
// this is needed for realtime and OOM guarantees... |
|
944 |
aMemory->DenyMappings(); |
|
945 |
} |
|
946 |
} |
|
947 |
// Note, no cleanup is done if an error occurs because this function is only |
|
948 |
// used at boot time and the system can't recover from an error |
|
949 |
return r; |
|
950 |
} |
|
951 |
||
952 |
||
953 |
void MM::Panic(MM::TMemModelPanic aPanic) |
|
954 |
{ |
|
955 |
Kern::Fault("MemModel", aPanic); |
|
956 |
} |
|
957 |
||
958 |
||
959 |
// |
|
960 |
// |
|
961 |
// |
|
962 |
||
963 |
TUint MM::BytesToPages(TUint aBytes) |
|
964 |
{ |
|
965 |
if(aBytes&KPageMask) |
|
966 |
Panic(EBadBytesToPages); |
|
967 |
return aBytes>>KPageShift; |
|
968 |
} |
|
969 |
||
970 |
||
971 |
TUint MM::RoundToPageSize(TUint aSize) |
|
972 |
{ |
|
973 |
return (aSize+KPageMask)&~KPageMask; |
|
974 |
} |
|
975 |
||
976 |
||
977 |
TUint MM::RoundToPageCount(TUint aSize) |
|
978 |
{ |
|
979 |
return (aSize+KPageMask)>>KPageShift; |
|
980 |
} |
|
981 |
||
982 |
||
983 |
TUint MM::RoundToPageShift(TUint aShift) |
|
984 |
{ |
|
985 |
return aShift>(TUint)KPageShift ? aShift-KPageShift : 0; |
|
986 |
} |
|
987 |
||
988 |
||
989 |
// |
|
990 |
// |
|
991 |
// |
|
992 |
||
993 |
void MM::ValidateLocalIpcAddress(TLinAddr aAddr, TUint aSize, TBool aWrite) |
|
994 |
{ |
|
995 |
__NK_ASSERT_DEBUG(aSize); |
|
996 |
||
997 |
TLinAddr end = aAddr+aSize-1; |
|
998 |
if(end<aAddr) |
|
999 |
end = ~(TLinAddr)0; // clip to end of memory |
|
1000 |
||
1001 |
// if IPC region is in process local data area then it's OK... |
|
1002 |
if(end<KUserLocalDataEnd && aAddr>=KUserLocalDataBase) |
|
1003 |
return; |
|
1004 |
||
1005 |
// if region overlaps alias region... |
|
1006 |
if(end>=KIPCAlias && aAddr<KIPCAlias+KIPCAliasAreaSize) |
|
1007 |
{ |
|
1008 |
// remove alias... |
|
1009 |
((DMemModelThread*)TheCurrentThread)->RemoveAlias(); |
|
1010 |
// make sure start address is in alias region... |
|
1011 |
if(aAddr<KIPCAlias) |
|
1012 |
aAddr = KIPCAlias; |
|
1013 |
// then cause fault now... |
|
1014 |
MM::UserPermissionFault(aAddr,aWrite); |
|
1015 |
} |
|
1016 |
||
1017 |
if(end<(TLinAddr)KUserMemoryLimit) |
|
1018 |
return; // user memory is safe |
|
1019 |
||
1020 |
// Compare the current thread's process os asid to kernel asid, no need to |
|
1021 |
// open a reference on the os asid as it is the current thread. |
|
1022 |
if(((DMemModelProcess*)TheCurrentThread->iOwningProcess)->OsAsid()==(TInt)KKernelOsAsid) |
|
1023 |
return; // kernel can access everything |
|
1024 |
||
1025 |
// make sure address is in supervisor only region... |
|
1026 |
if(aAddr<KUserMemoryLimit) |
|
1027 |
aAddr = KUserMemoryLimit; |
|
1028 |
// then cause fault now... |
|
1029 |
MM::UserPermissionFault(aAddr,aWrite); |
|
1030 |
} |
|
1031 |
||
1032 |
||
1033 |
void MM::UserPermissionFault(TLinAddr aAddr, TBool aWrite) |
|
1034 |
{ |
|
1035 |
// Access aAddr with user permissions to generate an exception... |
|
1036 |
if(aWrite) |
|
1037 |
UserWriteFault(aAddr); |
|
1038 |
else |
|
1039 |
UserReadFault(aAddr); |
|
1040 |
__NK_ASSERT_ALWAYS(0); // shouldn't get here |
|
1041 |
} |
|
1042 |
||
1043 |
||
1044 |
#ifndef __SMP__ |
|
1045 |
void MM::IpcAliasPde(TPde*& aPdePtr, TUint aOsAsid) |
|
1046 |
{ |
|
1047 |
aPdePtr = &Mmu::PageDirectory(aOsAsid)[KIPCAlias>>KChunkShift]; |
|
1048 |
} |
|
1049 |
#endif |
|
1050 |
||
1051 |
||
1052 |
TMappingPermissions MM::MappingPermissions(TBool aUser, TBool aWrite, TBool aExecute) |
|
1053 |
{ |
|
1054 |
TUint perm = 0; |
|
1055 |
if(aUser) |
|
1056 |
perm |= EUser; |
|
1057 |
if(aWrite) |
|
1058 |
perm |= EReadWrite; |
|
1059 |
if(aExecute) |
|
1060 |
perm |= EExecute; |
|
1061 |
return (TMappingPermissions)perm; |
|
1062 |
} |
|
1063 |
||
1064 |
||
1065 |
TInt MM::MappingPermissions(TMappingPermissions& aPermissions, TMappingAttributes2 aLegacyAttributes) |
|
1066 |
{ |
|
1067 |
TUint attr2 = *(TUint32*)&aLegacyAttributes; |
|
1068 |
||
1069 |
TUint read = attr2&EMapAttrReadMask; |
|
1070 |
TUint write = (attr2&EMapAttrWriteMask)>>4; |
|
1071 |
TUint execute = (attr2&EMapAttrExecMask)>>8; |
|
1072 |
||
1073 |
read |= execute; // execute access requires read access |
|
1074 |
||
1075 |
if(write==0) // no write required |
|
1076 |
{ |
|
1077 |
if((read&5)==0) |
|
1078 |
return KErrNotSupported; // neither supervisor nor user read specified |
|
1079 |
} |
|
1080 |
else if(write<4) // supervisor write required |
|
1081 |
{ |
|
1082 |
if(read>=4) |
|
1083 |
return KErrNotSupported; // user read requested (but no user write) |
|
1084 |
} |
|
1085 |
||
1086 |
read |= write; // write access implies read access |
|
1087 |
||
1088 |
TUint user = read&4; |
|
1089 |
aPermissions = MappingPermissions(user,write,execute); |
|
1090 |
||
1091 |
return KErrNone; |
|
1092 |
} |
|
1093 |
||
1094 |
||
1095 |
TInt MM::MemoryAttributes(TMemoryAttributes& aAttributes, TMappingAttributes2 aLegacyAttributes) |
|
1096 |
{ |
|
1097 |
TUint attr = aLegacyAttributes.Type(); |
|
1098 |
if (aLegacyAttributes.Shared()) |
|
1099 |
attr |= EMemoryAttributeShareable; |
|
1100 |
if (aLegacyAttributes.Parity()) |
|
1101 |
attr |= EMemoryAttributeUseECC; |
|
1102 |
aAttributes = Mmu::CanonicalMemoryAttributes((TMemoryAttributes)attr); |
|
1103 |
return KErrNone; |
|
1104 |
} |
|
1105 |
||
1106 |
||
1107 |
TMappingAttributes2 MM::LegacyMappingAttributes(TMemoryAttributes aAttributes, TMappingPermissions aPermissions) |
|
1108 |
{ |
|
1109 |
TUint attr = Mmu::CanonicalMemoryAttributes(aAttributes); |
|
1110 |
return TMappingAttributes2 |
|
1111 |
( |
|
1112 |
(TMemoryType)(attr&EMemoryAttributeTypeMask), |
|
1113 |
aPermissions&EUser, |
|
1114 |
aPermissions&EReadWrite, |
|
1115 |
aPermissions&EExecute, |
|
1116 |
attr&EMemoryAttributeShareable, |
|
1117 |
attr&EMemoryAttributeUseECC |
|
1118 |
); |
|
1119 |
} |
|
1120 |
||
1121 |