os/kernelhwsrv/kernel/eka/include/memmodel/emul/win32/memmodel.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32\include\memmodel\emul\win32\memmodel.h
sl@0
    15
// 
sl@0
    16
// WARNING: This file contains some APIs which are internal and are subject
sl@0
    17
//          to change without notice. Such APIs should therefore not be used
sl@0
    18
//          outside the Kernel and Hardware Services package.
sl@0
    19
//
sl@0
    20
sl@0
    21
/**
sl@0
    22
 @file
sl@0
    23
 @internalComponent
sl@0
    24
*/
sl@0
    25
sl@0
    26
#ifndef __WIN32_MEM_H__
sl@0
    27
#define __WIN32_MEM_H__
sl@0
    28
#include <win32.h>
sl@0
    29
#include <plat_priv.h>
sl@0
    30
#include <kernel/sshbuf.h>
sl@0
    31
sl@0
    32
const TInt KRamChunkSize=0x10000;
sl@0
    33
const TInt KRamChunkShift=16;
sl@0
    34
const TInt KRamPageSize=0x1000;
sl@0
    35
const TInt KRamPageShift=12;
sl@0
    36
sl@0
    37
/********************************************
sl@0
    38
 * Thread Control Block
sl@0
    39
 ********************************************/
sl@0
    40
sl@0
    41
class DWin32Thread : public DThread
sl@0
    42
	{
sl@0
    43
public:
sl@0
    44
	~DWin32Thread();
sl@0
    45
	virtual TInt Context(TDes8& aDes);
sl@0
    46
	virtual TInt SetupContext(SThreadCreateInfo& anInfo);
sl@0
    47
	virtual void DoExit2();
sl@0
    48
	};
sl@0
    49
sl@0
    50
/********************************************
sl@0
    51
 * Process Control Block
sl@0
    52
 ********************************************/
sl@0
    53
class DWin32CodeSeg;
sl@0
    54
class DWin32Chunk;
sl@0
    55
struct SProcessDllDataBlock;
sl@0
    56
class DWin32Process : public DProcess
sl@0
    57
	{
sl@0
    58
public:
sl@0
    59
	DWin32Process();
sl@0
    60
	~DWin32Process();
sl@0
    61
public:
sl@0
    62
	virtual TInt DoCreate(TBool aKernelProcess, TProcessCreateInfo& aInfo);
sl@0
    63
	virtual TInt NewChunk(DChunk*& aChunk, SChunkCreateInfo& aInfo, TLinAddr& aRunAddr);
sl@0
    64
	virtual TInt AddChunk(DChunk* aChunk,TBool isReadOnly);
sl@0
    65
	virtual TInt NewShPool(DShPool*& aPool, TShPoolCreateInfo& aInfo);
sl@0
    66
	virtual TInt CreateDataBssStackArea(TProcessCreateInfo& aInfo);
sl@0
    67
	virtual TInt GetNewThread(DThread*& aThread, SThreadCreateInfo& anInfo);
sl@0
    68
	virtual TInt AttachExistingCodeSeg(TProcessCreateInfo& aInfo);
sl@0
    69
	virtual TInt MapCodeSeg(DCodeSeg* aCodeSeg);
sl@0
    70
	virtual void UnmapCodeSeg(DCodeSeg* aCodeSeg);
sl@0
    71
	virtual void RemoveDllData();
sl@0
    72
	virtual void FinalRelease();
sl@0
    73
	virtual void Release();
sl@0
    74
	void CallRuntimeHook(TWin32RuntimeReason aReason);
sl@0
    75
public:
sl@0
    76
	TWin32RuntimeHook iWin32RuntimeHook;
sl@0
    77
	RArray<SProcessDllDataBlock> iDllData;
sl@0
    78
	};
sl@0
    79
sl@0
    80
/******************************************************************
sl@0
    81
 * structure to keep static data in code segments within a process
sl@0
    82
 ******************************************************************/
sl@0
    83
struct SProcessDllDataBlock
sl@0
    84
	{
sl@0
    85
	DWin32CodeSeg* iCodeSeg;
sl@0
    86
	TAny* iDataCopy;			// copy of .data
sl@0
    87
	TAny* iBssCopy;				// copy of .bss
sl@0
    88
	};
sl@0
    89
sl@0
    90
