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.

358 lines
7.7 KiB

  1. /*++
  2. Copyright (c) 1995-1998 Microsoft Corporation
  3. Module Name:
  4. fraginit.c
  5. Abstract:
  6. Initialization, termination, and CPU interface functions
  7. Author:
  8. 25-Aug-1995 BarryBo
  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 _WX86CPUAPI_
  17. #include "wx86.h"
  18. #include "wx86nt.h"
  19. #include "wx86cpu.h"
  20. #include "cpuassrt.h"
  21. #ifdef MSCCPU
  22. #include "ccpu.h"
  23. #include "msccpup.h"
  24. #undef GET_BYTE
  25. #undef GET_SHORT
  26. #undef GET_LONG
  27. #else
  28. #include "threadst.h"
  29. #include "instr.h"
  30. #include "frag.h"
  31. ASSERTNAME;
  32. #endif
  33. #include "fragp.h"
  34. //
  35. // Table mapping a byte to a 0 or 1, corresponding to the parity bit for
  36. // that byte.
  37. //
  38. const BYTE ParityBit[] = {
  39. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  40. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  41. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  42. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  43. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  44. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  45. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  46. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  47. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  48. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  49. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  50. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  51. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  52. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  53. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  54. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1
  55. };
  56. #if _ALPHA_
  57. //
  58. // TRUE if the CPU should generate the new LDB/STB instructions for accessing
  59. // data less than one DWORD long or when accessing unaligned data.
  60. //
  61. DWORD fByteInstructionsOK;
  62. #endif
  63. int *
  64. _errno(
  65. )
  66. /*++
  67. Routine Description:
  68. Stub function so the CPU can pull in floating-point CRT support
  69. without the C startup code.
  70. Arguments:
  71. None.
  72. Return Value:
  73. Pointer to per-thread [actually per-fiber] errno value.
  74. --*/
  75. {
  76. DECLARE_CPU;
  77. return &cpu->ErrnoVal;
  78. }
  79. BOOL
  80. FragLibInit(
  81. PCPUCONTEXT cpu,
  82. DWORD StackBase
  83. )
  84. /*++
  85. Routine Description:
  86. This routine initializes the fragment library.
  87. Arguments:
  88. cpu - per-thread CPU data
  89. StackBase - initial ESP value
  90. Return Value:
  91. True if successful.
  92. --*/
  93. {
  94. //
  95. // Initialize the 487 emulator
  96. //
  97. FpuInit(cpu);
  98. //
  99. // Initialize all non-zero fields in the cpu
  100. //
  101. cpu->flag_df = 1; // direction flag is initially UP
  102. cpu->flag_if = 1; // enable interrupts
  103. ES = SS = DS = KGDT_R3_DATA+3;
  104. CS = KGDT_R3_CODE+3;
  105. FS = KGDT_R3_TEB+3;
  106. esp = StackBase; // set up the initial ESP value
  107. #if _ALPHA_
  108. //
  109. // See if LDB/STB instructions are implemented.
  110. //
  111. fByteInstructionsOK = (DWORD)ProxyIsProcessorFeaturePresent(
  112. PF_ALPHA_BYTE_INSTRUCTIONS);
  113. #endif
  114. return TRUE;
  115. }
  116. DWORD GetEax(PVOID CpuContext)
  117. {
  118. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  119. return eax;
  120. }
  121. DWORD GetEbx(PVOID CpuContext)
  122. {
  123. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  124. return ebx;
  125. }
  126. DWORD GetEcx(PVOID CpuContext)
  127. {
  128. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  129. return ecx;
  130. }
  131. DWORD GetEdx(PVOID CpuContext)
  132. {
  133. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  134. return edx;
  135. }
  136. DWORD GetEsp(PVOID CpuContext)
  137. {
  138. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  139. return esp;
  140. }
  141. DWORD GetEbp(PVOID CpuContext)
  142. {
  143. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  144. return ebp;
  145. }
  146. DWORD GetEsi(PVOID CpuContext)
  147. {
  148. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  149. return esi;
  150. }
  151. DWORD GetEdi(PVOID CpuContext)
  152. {
  153. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  154. return edi;
  155. }
  156. DWORD GetEip(PVOID CpuContext)
  157. {
  158. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  159. return eip;
  160. }
  161. void SetEax(PVOID CpuContext, DWORD dw)
  162. {
  163. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  164. eax = dw;
  165. }
  166. void SetEbx(PVOID CpuContext, DWORD dw)
  167. {
  168. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  169. ebx = dw;
  170. }
  171. void SetEcx(PVOID CpuContext, DWORD dw)
  172. {
  173. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  174. ecx = dw;
  175. }
  176. void SetEdx(PVOID CpuContext, DWORD dw)
  177. {
  178. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  179. edx = dw;
  180. }
  181. void SetEsp(PVOID CpuContext, DWORD dw)
  182. {
  183. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  184. esp = dw;
  185. }
  186. void SetEbp(PVOID CpuContext, DWORD dw)
  187. {
  188. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  189. ebp = dw;
  190. }
  191. void SetEsi(PVOID CpuContext, DWORD dw)
  192. {
  193. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  194. esi = dw;
  195. }
  196. void SetEdi(PVOID CpuContext, DWORD dw)
  197. {
  198. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  199. edi = dw;
  200. }
  201. void SetEip(PVOID CpuContext, DWORD dw)
  202. {
  203. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  204. eip = dw;
  205. }
  206. VOID SetCs(PVOID CpuContext, USHORT us)
  207. {
  208. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  209. CS = us;
  210. }
  211. VOID SetSs(PVOID CpuContext, USHORT us)
  212. {
  213. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  214. SS = us;
  215. }
  216. VOID SetDs(PVOID CpuContext, USHORT us)
  217. {
  218. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  219. DS = us;
  220. }
  221. VOID SetEs(PVOID CpuContext, USHORT us)
  222. {
  223. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  224. ES = us;
  225. }
  226. VOID SetFs(PVOID CpuContext, USHORT us)
  227. {
  228. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  229. FS = us;
  230. }
  231. VOID SetGs(PVOID CpuContext, USHORT us)
  232. {
  233. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  234. GS = us;
  235. }
  236. USHORT GetCs(PVOID CpuContext)
  237. {
  238. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  239. return CS;
  240. }
  241. USHORT GetSs(PVOID CpuContext)
  242. {
  243. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  244. return SS;
  245. }
  246. USHORT GetDs(PVOID CpuContext)
  247. {
  248. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  249. return DS;
  250. }
  251. USHORT GetEs(PVOID CpuContext)
  252. {
  253. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  254. return ES;
  255. }
  256. USHORT GetFs(PVOID CpuContext)
  257. {
  258. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  259. return FS;
  260. }
  261. USHORT GetGs(PVOID CpuContext)
  262. {
  263. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  264. return GS;
  265. }
  266. ULONG GetEfl(PVOID CpuContext)
  267. {
  268. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  269. DWORD dw;
  270. dw = ((GET_CFLAG) ? FLAG_CF : 0)
  271. | 2
  272. | 3 << 12 // iopl
  273. | ((GET_AUXFLAG) ? FLAG_AUX : 0)
  274. | ((GET_PFLAG) ? FLAG_PF : 0)
  275. | ((cpu->flag_zf) ? 0 : FLAG_ZF) // zf has inverse logic
  276. | ((GET_SFLAG) ? FLAG_SF : 0)
  277. | ((cpu->flag_tf) ? FLAG_TF : 0)
  278. | ((cpu->flag_if) ? FLAG_IF : 0)
  279. | ((cpu->flag_df == -1) ? FLAG_DF : 0)
  280. | ((GET_OFLAG) ? FLAG_OF : 0)
  281. | cpu->flag_ac;
  282. return dw;
  283. }
  284. void SetEfl(PVOID CpuContext, ULONG RegValue)
  285. {
  286. PCPUCONTEXT cpu = (PCPUCONTEXT)CpuContext;
  287. // IOPL, IF, NT, RF, VM, AC ignored.
  288. SET_CFLAG_IND(RegValue & FLAG_CF);
  289. cpu->flag_pf = (RegValue & FLAG_PF) ? 0 : 1; // see ParityBit[] table
  290. cpu->flag_aux= (RegValue & FLAG_AUX) ? AUX_VAL : 0;
  291. cpu->flag_zf = (RegValue & FLAG_ZF) ? 0 : 1; // inverse logic
  292. SET_SFLAG_IND(RegValue & FLAG_SF);
  293. cpu->flag_tf = (RegValue & FLAG_TF) ? 1 : 0;
  294. cpu->flag_df = (RegValue & FLAG_DF) ? -1 : 1;
  295. SET_OFLAG_IND(RegValue & FLAG_OF);
  296. cpu->flag_ac = (RegValue & FLAG_AC);
  297. }
  298. #if DBG
  299. VOID
  300. DoAssert(
  301. PSZ exp,
  302. PSZ msg,
  303. PSZ mod,
  304. INT line
  305. )
  306. {
  307. if (msg) {
  308. LOGPRINT((ERRORLOG, "CPU ASSERTION FAILED:\r\n %s\r\n%s\r\nFile: %s Line %d\r\n", msg, exp, mod, line));
  309. } else {
  310. LOGPRINT((ERRORLOG, "CPU ASSERTION FAILED:\r\n %s\r\nFile: %s Line %d\r\n", exp, mod, line));
  311. }
  312. DbgBreakPoint();
  313. }
  314. #endif //DBG