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\emul\win32\memmodel.h
16 // WARNING: This file contains some APIs which are internal and are subject
17 // to change without notice. Such APIs should therefore not be used
18 // outside the Kernel and Hardware Services package.
26 #ifndef __WIN32_MEM_H__
27 #define __WIN32_MEM_H__
29 #include <plat_priv.h>
30 #include <kernel/sshbuf.h>
32 const TInt KRamChunkSize=0x10000;
33 const TInt KRamChunkShift=16;
34 const TInt KRamPageSize=0x1000;
35 const TInt KRamPageShift=12;
37 /********************************************
38 * Thread Control Block
39 ********************************************/
41 class DWin32Thread : public DThread
45 virtual TInt Context(TDes8& aDes);
46 virtual TInt SetupContext(SThreadCreateInfo& anInfo);
47 virtual void DoExit2();
50 /********************************************
51 * Process Control Block
52 ********************************************/
55 struct SProcessDllDataBlock;
56 class DWin32Process : public DProcess
62 virtual TInt DoCreate(TBool aKernelProcess, TProcessCreateInfo& aInfo);
63 virtual TInt NewChunk(DChunk*& aChunk, SChunkCreateInfo& aInfo, TLinAddr& aRunAddr);
64 virtual TInt AddChunk(DChunk* aChunk,TBool isReadOnly);
65 virtual TInt NewShPool(DShPool*& aPool, TShPoolCreateInfo& aInfo);
66 virtual TInt CreateDataBssStackArea(TProcessCreateInfo& aInfo);
67 virtual TInt GetNewThread(DThread*& aThread, SThreadCreateInfo& anInfo);
68 virtual TInt AttachExistingCodeSeg(TProcessCreateInfo& aInfo);
69 virtual TInt MapCodeSeg(DCodeSeg* aCodeSeg);
70 virtual void UnmapCodeSeg(DCodeSeg* aCodeSeg);
71 virtual void RemoveDllData();
72 virtual void FinalRelease();
73 virtual void Release();
74 void CallRuntimeHook(TWin32RuntimeReason aReason);
76 TWin32RuntimeHook iWin32RuntimeHook;
77 RArray<SProcessDllDataBlock> iDllData;
80 /******************************************************************
81 * structure to keep static data in code segments within a process
82 ******************************************************************/
83 struct SProcessDllDataBlock
85 DWin32CodeSeg* iCodeSeg;
86 TAny* iDataCopy; // copy of .data
87 TAny* iBssCopy; // copy of .bss
90 /********************************************
92 ********************************************/
93 class DWin32Chunk : public DChunk
96 enum TMemModelChunkAttributes
100 EMMChunkAttributesMask = EPrivate,
106 virtual TInt DoCreate(SChunkCreateInfo& aInfo);
107 virtual TInt Adjust(TInt aNewSize);
108 virtual TInt AdjustDoubleEnded(TInt aBottom, TInt aTop);
109 virtual TInt CheckAccess();
110 virtual TInt Commit(TInt anOffset, TInt aSize, TCommitType aCommitType=DChunk::ECommitDiscontiguous, TUint32* aExtraArg=0);
111 virtual TInt Allocate(TInt aSize, TInt aGuard=0, TInt aAlign=0);
112 virtual TInt Decommit(TInt anOffset, TInt aSize);
113 virtual TInt Lock(TInt anOffset, TInt aSize);
114 virtual TInt Unlock(TInt anOffset, TInt aSize);
115 virtual TInt Address(TInt aOffset, TInt aSize, TLinAddr& aKernelAddress);
116 virtual TInt PhysicalAddress(TInt aOffset, TInt aSize, TLinAddr& aKernelAddress, TUint32& aPhysicalAddress, TUint32* aPhysicalPageList=NULL);
117 virtual void BTracePrime(TInt aCategory);
118 virtual void Substitute(TInt aOffset, TPhysAddr aOldAddr, TPhysAddr aNewAddr);
119 virtual TUint8* Base(DProcess* aProcess);
120 inline TUint8* Base() const { return DChunk::Base(); }
122 TInt DoCommit(TInt aOffset, TInt aSize);
123 void DoDecommit(TInt aOffset, TInt aSize);
125 TBitMapAllocator* iPageBitMap;
126 TBitMapAllocator* iUnlockedPageBitMap;
127 TBitMapAllocator* iPermanentPageBitMap;
130 /********************************************
132 ********************************************/
134 class DWin32CodeSeg: public DCodeSeg
138 virtual ~DWin32CodeSeg();
139 TInt ProcessImports(DProcess* aProcess);
140 TInt CreateAlreadyLoaded(HMODULE aModule, TInt aDepCount);
141 TInt RegisterCodeSeg(HMODULE aModule);
144 virtual TLibraryFunction Lookup(TInt aOrdinal);
145 virtual TInt GetMemoryInfo(TModuleMemoryInfo& aInfo, DProcess* aProcess);
146 virtual TInt DoCreate(TCodeSegCreateInfo& aInfo, DProcess* aProcess);
147 virtual TInt Loaded(TCodeSegCreateInfo& aInfo);
148 virtual void InitData();
149 virtual void ReadExportDir(TUint32* aDest);
150 virtual TBool FindCheck(DProcess* aProcess);
151 virtual TBool OpenCheck(DProcess* aProcess);
152 virtual void Info(TCodeSegCreateInfo& aInfo);
154 HINSTANCE iWinInstance;
155 HMODULE iModuleHandle;
156 wchar_t* iModuleFile;
157 TBool iAlwaysLoaded; // TRUE for variant or extension
158 DModuleList* iModuleList;
159 TAny* iDataCopy; // copy of .data
162 TLinAddr iDataDest; // load address of .data
163 TLinAddr iBssDest; // load address of .bss
164 TInt iCodeSegId; // unique ID, incremented each time a code segment is loaded
165 DWin32Process* iLiveProcess;// process who's static data is currently loaded in the codeseg
170 TLinAddr iWin32ModuleHandle;
171 DWin32CodeSeg* iCodeSeg;
175 /********************************************
176 * Functions/Data defined in memory model
177 ********************************************/
184 EKernelHeapReserveFailed = 0,
185 EKernelHeapCommitFailed = 1,
186 ERamAllocMutexCreateFailed = 2,
187 EInvalidChunkCreate = 3,
188 EInvalidSharedModule = 4,
190 EWin32RuntimeError=6,
191 ENotSupportedOnEmulator=7,
193 EWsdDllNotInProcess=9,
196 static void Panic(TMemModelPanic aPanic);
200 static void Signal();
201 static TUint32 RoundToPageSize(TUint32 aSize);
202 static TUint32 RoundToChunkSize(TUint32 aSize);
203 static TInt RegisterModule(HMODULE aModule);
204 static TInt Commit(TLinAddr aBase, TInt aSize, TInt aClearByte, TBool aExecute);
205 static TInt Decommit(TLinAddr aBase, TInt aSize);
206 static void CheckMemoryCounters();
207 static void DoProcessSwitch(TAny* aAddressSpace);
208 static TAny* CurrentAddress(DThread* aThread, const TAny* aPtr, TInt aSize, TBool aWrite, TBool& aLocked);
210 static TAny* KernelHeapAddress;
211 static DMutex* RamAllocatorMutex;
212 static TInt RamChunkSize;
213 static TInt RamChunkShift;
214 static TInt RamPageSize;
215 static TInt RamPageShift;
216 static TInt FreeMemory; // number of bytes in the system free memory
217 static TInt CacheMemory; // number of bytes of memory being used for cache chunks (RChunk::Unlock)
218 static TInt ReclaimedCacheMemory; // number of bytes of memory removed from CacheMemory in order to satisfy memory allocation
219 static TInt InitialFreeMemory;
220 static TBool AllocFailed;
221 static RArray<SWin32Module> Win32Modules;
222 static TInt NextCodeSegId;
226 /********************************************
227 * Shared buffers and pools
228 ********************************************/
230 class DWin32ShBuf : public DShBuf
233 DWin32ShBuf(DShPool* aPool, TLinAddr aRelAddr);
236 TInt AddToProcess(DProcess* aProcess, TUint aAttr);
238 TInt Close(TAny* aPtr);
241 virtual TInt Map(TUint, DProcess*, TLinAddr&);
242 virtual TInt UnMap(DProcess*);
243 virtual TUint8* Base(DProcess* aProcess);
244 virtual TUint8* Base();
251 class DWin32ShPool : public DShPool
255 virtual ~DWin32ShPool();
257 TInt Close(TAny* aPtr);
258 TInt AddToProcess(DProcess* aProcess, TUint aAttr);
259 TInt Alloc(DShBuf*&);
262 TInt DoCreate(TShPoolCreateInfo& aInfo);
263 TInt AddToFreeList(TInt aOffset);
264 TInt CreateInitialBuffers();
265 TInt DeleteInitialBuffers();
266 TInt DestroyHandles(DProcess* aProcess);
267 void DestroyClientResources(DProcess* aProcess);
269 void Free(DShBuf* aBuf);
270 TInt UpdateFreeList();
273 TUint8* Base(DProcess* aProcess);
274 TBool IsOpen(DProcess* aProcess);
276 TBitMapAllocator* iBufMap;
277 DWin32ShBuf* iInitialBuffersArray;
278 TUint8* iWin32MemoryBase;
279 TInt iWin32MemorySize;
280 friend class DWin32ShBuf;
284 class DWin32AlignedShPool : public DWin32ShPool
287 DWin32AlignedShPool();
288 virtual ~DWin32AlignedShPool();
289 TInt SetBufferWindow(DProcess* aProcess, TInt aWindowSize);
292 TInt DoCreate(TShPoolCreateInfo& aInfo);
298 class DWin32NonAlignedShPool : public DWin32ShPool
301 DWin32NonAlignedShPool();
302 virtual ~DWin32NonAlignedShPool();
305 TInt DoCreate(TShPoolCreateInfo& aInfo);
306 void FreeBufferPages(TUint aOffset);
310 TBitMapAllocator* iPagesMap;
313 #endif // __WIN32_MEM_H__