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.

1480 lines
47 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. exceptn.c
  5. Abstract:
  6. This module implement the code necessary to dispatch expections to the
  7. proper mode and invoke the exception dispatcher.
  8. Author:
  9. David N. Cutler (davec) 30-Apr-1989
  10. Environment:
  11. Kernel mode only.
  12. --*/
  13. #include "ki.h"
  14. #define FN_BITS_PER_TAGWORD 16
  15. #define FN_TAG_EMPTY 0x3
  16. #define FN_TAG_MASK 0x3
  17. #define FX_TAG_VALID 0x1
  18. #define NUMBER_OF_FP_REGISTERS 8
  19. #define BYTES_PER_FP_REGISTER 10
  20. #define BYTES_PER_FX_REGISTER 16
  21. extern UCHAR VdmUserCr0MapIn[];
  22. extern BOOLEAN KeI386FxsrPresent;
  23. extern BOOLEAN KeI386XMMIPresent;
  24. VOID
  25. Ki386AdjustEsp0(
  26. IN PKTRAP_FRAME TrapFrame
  27. );
  28. BOOLEAN
  29. KiEm87StateToNpxFrame(
  30. OUT PFLOATING_SAVE_AREA NpxFrmae
  31. );
  32. BOOLEAN
  33. KiNpxFrameToEm87State(
  34. IN PFLOATING_SAVE_AREA NpxFrmae
  35. );
  36. ULONG
  37. KiEspFromTrapFrame(
  38. IN PKTRAP_FRAME TrapFrame
  39. )
  40. /*++
  41. Routine Description:
  42. This routine fetches the correct esp from a trapframe, accounting
  43. for whether the frame is a user or kernel mode frame, and whether
  44. it has been edited.
  45. Arguments:
  46. TrapFrame - Supplies a pointer to a trap frame from which volatile context
  47. should be copied into the context record.
  48. Return Value:
  49. Value of Esp.
  50. --*/
  51. {
  52. if (((TrapFrame->SegCs & MODE_MASK) != KernelMode) ||
  53. (TrapFrame->EFlags & EFLAGS_V86_MASK)) {
  54. // User mode frame, real value of Esp is always in HardwareEsp.
  55. return TrapFrame->HardwareEsp;
  56. } else {
  57. if ((TrapFrame->SegCs & FRAME_EDITED) == 0) {
  58. // Kernel mode frame which has had esp edited,
  59. // value of Esp is in TempEsp.
  60. return TrapFrame->TempEsp;
  61. } else {
  62. // Kernel mode frame has has not had esp edited, compute esp.
  63. return (ULONG)&TrapFrame->HardwareEsp;
  64. }
  65. }
  66. }
  67. VOID
  68. KiEspToTrapFrame(
  69. IN PKTRAP_FRAME TrapFrame,
  70. IN ULONG Esp
  71. )
  72. /*++
  73. Routine Description:
  74. This routine sets the specified value Esp into the trap frame,
  75. accounting for whether the frame is a user or kernel mode frame,
  76. and whether it has been edited before.
  77. Arguments:
  78. TrapFrame - Supplies a pointer to a trap frame from which volatile context
  79. should be copied into the context record.
  80. Esp - New value for Esp.
  81. Return Value:
  82. None.
  83. --*/
  84. {
  85. ULONG OldEsp;
  86. KIRQL OldIrql;
  87. //
  88. // Raise to at least APC_LEVEL so we can test SegCs and EFLAGS
  89. // as an atomic operation since NtSetContextThread may modify these.
  90. //
  91. OldIrql = KeGetCurrentIrql ();
  92. if (OldIrql < APC_LEVEL) {
  93. KeRaiseIrql (APC_LEVEL, &OldIrql);
  94. }
  95. OldEsp = KiEspFromTrapFrame(TrapFrame);
  96. if (((TrapFrame->SegCs & MODE_MASK) != KernelMode) ||
  97. (TrapFrame->EFlags & EFLAGS_V86_MASK)) {
  98. //
  99. // User mode trap frame
  100. //
  101. TrapFrame->HardwareEsp = Esp;
  102. } else {
  103. //
  104. // Kernel mode esp can't be lowered or iret emulation will fail
  105. //
  106. if (Esp < OldEsp)
  107. KeBugCheckEx(SET_OF_INVALID_CONTEXT,
  108. Esp, OldEsp, (ULONG_PTR)TrapFrame, 0);
  109. //
  110. // Edit frame, setting edit marker as needed.
  111. //
  112. if ((TrapFrame->SegCs & FRAME_EDITED) == 0) {
  113. // Kernel frame that has already been edited,
  114. // store value in TempEsp.
  115. TrapFrame->TempEsp = Esp;
  116. } else {
  117. // Kernel frame for which Esp is being edited first time.
  118. // Save real SegCs, set marked in SegCs, save Esp value.
  119. if (OldEsp != Esp) {
  120. TrapFrame->TempSegCs = TrapFrame->SegCs;
  121. TrapFrame->SegCs = TrapFrame->SegCs & ~FRAME_EDITED;
  122. TrapFrame->TempEsp = Esp;
  123. }
  124. }
  125. }
  126. //
  127. // Restore original IRQL
  128. //
  129. if (OldIrql < APC_LEVEL) {
  130. KeLowerIrql (OldIrql);
  131. }
  132. }
  133. ULONG
  134. KiSegSsFromTrapFrame(
  135. IN PKTRAP_FRAME TrapFrame
  136. )
  137. /*++
  138. Routine Description:
  139. This routine fetches the correct ss from a trapframe, accounting
  140. for whether the frame is a user or kernel mode frame.
  141. Arguments:
  142. TrapFrame - Supplies a pointer to a trap frame from which volatile context
  143. should be copied into the context record.
  144. Return Value:
  145. Value of SegSs.
  146. --*/
  147. {
  148. if (TrapFrame->EFlags & EFLAGS_V86_MASK){
  149. return TrapFrame->HardwareSegSs;
  150. } else if ((TrapFrame->SegCs & MODE_MASK) != KernelMode) {
  151. //
  152. // It's user mode. The HardwareSegSs contains R3 data selector.
  153. //
  154. return TrapFrame->HardwareSegSs | RPL_MASK;
  155. } else {
  156. return KGDT_R0_DATA;
  157. }
  158. }
  159. VOID
  160. KiSegSsToTrapFrame(
  161. IN PKTRAP_FRAME TrapFrame,
  162. IN ULONG SegSs
  163. )
  164. /*++
  165. Routine Description:
  166. It turns out that in a flat system there are only two legal values
  167. for SS. Therefore, this procedure forces the appropriate one
  168. of those values to be used. The legal SS value is a function of
  169. which CS value is already set.
  170. Arguments:
  171. TrapFrame - Supplies a pointer to a trap frame from which volatile context
  172. should be copied into the context record.
  173. SegSs - value of SS caller would like to set.
  174. Return Value:
  175. Nothing.
  176. --*/
  177. {
  178. SegSs &= SEGMENT_MASK; // Throw away the high order trash bits
  179. if (TrapFrame->EFlags & EFLAGS_V86_MASK) {
  180. TrapFrame->HardwareSegSs = SegSs;
  181. } else if ((TrapFrame->SegCs & MODE_MASK) == UserMode) {
  182. //
  183. // If user mode, we simply put SegSs to trapfram. If the SegSs
  184. // is a bogus value. The trap0d handler will be able to detect
  185. // this and handle it appropriately.
  186. //
  187. TrapFrame->HardwareSegSs = SegSs | RPL_MASK;
  188. }
  189. //
  190. // else {
  191. // The frame is a kernel mode frame, which does not have
  192. // a place to store SS. Therefore, do nothing.
  193. //
  194. }
  195. VOID
  196. KeContextFromKframes (
  197. IN PKTRAP_FRAME TrapFrame,
  198. IN PKEXCEPTION_FRAME ExceptionFrame,
  199. IN OUT PCONTEXT ContextFrame
  200. )
  201. /*++
  202. Routine Description:
  203. This routine moves the selected contents of the specified trap and exception frames
  204. frames into the specified context frame according to the specified context
  205. flags.
  206. Arguments:
  207. TrapFrame - Supplies a pointer to a trap frame from which volatile context
  208. should be copied into the context record.
  209. ExceptionFrame - Supplies a pointer to an exception frame from which context
  210. should be copied into the context record. This argument is ignored since
  211. there is no exception frame on NT386.
  212. ContextFrame - Supplies a pointer to the context frame that receives the
  213. context copied from the trap and exception frames.
  214. Return Value:
  215. None.
  216. --*/
  217. {
  218. PFX_SAVE_AREA NpxFrame;
  219. BOOLEAN StateSaved;
  220. ULONG i;
  221. struct _FPSaveBuffer {
  222. UCHAR Buffer[15];
  223. FLOATING_SAVE_AREA SaveArea;
  224. } FloatSaveBuffer;
  225. PFLOATING_SAVE_AREA PSaveArea;
  226. KIRQL OldIrql;
  227. UNREFERENCED_PARAMETER( ExceptionFrame );
  228. //
  229. // This routine is called at both PASSIVE_LEVEL by exception dispatch
  230. // and at APC_LEVEL by NtSetContextThread. We raise to APC_LEVEL to
  231. // make the trap frame capture atomic.
  232. //
  233. OldIrql = KeGetCurrentIrql ();
  234. if (OldIrql < APC_LEVEL) {
  235. KeRaiseIrql (APC_LEVEL, &OldIrql);
  236. }
  237. //
  238. // Set control information if specified.
  239. //
  240. if ((ContextFrame->ContextFlags & CONTEXT_CONTROL) == CONTEXT_CONTROL) {
  241. //
  242. // Set registers ebp, eip, cs, eflag, esp and ss.
  243. //
  244. ContextFrame->Ebp = TrapFrame->Ebp;
  245. ContextFrame->Eip = TrapFrame->Eip;
  246. if (((TrapFrame->SegCs & FRAME_EDITED) == 0) &&
  247. ((TrapFrame->EFlags & EFLAGS_V86_MASK) == 0)) {
  248. ContextFrame->SegCs = TrapFrame->TempSegCs & SEGMENT_MASK;
  249. } else {
  250. ContextFrame->SegCs = TrapFrame->SegCs & SEGMENT_MASK;
  251. }
  252. ContextFrame->EFlags = TrapFrame->EFlags;
  253. ContextFrame->SegSs = KiSegSsFromTrapFrame(TrapFrame);
  254. ContextFrame->Esp = KiEspFromTrapFrame(TrapFrame);
  255. }
  256. //
  257. // Set segment register contents if specified.
  258. //
  259. if ((ContextFrame->ContextFlags & CONTEXT_SEGMENTS) == CONTEXT_SEGMENTS) {
  260. //
  261. // Set segment registers gs, fs, es, ds.
  262. //
  263. // These values are junk most of the time, but useful
  264. // for debugging under certain conditions. Therefore,
  265. // we report whatever was in the frame.
  266. //
  267. if (TrapFrame->EFlags & EFLAGS_V86_MASK) {
  268. ContextFrame->SegGs = TrapFrame->V86Gs & SEGMENT_MASK;
  269. ContextFrame->SegFs = TrapFrame->V86Fs & SEGMENT_MASK;
  270. ContextFrame->SegEs = TrapFrame->V86Es & SEGMENT_MASK;
  271. ContextFrame->SegDs = TrapFrame->V86Ds & SEGMENT_MASK;
  272. }
  273. else {
  274. if (TrapFrame->SegCs == KGDT_R0_CODE) {
  275. //
  276. // Trap frames created from R0_CODE traps do not save
  277. // the following selectors. Set them in the frame now.
  278. //
  279. TrapFrame->SegGs = 0;
  280. TrapFrame->SegFs = KGDT_R0_PCR;
  281. TrapFrame->SegEs = KGDT_R3_DATA | RPL_MASK;
  282. TrapFrame->SegDs = KGDT_R3_DATA | RPL_MASK;
  283. }
  284. ContextFrame->SegGs = TrapFrame->SegGs & SEGMENT_MASK;
  285. ContextFrame->SegFs = TrapFrame->SegFs & SEGMENT_MASK;
  286. ContextFrame->SegEs = TrapFrame->SegEs & SEGMENT_MASK;
  287. ContextFrame->SegDs = TrapFrame->SegDs & SEGMENT_MASK;
  288. }
  289. }
  290. //
  291. // Set integer register contents if specified.
  292. //
  293. if ((ContextFrame->ContextFlags & CONTEXT_INTEGER) == CONTEXT_INTEGER) {
  294. //
  295. // Set integer registers edi, esi, ebx, edx, ecx, eax
  296. //
  297. ContextFrame->Edi = TrapFrame->Edi;
  298. ContextFrame->Esi = TrapFrame->Esi;
  299. ContextFrame->Ebx = TrapFrame->Ebx;
  300. ContextFrame->Ecx = TrapFrame->Ecx;
  301. ContextFrame->Edx = TrapFrame->Edx;
  302. ContextFrame->Eax = TrapFrame->Eax;
  303. }
  304. if (((ContextFrame->ContextFlags & CONTEXT_EXTENDED_REGISTERS) ==
  305. CONTEXT_EXTENDED_REGISTERS) &&
  306. ((TrapFrame->SegCs & MODE_MASK) == UserMode)) {
  307. //
  308. // This is the base TrapFrame, and the NpxFrame is on the base
  309. // of the kernel stack, just above it in memory.
  310. //
  311. NpxFrame = (PFX_SAVE_AREA)(TrapFrame + 1);
  312. if (KeI386NpxPresent) {
  313. KiFlushNPXState (NULL);
  314. RtlCopyMemory( (PVOID)&(ContextFrame->ExtendedRegisters[0]),
  315. (PVOID)&(NpxFrame->U.FxArea),
  316. MAXIMUM_SUPPORTED_EXTENSION
  317. );
  318. }
  319. }
  320. //
  321. // Fetch floating register contents if requested, and type of target
  322. // is user. (system frames have no fp state, so ignore request)
  323. //
  324. if ( ((ContextFrame->ContextFlags & CONTEXT_FLOATING_POINT) ==
  325. CONTEXT_FLOATING_POINT) &&
  326. ((TrapFrame->SegCs & MODE_MASK) == UserMode)) {
  327. //
  328. // This is the base TrapFrame, and the NpxFrame is on the base
  329. // of the kernel stack, just above it in memory.
  330. //
  331. NpxFrame = (PFX_SAVE_AREA)(TrapFrame + 1);
  332. if (KeI386NpxPresent) {
  333. //
  334. // Force the coprocessors state to the save area and copy it
  335. // to the context frame.
  336. //
  337. if (KeI386FxsrPresent == TRUE) {
  338. //
  339. // FP state save was done using fxsave. Get the save
  340. // area in fnsave format
  341. //
  342. // Save area must be 16 byte aligned so we cushion it with
  343. // 15 bytes (in the locals declaration above) and round
  344. // down to align.
  345. //
  346. ULONG_PTR Temp;
  347. Temp = (ULONG_PTR)&FloatSaveBuffer.SaveArea;
  348. Temp &= ~0xf;
  349. PSaveArea = (PFLOATING_SAVE_AREA)Temp;
  350. KiFlushNPXState (PSaveArea);
  351. } else {
  352. PSaveArea = (PFLOATING_SAVE_AREA)&(NpxFrame->U.FnArea);
  353. KiFlushNPXState (NULL);
  354. }
  355. ContextFrame->FloatSave.ControlWord = PSaveArea->ControlWord;
  356. ContextFrame->FloatSave.StatusWord = PSaveArea->StatusWord;
  357. ContextFrame->FloatSave.TagWord = PSaveArea->TagWord;
  358. ContextFrame->FloatSave.ErrorOffset = PSaveArea->ErrorOffset;
  359. ContextFrame->FloatSave.ErrorSelector = PSaveArea->ErrorSelector;
  360. ContextFrame->FloatSave.DataOffset = PSaveArea->DataOffset;
  361. ContextFrame->FloatSave.DataSelector = PSaveArea->DataSelector;
  362. ContextFrame->FloatSave.Cr0NpxState = NpxFrame->Cr0NpxState;
  363. for (i = 0; i < SIZE_OF_80387_REGISTERS; i++) {
  364. ContextFrame->FloatSave.RegisterArea[i] = PSaveArea->RegisterArea[i];
  365. }
  366. } else {
  367. //
  368. // The 80387 is being emulated by the R3 emulator.
  369. // ** The only time the Npx state is ever obtained or set is
  370. // ** for userlevel handling. Current Irql must be 0 or 1.
  371. // Go slurp the emulator's R3 data and generate the
  372. // floating point context
  373. //
  374. StateSaved = KiEm87StateToNpxFrame(&ContextFrame->FloatSave);
  375. if (StateSaved) {
  376. ContextFrame->FloatSave.Cr0NpxState = NpxFrame->Cr0NpxState;
  377. } else {
  378. //
  379. // The floatingpoint state can not be determined.
  380. // Remove the floatingpoint flag from the context frame flags.
  381. //
  382. ContextFrame->ContextFlags &= (~CONTEXT_FLOATING_POINT) | CONTEXT_i386;
  383. }
  384. }
  385. }
  386. //
  387. // Fetch Dr register contents if requested.
  388. //
  389. if ((ContextFrame->ContextFlags & CONTEXT_DEBUG_REGISTERS) ==
  390. CONTEXT_DEBUG_REGISTERS) {
  391. if (TrapFrame->Dr7&DR7_ACTIVE) {
  392. ContextFrame->Dr0 = TrapFrame->Dr0;
  393. ContextFrame->Dr1 = TrapFrame->Dr1;
  394. ContextFrame->Dr2 = TrapFrame->Dr2;
  395. ContextFrame->Dr3 = TrapFrame->Dr3;
  396. ContextFrame->Dr6 = TrapFrame->Dr6;
  397. ContextFrame->Dr7 = TrapFrame->Dr7;
  398. } else {
  399. ContextFrame->Dr0 = 0;
  400. ContextFrame->Dr1 = 0;
  401. ContextFrame->Dr2 = 0;
  402. ContextFrame->Dr3 = 0;
  403. ContextFrame->Dr6 = 0;
  404. ContextFrame->Dr7 = 0;
  405. }
  406. }
  407. //
  408. // Lower IRQL if we had to raise it
  409. //
  410. if (OldIrql < APC_LEVEL) {
  411. KeLowerIrql (OldIrql);
  412. }
  413. }
  414. VOID
  415. KeContextToKframes (
  416. IN OUT PKTRAP_FRAME TrapFrame,
  417. IN OUT PKEXCEPTION_FRAME ExceptionFrame,
  418. IN PCONTEXT ContextFrame,
  419. IN ULONG ContextFlags,
  420. IN KPROCESSOR_MODE PreviousMode
  421. )
  422. /*++
  423. Routine Description:
  424. This routine moves the selected contents of the specified context frame into
  425. the specified trap and exception frames according to the specified context
  426. flags.
  427. Arguments:
  428. TrapFrame - Supplies a pointer to a trap frame that receives the volatile
  429. context from the context record.
  430. ExceptionFrame - Supplies a pointer to an exception frame that receives
  431. the nonvolatile context from the context record. This argument is
  432. ignored since there is no exception frame on NT386.
  433. ContextFrame - Supplies a pointer to a context frame that contains the
  434. context that is to be copied into the trap and exception frames.
  435. ContextFlags - Supplies the set of flags that specify which parts of the
  436. context frame are to be copied into the trap and exception frames.
  437. PreviousMode - Supplies the processor mode for which the trap and exception
  438. frames are being built.
  439. Return Value:
  440. None.
  441. --*/
  442. {
  443. PFX_SAVE_AREA NpxFrame;
  444. ULONG i;
  445. ULONG j;
  446. ULONG TagWord;
  447. BOOLEAN StateSaved;
  448. BOOLEAN ModeChanged;
  449. #if DBG
  450. PKPCR Pcr;
  451. #endif
  452. KIRQL OldIrql;
  453. #if DBG
  454. KIRQL OldIrql2;
  455. #endif
  456. UNREFERENCED_PARAMETER( ExceptionFrame );
  457. //
  458. // This routine is called at both PASSIVE_LEVEL by exception dispatch
  459. // and at APC_LEVEL by NtSetContextThread. We raise to APC_LEVEL to
  460. // make the trap frame modifications atomic.
  461. //
  462. OldIrql = KeGetCurrentIrql ();
  463. if (OldIrql < APC_LEVEL) {
  464. KeRaiseIrql (APC_LEVEL, &OldIrql);
  465. }
  466. //
  467. // Set control information if specified.
  468. //
  469. if ((ContextFlags & CONTEXT_CONTROL) == CONTEXT_CONTROL) {
  470. if ((ContextFrame->EFlags & EFLAGS_V86_MASK) !=
  471. (TrapFrame->EFlags & EFLAGS_V86_MASK)) {
  472. ModeChanged = TRUE;
  473. } else {
  474. ModeChanged = FALSE;
  475. }
  476. //
  477. // Set registers eflag, ebp, eip, cs, esp and ss.
  478. // Eflags is set first, so that the auxilliary routines
  479. // can check the v86 bit to determine as well as cs, to
  480. // determine if the frame is kernel or user mode. (v86 mode cs
  481. // can have any value)
  482. //
  483. TrapFrame->EFlags = SANITIZE_FLAGS(ContextFrame->EFlags, PreviousMode);
  484. TrapFrame->Ebp = ContextFrame->Ebp;
  485. TrapFrame->Eip = ContextFrame->Eip;
  486. if (TrapFrame->EFlags & EFLAGS_V86_MASK) {
  487. TrapFrame->SegCs = ContextFrame->SegCs;
  488. } else {
  489. TrapFrame->SegCs = SANITIZE_SEG(ContextFrame->SegCs, PreviousMode);
  490. if (PreviousMode != KernelMode && TrapFrame->SegCs < 8) {
  491. //
  492. // If user mode and the selector value is less than 8, we
  493. // know it is an invalid selector. Set it to flat user
  494. // mode selector. Another reason we need to check for this
  495. // is that any cs value less than 8 causes our exit kernel
  496. // macro to treat its exit trap fram as an edited frame.
  497. //
  498. TrapFrame->SegCs = KGDT_R3_CODE | RPL_MASK;
  499. }
  500. }
  501. KiSegSsToTrapFrame(TrapFrame, ContextFrame->SegSs);
  502. KiEspToTrapFrame(TrapFrame, ContextFrame->Esp);
  503. if (ModeChanged) {
  504. Ki386AdjustEsp0(TrapFrame); // realign esp0 in the tss
  505. }
  506. }
  507. //
  508. // Set segment register contents if specified.
  509. //
  510. if ((ContextFlags & CONTEXT_SEGMENTS) == CONTEXT_SEGMENTS) {
  511. //
  512. // Set segment registers gs, fs, es, ds.
  513. //
  514. //
  515. // There's only one legal value for DS and ES, so simply set it.
  516. // This allows KeContextFromKframes to report the real values in
  517. // the frame. (which are junk most of the time, but sometimes useful
  518. // for debugging.)
  519. // Only 2 legal values for FS, let either one be set.
  520. // Force GS to be 0 to deal with entry via SysCall and exit
  521. // via exception.
  522. //
  523. // For V86 mode, the FS, GS, DS, and ES registers must be properly
  524. // set from the supplied context.
  525. //
  526. if (TrapFrame->EFlags & EFLAGS_V86_MASK) {
  527. TrapFrame->V86Fs = ContextFrame->SegFs;
  528. TrapFrame->V86Es = ContextFrame->SegEs;
  529. TrapFrame->V86Ds = ContextFrame->SegDs;
  530. TrapFrame->V86Gs = ContextFrame->SegGs;
  531. } else if (((TrapFrame->SegCs & MODE_MASK) == KernelMode)) {
  532. //
  533. // set up the standard selectors
  534. //
  535. TrapFrame->SegFs = SANITIZE_SEG(ContextFrame->SegFs, PreviousMode);
  536. TrapFrame->SegEs = KGDT_R3_DATA | RPL_MASK;
  537. TrapFrame->SegDs = KGDT_R3_DATA | RPL_MASK;
  538. TrapFrame->SegGs = 0;
  539. } else {
  540. //
  541. // If user mode, we simply return whatever left in context frame
  542. // and let trap 0d handle it (if later we trap while popping the
  543. // trap frame.) V86 mode also get handled here.
  544. //
  545. TrapFrame->SegFs = ContextFrame->SegFs;
  546. TrapFrame->SegEs = ContextFrame->SegEs;
  547. TrapFrame->SegDs = ContextFrame->SegDs;
  548. if (TrapFrame->SegCs == (KGDT_R3_CODE | RPL_MASK)) {
  549. TrapFrame->SegGs = 0;
  550. } else {
  551. TrapFrame->SegGs = ContextFrame->SegGs;
  552. }
  553. }
  554. }
  555. //
  556. // Set integer registers contents if specified.
  557. //
  558. if ((ContextFlags & CONTEXT_INTEGER) == CONTEXT_INTEGER) {
  559. //
  560. // Set integer registers edi, esi, ebx, edx, ecx, eax.
  561. //
  562. // Can NOT call RtlCopyMemory here because the regs aren't
  563. // contiguous in pusha frame, and we don't want to export
  564. // bits of junk into context record.
  565. //
  566. TrapFrame->Edi = ContextFrame->Edi;
  567. TrapFrame->Esi = ContextFrame->Esi;
  568. TrapFrame->Ebx = ContextFrame->Ebx;
  569. TrapFrame->Ecx = ContextFrame->Ecx;
  570. TrapFrame->Edx = ContextFrame->Edx;
  571. TrapFrame->Eax = ContextFrame->Eax;
  572. }
  573. //
  574. // Set extended register contents if requested, and type of target
  575. // is user. (system frames have no extended state, so ignore request)
  576. //
  577. if (((ContextFlags & CONTEXT_EXTENDED_REGISTERS) == CONTEXT_EXTENDED_REGISTERS) &&
  578. ((TrapFrame->SegCs & MODE_MASK) == UserMode)) {
  579. //
  580. // This is the base TrapFrame, and the NpxFrame is on the base
  581. // of the kernel stack, just above it in memory.
  582. //
  583. NpxFrame = (PFX_SAVE_AREA)(TrapFrame + 1);
  584. if (KeI386NpxPresent) {
  585. KiFlushNPXState (NULL);
  586. RtlCopyMemory( (PVOID)&(NpxFrame->U.FxArea),
  587. (PVOID)&(ContextFrame->ExtendedRegisters[0]),
  588. MAXIMUM_SUPPORTED_EXTENSION
  589. );
  590. //
  591. // Make sure only valid floating state bits are moved to Cr0NpxState.
  592. //
  593. NpxFrame->Cr0NpxState &= ~(CR0_EM | CR0_MP | CR0_TS);
  594. //
  595. // Make sure all reserved bits are clear in MXCSR so we don't get a GP
  596. // fault when doing an FRSTOR on this state.
  597. //
  598. NpxFrame->U.FxArea.MXCsr = SANITIZE_MXCSR(NpxFrame->U.FxArea.MXCsr);
  599. //
  600. // Only let VDMs turn on the EM bit. The kernel can't do
  601. // anything for FLAT apps
  602. //
  603. if (PsGetCurrentProcess()->VdmObjects != NULL) {
  604. NpxFrame->Cr0NpxState |= ContextFrame->FloatSave.Cr0NpxState &
  605. (CR0_EM | CR0_MP);
  606. }
  607. }
  608. }
  609. //
  610. // Set floating register contents if requested, and type of target
  611. // is user. (system frames have no fp state, so ignore request)
  612. //
  613. if (((ContextFlags & CONTEXT_FLOATING_POINT) == CONTEXT_FLOATING_POINT) &&
  614. ((TrapFrame->SegCs & MODE_MASK) == UserMode)) {
  615. //
  616. // This is the base TrapFrame, and the NpxFrame is on the base
  617. // of the kernel stack, just above it in memory.
  618. //
  619. NpxFrame = (PFX_SAVE_AREA)(TrapFrame + 1);
  620. if (KeI386NpxPresent) {
  621. //
  622. // Set coprocessor stack, control and status registers
  623. //
  624. KiFlushNPXState (NULL);
  625. if (KeI386FxsrPresent == TRUE) {
  626. //
  627. // Restore FP state in the fxrstor format
  628. //
  629. NpxFrame->U.FxArea.ControlWord =
  630. (USHORT)ContextFrame->FloatSave.ControlWord;
  631. NpxFrame->U.FxArea.StatusWord =
  632. (USHORT)ContextFrame->FloatSave.StatusWord;
  633. //
  634. // Construct the tag word from fnsave format to fxsave format
  635. //
  636. NpxFrame->U.FxArea.TagWord = 0; // Mark every register invalid
  637. TagWord = ContextFrame->FloatSave.TagWord;
  638. for (i = 0; i < FN_BITS_PER_TAGWORD; i+=2) {
  639. if (((TagWord >> i) & FN_TAG_MASK) != FN_TAG_EMPTY) {
  640. //
  641. // This register is valid
  642. //
  643. NpxFrame->U.FxArea.TagWord |= (FX_TAG_VALID << (i/2));
  644. }
  645. }
  646. NpxFrame->U.FxArea.ErrorOffset =
  647. ContextFrame->FloatSave.ErrorOffset;
  648. NpxFrame->U.FxArea.ErrorSelector =
  649. (ContextFrame->FloatSave.ErrorSelector & 0xFFFF);
  650. NpxFrame->U.FxArea.ErrorOpcode =
  651. (USHORT)((ContextFrame->FloatSave.ErrorSelector >> 16) & 0xFFFF);
  652. NpxFrame->U.FxArea.DataOffset =
  653. ContextFrame->FloatSave.DataOffset;
  654. NpxFrame->U.FxArea.DataSelector =
  655. ContextFrame->FloatSave.DataSelector;
  656. //
  657. // Fxrstor format has each FP register in 128 bits (16 bytes)
  658. // where as fnsave saves each FP register in 80 bits (10 bytes)
  659. //
  660. RtlZeroMemory ((PVOID)&NpxFrame->U.FxArea.RegisterArea[0],
  661. SIZE_OF_FX_REGISTERS
  662. );
  663. for (i = 0; i < NUMBER_OF_FP_REGISTERS; i++) {
  664. for (j = 0; j < BYTES_PER_FP_REGISTER; j++) {
  665. NpxFrame->U.FxArea.RegisterArea[i*BYTES_PER_FX_REGISTER+j] =
  666. ContextFrame->FloatSave.RegisterArea[i*BYTES_PER_FP_REGISTER+j];
  667. }
  668. }
  669. } else {
  670. NpxFrame->U.FnArea.ControlWord =
  671. ContextFrame->FloatSave.ControlWord;
  672. NpxFrame->U.FnArea.StatusWord =
  673. ContextFrame->FloatSave.StatusWord;
  674. NpxFrame->U.FnArea.TagWord =
  675. ContextFrame->FloatSave.TagWord;
  676. NpxFrame->U.FnArea.ErrorOffset =
  677. ContextFrame->FloatSave.ErrorOffset;
  678. NpxFrame->U.FnArea.ErrorSelector =
  679. ContextFrame->FloatSave.ErrorSelector;
  680. NpxFrame->U.FnArea.DataOffset =
  681. ContextFrame->FloatSave.DataOffset;
  682. NpxFrame->U.FnArea.DataSelector =
  683. ContextFrame->FloatSave.DataSelector;
  684. for (i = 0; i < SIZE_OF_80387_REGISTERS; i++) {
  685. NpxFrame->U.FnArea.RegisterArea[i] =
  686. ContextFrame->FloatSave.RegisterArea[i];
  687. }
  688. }
  689. //
  690. // Make sure only valid floating state bits are moved to Cr0NpxState.
  691. //
  692. NpxFrame->Cr0NpxState &= ~(CR0_EM | CR0_MP | CR0_TS);
  693. //
  694. // Only let VDMs turn on the EM bit. The kernel can't do
  695. // anything for FLAT apps
  696. //
  697. if (PsGetCurrentProcess()->VdmObjects != NULL) {
  698. NpxFrame->Cr0NpxState |= ContextFrame->FloatSave.Cr0NpxState &
  699. (CR0_EM | CR0_MP);
  700. }
  701. } else {
  702. if (PsGetCurrentProcess()->VdmObjects != NULL) {
  703. //
  704. // This is a special hack to allow SetContext for VDMs to
  705. // turn on/off it's CR0_EM bit.
  706. //
  707. NpxFrame->Cr0NpxState &= ~(CR0_MP | CR0_TS | CR0_EM | CR0_PE);
  708. NpxFrame->Cr0NpxState |=
  709. VdmUserCr0MapIn[ContextFrame->FloatSave.Cr0NpxState & (CR0_EM | CR0_MP)];
  710. } else {
  711. //
  712. // The 80387 is being emulated by the R3 emulator.
  713. // ** The only time the Npx state is ever obtained or set is
  714. // ** for userlevel handling. Current Irql must be 0 or 1.
  715. // And the context being set must be for the current thread.
  716. // Go smash the floatingpoint context into the R3 emulator's
  717. // data area.
  718. //
  719. #if DBG
  720. OldIrql2 = KeRaiseIrqlToSynchLevel();
  721. Pcr = KeGetPcr();
  722. ASSERT (Pcr->Prcb->CurrentThread->Teb == Pcr->NtTib.Self);
  723. KeLowerIrql (OldIrql2);
  724. #endif
  725. StateSaved = KiNpxFrameToEm87State(&ContextFrame->FloatSave);
  726. if (StateSaved) {
  727. //
  728. // Make sure only valid floating state bits are moved to
  729. // Cr0NpxState. Since we are emulating, don't allow
  730. // resetting CR0_EM.
  731. //
  732. NpxFrame->Cr0NpxState &= ~(CR0_MP | CR0_TS);
  733. NpxFrame->Cr0NpxState |=
  734. ContextFrame->FloatSave.Cr0NpxState & CR0_MP;
  735. }
  736. }
  737. }
  738. }
  739. //
  740. // Set debug register state if specified.
  741. //
  742. if ((ContextFlags & CONTEXT_DEBUG_REGISTERS) == CONTEXT_DEBUG_REGISTERS) {
  743. TrapFrame->Dr0 = SANITIZE_DRADDR(ContextFrame->Dr0, PreviousMode);
  744. TrapFrame->Dr1 = SANITIZE_DRADDR(ContextFrame->Dr1, PreviousMode);
  745. TrapFrame->Dr2 = SANITIZE_DRADDR(ContextFrame->Dr2, PreviousMode);
  746. TrapFrame->Dr3 = SANITIZE_DRADDR(ContextFrame->Dr3, PreviousMode);
  747. TrapFrame->Dr6 = SANITIZE_DR6(ContextFrame->Dr6, PreviousMode);
  748. TrapFrame->Dr7 = SANITIZE_DR7(ContextFrame->Dr7, PreviousMode);
  749. if (PreviousMode != KernelMode) {
  750. KeGetCurrentThread()->Header.DebugActive = (BOOLEAN) ((TrapFrame->Dr7&DR7_ACTIVE) != 0);
  751. }
  752. }
  753. //
  754. // If thread is supposed to have IOPL, then force it on in eflags
  755. //
  756. if (KeGetCurrentThread()->Iopl) {
  757. TrapFrame->EFlags |= (EFLAGS_IOPL_MASK & -1); // IOPL = 3
  758. }
  759. //
  760. // Lower IRQL if we had to raise it
  761. //
  762. if (OldIrql < APC_LEVEL) {
  763. KeLowerIrql (OldIrql);
  764. }
  765. return;
  766. }
  767. VOID
  768. KiDispatchException (
  769. IN PEXCEPTION_RECORD ExceptionRecord,
  770. IN PKEXCEPTION_FRAME ExceptionFrame,
  771. IN PKTRAP_FRAME TrapFrame,
  772. IN KPROCESSOR_MODE PreviousMode,
  773. IN BOOLEAN FirstChance
  774. )
  775. /*++
  776. Routine Description:
  777. This function is called to dispatch an exception to the proper mode and
  778. to cause the exception dispatcher to be called. If the previous mode is
  779. kernel, then the exception dispatcher is called directly to process the
  780. exception. Otherwise the exception record, exception frame, and trap
  781. frame contents are copied to the user mode stack. The contents of the
  782. exception frame and trap are then modified such that when control is
  783. returned, execution will commense in user mode in a routine which will
  784. call the exception dispatcher.
  785. Arguments:
  786. ExceptionRecord - Supplies a pointer to an exception record.
  787. ExceptionFrame - Supplies a pointer to an exception frame. For NT386,
  788. this should be NULL.
  789. TrapFrame - Supplies a pointer to a trap frame.
  790. PreviousMode - Supplies the previous processor mode.
  791. FirstChance - Supplies a boolean value that specifies whether this is
  792. the first (TRUE) or second (FALSE) chance for the exception.
  793. Return Value:
  794. None.
  795. --*/
  796. {
  797. CONTEXT ContextFrame;
  798. EXCEPTION_RECORD ExceptionRecord1, ExceptionRecord2;
  799. LONG Length;
  800. ULONG UserStack1;
  801. ULONG UserStack2;
  802. //
  803. // Move machine state from trap and exception frames to a context frame,
  804. // and increment the number of exceptions dispatched.
  805. //
  806. KeGetCurrentPrcb()->KeExceptionDispatchCount += 1;
  807. ContextFrame.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
  808. if ((PreviousMode == UserMode) || KdDebuggerEnabled) {
  809. //
  810. // For usermode exceptions always try to dispatch the floating
  811. // point state. This allows expection handlers & debuggers to
  812. // examine/edit the npx context if required. Plus it allows
  813. // exception handlers to use fp instructions without detroying
  814. // the npx state at the time of the exception.
  815. //
  816. // Note: If there's no 80387, ContextTo/FromKFrames will use the
  817. // emulator's current state. If the emulator can not give the
  818. // current state, then the context_floating_point bit will be
  819. // turned off by ContextFromKFrames.
  820. //
  821. ContextFrame.ContextFlags |= CONTEXT_FLOATING_POINT;
  822. if (KeI386XMMIPresent) {
  823. ContextFrame.ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
  824. }
  825. }
  826. KeContextFromKframes(TrapFrame, ExceptionFrame, &ContextFrame);
  827. //
  828. // if it is BREAK_POINT exception, we subtract 1 from EIP and report
  829. // the updated EIP to user. This is because Cruiser requires EIP
  830. // points to the int 3 instruction (not the instruction following int 3).
  831. // In this case, BreakPoint exception is fatal. Otherwise we will step
  832. // on the int 3 over and over again, if user does not handle it
  833. //
  834. // if the BREAK_POINT occured in V86 mode, the debugger running in the
  835. // VDM will expect CS:EIP to point after the exception (the way the
  836. // processor left it. this is also true for protected mode dos
  837. // app debuggers. We will need a way to detect this.
  838. //
  839. //
  840. // if ((ExceptionRecord->ExceptionCode == STATUS_BREAKPOINT) &&
  841. // !(ContextFrame.EFlags & EFLAGS_V86_MASK)) {
  842. switch (ExceptionRecord->ExceptionCode) {
  843. case STATUS_BREAKPOINT:
  844. ContextFrame.Eip--;
  845. break;
  846. }
  847. //
  848. // Select the method of handling the exception based on the previous mode.
  849. //
  850. ASSERT ((
  851. !((PreviousMode == KernelMode) &&
  852. (ContextFrame.EFlags & EFLAGS_V86_MASK))
  853. ));
  854. if (PreviousMode == KernelMode) {
  855. //
  856. // Previous mode was kernel.
  857. //
  858. // If the kernel debugger is active, then give the kernel debugger the
  859. // first chance to handle the exception. If the kernel debugger handles
  860. // the exception, then continue execution. Else attempt to dispatch the
  861. // exception to a frame based handler. If a frame based handler handles
  862. // the exception, then continue execution.
  863. //
  864. // If a frame based handler does not handle the exception,
  865. // give the kernel debugger a second chance, if it's present.
  866. //
  867. // If the exception is still unhandled, call KeBugCheck().
  868. //
  869. if (FirstChance == TRUE) {
  870. if ((KiDebugRoutine != NULL) &&
  871. (((KiDebugRoutine) (TrapFrame,
  872. ExceptionFrame,
  873. ExceptionRecord,
  874. &ContextFrame,
  875. PreviousMode,
  876. FALSE)) != FALSE)) {
  877. goto Handled1;
  878. }
  879. // Kernel debugger didn't handle exception.
  880. if (RtlDispatchException(ExceptionRecord, &ContextFrame) == TRUE) {
  881. goto Handled1;
  882. }
  883. }
  884. //
  885. // This is the second chance to handle the exception.
  886. //
  887. if ((KiDebugRoutine != NULL) &&
  888. (((KiDebugRoutine) (TrapFrame,
  889. ExceptionFrame,
  890. ExceptionRecord,
  891. &ContextFrame,
  892. PreviousMode,
  893. TRUE)) != FALSE)) {
  894. goto Handled1;
  895. }
  896. KeBugCheckEx(
  897. KERNEL_MODE_EXCEPTION_NOT_HANDLED,
  898. ExceptionRecord->ExceptionCode,
  899. (ULONG)ExceptionRecord->ExceptionAddress,
  900. (ULONG)TrapFrame,
  901. 0);
  902. } else {
  903. //
  904. // Previous mode was user.
  905. //
  906. // If this is the first chance and the current process has a debugger
  907. // port, then send a message to the debugger port and wait for a reply.
  908. // If the debugger handles the exception, then continue execution. Else
  909. // transfer the exception information to the user stack, transition to
  910. // user mode, and attempt to dispatch the exception to a frame based
  911. // handler. If a frame based handler handles the exception, then continue
  912. // execution with the continue system service. Else execute the
  913. // NtRaiseException system service with FirstChance == FALSE, which
  914. // will call this routine a second time to process the exception.
  915. //
  916. // If this is the second chance and the current process has a debugger
  917. // port, then send a message to the debugger port and wait for a reply.
  918. // If the debugger handles the exception, then continue execution. Else
  919. // if the current process has a subsystem port, then send a message to
  920. // the subsystem port and wait for a reply. If the subsystem handles the
  921. // exception, then continue execution. Else terminate the thread.
  922. //
  923. if (FirstChance == TRUE) {
  924. //
  925. // This is the first chance to handle the exception.
  926. //
  927. if ((KiDebugRoutine != NULL) &&
  928. ((PsGetCurrentProcess()->DebugPort == NULL &&
  929. !KdIgnoreUmExceptions) ||
  930. (KdIsThisAKdTrap(ExceptionRecord, &ContextFrame, UserMode)))) {
  931. //
  932. // Now dispatch the fault to the kernel debugger.
  933. //
  934. if ((((KiDebugRoutine) (TrapFrame,
  935. ExceptionFrame,
  936. ExceptionRecord,
  937. &ContextFrame,
  938. PreviousMode,
  939. FALSE)) != FALSE)) {
  940. goto Handled1;
  941. }
  942. }
  943. if (DbgkForwardException(ExceptionRecord, TRUE, FALSE)) {
  944. goto Handled2;
  945. }
  946. //
  947. // Transfer exception information to the user stack, transition
  948. // to user mode, and attempt to dispatch the exception to a frame
  949. // based handler.
  950. ExceptionRecord1.ExceptionCode = 0; // satisfy no_opt compilation
  951. repeat:
  952. try {
  953. //
  954. // If the SS segment is not 32 bit flat, there is no point
  955. // to dispatch exception to frame based exception handler.
  956. //
  957. if (TrapFrame->HardwareSegSs != (KGDT_R3_DATA | RPL_MASK) ||
  958. TrapFrame->EFlags & EFLAGS_V86_MASK ) {
  959. ExceptionRecord2.ExceptionCode = STATUS_ACCESS_VIOLATION;
  960. ExceptionRecord2.ExceptionFlags = 0;
  961. ExceptionRecord2.NumberParameters = 0;
  962. ExRaiseException(&ExceptionRecord2);
  963. }
  964. //
  965. // Compute length of context record and new aligned user stack
  966. // pointer.
  967. //
  968. Length = (sizeof(CONTEXT) + CONTEXT_ROUND) & ~CONTEXT_ROUND;
  969. UserStack1 = (ContextFrame.Esp & ~CONTEXT_ROUND) - Length;
  970. //
  971. // Probe user stack area for writability and then transfer the
  972. // context record to the user stack.
  973. //
  974. ProbeForWrite((PCHAR)UserStack1, Length, CONTEXT_ALIGN);
  975. RtlCopyMemory((PULONG)UserStack1, &ContextFrame, sizeof(CONTEXT));
  976. //
  977. // Compute length of exception record and new aligned stack
  978. // address.
  979. //
  980. Length = (sizeof(EXCEPTION_RECORD) - (EXCEPTION_MAXIMUM_PARAMETERS -
  981. ExceptionRecord->NumberParameters) * sizeof(ULONG) +3) &
  982. (~3);
  983. UserStack2 = UserStack1 - Length;
  984. //
  985. // Probe user stack area for writeability and then transfer the
  986. // context record to the user stack area.
  987. // N.B. The probing length is Length+8 because there are two
  988. // arguments need to be pushed to user stack later.
  989. //
  990. ProbeForWrite((PCHAR)(UserStack2 - 8), Length + 8, sizeof(ULONG));
  991. RtlCopyMemory((PULONG)UserStack2, ExceptionRecord, Length);
  992. //
  993. // Push address of exception record, context record to the
  994. // user stack. They are the two parameters required by
  995. // _KiUserExceptionDispatch.
  996. //
  997. *(PULONG)(UserStack2 - sizeof(ULONG)) = UserStack1;
  998. *(PULONG)(UserStack2 - 2*sizeof(ULONG)) = UserStack2;
  999. //
  1000. // Set new stack pointer to the trap frame.
  1001. //
  1002. KiSegSsToTrapFrame(TrapFrame, KGDT_R3_DATA);
  1003. KiEspToTrapFrame(TrapFrame, (UserStack2 - sizeof(ULONG)*2));
  1004. //
  1005. // Force correct R3 selectors into TrapFrame.
  1006. //
  1007. TrapFrame->SegCs = SANITIZE_SEG(KGDT_R3_CODE, PreviousMode);
  1008. TrapFrame->SegDs = SANITIZE_SEG(KGDT_R3_DATA, PreviousMode);
  1009. TrapFrame->SegEs = SANITIZE_SEG(KGDT_R3_DATA, PreviousMode);
  1010. TrapFrame->SegFs = SANITIZE_SEG(KGDT_R3_TEB, PreviousMode);
  1011. TrapFrame->SegGs = 0;
  1012. //
  1013. // Set the address of the exception routine that will call the
  1014. // exception dispatcher and then return to the trap handler.
  1015. // The trap handler will restore the exception and trap frame
  1016. // context and continue execution in the routine that will
  1017. // call the exception dispatcher.
  1018. //
  1019. TrapFrame->Eip = (ULONG)KeUserExceptionDispatcher;
  1020. return;
  1021. } except (KiCopyInformation(&ExceptionRecord1,
  1022. (GetExceptionInformation())->ExceptionRecord)) {
  1023. //
  1024. // If the exception is a stack overflow, then attempt
  1025. // to raise the stack overflow exception. Otherwise,
  1026. // the user's stack is not accessible, or is misaligned,
  1027. // and second chance processing is performed.
  1028. //
  1029. if (ExceptionRecord1.ExceptionCode == STATUS_STACK_OVERFLOW) {
  1030. ExceptionRecord1.ExceptionAddress = ExceptionRecord->ExceptionAddress;
  1031. RtlCopyMemory((PVOID)ExceptionRecord,
  1032. &ExceptionRecord1, sizeof(EXCEPTION_RECORD));
  1033. goto repeat;
  1034. }
  1035. }
  1036. }
  1037. //
  1038. // This is the second chance to handle the exception.
  1039. //
  1040. if (DbgkForwardException(ExceptionRecord, TRUE, TRUE)) {
  1041. goto Handled2;
  1042. } else if (DbgkForwardException(ExceptionRecord, FALSE, TRUE)) {
  1043. goto Handled2;
  1044. } else {
  1045. ZwTerminateThread(NtCurrentThread(), ExceptionRecord->ExceptionCode);
  1046. KeBugCheckEx(
  1047. KERNEL_MODE_EXCEPTION_NOT_HANDLED,
  1048. ExceptionRecord->ExceptionCode,
  1049. (ULONG)ExceptionRecord->ExceptionAddress,
  1050. (ULONG)TrapFrame,
  1051. 0);
  1052. }
  1053. }
  1054. //
  1055. // Move machine state from context frame to trap and exception frames and
  1056. // then return to continue execution with the restored state.
  1057. //
  1058. Handled1:
  1059. KeContextToKframes(TrapFrame, ExceptionFrame, &ContextFrame,
  1060. ContextFrame.ContextFlags, PreviousMode);
  1061. //
  1062. // Exception was handled by the debugger or the associated subsystem
  1063. // and state was modified, if necessary, using the get state and set
  1064. // state capabilities. Therefore the context frame does not need to
  1065. // be transfered to the trap and exception frames.
  1066. //
  1067. Handled2:
  1068. return;
  1069. }
  1070. ULONG
  1071. KiCopyInformation (
  1072. IN OUT PEXCEPTION_RECORD ExceptionRecord1,
  1073. IN PEXCEPTION_RECORD ExceptionRecord2
  1074. )
  1075. /*++
  1076. Routine Description:
  1077. This function is called from an exception filter to copy the exception
  1078. information from one exception record to another when an exception occurs.
  1079. Arguments:
  1080. ExceptionRecord1 - Supplies a pointer to the destination exception record.
  1081. ExceptionRecord2 - Supplies a pointer to the source exception record.
  1082. Return Value:
  1083. A value of EXCEPTION_EXECUTE_HANDLER is returned as the function value.
  1084. --*/
  1085. {
  1086. //
  1087. // Copy one exception record to another and return value that causes
  1088. // an exception handler to be executed.
  1089. //
  1090. RtlCopyMemory((PVOID)ExceptionRecord1,
  1091. (PVOID)ExceptionRecord2,
  1092. sizeof(EXCEPTION_RECORD));
  1093. return EXCEPTION_EXECUTE_HANDLER;
  1094. }
  1095. NTSTATUS
  1096. KeRaiseUserException(
  1097. IN NTSTATUS ExceptionCode
  1098. )
  1099. /*++
  1100. Routine Description:
  1101. This function causes an exception to be raised in the calling thread's user-mode
  1102. context. It does this by editing the trap frame the kernel was entered with to
  1103. point to trampoline code that raises the requested exception.
  1104. Arguments:
  1105. ExceptionCode - Supplies the status value to be used as the exception
  1106. code for the exception that is to be raised.
  1107. Return Value:
  1108. The status value that should be returned by the caller.
  1109. --*/
  1110. {
  1111. PKTHREAD Thread;
  1112. PKTRAP_FRAME TrapFrame;
  1113. PTEB Teb;
  1114. ULONG PreviousEsp;
  1115. Thread = KeGetCurrentThread();
  1116. TrapFrame = Thread->TrapFrame;
  1117. if (TrapFrame == NULL || ((TrapFrame->SegCs & MODE_MASK) != UserMode)) {
  1118. return ExceptionCode;
  1119. }
  1120. Teb = (PTEB)Thread->Teb;
  1121. //
  1122. // In order to create the correct call stack, we push the old return
  1123. // address onto the stack. The status code to be raised is passed
  1124. // in the TEB.
  1125. //
  1126. try {
  1127. Teb->ExceptionCode = ExceptionCode;
  1128. PreviousEsp = KiEspFromTrapFrame (TrapFrame) - sizeof (ULONG);
  1129. ProbeForWriteSmallStructure ((PLONG)PreviousEsp, sizeof (LONG), sizeof (UCHAR));
  1130. *(PLONG)PreviousEsp = TrapFrame->Eip;
  1131. } except(EXCEPTION_EXECUTE_HANDLER) {
  1132. return(ExceptionCode);
  1133. }
  1134. KiEspToTrapFrame (TrapFrame, PreviousEsp);
  1135. TrapFrame->Eip = (ULONG)KeRaiseUserExceptionDispatcher;
  1136. return ExceptionCode;
  1137. }