/********************************************
sl@0
    91
 * Chunk Control Block
sl@0
    92
 ********************************************/
sl@0
    93
class DWin32Chunk : public DChunk
sl@0
    94
	{
sl@0
    95
public:
sl@0
    96
	enum TMemModelChunkAttributes
sl@0
    97
		{
sl@0
    98
		EPrivate=0x10000000,
sl@0
    99
sl@0
   100
		EMMChunkAttributesMask = EPrivate,
sl@0
   101
		};
sl@0
   102
sl@0
   103
public:
sl@0
   104
	~DWin32Chunk();
sl@0
   105
public:
sl@0
   106
	virtual TInt DoCreate(SChunkCreateInfo& aInfo);
sl@0
   107
	virtual TInt Adjust(TInt aNewSize);
sl@0
   108
	virtual TInt AdjustDoubleEnded(TInt aBottom, TInt aTop);
sl@0
   109
	virtual TInt CheckAccess();
sl@0
   110
	virtual TInt Commit(TInt anOffset, TInt aSize, TCommitType aCommitType=DChunk::ECommitDiscontiguous, TUint32* aExtraArg=0);
sl@0
   111
	virtual TInt Allocate(TInt aSize, TInt aGuard=0, TInt aAlign=0);
sl@0
   112
	virtual TInt Decommit(TInt anOffset, TInt aSize);
sl@0
   113
	virtual TInt Lock(TInt anOffset, TInt aSize);
sl@0
   114
	virtual TInt Unlock(TInt anOffset, TInt aSize);
sl@0
   115
	virtual TInt Address(TInt aOffset, TInt aSize, TLinAddr& aKernelAddress);
sl@0
   116
	virtual TInt PhysicalAddress(TInt aOffset, TInt aSize, TLinAddr& aKernelAddress, TUint32& aPhysicalAddress, TUint32* aPhysicalPageList=NULL);
sl@0
   117
	virtual void BTracePrime(TInt aCategory);
sl@0
   118
	virtual void Substitute(TInt aOffset, TPhysAddr aOldAddr, TPhysAddr aNewAddr);
sl@0
   119
	virtual TUint8* Base(DProcess* aProcess);
sl@0
   120
	inline TUint8* Base() const { return DChunk::Base(); }
sl@0
   121
private:
sl@0
   122
	TInt DoCommit(TInt aOffset, TInt aSize);
sl@0
   123
	void DoDecommit(TInt aOffset, TInt aSize);
sl@0
   124
public:
sl@0
   125
	TBitMapAllocator* iPageBitMap;
sl@0
   126
	TBitMapAllocator* iUnlockedPageBitMap;
sl@0
   127
	TBitMapAllocator* iPermanentPageBitMap;
sl@0
   128
	};
sl@0
   129
sl@0
   130
/********************************************
sl@0
   131
 * Code segment
sl@0
   132
 ********************************************/
sl@0
   133
class DModuleList;
sl@0
   134
class DWin32CodeSeg: public DCodeSeg
sl@0
   135
	{
sl@0
   136
public:
sl@0
   137
	DWin32CodeSeg();
sl@0
   138
	virtual ~DWin32CodeSeg();
sl@0
   139
	TInt ProcessImports(DProcess* aProcess);
sl@0
   140
	TInt CreateAlreadyLoaded(HMODULE aModule, TInt aDepCount);
sl@0
   141
	TInt RegisterCodeSeg(HMODULE aModule);
sl@0
   142
	TInt CopyDataBss();
sl@0
   143
public:
sl@0
   144
	virtual TLibraryFunction Lookup(TInt aOrdinal);
sl@0
   145
	virtual TInt GetMemoryInfo(TModuleMemoryInfo& aInfo, DProcess* aProcess);
sl@0
   146
	virtual TInt DoCreate(TCodeSegCreateInfo& aInfo, DProcess* aProcess);
sl@0
   147
	virtual TInt Loaded(TCodeSegCreateInfo& aInfo);
sl@0
   148
	virtual void InitData();
sl@0
   149
	virtual void ReadExportDir(TUint32* aDest);
sl@0
   150
	virtual TBool FindCheck(DProcess* aProcess);
sl@0
   151
	virtual TBool OpenCheck(DProcess* aProcess);
sl@0
   152
	virtual void Info(TCodeSegCreateInfo& aInfo);
sl@0
   153
public:
sl@0
   154
	HINSTANCE iWinInstance;
sl@0
   155
	HMODULE iModuleHandle;
sl@0
   156
	wchar_t* iModuleFile;
sl@0
   157
	TBool iAlwaysLoaded;		// TRUE for variant or extension
sl@0
   158
	DModuleList* iModuleList;
sl@0
   159
	TAny* iDataCopy;			// copy of .data
sl@0
   160
	TInt iRealDataSize;
sl@0
   161
	TInt iRealBssSize;
sl@0
   162
	TLinAddr iDataDest;			// load address of .data
sl@0
   163
	TLinAddr iBssDest;			// load address of .bss
sl@0
   164
	TInt iCodeSegId;			// unique ID, incremented each time a code segment is loaded
sl@0
   165
	DWin32Process* iLiveProcess;// process who's static data is currently loaded in the codeseg
sl@0
   166
	};
