kernel/eka/include/memmodel/emul/win32/memmodel.h
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32\include\memmodel\emul\win32\memmodel.h
       
    15 // 
       
    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.
       
    19 //
       
    20 
       
    21 /**
       
    22  @file
       
    23  @internalComponent
       
    24 */
       
    25 
       
    26 #ifndef __WIN32_MEM_H__
       
    27 #define __WIN32_MEM_H__
       
    28 #include <win32.h>
       
    29 #include <plat_priv.h>
       
    30 #include <kernel/sshbuf.h>
       
    31 
       
    32 const TInt KRamChunkSize=0x10000;
       
    33 const TInt KRamChunkShift=16;
       
    34 const TInt KRamPageSize=0x1000;
       
    35 const TInt KRamPageShift=12;
       
    36 
       
    37 /********************************************
       
    38  * Thread Control Block
       
    39  ********************************************/
       
    40 
       
    41 class DWin32Thread : public DThread
       
    42 	{
       
    43 public:
       
    44 	~DWin32Thread();
       
    45 	virtual TInt Context(TDes8& aDes);
       
    46 	virtual TInt SetupContext(SThreadCreateInfo& anInfo);
       
    47 	virtual void DoExit2();
       
    48 	};
       
    49 
       
    50 /********************************************
       
    51  * Process Control Block
       
    52  ********************************************/
       
    53 class DWin32CodeSeg;
       
    54 class DWin32Chunk;
       
    55 struct SProcessDllDataBlock;
       
    56 class DWin32Process : public DProcess
       
    57 	{
       
    58 public:
       
    59 	DWin32Process();
       
    60 	~DWin32Process();
       
    61 public:
       
    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);
       
    75 public:
       
    76 	TWin32RuntimeHook iWin32RuntimeHook;
       
    77 	RArray<SProcessDllDataBlock> iDllData;
       
    78 	};
       
    79 
       
    80 /******************************************************************
       
    81  * structure to keep static data in code segments within a process
       
    82  ******************************************************************/
       
    83 struct SProcessDllDataBlock
       
    84 	{
       
    85 	DWin32CodeSeg* iCodeSeg;
       
    86 	TAny* iDataCopy;			// copy of .data
       
    87 	TAny* iBssCopy;				// copy of .bss
       
    88 	};
       
    89 
       
    90 /********************************************
       
    91  * Chunk Control Block
       
    92  ********************************************/
       
    93 class DWin32Chunk : public DChunk
       
    94 	{
       
    95 public:
       
    96 	enum TMemModelChunkAttributes
       
    97 		{
       
    98 		EPrivate=0x10000000,
       
    99 
       
   100 		EMMChunkAttributesMask = EPrivate,
       
   101 		};
       
   102 
       
   103 public:
       
   104 	~DWin32Chunk();
       
   105 public:
       
   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(); }
       
   121 private:
       
   122 	TInt DoCommit(TInt aOffset, TInt aSize);
       
   123 	void DoDecommit(TInt aOffset, TInt aSize);
       
   124 public:
       
   125 	TBitMapAllocator* iPageBitMap;
       
   126 	TBitMapAllocator* iUnlockedPageBitMap;
       
   127 	TBitMapAllocator* iPermanentPageBitMap;
       
   128 	};
       
   129 
       
   130 /********************************************
       
   131  * Code segment
       
   132  ********************************************/
       
   133 class DModuleList;
       
   134 class DWin32CodeSeg: public DCodeSeg
       
   135 	{
       
   136 public:
       
   137 	DWin32CodeSeg();
       
   138 	virtual ~DWin32CodeSeg();
       
   139 	TInt ProcessImports(DProcess* aProcess);
       
   140 	TInt CreateAlreadyLoaded(HMODULE aModule, TInt aDepCount);
       
   141 	TInt RegisterCodeSeg(HMODULE aModule);
       
   142 	TInt CopyDataBss();
       
   143 public:
       
   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);
       
   153 public:
       
   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
       
   160 	TInt iRealDataSize;
       
   161 	TInt iRealBssSize;
       
   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
       
   166 	};
       
   167 
       
   168 struct SWin32Module
       
   169 	{
       
   170 	TLinAddr iWin32ModuleHandle;
       
   171 	DWin32CodeSeg* iCodeSeg;
       
   172 	};
       
   173 
       
   174 	
       
   175 /********************************************
       
   176  * Functions/Data defined in memory model
       
   177  ********************************************/
       
   178 
       
   179 class MM
       
   180 	{
       
   181 public:
       
   182 	enum TMemModelPanic
       
   183 		{
       
   184 		EKernelHeapReserveFailed = 0,
       
   185 		EKernelHeapCommitFailed = 1,
       
   186 		ERamAllocMutexCreateFailed = 2,
       
   187 		EInvalidChunkCreate = 3,
       
   188 		EInvalidSharedModule = 4,
       
   189 		ECompileDepLists=5,
       
   190 		EWin32RuntimeError=6,
       
   191 		ENotSupportedOnEmulator=7,
       
   192 		EWsdBadReserve=8,
       
   193 		EWsdDllNotInProcess=9,
       
   194     	};
       
   195 
       
   196 	static void Panic(TMemModelPanic aPanic);
       
   197 public:
       
   198 	static void Init1();
       
   199 	static void Wait();
       
   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);
       
   209 public:
       
   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;
       
   223 	};
       
   224 
       
   225 
       
   226 /********************************************
       
   227  * Shared buffers and pools
       
   228  ********************************************/
       
   229 
       
   230 class DWin32ShBuf : public DShBuf
       
   231 	{
       
   232 public:
       
   233 	DWin32ShBuf(DShPool* aPool, TLinAddr aRelAddr);
       
   234 	~DWin32ShBuf();
       
   235 
       
   236 	TInt AddToProcess(DProcess* aProcess, TUint aAttr);
       
   237 
       
   238 	TInt Close(TAny* aPtr);
       
   239 
       
   240 protected:
       
   241 	virtual TInt Map(TUint, DProcess*, TLinAddr&);
       
   242 	virtual TInt UnMap(DProcess*);
       
   243 	virtual TUint8* Base(DProcess* aProcess);
       
   244 	virtual TUint8* Base();
       
   245 
       
   246 private:
       
   247 	TBool iMapped;
       
   248 	};
       
   249 
       
   250 
       
   251 class DWin32ShPool : public DShPool
       
   252 	{
       
   253 public:
       
   254 	DWin32ShPool();
       
   255 	virtual ~DWin32ShPool();
       
   256 
       
   257 	TInt Close(TAny* aPtr);
       
   258 	TInt AddToProcess(DProcess* aProcess, TUint aAttr);
       
   259 	TInt Alloc(DShBuf*&);
       
   260 
       
   261 protected:
       
   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);
       
   268 
       
   269 	void Free(DShBuf* aBuf);
       
   270 	TInt UpdateFreeList();
       
   271 
       
   272 	TUint8* Base();
       
   273 	TUint8* Base(DProcess* aProcess);
       
   274 	TBool IsOpen(DProcess* aProcess);
       
   275 
       
   276 	TBitMapAllocator* iBufMap;
       
   277 	DWin32ShBuf* iInitialBuffersArray;
       
   278 	TUint8* iWin32MemoryBase;
       
   279 	TInt iWin32MemorySize;
       
   280 	friend class DWin32ShBuf;
       
   281 	};
       
   282 
       
   283 
       
   284 class DWin32AlignedShPool : public DWin32ShPool
       
   285 	{
       
   286 public:
       
   287 	DWin32AlignedShPool();
       
   288 	virtual ~DWin32AlignedShPool();
       
   289 	TInt SetBufferWindow(DProcess* aProcess, TInt aWindowSize);
       
   290 
       
   291 private:
       
   292 	TInt DoCreate(TShPoolCreateInfo& aInfo);
       
   293 	TInt GrowPool();
       
   294 	TInt ShrinkPool();
       
   295 	};
       
   296 
       
   297 
       
   298 class DWin32NonAlignedShPool : public DWin32ShPool
       
   299 	{
       
   300 public:
       
   301 	DWin32NonAlignedShPool();
       
   302 	virtual ~DWin32NonAlignedShPool();
       
   303 
       
   304 private:
       
   305 	TInt DoCreate(TShPoolCreateInfo& aInfo);
       
   306 	void FreeBufferPages(TUint aOffset);
       
   307 	TInt GrowPool();
       
   308 	TInt ShrinkPool();
       
   309 
       
   310 	TBitMapAllocator* iPagesMap;
       
   311 	};
       
   312 
       
   313 #endif	// __WIN32_MEM_H__