os/kernelhwsrv/kernel/eka/include/emulator.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
parent 0 bde4ae8d615e
permissions -rw-r--r--
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".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // e32\include\emulator.h
    15 // 
    16 //
    17 
    18 /**
    19  @file
    20  @publishedPartner
    21  @released
    22 */
    23 
    24 #ifndef __EMULATOR_H__
    25 #define __EMULATOR_H__
    26 
    27 #include <e32def.h>
    28 #ifndef WIN32_LEAN_AND_MEAN
    29 #	define WIN32_LEAN_AND_MEAN
    30 #endif
    31 #pragma warning( disable : 4201 ) // nonstandard extension used : nameless struct/union
    32 #include <windows.h>
    33 #pragma warning( default : 4201 ) // nonstandard extension used : nameless struct/union
    34 
    35 class TUidType;
    36 class TProcessCreateInfo;
    37 
    38 static const BYTE KWin32SectionName_Symbian[IMAGE_SIZEOF_SHORT_NAME]	= {'.','S','Y','M','B','I','A','N'};
    39 static const BYTE KWin32SectionName_Import[IMAGE_SIZEOF_SHORT_NAME]		= {'.','i','d','a','t','a','\0','\0'};
    40 static const BYTE KWin32SectionName_EpocData[IMAGE_SIZEOF_SHORT_NAME]	= {'.','d','a','t','a','\0','\0','\0'};
    41 static const BYTE KWin32SectionName_EpocBss[IMAGE_SIZEOF_SHORT_NAME]	= {'.','b','s','s','\0','\0','\0','\0'};
    42 static const BYTE KWin32SectionName_Text[IMAGE_SIZEOF_SHORT_NAME]		= {'.','t','e','x','t','\0','\0','\0'};
    43 static const BYTE KWin32SectionName_RData[IMAGE_SIZEOF_SHORT_NAME]		= {'.','r','d','a','t','a','\0','\0'};
    44 static const BYTE KWin32SectionName_NmdExpData[IMAGE_SIZEOF_SHORT_NAME]	= {'.','e','x','p','d','a','t','a'};
    45 
    46 /**
    47 Emulator utility functions. These APIs should only be used for extensions to emulator 
    48 functionality and are unsupported on non-emulator platforms.
    49 
    50 @publishedPartner
    51 @released
    52 */
    53 struct Emulator
    54 	{
    55 public:
    56 
    57 	/**
    58 	Declares a function pointer to TLock.
    59 	
    60 	@see TLock.
    61 	*/
    62 	typedef void (*TLockFunc)();
    63 	
    64 	/**
    65 	Declares a function pointer to Win32SEHException.
    66 	
    67 	@see Win32SEHException.		
    68 	*/
    69 	typedef DWORD (*TExceptionFunc)(EXCEPTION_RECORD* aException, CONTEXT* aContext);
    70 	
    71 	/**
    72 	A structure which packs parameters to be inintialized. 
    73 	*/
    74 	struct SInit
    75 		{
    76 		TUint iCodePage;
    77 		TLockFunc iLock;
    78 		TLockFunc iUnlock;
    79 		TLockFunc iEscape;
    80 		TLockFunc iReenter;
    81 		TExceptionFunc iException;
    82 		};
    83 	
    84 	/**
    85 	A structure which handles the global lock for host interaction.
    86 	*/
    87 	struct TLock
    88 		{
    89 		
    90 		/**
    91 		Constructor which calls the Emulator::Lock() function.
    92 		
    93 		@see Emulator::Lock().
    94 		*/
    95 		inline TLock()
    96 			{Emulator::Lock();}
    97 		
    98 		/**
    99 		Destructor which calls the Emulator::Unlock() function.
   100 		
   101 		@see Emulator::Unlock().
   102 		*/
   103 		inline ~TLock()
   104 			{Emulator::Unlock();}
   105 		};
   106 		
   107 	/**
   108 	A structure which handles current thread by taking in or out of the emulator scheduling model.
   109 	*/
   110 	struct TEscape
   111 		{
   112 		
   113 		/**
   114 		Constructor which calls the Emulator::Escape() function.
   115 		
   116 		@see Emulator::Escape().
   117 		*/
   118 		inline TEscape()
   119 			{Emulator::Escape();}
   120 		
   121 		/**
   122 		Destructor which calls the Emulator::Reenter() function.
   123 		
   124 		@see Emulator::Reenter().
   125 		*/
   126 		inline ~TEscape()
   127 			{Emulator::Reenter();}
   128 		};
   129 #define __LOCK_HOST Emulator::TLock __lock
   130 #define __ESCAPE_HOST Emulator::TEscape __escape
   131 public:
   132 
   133 	IMPORT_C static void Init(const SInit& aInit);
   134 //
   135 	IMPORT_C static void Lock();
   136 	IMPORT_C static void Unlock();
   137 	IMPORT_C static void Escape();
   138 	IMPORT_C static void Reenter();
   139 //
   140 	IMPORT_C static DWORD Win32SEHException(EXCEPTION_RECORD* aException, CONTEXT* aContext);
   141 //
   142 	IMPORT_C static TInt LastError();
   143 	IMPORT_C static BOOL CreateDirectory(LPCTSTR, LPSECURITY_ATTRIBUTES);
   144 	IMPORT_C static BOOL CreateAllDirectories(LPCSTR);
   145 	IMPORT_C static HANDLE CreateFile(LPCTSTR ,DWORD ,DWORD ,LPSECURITY_ATTRIBUTES ,DWORD ,DWORD ,HANDLE);
   146 	IMPORT_C static BOOL DeleteFile(LPCTSTR);
   147 	IMPORT_C static HANDLE FindFirstFile(LPCTSTR ,LPWIN32_FIND_DATA);	
   148 	IMPORT_C static BOOL FindNextFile(HANDLE ,LPWIN32_FIND_DATA);
   149 	IMPORT_C static BOOL GetDiskFreeSpace(LPCTSTR ,LPDWORD ,LPDWORD ,LPDWORD ,LPDWORD);
   150 	IMPORT_C static DWORD GetFileAttributes(LPCTSTR);
   151 	IMPORT_C static HMODULE GetModuleHandle(LPCTSTR);
   152 	IMPORT_C static DWORD GetCurrentDirectory(DWORD ,LPTSTR);
   153 	IMPORT_C static DWORD GetTempPath(DWORD ,LPTSTR);
   154 	IMPORT_C static BOOL GetVolumeInformation(LPCTSTR ,LPTSTR ,DWORD ,LPDWORD ,LPDWORD ,LPDWORD ,LPTSTR,DWORD);
   155 	IMPORT_C static HMODULE LoadLibrary(LPCTSTR);	
   156 	IMPORT_C static BOOL FreeLibrary(HMODULE);	 
   157 	IMPORT_C static BOOL MoveFile(LPCTSTR ,LPCTSTR);
   158 	IMPORT_C static BOOL CopyFile(LPCTSTR ,LPCTSTR, BOOL);
   159 	IMPORT_C static VOID OutputDebugString(LPCTSTR);	
   160 	IMPORT_C static BOOL RemoveDirectory(LPCTSTR);
   161 	IMPORT_C static BOOL SetFileAttributes(LPCTSTR ,DWORD);	
   162 	IMPORT_C static BOOL SetVolumeLabel(LPCTSTR ,LPCTSTR);	
   163 	IMPORT_C static FARPROC GetProcAddress(HMODULE, LPCSTR);
   164 	IMPORT_C static DWORD GetModuleFileName(HMODULE hModule, LPWSTR lpFilename);
   165 public:
   166 
   167 	/**
   168 	A class which holds a loaded module of the file system. 
   169 	*/
   170 	class TModule
   171 		{
   172 	public:
   173 	
   174 		/**
   175 		Constructor which sets the handles of loaded module	to current instance of the application.
   176 		
   177 		@param aModule		A handle to the current instance of the application.
   178 		*/
   179 		inline TModule(HINSTANCE aModule)
   180 			:iModule(aModule), iBase(aModule)
   181 			{}
   182 			
   183 		IMPORT_C TModule(LPCSTR aModuleName);
   184 		
   185 		/**
   186 		Checks the validity of the loaded module.
   187 		
   188 		@return		TRUE, if the function succeeds
   189 					FALSE, if the function fails		 
   190 		*/
   191 		inline TBool IsValid() const
   192 			{return iBase!=NULL;}
   193 			
   194 		/**
   195 		Relocates the address of the loaded module by appending aRawVirtualAddress to the handle of loaded module.
   196 		
   197 		@param aRawVirtualAddress	Contains the address of the first byte of the section when loaded into 
   198 									memory, relative to the image base. For object files, this is the 
   199 									address of the first byte before relocation is applied.
   200 		
   201 		@return 	Returns the actual address of the handle.
   202 		*/
   203 		inline const TAny* Translate(TInt32 aRawVirtualAddress) const
   204 			{return (const TUint8*)iBase + aRawVirtualAddress;}
   205 	
   206 		
   207 		IMPORT_C void GetUids(TUidType& aType) const;
   208 		IMPORT_C void GetInfo(TProcessCreateInfo& aInfo) const;
   209 	public:
   210 	
   211 		/**
   212 		Default constructor which sets the handles of loaded module or mapped file image to the default handle.	
   213 		*/
   214 		inline TModule()
   215 			:iModule(0), iBase(0)
   216 			{}
   217 	
   218 		/**
   219 		Checks whether the current module is a loaded module, or just a mapped file image.
   220 		
   221 		@return		TRUE, if the current module is a loaded module 
   222 					FALSE, if the current module is a mapped image 
   223 		*/
   224 		TBool IsLoaded() const		// Is this a loaded module, or just a mapped file image.
   225 			{return iModule != 0;}
   226 			
   227 		IMPORT_C const IMAGE_NT_HEADERS32* NtHeader() const;
   228 		
   229 		/**
   230 		Passes the handle of a loaded module to the calling module.
   231 		
   232 		@return 	Returns a handle of a loaded module.
   233 	    */
   234 		const TAny* Base() const
   235 			{return iBase;}
   236 
   237 		IMPORT_C const IMAGE_SECTION_HEADER* SectionHeader(const BYTE aSection[]) const;
   238 		IMPORT_C const TAny* Section(const BYTE aSection[]) const;	
   239 		IMPORT_C const IMAGE_IMPORT_DESCRIPTOR* Imports() const;
   240 	public:
   241 	
   242 		/**
   243 		A handle to a loaded module.
   244 		*/
   245 		HMODULE iModule;
   246 		
   247 		/**
   248 		A handle to a loaded module for the mapped view.
   249 		*/
   250 		const TAny* iBase;
   251 		};
   252 		
   253 	/**
   254 	A class which holds a mapped image of the file system.
   255 	*/
   256 	class RImageFile : public TModule
   257 		{
   258 	public:
   259 	
   260 		/**
   261 		Constructor which sets the handle of the mapped image file to default handle.	
   262 		*/
   263 		inline RImageFile()
   264 			: iMapping(0)
   265 			{}
   266 			
   267 		IMPORT_C TInt Open(LPCTSTR aImageFile);
   268 		IMPORT_C void Close();
   269 	private:
   270 	
   271 		/**
   272 		A handle to the mapped image file.
   273 		*/
   274 		HANDLE iMapping;
   275 		};
   276 	};
   277 
   278 
   279 #endif