Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

398 lines
15 KiB

  1. /*++
  2. INTEL CORPORATION PROPRIETARY INFORMATION
  3. This software is supplied under the terms of a license
  4. agreement or nondisclosure agreement with Intel Corporation
  5. and may not be copied or disclosed except in accordance with
  6. the terms of that agreement.
  7. Copyright (c) 1991-2002 INTEL CORPORATION
  8. Module Name:
  9. BTLib.h
  10. Abstract:
  11. Windows-specific definitions used by wowIA32X.dll
  12. --*/
  13. #ifndef BTLIB_H
  14. #define BTLIB_H
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #if !defined(_WOW64BTAPI_)
  19. #define WOW64BTAPI DECLSPEC_IMPORT
  20. #else
  21. #define WOW64BTAPI
  22. #endif
  23. #include <nt.h>
  24. #include <ntrtl.h>
  25. #include <nturtl.h>
  26. #include <windows.h>
  27. #if defined(BT_NT_BUILD)
  28. #include <wow64t.h>
  29. #endif
  30. #include <stddef.h>
  31. #include <setjmp.h> // jmp_buf
  32. #include <assert.h>
  33. #include <tstr.h>
  34. #include <string.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. //
  38. // BTGENERIC_IA32_CONTEXT and BTGENERIC_IA64_CONTEXT and the api table types
  39. // are shared between wowIA32X.dll and IA32Exec.bin.
  40. // This h file specifies the structs using U## types
  41. // which should be defined properly for the wowIA32X.dll library compiler:
  42. //
  43. // U8 8bit unsigned type
  44. // U32 32bit unsigned type
  45. // S32 32bit signed type
  46. // U64 64bit unsigned type
  47. // WCHAR wide-character type
  48. //
  49. #define U8 unsigned char
  50. #define U32 unsigned int
  51. #define S32 int
  52. #define U64 unsigned __int64
  53. #include "BTGeneric.h"
  54. #ifndef NODEBUG
  55. #define DBCODE(switch, expr) do{if(switch){expr;}}while(0)
  56. #else
  57. #define DBCODE(switch, expr) // nothing
  58. #endif // DEBUG
  59. #ifdef COEXIST // IVE coexistance mode
  60. #define BTAPI(NAME) BTCpu##NAME
  61. #else
  62. #define BTAPI(NAME) Cpu##NAME
  63. #endif
  64. // NT64 OS specifics
  65. #define BT_CURRENT_TEB() NtCurrentTeb()
  66. #define BT_TEB32_OF(pTEB) (PTEB32)((pTEB)->NtTib.ExceptionList)
  67. #define BT_CURRENT_TEB32() BT_TEB32_OF(BT_CURRENT_TEB())
  68. #define BT_TLS_OF(pTEB) (void *)((pTEB)->TlsSlots[1])
  69. #define BT_CURRENT_TLS() BT_TLS_OF(BT_CURRENT_TEB())
  70. #define BT_TLS_OFFSET offsetof (TEB, TlsSlots[1])
  71. #define BTL_THREAD_INITIALIZED() (BT_CURRENT_TLS() != 0)
  72. //Get unique (throughout the system) ID of the current process
  73. #define BT_CURRENT_PROC_UID() ((U64)(BT_CURRENT_TEB()->ClientId.UniqueProcess))
  74. //Get unique (throughout the system) ID of the current thread
  75. #define BT_CURRENT_THREAD_UID() ((U64)(BT_CURRENT_TEB()->ClientId.UniqueThread))
  76. //Current wowIA32X.dll signature. Used to check BTLIB_INFO_TYPE compatibility from a remote process
  77. #define BTL_SIGNATURE 0x42544C4942012E02 /*1.2*/
  78. //STRUCTURE: BTLIB_SHARED_INFO_TYPE
  79. //Part of the wowIA32X.dll-thread local storage accessible by remote process
  80. //when suspended.
  81. //LOCAL THREAD ACCESS: a)initialization, b)reading, c)changing SuspendDisabledCounter
  82. //EXTERNAL THREAD ACCESS: a)reading, b)setting SuspendRequest
  83. //DO NOT use conditional compilation (#ifdef) inside this structure - remote
  84. //process suppose the same structure if BTL_SIGNATURE matches.
  85. //Any change to the structure must be accompanied by changing BTL_SIGNATURE.
  86. typedef struct {
  87. BOOL Active;
  88. HANDLE ReadyEvent; //meaningfull iff Active == TRUE
  89. HANDLE ResumeEvent; //meaningfull iff Active == TRUE
  90. } BTLIB_SUSPEND_REQUEST;
  91. typedef struct {
  92. U64 BtlSignature;
  93. S32 SuspendDisabledCounter;
  94. BTLIB_SUSPEND_REQUEST SuspendRequest;
  95. BOOL ConsistentExceptionState; //TRUE if 32-bit thread state, provided by the
  96. //CpuGetContext function, corresponds to current
  97. //exception context.
  98. } BTLIB_SHARED_INFO_TYPE;
  99. #define BTLIB_SI_SET_SIGNATURE(pBtSi) ((pBtSi)->BtlSignature = BTL_SIGNATURE)
  100. #define BTLIB_SI_CHECK_SIGNATURE(pBtSi) ((pBtSi)->BtlSignature == BTL_SIGNATURE)
  101. #define BTLIB_SI_SUSPENSION_DISABLED(pBtSi) ((pBtSi)->SuspendDisabledCounter)
  102. #define BTLIB_SI_INIT_SUSPENSION_PERMISSION(pBtSi) ((pBtSi)->SuspendDisabledCounter = 0)
  103. #define BTLIB_SI_DISABLE_SUSPENSION(pBtSi) (((pBtSi)->SuspendDisabledCounter)++)
  104. #define BTLIB_SI_ENABLE_SUSPENSION(pBtSi) (((pBtSi)->SuspendDisabledCounter)--)
  105. #define BTLIB_SI_HAS_SUSPEND_REQUEST(pBtSi) ((pBtSi)->SuspendRequest.Active)
  106. #define BTLIB_SI_INIT_SUSPEND_REQUEST(pBtSi) ((pBtSi)->SuspendRequest.Active = FALSE)
  107. #define BTLIB_SI_EXCEPT_STATE_CONSISTENT(pBtSi) ((pBtSi)->ConsistentExceptionState)
  108. #define BTLIB_SI_SET_CONSISTENT_EXCEPT_STATE(pBtSi) ((pBtSi)->ConsistentExceptionState = TRUE)
  109. #define BTLIB_SI_CLEAR_CONSISTENT_EXCEPT_STATE(pBtSi) ((pBtSi)->ConsistentExceptionState = FALSE)
  110. // Simulation exit codes
  111. enum BtSimExitCode {
  112. SIM_EXIT_EXCEPTION_CODE = 1, //raise IA32 exception
  113. SIM_EXIT_UNHANDLED_EXCEPTION_CODE, //pass BT-unhandled exception to
  114. //higher-level exception handler
  115. SIM_EXIT_JMPE_CODE, //simulate sys.call
  116. SIM_EXIT_LCALL_CODE, //simulate LCALL
  117. SIM_EXIT_RESTART_CODE, //restart code simulation
  118. SIM_EXIT_IA64_EXCEPTION_CODE //raise IA64 exception
  119. };
  120. typedef U32 BT_SIM_EXIT_CODE;
  121. //STRUCTURE: BT_SIM_EXIT_INFO
  122. //Represents simulation exit code and code-dependent data defining the cause of the exit
  123. typedef struct {
  124. BT_SIM_EXIT_CODE ExitCode;
  125. union {
  126. //ExitCode == SIM_EXIT_EXCEPTION_CODE
  127. struct {
  128. BT_EXCEPTION_CODE ExceptionCode;
  129. U32 ReturnAddr;
  130. } ExceptionRecord;
  131. //ExitCode == SIM_EXIT_JMPE_CODE
  132. struct {
  133. U32 TargetAddr; //Currently unused
  134. U32 ReturnAddr; //Currently unused
  135. } JmpeRecord;
  136. //ExitCode == SIM_EXIT_LCALL_CODE
  137. struct {
  138. U32 TargetAddr; //Currently unused
  139. U32 ReturnAddr; //Currently unused
  140. } LcallRecord;
  141. //ExitCode == SIM_EXIT_IA64_EXCEPTION_CODE
  142. struct {
  143. CONTEXT ExceptionContext; //Currently unused
  144. EXCEPTION_RECORD ExceptionRecord;
  145. } IA64Exception;
  146. } u;
  147. } BT_SIM_EXIT_INFO;
  148. //STRUCTURE: BTLIB_CPU_SIM_DATA
  149. //This structure keeps externally accessible data allocated by the CpuSimulate function for
  150. //the current code simulation session. External access to this data is only possible
  151. //if BTLIB_INSIDE_CPU_SIMULATION() = TRUE.
  152. typedef struct {
  153. _JBTYPE Jmpbuf[_JBLEN]; //Current longjmp/setjmp buffer
  154. BT_SIM_EXIT_INFO ExitData; //Exit info of the current simulation session
  155. } BTLIB_CPU_SIM_DATA;
  156. //STRUCTURE: BTLIB_INFO_TYPE
  157. //wowIA32X.dll-thread local storage
  158. //DO NOT use conditional compilation (#ifdef) inside this structure - remote
  159. //process suppose the same structure if BTL_SIGNATURE matches.
  160. //Any change to the structure must be accompanied by changing BTL_SIGNATURE.
  161. typedef struct {
  162. BTLIB_CPU_SIM_DATA * CpuSimDataPtr;
  163. BTLIB_SHARED_INFO_TYPE SharedInfo;
  164. HANDLE ExternalHandle;
  165. HANDLE LogFile; /* Used in !NODEBUG only */
  166. DWORD LogOffset; /* Used in !NODEBUG only */
  167. S32 NonBlockedLog; /* Flag that enables (zero)/disables (non-zero) blocked access to log file*/
  168. } BTLIB_INFO_TYPE;
  169. #define BTLIB_INFO_SIZE sizeof(BTLIB_INFO_TYPE)
  170. #define BTLIB_INFO_ALIGNMENT 32
  171. extern U32 BtlpInfoOffset;
  172. extern U32 BtlpGenericIA32ContextOffset;
  173. #define BTLIB_INFO_PTR_OF(pTEB) ((BTLIB_INFO_TYPE *)((ULONG_PTR)BT_TLS_OF(pTEB) + BtlpInfoOffset))
  174. #define BTLIB_INFO_PTR() BTLIB_INFO_PTR_OF(BT_CURRENT_TEB())
  175. #define BTLIB_CONTEXT_IA32_PTR() ((BTGENERIC_IA32_CONTEXT *)((ULONG_PTR)BT_TLS_OF(BT_CURRENT_TEB()) + BtlpGenericIA32ContextOffset))
  176. #define BTLIB_MEMBER_OFFSET(member) (offsetof(BTLIB_INFO_TYPE, member) + BtlpInfoOffset)
  177. #define BTLIB_MEMBER_PTR(pTLS, member) ((PVOID)((ULONG_PTR)pTLS + BTLIB_MEMBER_OFFSET(member)))
  178. #define BTLIB_INSIDE_CPU_SIMULATION() (BTLIB_INFO_PTR()->CpuSimDataPtr != 0)
  179. #define BTLIB_ENTER_CPU_SIMULATION(CpuSimDataP) (BTLIB_INFO_PTR()->CpuSimDataPtr = (CpuSimDataP))
  180. #define BTLIB_LEAVE_CPU_SIMULATION() (BTLIB_INFO_PTR()->CpuSimDataPtr = 0)
  181. #define BTLIB_SIM_EXIT_INFO_PTR() (&(BTLIB_INFO_PTR()->CpuSimDataPtr->ExitData))
  182. #define BTLIB_SIM_JMPBUF() (BTLIB_INFO_PTR()->CpuSimDataPtr->Jmpbuf)
  183. #define BTLIB_EXTERNAL_HANDLE_OF(pTEB) (BTLIB_INFO_PTR_OF(pTEB)->ExternalHandle)
  184. #define BTLIB_EXTERNAL_HANDLE() BTLIB_EXTERNAL_HANDLE_OF(BT_CURRENT_TEB())
  185. #define BTLIB_SET_EXTERNAL_HANDLE(h) (BTLIB_INFO_PTR()->ExternalHandle = (h))
  186. #define BTLIB_LOG_FILE_OF(pTEB) (BTLIB_INFO_PTR_OF(pTEB)->LogFile)
  187. #define BTLIB_LOG_FILE() BTLIB_LOG_FILE_OF(BT_CURRENT_TEB())
  188. #define BTLIB_SET_LOG_FILE(h) (BTLIB_INFO_PTR()->LogFile = (h))
  189. #define BTLIB_LOG_OFFSET_OF(pTEB) (BTLIB_INFO_PTR_OF(pTEB)->LogOffset)
  190. #define BTLIB_LOG_OFFSET() BTLIB_LOG_OFFSET_OF(BT_CURRENT_TEB())
  191. #define BTLIB_SET_LOG_OFFSET(n) (BTLIB_INFO_PTR()->LogOffset = (n))
  192. #define BTLIB_BLOCKED_LOG_DISABLED() (BTLIB_INFO_PTR()->NonBlockedLog)
  193. #define BTLIB_DISABLE_BLOCKED_LOG() (BTLIB_INFO_PTR()->NonBlockedLog++)
  194. #define BTLIB_ENABLE_BLOCKED_LOG() (BTLIB_INFO_PTR()->NonBlockedLog--)
  195. #define BTLIB_INIT_BLOCKED_LOG_FLAG() (BTLIB_INFO_PTR()->NonBlockedLog = 0)
  196. #define BTLIB_SET_SIGNATURE() BTLIB_SI_SET_SIGNATURE(&(BTLIB_INFO_PTR()->SharedInfo))
  197. #define BTLIB_INIT_SUSPENSION_PERMISSION() BTLIB_SI_INIT_SUSPENSION_PERMISSION(&(BTLIB_INFO_PTR()->SharedInfo))
  198. #define BTLIB_DISABLE_SUSPENSION() BTLIB_SI_DISABLE_SUSPENSION(&(BTLIB_INFO_PTR()->SharedInfo))
  199. #define BTLIB_ENABLE_SUSPENSION() BTLIB_SI_ENABLE_SUSPENSION(&(BTLIB_INFO_PTR()->SharedInfo))
  200. #define BTLIB_HAS_SUSPEND_REQUEST() BTLIB_SI_HAS_SUSPEND_REQUEST(&(BTLIB_INFO_PTR()->SharedInfo))
  201. #define BTLIB_INIT_SUSPEND_REQUEST() BTLIB_SI_INIT_SUSPEND_REQUEST(&(BTLIB_INFO_PTR()->SharedInfo))
  202. #define BTLIB_SET_CONSISTENT_EXCEPT_STATE() BTLIB_SI_SET_CONSISTENT_EXCEPT_STATE(&(BTLIB_INFO_PTR()->SharedInfo))
  203. #define BTLIB_CLEAR_CONSISTENT_EXCEPT_STATE() BTLIB_SI_CLEAR_CONSISTENT_EXCEPT_STATE(&(BTLIB_INFO_PTR()->SharedInfo))
  204. #define BTLIB_EXCEPT_STATE_CONSISTENT() BTLIB_SI_EXCEPT_STATE_CONSISTENT(&(BTLIB_INFO_PTR()->SharedInfo))
  205. // NT WOW64 specifics
  206. #define TYPE32(x) ULONG
  207. #define TYPE64(x) ULONGLONG
  208. //CpuFlushInstructionCache reason codes
  209. typedef enum {
  210. WOW64_FLUSH_FORCE,
  211. WOW64_FLUSH_FREE,
  212. WOW64_FLUSH_ALLOC,
  213. WOW64_FLUSH_PROTECT
  214. } WOW64_FLUSH_REASON;
  215. // Wow64 services
  216. NTSTATUS Wow64RaiseException (DWORD InterruptNumber, PEXCEPTION_RECORD ExceptionRecord);
  217. LONG Wow64SystemService (int Code, BTGENERIC_IA32_CONTEXT * ContextIA32);
  218. //
  219. // GDT selectors - These defines are R0 selector numbers, which means
  220. // they happen to match the byte offset relative to
  221. // the base of the GDT.
  222. //
  223. #define KGDT_NULL 0
  224. #define KGDT_R0_CODE 8
  225. #define KGDT_R0_DATA 16
  226. #define KGDT_R3_CODE 24
  227. #define KGDT_R3_DATA 32
  228. #define KGDT_TSS 40
  229. #define KGDT_R0_PCR 48
  230. #define KGDT_R3_TEB 56
  231. #define KGDT_VDM_TILE 64
  232. #define KGDT_LDT 72
  233. #define KGDT_DF_TSS 80
  234. #define KGDT_NMI_TSS 88
  235. // Initial values of the IA32 thread context registers.
  236. // Any value not listed below is initialized to zero
  237. // The value of the segment register is the OR composition of GDT offset = GDT index*8 and
  238. // RPL = 0-3 (should never change)
  239. #define CS_INIT_VAL (KGDT_R3_CODE | 3);
  240. #define DS_INIT_VAL (KGDT_R3_DATA | 3);
  241. #define ES_INIT_VAL (KGDT_R3_DATA | 3);
  242. #define FS_INIT_VAL (KGDT_R3_TEB | 3);
  243. #define SS_INIT_VAL (KGDT_R3_DATA | 3);
  244. #define EFLAGS_INIT_VAL 0x202
  245. #define FPCW_INIT_VAL 0x27f
  246. #define FPTW_INIT_VAL 0xffff
  247. #define MXCSR_INIT_VAL 0x1f80
  248. #if !defined(BT_NT_BUILD)
  249. typedef struct _CLIENT_ID32 {
  250. TYPE32(HANDLE) UniqueProcess;
  251. TYPE32(HANDLE) UniqueThread;
  252. } CLIENT_ID32;
  253. typedef CLIENT_ID32 *PCLIENT_ID32;
  254. #define WIN32_CLIENT_INFO_LENGTH 62
  255. #define GDI_BATCH_BUFFER_SIZE 310
  256. typedef struct _GDI_TEB_BATCH32 {
  257. TYPE32(ULONG) Offset;
  258. TYPE32(ULONG_PTR) HDC;
  259. TYPE32(ULONG) Buffer[GDI_BATCH_BUFFER_SIZE];
  260. } GDI_TEB_BATCH32,*PGDI_TEB_BATCH32;
  261. typedef struct _Wx86ThreadState32 {
  262. TYPE32(PULONG) CallBx86Eip;
  263. TYPE32(PVOID) DeallocationCpu;
  264. BOOLEAN UseKnownWx86Dll;
  265. char OleStubInvoked;
  266. } WX86THREAD32, *PWX86THREAD32;
  267. typedef struct _TEB32 {
  268. NT_TIB32 NtTib;
  269. TYPE32(PVOID) EnvironmentPointer;
  270. CLIENT_ID32 ClientId;
  271. TYPE32(PVOID) ActiveRpcHandle;
  272. TYPE32(PVOID) ThreadLocalStoragePointer;
  273. TYPE32(PPEB) ProcessEnvironmentBlock;
  274. ULONG LastErrorValue;
  275. ULONG CountOfOwnedCriticalSections;
  276. TYPE32(PVOID) CsrClientThread;
  277. TYPE32(PVOID) Win32ThreadInfo; // PtiCurrent
  278. ULONG User32Reserved[26]; // user32.dll items
  279. ULONG UserReserved[5]; // Winsrv SwitchStack
  280. TYPE32(PVOID) WOW32Reserved; // used by WOW
  281. LCID CurrentLocale;
  282. ULONG FpSoftwareStatusRegister; // offset known by outsiders!
  283. TYPE32(PVOID) SystemReserved1[54]; // Used by FP emulator
  284. NTSTATUS ExceptionCode; // for RaiseUserException
  285. UCHAR SpareBytes1[44];
  286. GDI_TEB_BATCH32 GdiTebBatch; // Gdi batching
  287. CLIENT_ID32 RealClientId;
  288. TYPE32(HANDLE) GdiCachedProcessHandle;
  289. ULONG GdiClientPID;
  290. ULONG GdiClientTID;
  291. TYPE32(PVOID) GdiThreadLocalInfo;
  292. TYPE32(ULONG_PTR) Win32ClientInfo[WIN32_CLIENT_INFO_LENGTH]; // User32 Client Info
  293. TYPE32(PVOID) glDispatchTable[233]; // OpenGL
  294. ULONG glReserved1[29]; // OpenGL
  295. TYPE32(PVOID) glReserved2; // OpenGL
  296. TYPE32(PVOID) glSectionInfo; // OpenGL
  297. TYPE32(PVOID) glSection; // OpenGL
  298. TYPE32(PVOID) glTable; // OpenGL
  299. TYPE32(PVOID) glCurrentRC; // OpenGL
  300. TYPE32(PVOID) glContext; // OpenGL
  301. ULONG LastStatusValue;
  302. UNICODE_STRING32 StaticUnicodeString;
  303. WCHAR StaticUnicodeBuffer[STATIC_UNICODE_BUFFER_LENGTH];
  304. TYPE32(PVOID) DeallocationStack;
  305. TYPE32(PVOID) TlsSlots[TLS_MINIMUM_AVAILABLE];
  306. LIST_ENTRY32 TlsLinks;
  307. TYPE32(PVOID) Vdm;
  308. TYPE32(PVOID) ReservedForNtRpc;
  309. TYPE32(PVOID) DbgSsReserved[2];
  310. ULONG HardErrorsAreDisabled;
  311. TYPE32(PVOID) Instrumentation[16];
  312. TYPE32(PVOID) WinSockData; // WinSock
  313. ULONG GdiBatchCount;
  314. ULONG Spare2;
  315. ULONG Spare3;
  316. TYPE32(PVOID) ReservedForPerf;
  317. TYPE32(PVOID) ReservedForOle;
  318. ULONG WaitingOnLoaderLock;
  319. WX86THREAD32 Wx86Thread;
  320. TYPE32(PVOID *) TlsExpansionSlots;
  321. } TEB32;
  322. typedef TEB32 *PTEB32;
  323. #endif
  324. // wowIA32X.dll-specific globals
  325. // BtlAPITable
  326. extern API_TABLE_TYPE BtlAPITable;
  327. // wowIA32X.dll placeholder table for IA32Exec.bin plabel pointers
  328. extern PLABEL_PTR_TYPE BtlpPlaceHolderTable[NO_OF_APIS];
  329. #ifdef __cplusplus
  330. }
  331. #endif
  332. #endif // BTLIB_H