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.

1309 lines
32 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. dpmiint.c
  5. Abstract:
  6. This file contains the interrupt support for DPMI. Most of this is
  7. for supporting the 486 emulator on risc platforms, but some code
  8. is shared with x86.
  9. Author:
  10. Neil Sandlin (neilsa) 1-Jun-1995
  11. Revision History:
  12. Comments:
  13. DPMI stack switching is accomplished by keeping a "locked pm stack"
  14. count, and when the count is zero, a stack switch occurs. This keeps
  15. track of the situation with recursive interrupts where the client
  16. may switch to its own stack. So, a stack switch to our locked stack
  17. occurs on the first level interrupt, and on subsequent nested interrupts,
  18. only the count is maintained. This is identical to how win31 managed
  19. the stack.
  20. If a client specifies that it is a 32-bit dpmi client, this only affects
  21. the "width" of a stack frame. A 16-bit client gets 16-bit frames, and
  22. a 32 bit client gets 32-bit frames. It is still necessary to check
  23. the size of the stack segment to determine if SP or ESP should be used.
  24. --*/
  25. #include "precomp.h"
  26. #pragma hdrstop
  27. #include <softpc.h>
  28. #include <dpmiint.h>
  29. BOOL
  30. SetProtectedModeInterrupt(
  31. USHORT IntNumber,
  32. USHORT Sel,
  33. ULONG Offset,
  34. USHORT Flags
  35. )
  36. /*++
  37. Routine Description:
  38. This function services the SetProtectedmodeInterrupt bop. It retrieves
  39. the handler information from the Dos application stack, and puts it into
  40. the VdmTib, for use by instruction emulation.
  41. --*/
  42. {
  43. DECLARE_LocalVdmContext;
  44. PVDM_INTERRUPTHANDLER Handlers = DpmiInterruptHandlers;
  45. if (IntNumber >= 256) {
  46. return FALSE;
  47. }
  48. if ((IntNumber >= 8 && IntNumber <= 0xf) ||
  49. (IntNumber >= 0x70 && IntNumber <= 0x7f)) {
  50. //
  51. // Hardware Interrupt
  52. //
  53. Flags |= VDM_INT_INT_GATE;
  54. } else {
  55. //
  56. // Software Interrupt
  57. //
  58. Flags |= VDM_INT_TRAP_GATE;
  59. }
  60. if (Sel != PMReflectorSeg) {
  61. //
  62. // The caller is setting the PM interrupt vector to be something other
  63. // than the dpmi default "end-of-the-chain" PM handler. Now we check
  64. // to see if the interrupt needs to be sent up to PM when it is encountered
  65. // in v86 mode.
  66. //
  67. if ((IntNumber == 0x1b) || //^Break?
  68. (IntNumber == 0x1c) || //Timer Tick?
  69. (IntNumber == 0x23) || //Ctrl-C?
  70. (IntNumber == 0x24) || //Critical Error Handler?
  71. (IntNumber == 0x02) || //Math co-processor exception used by math library routines!
  72. ((IntNumber >= 0x08) && (IntNumber <= 0xf)) || //Hardware?
  73. ((IntNumber >= 0x70) && (IntNumber <= 0x77))) {
  74. // Flag this so that the v86 reflector code will send it to PM
  75. Flags |= VDM_INT_HOOKED;
  76. // Mark it down low so NTIO.SYS can do the right thing
  77. if ( (IntNumber == 0x1c) || (IntNumber == 8) ) {
  78. *(ULONG *)(IntelBase+FIXED_NTVDMSTATE_LINEAR) |= VDM_INTS_HOOKED_IN_PM;
  79. }
  80. }
  81. }
  82. Handlers[IntNumber].Flags = Flags;
  83. Handlers[IntNumber].CsSelector = Sel;
  84. Handlers[IntNumber].Eip = Offset;
  85. DBGTRACE((USHORT)(VDMTR_TYPE_DPMI_SI | IntNumber), Sel, Offset);
  86. #ifdef _X86_
  87. if (IntNumber == 0x21)
  88. {
  89. VDMSET_INT21_HANDLER_DATA ServiceData;
  90. NTSTATUS Status;
  91. ServiceData.Selector = Handlers[IntNumber].CsSelector;
  92. ServiceData.Offset = Handlers[IntNumber].Eip;
  93. ServiceData.Gate32 = Handlers[IntNumber].Flags & VDM_INT_32;
  94. Status = NtVdmControl(VdmSetInt21Handler, &ServiceData);
  95. #if DBG
  96. if (!NT_SUCCESS(Status)) {
  97. OutputDebugString("DPMI32: Error Setting Int21handler\n");
  98. }
  99. #endif
  100. }
  101. #endif //_X86_
  102. return TRUE;
  103. }
  104. VOID
  105. DpmiInitIDT(
  106. VOID
  107. )
  108. /*++
  109. Routine Description:
  110. This function initializes the state of the IDT. It takes as input the
  111. IDT set up by DOSX, updates the IDT's access bytes, and sets the DPMI32
  112. interrupt handlers by calling SetProtectedModeInterrupt.
  113. --*/
  114. {
  115. DECLARE_LocalVdmContext;
  116. USHORT IntNumber;
  117. USHORT Flags = getBX();
  118. Idt = (PVOID)VdmMapFlat(getAX(), 0, getMODE());
  119. for (IntNumber = 0; IntNumber<256; IntNumber++) {
  120. SetProtectedModeInterrupt(IntNumber,
  121. Idt[IntNumber].Selector,
  122. (((ULONG)Idt[IntNumber].OffsetHi)<<16) +
  123. Idt[IntNumber].OffsetLow,
  124. Flags);
  125. }
  126. }
  127. BOOL
  128. SetFaultHandler(
  129. USHORT IntNumber,
  130. USHORT Sel,
  131. ULONG Offset
  132. )
  133. /*++
  134. Routine Description:
  135. This function services the SetFaultHandler bop. It retrieves
  136. the handler information from the Dos application stack, and puts it into
  137. the VdmTib, for use by instruction emulation.
  138. --*/
  139. {
  140. DECLARE_LocalVdmContext;
  141. PVDM_FAULTHANDLER Handlers = DpmiFaultHandlers;
  142. if (IntNumber >= 32) {
  143. return FALSE;
  144. }
  145. Handlers[IntNumber].Flags = VDM_INT_INT_GATE;
  146. Handlers[IntNumber].CsSelector = Sel;
  147. Handlers[IntNumber].Eip = Offset;
  148. Handlers[IntNumber].SsSelector = 0; //BUGBUG These are obselete
  149. Handlers[IntNumber].Esp = 0; //BUGBUG These are obselete
  150. DBGTRACE((USHORT)(VDMTR_TYPE_DPMI_SF | IntNumber),
  151. Handlers[IntNumber].CsSelector,
  152. Handlers[IntNumber].Eip);
  153. return TRUE;
  154. }
  155. VOID
  156. DpmiInitExceptionHandlers(
  157. VOID
  158. )
  159. {
  160. DECLARE_LocalVdmContext;
  161. USHORT OffsetIncr = getAX();
  162. USHORT IntCount = getBX();
  163. USHORT Selector = getCX();
  164. ULONG Offset = (ULONG) getDX();
  165. USHORT IntNumber;
  166. for (IntNumber = 0; IntNumber < 32; IntNumber++) {
  167. SetFaultHandler(IntNumber, Selector, Offset);
  168. Offset += OffsetIncr;
  169. }
  170. }
  171. VOID
  172. DpmiUnhandledExceptionHandler(
  173. VOID
  174. )
  175. /*++
  176. Routine Description:
  177. This function gets control when a PM fault occurs that isn't handled
  178. by an installed handler. The body of this function emulates Win31
  179. DPMI behavior, where a fault that is reflected to the end of the
  180. PM fault handler chain is then reflected to the PM *interrupt*
  181. chain.
  182. Arguments:
  183. client SS:(E)SP points to dpmi fault stack frame
  184. --*/
  185. {
  186. DECLARE_LocalVdmContext;
  187. PVDM_INTERRUPTHANDLER Handlers = DpmiInterruptHandlers;
  188. USHORT SegSs, SegCs;
  189. UCHAR XNumber;
  190. PCHAR VdmStackPointer;
  191. PCHAR VdmCodePointer;
  192. USHORT FaultingCS;
  193. ULONG FaultingEip;
  194. SegSs = getSS();
  195. VdmStackPointer = Sim32GetVDMPointer(SegSs<<16, 1, TRUE);
  196. if (SEGMENT_IS_BIG(SegSs)) {
  197. VdmStackPointer += getESP();
  198. } else {
  199. VdmStackPointer += getSP();
  200. }
  201. SegCs = getCS();
  202. VdmCodePointer = Sim32GetVDMPointer(SegCs<<16, 1, TRUE);
  203. if (SEGMENT_IS_BIG(SegCs)) {
  204. VdmCodePointer += getEIP();
  205. } else {
  206. VdmCodePointer += getIP();
  207. }
  208. XNumber = *(VdmCodePointer);
  209. if ((XNumber > 7) || (XNumber == 6)) {
  210. DpmiFatalExceptionHandler(XNumber, VdmStackPointer);
  211. return;
  212. }
  213. if (Frame32) {
  214. PCHAR VdmNewStackPointer;
  215. ULONG FrameSS, FrameSP, FrameCS, FrameIP, FrameFlags;
  216. //
  217. // Build an iret frame on the faulting stack
  218. //
  219. FrameSS = *(PDWORD16) (VdmStackPointer+28);
  220. FrameSP = *(PDWORD16) (VdmStackPointer+24) - 12;
  221. *(PDWORD16) (VdmStackPointer+24) = FrameSP;
  222. VdmNewStackPointer = Sim32GetVDMPointer((ULONG)(FrameSS << 16), 1, TRUE);
  223. VdmNewStackPointer += FrameSP;
  224. FrameIP = *(PDWORD16) (VdmStackPointer+12);
  225. *(PDWORD16) (VdmStackPointer+12) = Handlers[XNumber].Eip;
  226. *(PDWORD16) (VdmNewStackPointer) = FrameIP;
  227. FrameCS = *(PDWORD16) (VdmStackPointer+16);
  228. *(PDWORD16) (VdmStackPointer+16) = (ULONG) Handlers[XNumber].CsSelector;
  229. *(PDWORD16) (VdmNewStackPointer+4) = FrameCS;
  230. FrameFlags = *(PDWORD16) (VdmStackPointer+20);
  231. *(PDWORD16) (VdmNewStackPointer+4) = FrameFlags;
  232. FrameFlags &= ~(EFLAGS_IF_MASK | EFLAGS_TF_MASK);
  233. *(PDWORD16) (VdmStackPointer+20) = FrameFlags;
  234. //
  235. // Simulate a dpmi fault handler retf
  236. //
  237. setCS((USHORT)*(PDWORD16)(VdmStackPointer+4));
  238. setEIP(*(PDWORD16)(VdmStackPointer));
  239. setESP(getESP() + 8);
  240. } else {
  241. USHORT FrameSS, FrameSP, FrameCS, FrameIP, FrameFlags;
  242. FrameSS = *(PWORD16) (VdmStackPointer+14);
  243. FrameCS = *(PWORD16) (VdmStackPointer+8);
  244. FrameFlags = *(PWORD16) (VdmStackPointer+10);
  245. if (!SEGMENT_IS_BIG(FrameSS) && !SEGMENT_IS_BIG(FrameCS)) {
  246. PCHAR VdmNewStackPointer;
  247. //
  248. // Build an iret frame on the faulting stack
  249. //
  250. FrameSP = *(PWORD16) (VdmStackPointer+12) - 6;
  251. *(PWORD16) (VdmStackPointer+12) = FrameSP;
  252. VdmNewStackPointer = Sim32GetVDMPointer((ULONG)(FrameSS << 16)+FrameSP, 1, TRUE);
  253. FrameIP = *(PWORD16) (VdmStackPointer+6);
  254. *(PWORD16) (VdmStackPointer+6) = (WORD) Handlers[XNumber].Eip;
  255. *(PWORD16) (VdmNewStackPointer) = FrameIP;
  256. *(PWORD16) (VdmStackPointer+8) = Handlers[XNumber].CsSelector;
  257. *(PWORD16) (VdmNewStackPointer+2) = FrameCS;
  258. *(PWORD16) (VdmNewStackPointer+4) = FrameFlags;
  259. FrameFlags &= ~(EFLAGS_IF_MASK | EFLAGS_TF_MASK);
  260. *(PWORD16) (VdmStackPointer+10) = FrameFlags;
  261. //
  262. // Simulate a dpmi fault handler retf
  263. //
  264. setCS(*(PWORD16)(VdmStackPointer+2));
  265. setEIP((DWORD)*(PWORD16)(VdmStackPointer));
  266. setSP(getSP() + 4);
  267. } else {
  268. //
  269. // Build an iret frame on the locked DPMI stack
  270. //
  271. FrameCS = *(PWORD16) (VdmStackPointer+2);
  272. FrameIP = *(PWORD16) (VdmStackPointer);
  273. FrameFlags &= ~EFLAGS_IF_MASK;
  274. setSP(getSP() - 2);
  275. *(PWORD16)(VdmStackPointer-2) = FrameIP;
  276. *(PWORD16)(VdmStackPointer) = FrameCS;
  277. *(PWORD16)(VdmStackPointer+2) = FrameFlags;
  278. setCS(Handlers[XNumber].CsSelector);
  279. setEIP((DWORD)LOWORD(Handlers[XNumber].Eip));
  280. setSTATUS((WORD) FrameFlags & ~EFLAGS_TF_MASK);
  281. }
  282. }
  283. }
  284. VOID
  285. DpmiFatalExceptionHandler(
  286. UCHAR XNumber,
  287. PCHAR VdmStackPointer
  288. )
  289. /*++
  290. Routine Description:
  291. This function gets control when a PM fault 6, 8-1f occurs that isn't
  292. handled by an installed handler. It pops up an error dialog for the
  293. user.
  294. Arguments:
  295. XNumber - exception number (0-1fh)
  296. VdmStackPointer - flat pointer to stack frame
  297. --*/
  298. {
  299. DECLARE_LocalVdmContext;
  300. char szBuffer[255];
  301. USHORT FaultingCS;
  302. ULONG FaultingEip;
  303. if (Frame32) {
  304. FaultingCS = (USHORT)*(PDWORD16)(VdmStackPointer+16);
  305. FaultingEip = *(PDWORD16)(VdmStackPointer+12);
  306. } else {
  307. FaultingCS = *(PWORD16)(VdmStackPointer+8);
  308. FaultingEip = (ULONG)*(PWORD16)(VdmStackPointer+6);
  309. }
  310. wsprintf(szBuffer, "X#=%.02X, CS=%.04X IP=%.08X",
  311. XNumber, FaultingCS, FaultingEip);
  312. RcErrorDialogBox(EG_BAD_FAULT, szBuffer, NULL);
  313. //
  314. // Need to try to ignore it. Since we are on a dpmi exception frame
  315. // we can just simulate a retf.
  316. //
  317. if (Frame32) {
  318. setCS((USHORT)*(PDWORD16)(VdmStackPointer+4));
  319. setEIP(*(PDWORD16)(VdmStackPointer));
  320. setESP(getESP() + 8);
  321. } else {
  322. setCS(*(PWORD16)(VdmStackPointer+2));
  323. setEIP((DWORD)*(PWORD16)(VdmStackPointer));
  324. setSP(getSP() + 4);
  325. }
  326. }
  327. VOID
  328. DpmiInitPmStackInfo(
  329. VOID
  330. )
  331. /*++
  332. Routine Description:
  333. This routine is called via BOP by DOSX to initialize values related
  334. to stack handling.
  335. Arguments:
  336. Client ES = selector of locked PM stack
  337. Return Value:
  338. None
  339. Notes:
  340. The offset of the locked pm stack is hard-coded to 0x1000, per dpmi
  341. and win31.
  342. --*/
  343. {
  344. DECLARE_LocalVdmContext;
  345. LockedPMStackSel = getES();
  346. LockedPMStackCount = 0;
  347. #ifdef _X86_
  348. ((PVDM_TIB)NtCurrentTeb()->Vdm)->DpmiInfo.Flags = CurrentAppFlags;
  349. #endif
  350. }
  351. BOOL
  352. DpmiSwIntHandler(
  353. ULONG IntNumber
  354. )
  355. /*++
  356. Routine Description:
  357. This routine is called by the emulator to dispatch a SW interrupt.
  358. Arguments:
  359. IntNumber - interrupt vector number
  360. Return Value:
  361. TRUE if the interrupt was dispatched, FALSE otherwise
  362. --*/
  363. {
  364. DECLARE_LocalVdmContext;
  365. PVDM_INTERRUPTHANDLER Handlers = DpmiInterruptHandlers;
  366. ULONG SaveEFLAGS;
  367. ULONG NewSP;
  368. DBGTRACE(VDMTR_TYPE_DPMI | DPMI_SW_INT, (USHORT)IntNumber, 0);
  369. //
  370. // If we're here via breakpoint, see if it belongs to NTVDM debug code.
  371. //
  372. if ((IntNumber == 3) &&
  373. (*(ULONG *)(IntelBase+FIXED_NTVDMSTATE_LINEAR) & VDM_BREAK_DEBUGGER) &&
  374. DbgBPInt()) {
  375. return TRUE;
  376. }
  377. if (!(getMSW() & MSW_PE)) {
  378. EmulateV86Int((UCHAR)IntNumber);
  379. } else {
  380. PUCHAR VdmStackPointer;
  381. // Protect mode
  382. SaveEFLAGS = getEFLAGS();
  383. //BUGBUG turn off task bits
  384. SaveEFLAGS &= ~EFLAGS_NT_MASK;
  385. setEFLAGS(SaveEFLAGS & ~EFLAGS_TF_MASK);
  386. if (!SEGMENT_IS_PRESENT(Handlers[IntNumber].CsSelector)) {
  387. return FALSE;
  388. }
  389. if (!BuildStackFrame(3, &VdmStackPointer, &NewSP)) {
  390. return FALSE;
  391. }
  392. if (Frame32) {
  393. *(PDWORD16)(VdmStackPointer-4) = SaveEFLAGS;
  394. *(PDWORD16)(VdmStackPointer-8) = getCS();
  395. *(PDWORD16)(VdmStackPointer-12) = getEIP();
  396. setEIP(Handlers[IntNumber].Eip);
  397. setESP(NewSP);
  398. } else {
  399. *(PWORD16)(VdmStackPointer-2) = (WORD) SaveEFLAGS;
  400. *(PWORD16)(VdmStackPointer-4) = (WORD) getCS();
  401. *(PWORD16)(VdmStackPointer-6) = (WORD) getEIP();
  402. setEIP((DWORD)LOWORD(Handlers[IntNumber].Eip));
  403. setSP((WORD)NewSP);
  404. }
  405. setCS(Handlers[IntNumber].CsSelector);
  406. #if DBG
  407. if (Handlers[IntNumber].CsSelector != getCS()) {
  408. char szFormat[] = "NTVDM Dpmi Error! Can't set CS to %.4X\n";
  409. char szMsg[sizeof(szFormat)+30];
  410. wsprintf(szMsg, szFormat, Handlers[IntNumber].CsSelector);
  411. OutputDebugString(szMsg);
  412. }
  413. #endif
  414. }
  415. DBGTRACE(VDMTR_TYPE_DPMI | DPMI_DISPATCH_INT, (USHORT)IntNumber, 0);
  416. return TRUE;
  417. }
  418. BOOL
  419. DpmiHwIntHandler(
  420. ULONG IntNumber
  421. )
  422. /*++
  423. Routine Description:
  424. This routine is called by the emulator to dispatch a HW interrupt.
  425. Arguments:
  426. IntNumber - interrupt vector number
  427. Return Value:
  428. TRUE if the interrupt was dispatched, FALSE otherwise
  429. --*/
  430. {
  431. DECLARE_LocalVdmContext;
  432. PVDM_INTERRUPTHANDLER Handlers = DpmiInterruptHandlers;
  433. ULONG SaveEFLAGS;
  434. ULONG NewSP;
  435. DBGTRACE(VDMTR_TYPE_DPMI | DPMI_HW_INT, (USHORT)IntNumber, 0);
  436. if (!(getMSW() & MSW_PE)) {
  437. EmulateV86Int((UCHAR)IntNumber);
  438. } else {
  439. PUCHAR VdmStackPointer;
  440. SaveEFLAGS = getEFLAGS();
  441. //BUGBUG turn off task bits
  442. SaveEFLAGS &= ~0x4000;
  443. setEFLAGS(SaveEFLAGS & ~(EFLAGS_IF_MASK | EFLAGS_TF_MASK));
  444. BeginUseLockedPMStack();
  445. if (!BuildStackFrame(6, &VdmStackPointer, &NewSP)) {
  446. EndUseLockedPMStack();
  447. return FALSE;
  448. }
  449. if (Frame32) {
  450. *(PDWORD16)(VdmStackPointer-4) = SaveEFLAGS;
  451. *(PDWORD16)(VdmStackPointer-8) = getCS();
  452. *(PDWORD16)(VdmStackPointer-12) = getEIP();
  453. *(PDWORD16)(VdmStackPointer-16) = getEFLAGS();
  454. *(PDWORD16)(VdmStackPointer-20) = (DWORD)HIWORD(DosxIntHandlerIretd);
  455. *(PDWORD16)(VdmStackPointer-24) = (DWORD)LOWORD(DosxIntHandlerIretd);
  456. setEIP(Handlers[IntNumber].Eip);
  457. setESP(NewSP);
  458. } else {
  459. *(PWORD16)(VdmStackPointer-2) = (WORD)SaveEFLAGS;
  460. *(PWORD16)(VdmStackPointer-4) = (WORD)getCS();
  461. *(PWORD16)(VdmStackPointer-6) = (WORD)getIP();
  462. *(PWORD16)(VdmStackPointer-8) = (WORD)getEFLAGS();
  463. *(PWORD16)(VdmStackPointer-10) = HIWORD(DosxIntHandlerIret);
  464. *(PWORD16)(VdmStackPointer-12) = LOWORD(DosxIntHandlerIret);
  465. setEIP((DWORD)LOWORD(Handlers[IntNumber].Eip));
  466. setSP((WORD)NewSP);
  467. }
  468. setCS(Handlers[IntNumber].CsSelector);
  469. }
  470. DBGTRACE(VDMTR_TYPE_DPMI | DPMI_DISPATCH_INT, (USHORT)IntNumber, 0);
  471. return TRUE;
  472. }
  473. VOID
  474. DpmiIntHandlerIret16(
  475. VOID
  476. )
  477. /*++
  478. Routine Description:
  479. This routine is an IRET hook called via a BOP in dosx. It is called
  480. at the end of a 16-bit HW or SW interrupt. The main reason we want
  481. to come in here is to maintain the DPMI stack, and know when to restore
  482. the original values when we pop back out to level zero.
  483. --*/
  484. {
  485. DECLARE_LocalVdmContext;
  486. PUCHAR VdmStackPointer;
  487. ULONG NewSP;
  488. USHORT SegSs;
  489. BOOL bSsBig;
  490. SegSs = getSS();
  491. VdmStackPointer = Sim32GetVDMPointer(SegSs<<16, 1, TRUE);
  492. if (bSsBig = SEGMENT_IS_BIG(SegSs)) {
  493. VdmStackPointer += getESP();
  494. } else {
  495. VdmStackPointer += getSP();
  496. }
  497. //
  498. // Fast iret (without executing final 16-bit iret)
  499. //
  500. #ifdef _X86_
  501. setCS(*(PWORD16)(VdmStackPointer+2));
  502. setEFLAGS((getEFLAGS()&0xffff0000) | *(PWORD16)(VdmStackPointer+4));
  503. //
  504. // if EndUseLockedPMStack fails, then we need to restore EIP and pop
  505. // the stack frame
  506. //
  507. if (!EndUseLockedPMStack()) {
  508. setEIP((DWORD)*(PWORD16)(VdmStackPointer));
  509. //
  510. // Pop iret frame off the stack
  511. //
  512. if (bSsBig) {
  513. setESP(getESP()+6);
  514. } else {
  515. setSP(getSP()+6);
  516. }
  517. }
  518. //
  519. // Slow iret (with executing final 16-bit iret)
  520. //
  521. #else
  522. if (EndUseLockedPMStack()) {
  523. ULONG NewEIP, NewEFLAGS, NewCS;
  524. NewEIP = getEIP();
  525. NewCS = (ULONG) *(PWORD16)(VdmStackPointer+2);
  526. NewEFLAGS = (getEFLAGS()&0xffff0000) | *(PWORD16)(VdmStackPointer+4);
  527. //
  528. // Since EndUseLockedPMStack() has restored all of EIP, and we may be
  529. // returning to a 32-bit code segment, build a 32-bit iret frame
  530. // even if this is a 16-bit client. That way, EIP will be restored
  531. // correctly.
  532. // Pass 6 to BuildStackFrame since 6 words = 3 dwords
  533. //
  534. if (!BuildStackFrame(6, &VdmStackPointer, &NewSP)) {
  535. #if DBG
  536. OutputDebugString("NTVDM: Dpmi encountered a stack fault!\n");
  537. #endif
  538. DpmiFaultHandler(STACK_FAULT, 0);
  539. return;
  540. }
  541. //
  542. // SS has changed, so we need to check LDT again
  543. //
  544. if (SEGMENT_IS_BIG(getSS())) {
  545. setESP(NewSP);
  546. } else {
  547. setSP((WORD)NewSP);
  548. }
  549. *(PDWORD16)(VdmStackPointer-4) = NewEFLAGS;
  550. *(PDWORD16)(VdmStackPointer-8) = NewCS;
  551. *(PDWORD16)(VdmStackPointer-12) = NewEIP;
  552. setCS(HIWORD(DosxIretd));
  553. setEIP((ULONG)LOWORD(DosxIretd));
  554. } else {
  555. // still on locked stack, just do a real iret (16-bit frame)
  556. setCS(HIWORD(DosxIret));
  557. setEIP((ULONG)LOWORD(DosxIret));
  558. }
  559. #endif // _X86_
  560. }
  561. VOID
  562. DpmiIntHandlerIret32(
  563. VOID
  564. )
  565. /*++
  566. Routine Description:
  567. This routine is an IRET hook called via a BOP in dosx. It is called
  568. at the end of a 32-bit HW or SW interrupt. The main reason we want
  569. to come in here is to maintain the DPMI stack, and know when to restore
  570. the original values when we pop back out to level zero.
  571. --*/
  572. {
  573. DECLARE_LocalVdmContext;
  574. PUCHAR VdmStackPointer;
  575. ULONG NewSP;
  576. USHORT SegSs;
  577. BOOL bSsBig;
  578. SegSs = getSS();
  579. VdmStackPointer = Sim32GetVDMPointer(SegSs<<16, 1, TRUE);
  580. if (bSsBig = SEGMENT_IS_BIG(SegSs)) {
  581. VdmStackPointer += getESP();
  582. } else {
  583. VdmStackPointer += getSP();
  584. }
  585. #ifdef _X86_
  586. setCS(*(PDWORD16)(VdmStackPointer+4));
  587. setEFLAGS(*(PDWORD16)(VdmStackPointer+8));
  588. //
  589. // if EndUseLockedPMStack succeeds, then we don't need to restore EIP
  590. //
  591. if (!EndUseLockedPMStack()) {
  592. setEIP(*(PDWORD16)(VdmStackPointer));
  593. //
  594. // Pop iret frame off the stack
  595. //
  596. if (bSsBig) {
  597. setESP(getESP()+12);
  598. } else {
  599. setSP(getSP()+12);
  600. }
  601. }
  602. #else
  603. if (EndUseLockedPMStack()) {
  604. ULONG NewEIP, NewCS, NewEFLAGS;
  605. NewEIP = getEIP();
  606. NewCS = *(PDWORD16)(VdmStackPointer+4);
  607. NewEFLAGS = *(PDWORD16)(VdmStackPointer+8);
  608. if (!BuildStackFrame(3, &VdmStackPointer, &NewSP)) {
  609. #if DBG
  610. OutputDebugString("NTVDM: Dpmi encountered a stack fault!\n");
  611. #endif
  612. DpmiFaultHandler(STACK_FAULT, 0);
  613. return;
  614. }
  615. //
  616. // SS has changed, so we need to check LDT again
  617. //
  618. if (SEGMENT_IS_BIG(getSS())) {
  619. setESP(NewSP);
  620. } else {
  621. setSP((WORD)NewSP);
  622. }
  623. *(PDWORD16)(VdmStackPointer-4) = NewEFLAGS;
  624. *(PDWORD16)(VdmStackPointer-8) = NewCS;
  625. *(PDWORD16)(VdmStackPointer-12) = NewEIP;
  626. }
  627. setCS(HIWORD(DosxIretd));
  628. setEIP((ULONG)LOWORD(DosxIretd));
  629. #endif // _X86_
  630. }
  631. #ifndef _X86_
  632. BOOL
  633. DpmiFaultHandler(
  634. ULONG IntNumber,
  635. ULONG ErrorCode
  636. )
  637. /*++
  638. Routine Description:
  639. This routine is called by the emulator when an exception occurs.
  640. Arguments:
  641. IntNumber - exception number (0-1f)
  642. ErrorCode - exception error code to be placed on the stack
  643. Return Value:
  644. TRUE if the interrupt was dispatched, FALSE otherwise
  645. --*/
  646. {
  647. DECLARE_LocalVdmContext;
  648. PVDM_FAULTHANDLER Handlers = DpmiFaultHandlers;
  649. PUCHAR VdmStackPointer;
  650. ULONG SaveSS, SaveESP, SaveEFLAGS, SaveCS, SaveEIP;
  651. ULONG StackOffset;
  652. ULONG NewSP;
  653. DBGTRACE(VDMTR_TYPE_DPMI | DPMI_FAULT, (USHORT)IntNumber, ErrorCode);
  654. if ((IntNumber == 1) &&
  655. (*(ULONG *)(IntelBase+FIXED_NTVDMSTATE_LINEAR) & VDM_BREAK_DEBUGGER) &&
  656. DbgTraceInt()) {
  657. return TRUE;
  658. }
  659. if (DbgFault(IntNumber)) { // try the debugger
  660. //
  661. // exception handled via user input
  662. //
  663. return TRUE;
  664. }
  665. if (!(getMSW() & MSW_PE)) {
  666. EmulateV86Int((UCHAR)IntNumber);
  667. return TRUE;
  668. }
  669. SaveSS = getSS();
  670. SaveESP = getESP();
  671. SaveEFLAGS = getEFLAGS();
  672. SaveEIP = getEIP();
  673. SaveCS = getCS();
  674. setEFLAGS(SaveEFLAGS & ~(EFLAGS_IF_MASK | EFLAGS_TF_MASK));
  675. if ((IntNumber == 13) || (IntNumber == 6)) {
  676. if (DpmiEmulateInstruction()) {
  677. return TRUE;
  678. }
  679. }
  680. if (!SEGMENT_IS_PRESENT(Handlers[IntNumber].CsSelector)) {
  681. return FALSE;
  682. }
  683. //
  684. // switch stacks
  685. //
  686. BeginUseLockedPMStack();
  687. //
  688. // Win31 has an undocumented feature of creating a 32byte area on the
  689. // stack. Krnl386 sticks stuff in there, so we emulate the behavior here.
  690. //
  691. setESP(getESP()-0x20);
  692. //
  693. // allocate space on new stack
  694. //
  695. if (!BuildStackFrame(8, &VdmStackPointer, &NewSP)) {
  696. //BUGBUG Check for double fault
  697. EndUseLockedPMStack();
  698. return FALSE;
  699. }
  700. if (Frame32) {
  701. *(PDWORD16)(VdmStackPointer-4) = SaveSS;
  702. *(PDWORD16)(VdmStackPointer-8) = SaveESP;
  703. *(PDWORD16)(VdmStackPointer-12) = SaveEFLAGS;
  704. *(PDWORD16)(VdmStackPointer-16) = SaveCS;
  705. *(PDWORD16)(VdmStackPointer-20) = SaveEIP;
  706. *(PDWORD16)(VdmStackPointer-24) = ErrorCode;
  707. *(PDWORD16)(VdmStackPointer-28) = (ULONG) HIWORD(DosxFaultHandlerIretd);
  708. *(PDWORD16)(VdmStackPointer-32) = (ULONG) LOWORD(DosxFaultHandlerIretd);
  709. setEIP(Handlers[IntNumber].Eip);
  710. setESP(NewSP);
  711. } else {
  712. *(PWORD16)(VdmStackPointer-2) = (WORD) SaveSS;
  713. *(PWORD16)(VdmStackPointer-4) = (WORD) SaveESP;
  714. *(PWORD16)(VdmStackPointer-6) = (WORD) SaveEFLAGS;
  715. *(PWORD16)(VdmStackPointer-8) = (WORD) SaveCS;
  716. *(PWORD16)(VdmStackPointer-10) = (WORD) SaveEIP;
  717. *(PWORD16)(VdmStackPointer-12) = (WORD) ErrorCode;
  718. *(PDWORD16)(VdmStackPointer-16) = DosxFaultHandlerIret;
  719. setEIP(LOWORD(Handlers[IntNumber].Eip));
  720. setSP((WORD)NewSP);
  721. }
  722. setCS(Handlers[IntNumber].CsSelector);
  723. #if DBG
  724. if (Handlers[IntNumber].CsSelector != getCS()) {
  725. char szFormat[] = "NTVDM Dpmi Error! Can't set CS to %.4X\n";
  726. char szMsg[sizeof(szFormat)+30];
  727. wsprintf(szMsg, szFormat, Handlers[IntNumber].CsSelector);
  728. OutputDebugString(szMsg);
  729. }
  730. #endif
  731. DBGTRACE(VDMTR_TYPE_DPMI | DPMI_DISPATCH_FAULT, (USHORT)IntNumber, 0);
  732. return TRUE;
  733. }
  734. #endif // _X86_
  735. VOID
  736. DpmiFaultHandlerIret16(
  737. VOID
  738. )
  739. /*++
  740. Routine Description:
  741. This routine is an IRET hook called via a BOP in dosx. It is called
  742. at the end of the execution of a 16-bit fault handler.
  743. --*/
  744. {
  745. DECLARE_LocalVdmContext;
  746. PUCHAR VdmStackPointer;
  747. USHORT SegSs;
  748. SegSs = getSS();
  749. VdmStackPointer = Sim32GetVDMPointer(SegSs<<16, 1, TRUE);
  750. if (SEGMENT_IS_BIG(SegSs)) {
  751. VdmStackPointer += getESP();
  752. } else {
  753. VdmStackPointer += getSP();
  754. }
  755. EndUseLockedPMStack();
  756. setEIP((DWORD)*(PWORD16)(VdmStackPointer+2));
  757. setCS(*(PWORD16)(VdmStackPointer+4));
  758. setSTATUS(*(PWORD16)(VdmStackPointer+6));
  759. setSP(*(PWORD16)(VdmStackPointer+8));
  760. setSS(*(PWORD16)(VdmStackPointer+10));
  761. }
  762. VOID
  763. DpmiFaultHandlerIret32(
  764. VOID
  765. )
  766. /*++
  767. Routine Description:
  768. This routine is an IRET hook called via a BOP in dosx. It is called
  769. at the end of the execution of a 32-bit fault handler.
  770. --*/
  771. {
  772. DECLARE_LocalVdmContext;
  773. PUCHAR VdmStackPointer;
  774. USHORT SegSs;
  775. SegSs = getSS();
  776. VdmStackPointer = Sim32GetVDMPointer(SegSs<<16, 1, TRUE);
  777. if (SEGMENT_IS_BIG(SegSs)) {
  778. VdmStackPointer += getESP();
  779. } else {
  780. VdmStackPointer += getSP();
  781. }
  782. EndUseLockedPMStack();
  783. setEIP(*(PDWORD16)(VdmStackPointer+4));
  784. setCS((USHORT)*(PDWORD16)(VdmStackPointer+8));
  785. setEFLAGS(*(PDWORD16)(VdmStackPointer+12));
  786. setESP(*(PDWORD16)(VdmStackPointer+16));
  787. setSS((USHORT)*(PWORD16)(VdmStackPointer+20));
  788. }
  789. VOID
  790. DpmiHungAppIretAndExit(
  791. VOID
  792. )
  793. /*++
  794. Routine Description:
  795. This routine is called via BOP during hung app processing. The
  796. Keyboard driver calls this in the context of a hw interrupt in
  797. order to terminate the app. We need to "unwind" the current
  798. interrupt, and transfer control to code which will execute
  799. a DOS exit.
  800. --*/
  801. {
  802. DECLARE_LocalVdmContext;
  803. EndUseLockedPMStack();
  804. setCS(HIWORD(DosxHungAppExit));
  805. setIP(LOWORD(DosxHungAppExit));
  806. }
  807. BOOL
  808. DispatchPMInt(
  809. UCHAR IntNumber
  810. )
  811. /*++
  812. Routine Description:
  813. This routine is called at the end of a PM int chain. It is provided
  814. for compatibility to win31/win95. On win31/win95, VMM and VxD's have
  815. the opportunity to perform some functionality at the point where
  816. the dpmi host is about to switch the machine to v86 mode to continue
  817. the interrupt chain. Sometimes, the function is totally handled by
  818. a hook at this point.
  819. This routine provides a framework for this mechanism to allow the
  820. emulation of this behavior.
  821. Arguments:
  822. IntNumber - the interrupt# that is about to be reflected to v86 mode
  823. Return Value:
  824. TRUE if the interrupt was handled and control can return to the app
  825. FALSE otherwise, continue the reflection to v86 mode.
  826. --*/
  827. {
  828. BOOL bHandled;
  829. switch(IntNumber) {
  830. case 0x2f:
  831. bHandled = PMInt2fHandler();
  832. break;
  833. default:
  834. bHandled = FALSE;
  835. }
  836. if (bHandled) {
  837. SimulateIret(RESTORE_FLAGS);
  838. }
  839. return bHandled;
  840. }
  841. BOOL
  842. CheckEIP(
  843. ULONG Increment
  844. )
  845. /*++
  846. Routine Description:
  847. This routine does a limit check on EIP.
  848. Arguments:
  849. None
  850. Return Value:
  851. TRUE if EIP is ok, FALSE otherwise
  852. --*/
  853. {
  854. //BUGBUG NEED TO RETURN FALSE HERE IF EIP WOULD BE OFF THE END OF SEGMENT
  855. return TRUE;
  856. }
  857. #ifndef _X86_
  858. BOOL
  859. DpmiEmulateInstruction(
  860. VOID
  861. )
  862. /*++
  863. Routine Description:
  864. This routine checks to see if the instruction which caused the
  865. fault really needs to be emulated. For example, the MS C compiler (v7.00)
  866. uses instructions to manipulate the FP flags in CR0. The compiler
  867. expects them to just work as they do on win31, which also emulates them.
  868. Arguments:
  869. None
  870. Return Value:
  871. TRUE if the instruction was emulated, FALSE otherwise
  872. --*/
  873. {
  874. DECLARE_LocalVdmContext;
  875. PUCHAR pCode;
  876. UCHAR Opcode;
  877. ULONG SegCS;
  878. BOOL bReturn = FALSE;
  879. SegCS = getCS();
  880. pCode = Sim32GetVDMPointer(SegCS<<16, 1, TRUE);
  881. if (Ldt[(SegCS & ~0x7)/sizeof(LDT_ENTRY)].HighWord.Bits.Default_Big) {
  882. pCode += getEIP();
  883. } else {
  884. pCode += getIP();
  885. }
  886. Opcode = *pCode++;
  887. switch (Opcode) {
  888. case 0xf:
  889. if (!CheckEIP(1)) {
  890. break;
  891. }
  892. bReturn = DpmiOp0f(pCode);
  893. break;
  894. case 0x8e:
  895. //
  896. // This is WIN31 compatibility. If we are trying to dispatch
  897. // the client, and we get a fault loading the segment registers,
  898. // then zero them out.
  899. // BUGBUG currently only looking for FS, GS
  900. //
  901. if (!CheckEIP(2)) {
  902. break;
  903. }
  904. //
  905. // Look for code in dxutil.asm EnterProtectedMode
  906. //
  907. if ((SegCS == DosxRmCodeSelector) &&
  908. ((*pCode == 0xe0) || // mov fs, ax
  909. (*pCode == 0xe8)) // mov gs, ax
  910. ) {
  911. setEIP(getEIP()+2);
  912. bReturn = TRUE;
  913. }
  914. break;
  915. }
  916. DBGTRACE(VDMTR_TYPE_DPMI | DPMI_OP_EMULATION, Opcode, (ULONG) bReturn);
  917. return bReturn;
  918. }
  919. #define MI_GET_CRx_OPCODE 0x20
  920. #define MI_SET_CRx_OPCODE 0x22
  921. #define MI_MODMASK 0xC0
  922. #define MI_MODMOVSPEC 0xC0
  923. #define MI_REGMASK 0x38
  924. #define MI_RMMASK 0x7
  925. BOOL
  926. DpmiOp0f(
  927. PUCHAR pCode
  928. )
  929. /*++
  930. Routine Description:
  931. This routine emulates instructions that have 0x0F as the first byte.
  932. Arguments:
  933. None
  934. Return Value:
  935. TRUE if the instruction was emulated, FALSE otherwise
  936. --*/
  937. {
  938. DECLARE_LocalVdmContext;
  939. ULONG Value;
  940. switch (*pCode++) {
  941. case MI_GET_CRx_OPCODE:
  942. if (!CheckEIP(2)) {
  943. break;
  944. }
  945. if ((*pCode & MI_MODMASK) != MI_MODMOVSPEC) {
  946. break;
  947. }
  948. if (*pCode & MI_REGMASK) {
  949. Value = 0; // not CR0
  950. } else {
  951. Value = getCR0();
  952. }
  953. SetRegisterByIndex[*pCode & MI_RMMASK](Value);
  954. setEIP(getEIP()+3);
  955. return TRUE;
  956. case MI_SET_CRx_OPCODE:
  957. if (!CheckEIP(2)) {
  958. break;
  959. }
  960. if ((*pCode & MI_MODMASK) != MI_MODMOVSPEC) {
  961. break;
  962. }
  963. if (*pCode & MI_REGMASK) {
  964. break; // not CR0
  965. }
  966. setCR0(GetRegisterByIndex[*pCode & MI_RMMASK]());
  967. setEIP(getEIP()+3);
  968. return TRUE;
  969. }
  970. return FALSE;
  971. }
  972. #endif