sl@0
   167
sl@0
   168
struct SWin32Module
sl@0
   169
	{
sl@0
   170
	TLinAddr iWin32ModuleHandle;
sl@0
   171
	DWin32CodeSeg* iCodeSeg;
sl@0
   172
	};
sl@0
   173
sl@0
   174
	
sl@0
   175
/********************************************
sl@0
   176
 * Functions/Data defined in memory model
sl@0
   177
 ********************************************/
sl@0
   178
sl@0
   179
class MM
sl@0
   180
	{
sl@0
   181
public:
sl@0
   182
	enum TMemModelPanic
sl@0
   183
		{
sl@0
   184
		EKernelHeapReserveFailed = 0,
sl@0
   185
		EKernelHeapCommitFailed = 1,
sl@0
   186
		ERamAllocMutexCreateFailed = 2,
sl@0
   187
		EInvalidChunkCreate = 3,
sl@0
   188
		EInvalidSharedModule = 4,
sl@0
   189
		ECompileDepLists=5,
sl@0
   190
		EWin32RuntimeError=6,
sl@0
   191
		ENotSupportedOnEmulator=7,
sl@0
   192
		EWsdBadReserve=8,
sl@0
   193
		EWsdDllNotInProcess=9,
sl@0
   194
    	};
sl@0
   195
sl@0
   196
	static void Panic(TMemModelPanic aPanic);
sl@0
   197
public:
sl@0
   198
	static void Init1();
sl@0
   199
	static void Wait();
sl@0
   200
	static void Signal();
sl@0
   201
	static TUint32 RoundToPageSize(TUint32 aSize);
sl@0
   202
	static TUint32 RoundToChunkSize(TUint32 aSize);
sl@0
   203
	static TInt RegisterModule(HMODULE aModule);
sl@0
   204
	static TInt Commit(TLinAddr aBase, TInt aSize, TInt aClearByte, TBool aExecute);
sl@0
   205
	static TInt Decommit(TLinAddr aBase, TInt aSize);
sl@0
   206
	static void CheckMemoryCounters();
sl@0
   207
	static void DoProcessSwitch(TAny* aAddressSpace);
sl@0
   208
	static TAny* CurrentAddress(DThread* aThread, const TAny* aPtr, TInt aSize, TBool aWrite, TBool& aLocked);
sl@0
   209
public:
sl@0
   210
	static TAny* KernelHeapAddress;
sl@0
   211
	static DMutex* RamAllocatorMutex;
sl@0
   212
	static TInt RamChunkSize;
sl@0
   213
	static TInt RamChunkShift;
sl@0
   214
	static TInt RamPageSize;
sl@0
   215
	static TInt RamPageShift;
sl@0
   216
	static TInt FreeMemory;	// number of bytes in the system free memory 
sl@0
   217
	static TInt CacheMemory; // number of bytes of memory being used for cache chunks (RChunk::Unlock)
sl@0
   218
	static TInt ReclaimedCacheMemory; // number of bytes of memory removed from CacheMemory in order to satisfy memory allocation
sl@0
   219
	static TInt InitialFreeMemory;
sl@0
   220
	static TBool AllocFailed;
sl@0
   221
	static RArray<SWin32Module> Win32Modules;
sl@0
   222
	static TInt NextCodeSegId;
sl@0
   223
	};
sl@0
   224
sl@0
   225
