Source code of Windows XP (NT5)
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.

493 lines
11 KiB

  1. /*++
  2. Copyright (c) 1998-2000 Microsoft Corporation
  3. Module Name:
  4. wow64.h
  5. Abstract:
  6. Public header for wow64.dll
  7. Author:
  8. 11-May-1998 BarryBo
  9. Revision History:
  10. 9-Aug-1999 [askhalid] added WOW64IsCurrentProcess
  11. --*/
  12. #ifndef _WOW64_INCLUDE
  13. #define _WOW64_INCLUDE
  14. //
  15. // Make wow64.dll exports __declspec(dllimport) when this header is included
  16. // by non-wow64 components
  17. //
  18. #if !defined(_WOW64DLLAPI_)
  19. #define WOW64DLLAPI DECLSPEC_IMPORT
  20. #else
  21. #define WOW64DLLAPI
  22. #endif
  23. // crank down some warnings
  24. #pragma warning(4:4312) // conversion to type of greater size
  25. // pull in typedefs for TEB32, PEB32, etc.
  26. #include "wow64t.h"
  27. #include <setjmp.h>
  28. #include <windef.h>
  29. // wow64log constatns
  30. #include "wow64log.h"
  31. //wow64 regremaping
  32. #include "regremap.h"
  33. //
  34. // Enable the wow64 history mechanism
  35. // Eventually, this may only be enabled for debug builds, but for
  36. // now, enable all the time
  37. //
  38. #define WOW64_HISTORY
  39. //
  40. // define a datatype corresponding to the 32-bit machine's CONTEXT
  41. //
  42. #include "wx86nt.h"
  43. #define CONTEXT32 CONTEXT_WX86
  44. #define PCONTEXT32 PCONTEXT_WX86
  45. #define CONTEXT32_CONTROL CONTEXT_CONTROL_WX86
  46. #define CONTEXT32_INTEGER CONTEXT_INTEGER_WX86
  47. #define CONTEXT32_SEGMENTS CONTEXT_SEGMENTS_WX86
  48. #define CONTEXT32_FLOATING_POINT CONTEXT_FLOATING_POINT_WX86
  49. #define CONTEXT32_EXTENDED_REGISTERS CONTEXT_EXTENDED_REGISTERS_WX86
  50. #define CONTEXT32_DEBUG_REGISTERS CONTEXT_DEBUG_REGISTERS_WX86
  51. #define CONTEXT32_FULL CONTEXT_FULL_WX86
  52. #define CONTEXT32_FULLFLOAT (CONTEXT_FULL_WX86|CONTEXT32_FLOATING_POINT|CONTEXT32_EXTENDED_REGISTERS)
  53. #if defined(_AXP64_)
  54. // Enable 4k page emulation in software. IA64 does it in h/w with OS support.
  55. #define SOFTWARE_4K_PAGESIZE 1
  56. #endif
  57. typedef enum _WOW64_API_ERROR_ACTION {
  58. ApiErrorNTSTATUS, //Return exception code as return value
  59. ApiErrorNTSTATUSTebCode, //Some as above with SetLastError on exception code
  60. ApiErrorRetval, //Return a constant parameter
  61. ApiErrorRetvalTebCode //Some as above with SetLastError on exception code
  62. } WOW64_API_ERROR_ACTION, *PWOW64_API_ERROR_ACTION;
  63. // This structure describes what action should occure when thunks hit an unhandled exception.
  64. typedef struct _WOW64_SERVICE_ERROR_CASE {
  65. WOW64_API_ERROR_ACTION ErrorAction;
  66. LONG ErrorActionParam;
  67. } WOW64_SERVICE_ERROR_CASE, *PWOW64_SERVICE_ERROR_CASE;
  68. // This is an extension of KSERVICE_TABLE_DESCRIPTOR
  69. typedef struct _WOW64SERVICE_TABLE_DESCRIPTOR {
  70. PULONG_PTR Base;
  71. PULONG Count;
  72. ULONG Limit;
  73. #if defined(_IA64_)
  74. LONG TableBaseGpOffset;
  75. #endif
  76. PUCHAR Number;
  77. WOW64_API_ERROR_ACTION DefaultErrorAction; //Action if ErrorCases is NULL.
  78. LONG DefaultErrorActionParam; //Action parameter if ErrorCases is NULL.
  79. PWOW64_SERVICE_ERROR_CASE ErrorCases;
  80. } WOW64SERVICE_TABLE_DESCRIPTOR, *PWOW64SERVICE_TABLE_DESCRIPTOR;
  81. // Used to log hit counts for APIs.
  82. typedef struct _WOW64SERVICE_PROFILE_TABLE WOW64SERVICE_PROFILE_TABLE;
  83. typedef struct _WOW64SERVICE_PROFILE_TABLE *PWOW64SERVICE_PROFILE_TABLE;
  84. typedef struct _WOW64SERVICE_PROFILE_TABLE_ELEMENT {
  85. PWSTR ApiName;
  86. SIZE_T HitCount;
  87. PWOW64SERVICE_PROFILE_TABLE SubTable;
  88. BOOLEAN ApiEnabled;
  89. } WOW64SERVICE_PROFILE_TABLE_ELEMENT, *PWOW64SERVICE_PROFILE_TABLE_ELEMENT;
  90. typedef struct _WOW64SERVICE_PROFILE_TABLE {
  91. PWSTR TableName; //OPTIONAL
  92. PWSTR FriendlyTableName; //OPTIONAL
  93. CONST PWOW64SERVICE_PROFILE_TABLE_ELEMENT ProfileTableElements;
  94. SIZE_T NumberProfileTableElements;
  95. } WOW64SERVICE_PROFILE_TABLE, *PWOW64SERVICE_PROFILE_TABLE;
  96. typedef struct _WOW64_SYSTEM_INFORMATION {
  97. SYSTEM_BASIC_INFORMATION BasicInfo;
  98. SYSTEM_PROCESSOR_INFORMATION ProcessorInfo;
  99. ULONG_PTR RangeInfo;
  100. } WOW64_SYSTEM_INFORMATION, *PWOW64_SYSTEM_INFORMATION;
  101. //
  102. // Indices for API thunks.
  103. //
  104. #define WHNT32_INDEX 0 // ntoskrnl
  105. #define WHCON_INDEX 1 // console (replaces LPC calls)
  106. #define WHWIN32_INDEX 2 // win32k
  107. #define WHBASE_INDEX 3 // base/nls (replaces LPC calls)
  108. #define MAX_TABLE_INDEX 4
  109. //
  110. // Logging mechanism. Usage:
  111. // LOGPRINT((verbosity, format, ...))
  112. //
  113. #define LOGPRINT(args) Wow64LogPrint args
  114. #define ERRORLOG LF_ERROR // Always output to debugger. Use for *unexpected*
  115. // errors only
  116. #define TRACELOG LF_TRACE // application trace information
  117. #define INFOLOG LF_TRACE // misc. informational log
  118. #define VERBOSELOG LF_NONE // practically never output to debugger
  119. #if DBG
  120. #define WOW64DOPROFILE
  121. #endif
  122. void
  123. WOW64DLLAPI
  124. Wow64LogPrint(
  125. UCHAR LogLevel,
  126. char *format,
  127. ...
  128. );
  129. //
  130. // WOW64 Assertion Mechanism. Usage:
  131. // - put an ASSERTNAME macro at the top of each .C file
  132. // - WOW64ASSERT(expression)
  133. // - WOW64ASSERTMSG(expression, message)
  134. //
  135. //
  136. VOID
  137. WOW64DLLAPI
  138. Wow64Assert(
  139. IN CONST PSZ exp,
  140. OPTIONAL IN CONST PSZ msg,
  141. IN CONST PSZ mod,
  142. IN LONG LINE
  143. );
  144. #if DBG
  145. #undef ASSERTNAME
  146. #define ASSERTNAME static CONST PSZ szModule = __FILE__;
  147. #define WOWASSERT(exp) \
  148. if (!(exp)) { \
  149. Wow64Assert( #exp, NULL, szModule, __LINE__); \
  150. }
  151. #define WOWASSERTMSG(exp, msg) \
  152. if (!(exp)) { \
  153. Wow64Assert( #exp, msg, szModule, __LINE__); \
  154. }
  155. #else // !DBG
  156. #define WOWASSERT(exp)
  157. #define WOWASSERTMSG(exp, msg)
  158. #endif // !DBG
  159. #define WOWASSERT_PTR32(ptr) WOWASSERT((ULONGLONG)ptr < 0xFFFFFFFF)
  160. WOW64DLLAPI
  161. PVOID
  162. Wow64AllocateHeap(
  163. SIZE_T Size
  164. );
  165. WOW64DLLAPI
  166. VOID
  167. Wow64FreeHeap(
  168. PVOID BaseAddress
  169. );
  170. //
  171. // 64-to-32 callback support for usermode APCs
  172. //
  173. // A list of these sits inside WOW64_TLS_APCLIST
  174. typedef struct tagUserApcList {
  175. struct tagUserApcList *Next;
  176. jmp_buf JumpBuffer;
  177. PCONTEXT32 pContext32;
  178. } USER_APC_ENTRY, *PUSER_APC_ENTRY;
  179. BOOL
  180. WOW64DLLAPI
  181. WOW64IsCurrentProcess (
  182. HANDLE hProcess
  183. );
  184. NTSTATUS
  185. Wow64WrapApcProc(
  186. IN OUT PVOID *pApcProc,
  187. IN OUT PVOID *pApcContext
  188. );
  189. typedef struct UserCallbackData {
  190. jmp_buf JumpBuffer;
  191. PVOID PreviousUserCallbackData;
  192. PVOID OutputBuffer;
  193. ULONG OutputLength;
  194. NTSTATUS Status;
  195. PVOID UserBuffer;
  196. } USERCALLBACKDATA, *PUSERCALLBACKDATA;
  197. ULONG
  198. Wow64KiUserCallbackDispatcher(
  199. PUSERCALLBACKDATA pUserCallbackData,
  200. ULONG ApiNumber,
  201. ULONG ApiArgument,
  202. ULONG ApiSize
  203. );
  204. NTSTATUS
  205. Wow64NtCallbackReturn(
  206. PVOID OutputBuffer,
  207. ULONG OutputLength,
  208. NTSTATUS Status
  209. );
  210. BOOL
  211. Wow64IsModule32bitHelper(
  212. HANDLE ProcessHandle,
  213. IN ULONG64 DllBase);
  214. BOOL
  215. Wow64IsModule32bit(
  216. IN PCLIENT_ID ClientId,
  217. IN ULONG64 DllBase);
  218. NTSTATUS
  219. Wow64SkipOverBreakPoint(
  220. IN PCLIENT_ID ClientId,
  221. IN PEXCEPTION_RECORD ExceptionRecord);
  222. NTSTATUS
  223. Wow64GetThreadSelectorEntry(
  224. IN HANDLE ThreadHandle,
  225. IN OUT PVOID DescriptorTableEntry,
  226. IN ULONG Length,
  227. OUT PULONG ReturnLength OPTIONAL);
  228. //
  229. // Thread Local Storage (TLS) support. TLS slots are statically allocated.
  230. //
  231. #define WOW64_TLS_STACKPTR64 0 // contains 64-bit stack ptr when simulating 32-bit code
  232. #define WOW64_TLS_CPURESERVED 1 // per-thread data for the CPU simulator
  233. #define WOW64_TLS_INCPUSIMULATION 2 // Set when inside the CPU
  234. #define WOW64_TLS_TEMPLIST 3 // List of memory allocated in thunk call.
  235. #define WOW64_TLS_EXCEPTIONADDR 4 // 32-bit exception address (used during exception unwinds)
  236. #define WOW64_TLS_USERCALLBACKDATA 5 // Used by win32k callbacks
  237. #define WOW64_TLS_EXTENDED_FLOAT 6 // Used in ia64 to pass in floating point
  238. #define WOW64_TLS_APCLIST 7 // List of outstanding usermode APCs
  239. #define WOW64_TLS_FILESYSREDIR 8 // Used to enable/disable the filesystem redirector
  240. #define WOW64_TLS_LASTWOWCALL 9 // Pointer to the last wow call struct (Used when wowhistory is enabled)
  241. #define WOW64_TLS_WOW64INFO 10 // Wow64Info address (structure shared between 32-bit and 64-bit code inside Wow64).
  242. // VOID Wow64TlsSetValue(DWORD dwIndex, LPVOID lpTlsValue);
  243. #define Wow64TlsSetValue(dwIndex, lpTlsValue) \
  244. NtCurrentTeb()->TlsSlots[dwIndex] = lpTlsValue;
  245. // LPVOID Wow64TlsGetValue(DWORD dwIndex);
  246. #define Wow64TlsGetValue(dwIndex) \
  247. (NtCurrentTeb()->TlsSlots[dwIndex])
  248. //
  249. // 32-to-64 thunk routine
  250. //
  251. LONG
  252. WOW64DLLAPI
  253. Wow64SystemService(
  254. IN ULONG ServiceNumber,
  255. IN PCONTEXT32 Context32
  256. );
  257. //
  258. // Wow64RaiseException
  259. //
  260. WOW64DLLAPI
  261. NTSTATUS
  262. Wow64RaiseException(
  263. IN DWORD InterruptNumber,
  264. IN OUT PEXCEPTION_RECORD ExceptionRecord);
  265. //
  266. // Helper routines, called from the thunks
  267. //
  268. #define CHILD_PROCESS_SIGNATURE 0xff00ff0011001100
  269. typedef struct _ChildProcessInfo {
  270. ULONG_PTR Signature;
  271. PPEB32 pPeb32;
  272. SECTION_IMAGE_INFORMATION ImageInformation;
  273. ULONG_PTR TailSignature;
  274. } CHILD_PROCESS_INFO, *PCHILD_PROCESS_INFO;
  275. PVOID
  276. WOW64DLLAPI
  277. Wow64AllocateTemp(
  278. SIZE_T Size
  279. );
  280. NTSTATUS
  281. WOW64DLLAPI
  282. Wow64QueryBasicInformationThread(
  283. IN HANDLE Thread,
  284. OUT PTHREAD_BASIC_INFORMATION ThreadInfo
  285. );
  286. WOW64DLLAPI
  287. NTSTATUS
  288. Wow64NtCreateThread(
  289. OUT PHANDLE ThreadHandle,
  290. IN ACCESS_MASK DesiredAccess,
  291. IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,
  292. IN HANDLE ProcessHandle,
  293. OUT PCLIENT_ID ClientId,
  294. IN PCONTEXT ThreadContext,
  295. IN PINITIAL_TEB InitialTeb,
  296. IN BOOLEAN CreateSuspended
  297. );
  298. WOW64DLLAPI
  299. NTSTATUS
  300. Wow64NtTerminateThread(
  301. HANDLE ThreadHandle,
  302. NTSTATUS ExitStatus
  303. );
  304. NTSTATUS
  305. Wow64ExitThread(
  306. HANDLE ThreadHandle,
  307. NTSTATUS ExitStatus
  308. );
  309. VOID
  310. Wow64BaseFreeStackAndTerminate(
  311. IN PVOID OldStack,
  312. IN ULONG ExitCode
  313. );
  314. VOID
  315. Wow64BaseSwitchStackThenTerminate (
  316. IN PVOID StackLimit,
  317. IN PVOID NewStack,
  318. IN ULONG ExitCode
  319. );
  320. NTSTATUS
  321. Wow64NtContinue(
  322. IN PCONTEXT ContextRecord, // really a PCONTEXT32
  323. IN BOOLEAN TestAlert
  324. );
  325. NTSTATUS
  326. WOW64DLLAPI
  327. Wow64SuspendThread(
  328. IN HANDLE ThreadHandle,
  329. OUT PULONG PreviousSuspendCount OPTIONAL
  330. );
  331. NTSTATUS
  332. WOW64DLLAPI
  333. Wow64GetContextThread(
  334. IN HANDLE ThreadHandle,
  335. IN OUT PCONTEXT ThreadContext // really a PCONTEXT32
  336. );
  337. NTSTATUS
  338. WOW64DLLAPI
  339. Wow64SetContextThread(
  340. IN HANDLE ThreadHandle,
  341. IN PCONTEXT ThreadContext // really a PCONTEXT32
  342. );
  343. NTSTATUS
  344. Wow64KiRaiseException(
  345. IN PEXCEPTION_RECORD ExceptionRecord,
  346. IN PCONTEXT ContextRecord,
  347. IN BOOLEAN FirstChance
  348. );
  349. ULONG
  350. Wow64SetupApcCall(
  351. IN ULONG NormalRoutine,
  352. IN PCONTEXT32 NormalContext,
  353. IN ULONG Arg1,
  354. IN ULONG Arg2
  355. );
  356. VOID
  357. ThunkExceptionRecord64To32(
  358. IN PEXCEPTION_RECORD pRecord64,
  359. OUT PEXCEPTION_RECORD32 pRecord32
  360. );
  361. BOOLEAN
  362. Wow64NotifyDebugger(
  363. IN PEXCEPTION_RECORD ExceptionRecord,
  364. IN BOOLEAN FirstChance
  365. );
  366. VOID
  367. Wow64SetupExceptionDispatch(
  368. IN PEXCEPTION_RECORD32 pRecord32,
  369. IN PCONTEXT32 pContext32
  370. );
  371. VOID
  372. Wow64NotifyDebuggerHelper(
  373. IN PEXCEPTION_RECORD ExceptionRecord,
  374. IN BOOLEAN FirstChance
  375. );
  376. NTSTATUS
  377. Wow64InitializeEmulatedSystemInformation(
  378. VOID
  379. );
  380. PWOW64_SYSTEM_INFORMATION
  381. Wow64GetEmulatedSystemInformation(
  382. VOID
  383. );
  384. PWOW64_SYSTEM_INFORMATION
  385. Wow64GetRealSystemInformation(
  386. VOID
  387. );
  388. VOID
  389. Wow64Shutdown(
  390. HANDLE ProcessHandle
  391. );
  392. // Defines the argsize of the emulated machine
  393. #define ARGSIZE 4
  394. VOID
  395. ThunkPeb64ToPeb32(
  396. IN PPEB Peb64,
  397. OUT PPEB32 Peb32
  398. );
  399. extern RTL_CRITICAL_SECTION HandleDataCriticalSection;
  400. #endif // _WOW64_INCLUDE