Update contrib.
1 // Copyright (c) 1998-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32/include/memmodel/epoc/flexible/memmodel.h
15 // Flexible Memory Model header file
23 #ifndef __MEMMODEL_H__
24 #define __MEMMODEL_H__
26 #include <plat_priv.h>
27 #include <memmodel/epoc/mmubase/kblockmap.h>
32 // SubScheduler fields for each processor
33 #define i_AliasLinAddr iExtras[0]
34 #define i_AliasPdePtr iExtras[1]
37 /********************************************
38 * Deterministic Scheduler Implementation
39 ********************************************/
44 #define TheCurrentAddressSpace ((DMemModelProcess*&)TheScheduler.iAddressSpace)
49 /********************************************
50 * Thread Control Block
51 ********************************************/
53 class DMemModelProcess;
58 class DMemModelThread : public DThread
61 TInt Alias(TLinAddr aAddr, DMemModelProcess* aProcess, TInt aSize, TLinAddr& aAliasAddr, TUint& aAliasSize);
64 virtual void DoExit1();
65 static void RestoreAddressSpace();
66 virtual void BTracePrime(TInt aCategory);
68 virtual void SetPaging(TUint& aCreateFlags);
70 void DoRemoveAlias(TLinAddr aAddr);
72 TLinAddr iAliasLinAddr; // linear address to access aliased memory (0 means no alias is present).
73 TPde* iAliasPdePtr; // Address of PDE which has been modified to make aliased memory accessible.
74 TPde iAliasPde; // PDE to store at iAliasPdePtr.
75 DMemModelProcess* iAliasProcess; // The process whose memory is aliased.
76 SDblQueLink iAliasLink; // link to make TheMmu.iAliasList.
77 TLinAddr iAliasTarget; // linear address of the memory which has been aliased
78 DMemoryMapping* iKernelStackMapping;
79 DMemoryMapping* iUserStackMapping;
81 TInt iCpuRestoreCookie;
86 /********************************************
87 * Process Control Block
88 ********************************************/
91 class DMemModelCodeSegMemory;
92 class RAddressedContainer;
94 #ifdef INCLUDED_FROM_ASM
95 #define PRIVATE_EXCEPT_ASM public
97 #define PRIVATE_EXCEPT_ASM private
104 class DMemModelProcess : public DEpocProcess
111 virtual TInt AttachExistingCodeSeg(TProcessCreateInfo& aInfo);
112 virtual TInt SetPaging(const TProcessCreateInfo& aInfo);
114 virtual TInt DoCreate(TBool aKernelProcess, TProcessCreateInfo& aInfo);
115 virtual TInt NewChunk(DChunk*& aChunk, SChunkCreateInfo& aInfo, TLinAddr& aRunAddr);
116 virtual TInt AddChunk(DChunk* aChunk,TBool aIsReadOnly);
117 virtual TInt NewShPool(DShPool*& aPool, TShPoolCreateInfo& aInfo);
118 virtual TInt CreateDataBssStackArea(TProcessCreateInfo& aInfo);
119 virtual TInt MapCodeSeg(DCodeSeg* aCodeSeg);
120 virtual void UnmapCodeSeg(DCodeSeg* aCodeSeg);
121 virtual void RemoveDllData();
122 virtual void FinalRelease();
123 virtual void BTracePrime(TInt aCategory);
125 TInt DoAddChunk(DMemModelChunk* aChunk, TBool aIsReadOnly);
126 TInt AllocateDataSectionBase(DMemModelChunk& aChunk, TUint& aBase);
127 TUint8* DataSectionBase(DMemModelChunk* aChunk);
128 void RemoveChunk(DMemModelChunk *aChunk);
129 void DoRemoveChunk(TInt aIndex);
130 TInt ChunkIndex(DMemModelChunk* aChunk);
131 TUint ChunkInsertIndex(DMemModelChunk* aChunk);
132 TInt CommitDllData(TLinAddr aBase, TInt aSize, DCodeSeg* aCodeSeg);
133 void DecommitDllData(TLinAddr aBase, TInt aSize);
134 TInt MapUserRamCode(DMemModelCodeSegMemory* aMemory);
135 void UnmapUserRamCode(DMemModelCodeSegMemory* aMemory);
137 {__NK_ASSERT_DEBUG( TheCurrentThread->iOwningProcess == this || // current thread's process so asid can't be freed.
138 iOsAsid == (TInt)KKernelOsAsid || // kernel process so asid can't be freed.
139 iContainerID != EProcess || // process not fully created yet so asid can't be freed.
140 iOsAsidRefCount > 1); // if none of the others are true then should have a reference
141 // to prevent asid being freed (this check isn't very
142 // robust but best we can do).
146 TInt TryOpenOsAsid();
148 void AsyncCloseOsAsid();
152 DMemModelChunk* iChunk;
153 DMemoryMapping* iMapping;
161 RAddressedContainer* iSharedChunks;
163 DMemoryMapping* iDataBssMapping;
165 Size of virtual memory allocated in process for EXE code, but which hasn't yet been been
166 adopted by the codeseg memory mapping.
168 TUint iCodeVirtualAllocSize;
170 Address of virtual memory allocated in process for EXE code.
172 TLinAddr iCodeVirtualAllocAddress;
175 TInt iOsAsid; // This should only be accessed directly by the scheduler,
176 // in all other cases use either OsAsid() or TryOpenOsAsid().
178 TUint iOsAsidRefCount;
180 friend class Monitor;
184 /********************************************
185 * Chunk Control Block
186 ********************************************/
191 class DMemModelChunk : public DChunk
195 @see DChunk::TChunkAttributes for generic attribute flags
197 enum TMemModelChunkAttributes
199 EPrivate =0x00000000, // need to be iOwningProcess in order to map or change chunk
200 EPublic =0x80000000, // not EPrivate
201 ECode =0x40000000, // contents are executable
202 EMMChunkAttributesMask = EPrivate | EPublic | ECode,
209 virtual TInt Close(TAny* aPtr);
210 virtual TInt DoCreate(SChunkCreateInfo& aInfo);
211 virtual TInt Adjust(TInt aNewSize);
212 virtual TInt AdjustDoubleEnded(TInt aBottom, TInt aTop);
213 virtual TInt CheckAccess();
214 virtual TInt Commit(TInt aOffset, TInt aSize, TCommitType aCommitType=DChunk::ECommitDiscontiguous, TUint32* aExtraArg=0);
215 virtual TInt Allocate(TInt aSize, TInt aGuard=0, TInt aAlign=0);
216 virtual TInt Decommit(TInt aOffset, TInt aSize);
217 virtual TInt Lock(TInt aOffset, TInt aSize);
218 virtual TInt Unlock(TInt aOffset, TInt aSize);
219 virtual TInt Address(TInt aOffset, TInt aSize, TLinAddr& aKernelAddress);
220 virtual TInt PhysicalAddress(TInt aOffset, TInt aSize, TLinAddr& aKernelAddress, TUint32& aPhysicalAddress, TUint32* aPhysicalPageList=NULL);
221 virtual void BTracePrime(TInt aCategory);
222 virtual void Substitute(TInt aOffset, TPhysAddr aOldAddr, TPhysAddr aNewAddr);
223 virtual TUint8* Base(DProcess* aProcess);
225 virtual void SetPaging(TUint aCreateAtt);
227 void SetFixedAddress(TLinAddr aAddr, TInt aInitialSize);
228 TInt SetAttributes(SChunkCreateInfo& aInfo);
229 TInt DoCommit(TInt aOffset, TInt aSize, TCommitType aCommitType=DChunk::ECommitDiscontiguous, TUint32* aExtraArg=0);
230 void DoDecommit(TInt aOffset, TInt aSize);
231 TInt CheckRegion(TInt& aOffset, TInt& aSize);
233 TBitMapAllocator* iPageBitMap; // NULL if not disconnected chunk
234 TBitMapAllocator* iPermanentPageBitMap;
237 The memory object containing this chunk's memory.
239 DMemoryObject* iMemoryObject;
242 For shared chunks and shared i/o buffers this is the mapping
243 which maps #iMemoryObject into the kernel's address space.
245 DMemoryMapping* iKernelMapping;
247 friend class Monitor;
254 class DMemModelChunkHw : public DPlatChunkHw
257 virtual TInt Close(TAny* aPtr);
259 DMemoryObject* iMemoryObject;
260 DMemoryMapping* iKernelMapping;
265 /********************************************
267 ********************************************/
269 class TPagedCodeInfo;
274 class DMemModelCodeSegMemory : public DEpocCodeSegMemory
277 DMemModelCodeSegMemory(DEpocCodeSeg* aCodeSeg);
278 ~DMemModelCodeSegMemory();
279 TInt Create(TCodeSegCreateInfo& aInfo, DMemModelProcess* aProcess);
280 TInt Loaded(TCodeSegCreateInfo& aInfo);
284 The process loading this code segment.
286 DMemModelProcess* iCreator;
289 Kernel side copy of the codeseg's export directory or NULL.
291 TLinAddr* iCopyOfExportDir;
294 For demand paged codeseg's, pointer to saved copy of the data section.
296 TAny* iDataSectionMemory;
299 The memory object containing the memory for the codeseg.
301 DMemoryObject* iCodeMemoryObject;
304 A writable mapping which maps #iCodeMemoryObject into the loader
305 process's memory so it can be accessed by the loader.
307 This mapping is destroyed once #Loaded is called.
309 DMemoryMapping* iCodeLoadMapping;
312 For demand paged codeseg's, this is a writable mapping added to the
313 loader process's address space which maps a memory object used to
314 hold the initial contents of the codeseg's data section.
316 This mapping, and it's memory object, is destroyed once #Loaded is called.
318 DMemoryMapping* iDataLoadMapping;
321 Size of any shared virtual memory allocated for the code.
323 TUint iVirtualAllocCommonSize;
326 For demand paged codeseg's, a pointer to the #TPagedCodeInfo
327 used with #iCodeMemoryObject.
329 TPagedCodeInfo* iPagedCodeInfo;
336 class DMemModelCodeSeg: public DEpocCodeSeg
340 virtual ~DMemModelCodeSeg();
341 virtual TInt DoCreateRam(TCodeSegCreateInfo& aInfo, DProcess* aProcess);
342 virtual TInt DoCreateXIP(DProcess* aProcess);
343 virtual TInt Loaded(TCodeSegCreateInfo& aInfo);
344 virtual void ReadExportDir(TUint32* aDest);
345 virtual TBool FindCheck(DProcess* aProcess);
346 virtual TBool OpenCheck(DProcess* aProcess);
347 inline DMemModelCodeSegMemory* Memory()
348 { return (DMemModelCodeSegMemory*)iMemory; }
349 virtual void BTracePrime(TInt aCategory);
352 For kernel codesegs, the address of the memory allocated for its static data.
357 The memory object containing the memory for this codeseg.
359 DMemoryObject* iCodeMemoryObject;
362 A writable mapping which maps #iCodeMemoryObject into the loader
363 process's memory so it can be accessed by the loader.
365 This mapping is destroyed once #Loaded is called.
367 DMemoryMapping* iCodeLoadMapping;
370 The read=only mapping used for kernel and global codesegs to map
371 #iCodeMemoryObject into the global address region.
373 DMemoryMapping* iCodeGlobalMapping;
376 Base address of virtual memory allocated for the codeseg's static data.
378 TLinAddr iDataAllocBase;
381 Size of virtual memory allocated for the codeseg's static data.
383 TUint iDataAllocSize;
387 /********************************************
388 * Shared buffers and pools
389 ********************************************/
391 #include <kernel/sshbuf.h>
396 class DMemModelNonAlignedShBuf : public DShBuf
399 DMemModelNonAlignedShBuf(DShPool* aPool, TLinAddr aRelAddr);
400 ~DMemModelNonAlignedShBuf();
403 TInt AddToProcess(DProcess* aProcess, TUint aAttr);
406 virtual TInt Pin(TPhysicalPinObject* aPinObject, TBool aReadOnly, TPhysAddr& aAddress, TPhysAddr* aPages, TUint32& aMapAttr, TUint& aColour);
407 virtual TInt Map(TUint, DProcess*, TLinAddr&);
408 virtual TInt UnMap(DProcess*);
409 virtual TUint8* Base(DProcess* aProcess);
410 virtual TUint8* Base();
414 class DMemModelAlignedShBuf : public DShBuf
417 DMemModelAlignedShBuf(DShPool* aPool);
418 virtual ~DMemModelAlignedShBuf();
421 virtual TInt Construct();
424 TInt AddToProcess(DProcess* aProcess, TUint aAttr);
427 TUint8* Base(DProcess* aProcess);
429 TInt Map(TUint, DProcess*, TLinAddr&);
430 TInt UnMap(DProcess*);
431 TInt Pin(TPhysicalPinObject* aPinObject, TBool aReadOnly, TPhysAddr& Address, TPhysAddr* aPages, TUint32& aMapAttr, TUint& aColour);
434 TInt FindMapping(DShBufMapping*&, DMemModelProcess*);
435 DMemoryObject* iMemoryObject;
436 DMemoryMapping* iKernelMapping;
438 friend class DMemModelAlignedShPool;
441 class DMemModelShPool : public DShPool
445 virtual ~DMemModelShPool();
447 void DestroyClientResources(DProcess* aProcess);
448 virtual TInt DestroyAllMappingsAndReservedHandles(DProcess* aProcess) = 0;
451 class DMemModelAlignedShPoolClient : public DShPoolClient
454 SDblQue iMappingFreeList;
458 class DMemModelNonAlignedShPoolClient : public DShPoolClient
461 DMemoryMapping* iMapping;
466 class DMemModelAlignedShPool : public DMemModelShPool
469 DMemModelAlignedShPool();
470 virtual ~DMemModelAlignedShPool();
472 TInt Close(TAny* aPtr);
473 TInt CreateInitialBuffers();
474 TInt SetBufferWindow(DProcess* aProcess, TInt aWindowSize);
475 TInt AddToProcess(DProcess* aProcess, TUint aAttr);
476 TInt Alloc(DShBuf*&);
479 TInt DoCreate(TShPoolCreateInfo& aInfo);
482 TInt MappingNew(DShBufMapping*& aMapping, DMemModelProcess* aProcess);
483 TInt GetFreeMapping(DShBufMapping*& aMapping, DMemModelAlignedShPoolClient* aClient);
484 TInt ReleaseMapping(DShBufMapping*& aMapping, DMemModelAlignedShPoolClient* aClient);
485 TInt UpdateMappingsAndReservedHandles(TInt aNoOfBuffers);
486 TInt UpdateFreeList();
487 void Free(DShBuf* aBuf);
488 TInt CreateMappings(DMemModelAlignedShPoolClient* aClient, TInt aNoOfMappings, DMemModelProcess* aProcess);
489 TInt DestroyAllMappingsAndReservedHandles(DProcess* aProcess);
490 TInt DestroyMappings(DMemModelAlignedShPoolClient* aClient, TInt aNoOfMappings);
492 TInt DeleteInitialBuffers();
494 SDblQue iPendingList;
495 DMemModelAlignedShBuf* iInitialBuffersArray;
497 friend class DMemModelAlignedShBuf;
500 class DMemModelNonAlignedShPool : public DMemModelShPool
503 DMemModelNonAlignedShPool();
504 virtual ~DMemModelNonAlignedShPool();
506 TInt Close(TAny* aPtr);
508 TInt DoInitFreeList();
509 TUint8* Base(DProcess* aProcess);
511 inline TUint8* Base()
513 return reinterpret_cast<TUint8*>(iBaseAddress);
515 TInt AddToProcess(DProcess* aProcess, TUint aAttr);
516 TInt CreateInitialBuffers();
518 TInt Alloc(DShBuf*&);
521 void Free(DShBuf* aBuf);
522 TInt UpdateFreeList();
524 TInt DoCreate(TShPoolCreateInfo& aInfo);
525 void FreeBufferPages(TUint aOffset);
528 TInt DeleteInitialBuffers();
529 TInt DestroyAllMappingsAndReservedHandles(DProcess* aProcess);
532 TLinAddr iBaseAddress;
533 DMemoryObject* iMemoryObject; // the 'real' memory pool (in the kernel)
534 DMemModelNonAlignedShBuf* iInitialBuffersArray;
535 TBitMapAllocator* iBufMap;
536 TBitMapAllocator* iPagesMap;
538 friend class DMemModelNonAlignedShBuf;