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.

285 lines
5.1 KiB

  1. /*++
  2. Copyright (c) 1999-2000 Microsoft Corporation
  3. Module Name:
  4. wowrap.c
  5. Abstract:
  6. This module implements some wrapper (on wx86cpu) functions wow64 might call.
  7. Author:
  8. 24-Aug-1999 askhalid
  9. Revision History:
  10. --*/
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #include <windows.h>
  15. #include <stdio.h>
  16. #define _WOW64CPUAPI_
  17. #define _WX86CPUAPI_
  18. #define __WOW64_WRAPPER__
  19. #include "wx86.h"
  20. #include "wow64.h"
  21. #include "wx86nt.h"
  22. #include "wx86cpu.h"
  23. #include "cpuassrt.h"
  24. #include "config.h"
  25. #include "entrypt.h"
  26. #include "instr.h"
  27. #include "compiler.h"
  28. #include "wow64cpu.h"
  29. ASSERTNAME;
  30. typedef struct _WowBopInstr {
  31. BOPINSTR Wx86Bop;
  32. BYTE Ret;
  33. } WOWBOPINSTR;
  34. WOWBOPINSTR Bop;
  35. NTSTATUS
  36. CpuProcessInit(PSIZE_T pCpuThreadDataSize)
  37. {
  38. NTSTATUS st;
  39. memset ( (char *)&Bop, 0, sizeof (Bop) );
  40. Bop.Wx86Bop.Instr1 = 0xc4;
  41. Bop.Wx86Bop.Instr2 = 0xc4;
  42. Bop.Ret = 0xc3; // ret
  43. st = MsCpuProcessInit();
  44. *pCpuThreadDataSize = sizeof(CPUCONTEXT);
  45. return st;
  46. }
  47. NTSTATUS
  48. CpuProcessTerm(VOID)
  49. {
  50. return 0;
  51. }
  52. NTSTATUS
  53. CpuThreadInit(PVOID pPerThreadData)
  54. {
  55. PTEB32 Teb32 = NtCurrentTeb32();
  56. //
  57. // Initialize the pointer to the DoSystemService function.
  58. Teb32->WOW32Reserved = (ULONG)(LONGLONG)&Bop;
  59. if ( MsCpuThreadInit()) {
  60. return 0;
  61. }
  62. return STATUS_SEVERITY_ERROR; //return right value
  63. }
  64. //
  65. // Execution
  66. //
  67. VOID
  68. CpuSimulate(VOID)
  69. {
  70. MsCpuSimulate(NULL);
  71. }
  72. //
  73. // Exception handling, context manipulation
  74. //
  75. /* already been defined
  76. VOID
  77. CpuResetToConsistentState(PEXCEPTION_POINTERS pExecptionPointers)
  78. {
  79. }*/
  80. NTSTATUS
  81. CpuGetContext(
  82. IN HANDLE ThreadHandle,
  83. IN HANDLE ProcessHandle,
  84. IN PTEB Teb,
  85. OUT PCONTEXT32 Context)
  86. /*++
  87. Routine Description:
  88. Extracts the cpu context of the specified thread. If the target thread isn't the currently
  89. executing thread, then it should be guaranteed by the caller that the target thread
  90. is suspended at a proper CPU state.
  91. Context->ContextFlags decides which IA32 register-set to retreive.
  92. Arguments:
  93. ThreadHandle - Target thread handle to retreive the context for
  94. ProcessHandle - Open handle to the process that the thread runs in
  95. Teb - Pointer to the target's thread TEB
  96. Context - Context record to fill
  97. Return Value:
  98. NTSTATUS.
  99. --*/
  100. {
  101. //Context->ContextFlags = CONTEXT_FULL_WX86;
  102. if (NtCurrentThread() == ThreadHandle)
  103. {
  104. return MsCpuGetContext(Context);
  105. }
  106. return MsCpuGetContextThread(ProcessHandle,
  107. Teb,
  108. Context);
  109. }
  110. NTSTATUS
  111. CpuSetContext(
  112. IN HANDLE ThreadHandle,
  113. IN HANDLE ProcessHandle,
  114. IN PTEB Teb,
  115. PCONTEXT32 Context)
  116. /*++
  117. Routine Description:
  118. Sets the cpu context for the specified thread. If the target thread isn't the currently
  119. executing thread, then it should be guaranteed by the caller that the target thread is
  120. suspended at a proper CPU state.
  121. Context->ContextFlags decides which IA32 register-set to be set.
  122. Arguments:
  123. ThreadHandle - Target thread handle to retreive the context for
  124. ProcessHandle - Open handle to the process that the thread runs in
  125. Teb - Pointer to the target's thread TEB
  126. Context - Context record to set
  127. Return Value:
  128. NTSTATUS.
  129. --*/
  130. {
  131. //Context->ContextFlags = CONTEXT_FULL_WX86; // make sure wow return the right flags
  132. if (NtCurrentThread() == ThreadHandle)
  133. {
  134. return MsCpuSetContext(Context);
  135. }
  136. return MsCpuSetContextThread(ProcessHandle,
  137. Teb,
  138. Context);
  139. }
  140. ULONG
  141. CpuGetStackPointer ( )
  142. // create a wrapper that calls the Wx86 CPU's GetEsp
  143. {
  144. DECLARE_CPU;
  145. return GetEsp(cpu);
  146. }
  147. VOID
  148. CpuNotifyDllLoad (
  149. LPWSTR DllName,
  150. PVOID DllBase,
  151. ULONG DllSize
  152. )
  153. // - create a wrapper on the Wx86 CPU's CpuMapNotify
  154. {
  155. CpuMapNotify( DllBase, TRUE );
  156. }
  157. VOID
  158. CpuNotifyDllUnload (
  159. PVOID DllBase
  160. )
  161. // - create a wrapper on the Wx86 CPU's CpuMapNotify
  162. {
  163. CpuMapNotify( DllBase, FALSE );
  164. }
  165. VOID
  166. CpuSetInstructionPointer (
  167. ULONG Value
  168. )
  169. //- wrapper on SetEip
  170. {
  171. DECLARE_CPU;
  172. SetEip( cpu, Value);
  173. }
  174. VOID
  175. CpuSetStackPointer (
  176. ULONG val
  177. )
  178. // - wrapper on SetEsp
  179. {
  180. DECLARE_CPU;
  181. SetEsp(cpu, val);
  182. }
  183. NTSTATUS
  184. CpuThreadTerm(VOID)
  185. //- just create an empty stub function - the Wx86 CPU doesn't care about this
  186. {
  187. return 0;
  188. }
  189. /*
  190. LONG
  191. WOW64DLLAPI
  192. Wow64SystemService(
  193. IN ULONG ServiceNumber,
  194. IN PCONTEXT32 Context32 //This is read only!
  195. )
  196. */
  197. DWORD
  198. ProxyWowDispatchBop(
  199. ULONG ServiceNumber,
  200. PCONTEXT_WX86 px86Context,
  201. PULONG ArgBase
  202. )
  203. {
  204. LONG ret=0;
  205. //CONTEXT32 _Context32;
  206. //_Context32.Edx = (ULONG)(ULONGLONG)ArgBase; //this is the only field wow64 using
  207. if ( px86Context != NULL )
  208. ret = Wow64SystemService ( ServiceNumber, px86Context );
  209. return ret;
  210. //[bb] The wow64 equivalent is Wow64SystemService.
  211. }