sl@0
   226
/********************************************
sl@0
   227
 * Shared buffers and pools
sl@0
   228
 ********************************************/
sl@0
   229
sl@0
   230
class DWin32ShBuf : public DShBuf
sl@0
   231
	{
sl@0
   232
public:
sl@0
   233
	DWin32ShBuf(DShPool* aPool, TLinAddr aRelAddr);
sl@0
   234
	~DWin32ShBuf();
sl@0
   235
sl@0
   236
	TInt AddToProcess(DProcess* aProcess, TUint aAttr);
sl@0
   237
sl@0
   238
	TInt Close(TAny* aPtr);
sl@0
   239
sl@0
   240
protected:
sl@0
   241
	virtual TInt Map(TUint, DProcess*, TLinAddr&);
sl@0
   242
	virtual TInt UnMap(DProcess*);
sl@0
   243
	virtual TUint8* Base(DProcess* aProcess);
sl@0
   244
	virtual TUint8* Base();
sl@0
   245
sl@0
   246
private:
sl@0
   247
	TBool iMapped;
sl@0
   248
	};
sl@0
   249
sl@0
   250
sl@0
   251
class DWin32ShPool : public DShPool
sl@0
   252
	{
sl@0
   253
public:
sl@0
   254
	DWin32ShPool();
sl@0
   255
	virtual ~DWin32ShPool();
sl@0
   256
sl@0
   257
	TInt Close(TAny* aPtr);
sl@0
   258
	TInt AddToProcess(DProcess* aProcess, TUint aAttr);
sl@0
   259
	TInt Alloc(DShBuf*&);
sl@0
   260
sl@0
   261
protected:
sl@0
   262
	TInt DoCreate(TShPoolCreateInfo& aInfo);
sl@0
   263
	TInt AddToFreeList(TInt aOffset);
sl@0
   264
	TInt CreateInitialBuffers();
sl@0
   265
	TInt DeleteInitialBuffers();
sl@0
   266
	TInt DestroyHandles(DProcess* aProcess);
sl@0
   267
	void DestroyClientResources(DProcess* aProcess);
sl@0
   268
sl@0
   269
	void Free(DShBuf* aBuf);
sl@0
   270
	TInt UpdateFreeList();
sl@0
   271
sl@0
   272
	TUint8* Base();
sl@0
   273
	TUint8* Base(DProcess* aProcess);
sl@0
   274
	TBool IsOpen(DProcess* aProcess);
sl@0
   275
sl@0
   276
	TBitMapAllocator* iBufMap;
sl@0
   277
	DWin32ShBuf* iInitialBuffersArray;
sl@0
   278
	TUint8* iWin32MemoryBase;
sl@0
   279
	TInt iWin32MemorySize;
sl@0
   280
	friend class DWin32ShBuf;
sl@0
   281
	};
sl@0
   282
sl@0
   283
sl@0
   284
class DWin32AlignedShPool : public DWin32ShPool
sl@0
   285
	{
sl@0
   286
public:
sl@0
   287
	DWin32AlignedShPool();
sl@0
   288
	virtual ~DWin32AlignedShPool();
sl@0
   289
	TInt SetBufferWindow(DProcess* aProcess, TInt aWindowSize);
sl@0
   290
sl@0
   291
private:
sl@0
   292
	TInt DoCreate(TShPoolCreateInfo& aInfo);
sl@0
   293
	TInt GrowPool();
sl@0
   294
	TInt ShrinkPool();
sl@0
   295
	};
sl@0
   296
sl@0
   297
sl@0
   298
class DWin32NonAlignedShPool : public DWin32ShPool
sl@0
   299
	{
sl@0
   300
public:
sl@0
   301
	DWin32NonAlignedShPool();
sl@0
   302
	virtual ~DWin32NonAlignedShPool();
sl@0
   303
sl@0
   304
private:
sl@0
   305
	TInt DoCreate(TShPoolCreateInfo& aInfo);
sl@0
   306
	void FreeBufferPages(TUint aOffset);
sl@0
   307
	TInt GrowPool();
sl@0
   308
	TInt ShrinkPool();
sl@0
   309
sl@0
   310
	TBitMapAllocator* iPagesMap;
sl@0
   311
	};
sl@0
   312
sl@0
   313
#endif	// __WIN32_MEM_H__