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.

821 lines
24 KiB

  1. /*++
  2. Copyright (c) 2000 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) 5-May-2000
  10. Environment:
  11. Kernel mode only.
  12. Revision History:
  13. --*/
  14. #include "ki.h"
  15. VOID
  16. KeContextFromKframes (
  17. IN PKTRAP_FRAME TrapFrame,
  18. IN PKEXCEPTION_FRAME ExceptionFrame,
  19. IN OUT PCONTEXT ContextRecord
  20. )
  21. /*++
  22. Routine Description:
  23. This routine moves the selected contents of the specified trap and
  24. exception frames into the specified context frame according to the
  25. specified context flags.
  26. Arguments:
  27. TrapFrame - Supplies a pointer to a trap frame from which volatile
  28. context should be copied into the context record.
  29. ExceptionFrame - Supplies a pointer to an exception frame from which
  30. context should be copied into the context record.
  31. ContextRecord - Supplies a pointer to the context frame that receives
  32. the context copied from the trap and exception frames.
  33. Return Value:
  34. None.
  35. --*/
  36. {
  37. ULONG ContextFlags;
  38. PLEGACY_SAVE_AREA NpxFrame;
  39. //
  40. // Set control information if specified.
  41. //
  42. ContextFlags = ContextRecord->ContextFlags;
  43. if ((ContextFlags & CONTEXT_CONTROL) == CONTEXT_CONTROL) {
  44. //
  45. // Set registers RIP, CS, RSP, SS, and EFlags.
  46. //
  47. ContextRecord->Rip = TrapFrame->Rip;
  48. ContextRecord->SegCs = TrapFrame->SegCs;
  49. ContextRecord->SegSs = TrapFrame->SegSs;
  50. ContextRecord->Rsp = TrapFrame->Rsp;
  51. ContextRecord->EFlags = TrapFrame->EFlags;
  52. }
  53. //
  54. // Set segment register contents if specified.
  55. //
  56. if ((ContextFlags & CONTEXT_SEGMENTS) == CONTEXT_SEGMENTS) {
  57. //
  58. // Set segment registers GS, FS, ES, DS.
  59. //
  60. ContextRecord->SegDs = KGDT64_R3_DATA | RPL_MASK;
  61. ContextRecord->SegEs = KGDT64_R3_DATA | RPL_MASK;
  62. ContextRecord->SegFs = KGDT64_R3_CMTEB | RPL_MASK;
  63. ContextRecord->SegGs = KGDT64_R3_DATA | RPL_MASK;
  64. }
  65. //
  66. // Set integer register contents if specified.
  67. //
  68. if ((ContextFlags & CONTEXT_INTEGER) == CONTEXT_INTEGER) {
  69. //
  70. // Set integer registers RAX, RCX, RDX, RSI, RDI, R8, R9, R10, RBX,
  71. // RBP, R11, R12, R13, R14, and R15.
  72. //
  73. ContextRecord->Rax = TrapFrame->Rax;
  74. ContextRecord->Rcx = TrapFrame->Rcx;
  75. ContextRecord->Rdx = TrapFrame->Rdx;
  76. ContextRecord->R8 = TrapFrame->R8;
  77. ContextRecord->R9 = TrapFrame->R9;
  78. ContextRecord->R10 = TrapFrame->R10;
  79. ContextRecord->R11 = TrapFrame->R11;
  80. ContextRecord->Rbp = TrapFrame->Rbp;
  81. ContextRecord->Rbx = ExceptionFrame->Rbx;
  82. ContextRecord->Rdi = ExceptionFrame->Rdi;
  83. ContextRecord->Rsi = ExceptionFrame->Rsi;
  84. ContextRecord->R12 = ExceptionFrame->R12;
  85. ContextRecord->R13 = ExceptionFrame->R13;
  86. ContextRecord->R14 = ExceptionFrame->R14;
  87. ContextRecord->R15 = ExceptionFrame->R15;
  88. }
  89. //
  90. // Set floating point context if specified.
  91. //
  92. //
  93. if ((ContextFlags & CONTEXT_FLOATING_POINT) == CONTEXT_FLOATING_POINT) {
  94. //
  95. // Set XMM registers Xmm0-Xmm15 and the XMM CSR contents.
  96. //
  97. RtlCopyMemory(&ContextRecord->Xmm0,
  98. &TrapFrame->Xmm0,
  99. sizeof(M128) * 6);
  100. RtlCopyMemory(&ContextRecord->Xmm6,
  101. &ExceptionFrame->Xmm6,
  102. sizeof(M128) * 10);
  103. ContextRecord->MxCsr = TrapFrame->MxCsr;
  104. //
  105. // If the specified mode is user, then set the legacy floating
  106. // point state.
  107. //
  108. if ((TrapFrame->SegCs & MODE_MASK) == UserMode) {
  109. //
  110. // Set the floating registers MM0/ST0 - MM7/ST7 and control state.
  111. //
  112. NpxFrame = (PLEGACY_SAVE_AREA)(TrapFrame + 1);
  113. RtlCopyMemory(&ContextRecord->FltSave,
  114. NpxFrame,
  115. sizeof(LEGACY_SAVE_AREA));
  116. }
  117. }
  118. //
  119. //
  120. // Set debug register contents if requested.
  121. //
  122. if ((ContextFlags & CONTEXT_DEBUG_REGISTERS) == CONTEXT_DEBUG_REGISTERS) {
  123. //
  124. // Set the debug registers DR0, DR1, DR2, DR3, DR6, and DR7.
  125. //
  126. ContextRecord->Dr0 = TrapFrame->Dr0;
  127. ContextRecord->Dr1 = TrapFrame->Dr1;
  128. ContextRecord->Dr2 = TrapFrame->Dr2;
  129. ContextRecord->Dr3 = TrapFrame->Dr3;
  130. ContextRecord->Dr6 = TrapFrame->Dr6;
  131. ContextRecord->Dr7 = TrapFrame->Dr7;
  132. }
  133. return;
  134. }
  135. VOID
  136. KeContextToKframes (
  137. IN OUT PKTRAP_FRAME TrapFrame,
  138. IN OUT PKEXCEPTION_FRAME ExceptionFrame,
  139. IN PCONTEXT ContextRecord,
  140. IN ULONG ContextFlags,
  141. IN KPROCESSOR_MODE PreviousMode
  142. )
  143. /*++
  144. Routine Description:
  145. This routine moves the selected contents of the specified context frame
  146. into the specified trap and exception frames according to the specified
  147. context flags.
  148. Arguments:
  149. TrapFrame - Supplies a pointer to a trap frame that receives the volatile
  150. context from the context record.
  151. ExceptionFrame - Supplies a pointer to an exception frame that receives
  152. the nonvolatile context from the context record.
  153. ContextRecord - Supplies a pointer to a context frame that contains the
  154. context that is to be copied into the trap and exception frames.
  155. ContextFlags - Supplies the set of flags that specify which parts of the
  156. context frame are to be copied into the trap and exception frames.
  157. PreviousMode - Supplies the processor mode for which the exception and
  158. trap frames are being built.
  159. Return Value:
  160. None.
  161. --*/
  162. {
  163. PLEGACY_SAVE_AREA NpxFrame;
  164. //
  165. // Set control information if specified.
  166. //
  167. if ((ContextFlags & CONTEXT_CONTROL) == CONTEXT_CONTROL) {
  168. //
  169. // Set registers RIP, RSP, and EFlags.
  170. //
  171. TrapFrame->EFlags = SANITIZE_EFLAGS(ContextRecord->EFlags, PreviousMode);
  172. TrapFrame->Rip = ContextRecord->Rip;
  173. TrapFrame->Rsp = ContextRecord->Rsp;
  174. }
  175. //
  176. // The segment registers DS, ES, FS, and GS are never restored from saved
  177. // data. However, SS and CS are restored from the trap frame. Make sure
  178. // that these segment registers have the proper values.
  179. //
  180. if (PreviousMode == UserMode) {
  181. TrapFrame->SegSs = KGDT64_R3_DATA | RPL_MASK;
  182. if (ContextRecord->SegCs != (KGDT64_R3_CODE | RPL_MASK)) {
  183. TrapFrame->SegCs = KGDT64_R3_CMCODE | RPL_MASK;
  184. } else {
  185. TrapFrame->SegCs = KGDT64_R3_CODE | RPL_MASK;
  186. }
  187. } else {
  188. TrapFrame->SegCs = KGDT64_R0_CODE;
  189. TrapFrame->SegSs = KGDT64_NULL;
  190. }
  191. //
  192. // Set integer registers contents if specified.
  193. //
  194. if ((ContextFlags & CONTEXT_INTEGER) == CONTEXT_INTEGER) {
  195. //
  196. // Set integer registers RAX, RCX, RDX, RSI, RDI, R8, R9, R10, RBX,
  197. // RBP, R11, R12, R13, R14, and R15.
  198. //
  199. TrapFrame->Rax = ContextRecord->Rax;
  200. TrapFrame->Rcx = ContextRecord->Rcx;
  201. TrapFrame->Rdx = ContextRecord->Rdx;
  202. TrapFrame->R8 = ContextRecord->R8;
  203. TrapFrame->R9 = ContextRecord->R9;
  204. TrapFrame->R10 = ContextRecord->R10;
  205. TrapFrame->R11 = ContextRecord->R11;
  206. TrapFrame->Rbp = ContextRecord->Rbp;
  207. ExceptionFrame->Rbx = ContextRecord->Rbx;
  208. ExceptionFrame->Rsi = ContextRecord->Rsi;
  209. ExceptionFrame->Rdi = ContextRecord->Rdi;
  210. ExceptionFrame->R12 = ContextRecord->R12;
  211. ExceptionFrame->R13 = ContextRecord->R13;
  212. ExceptionFrame->R14 = ContextRecord->R14;
  213. ExceptionFrame->R15 = ContextRecord->R15;
  214. }
  215. //
  216. // Set floating register contents if requested.
  217. //
  218. if ((ContextFlags & CONTEXT_FLOATING_POINT) == CONTEXT_FLOATING_POINT) {
  219. //
  220. // Set XMM registers Xmm0-Xmm15 and the XMM CSR contents.
  221. //
  222. RtlCopyMemory(&TrapFrame->Xmm0,
  223. &ContextRecord->Xmm0,
  224. sizeof(M128) * 6);
  225. RtlCopyMemory(&ExceptionFrame->Xmm6,
  226. &ContextRecord->Xmm6,
  227. sizeof(M128) * 10);
  228. //
  229. // Clear all reserved bits in MXCSR.
  230. //
  231. TrapFrame->MxCsr = SANITIZE_MXCSR(ContextRecord->MxCsr);
  232. //
  233. // If the specified mode is user, then also set the legacy floating
  234. // point state.
  235. //
  236. if ((TrapFrame->SegCs & MODE_MASK) == UserMode) {
  237. //
  238. // Set the floating state MM0/ST0 - MM7/ST7 and the control state.
  239. //
  240. NpxFrame = (PLEGACY_SAVE_AREA)(TrapFrame + 1);
  241. RtlCopyMemory(NpxFrame,
  242. &ContextRecord->FltSave,
  243. sizeof(LEGACY_SAVE_AREA));
  244. NpxFrame->ControlWord = SANITIZE_FCW(NpxFrame->ControlWord);
  245. }
  246. }
  247. //
  248. // Set debug register state if specified.
  249. //
  250. if ((ContextFlags & CONTEXT_DEBUG_REGISTERS) == CONTEXT_DEBUG_REGISTERS) {
  251. //
  252. // Set the debug registers DR0, DR1, DR2, DR3, DR6, and DR7.
  253. //
  254. TrapFrame->Dr0 = SANITIZE_DRADDR(ContextRecord->Dr0, PreviousMode);
  255. TrapFrame->Dr1 = SANITIZE_DRADDR(ContextRecord->Dr1, PreviousMode);
  256. TrapFrame->Dr2 = SANITIZE_DRADDR(ContextRecord->Dr2, PreviousMode);
  257. TrapFrame->Dr3 = SANITIZE_DRADDR(ContextRecord->Dr3, PreviousMode);
  258. TrapFrame->Dr6 = 0;
  259. TrapFrame->Dr7 = SANITIZE_DR7(ContextRecord->Dr7, PreviousMode);
  260. }
  261. return;
  262. }
  263. VOID
  264. KiDispatchException (
  265. IN PEXCEPTION_RECORD ExceptionRecord,
  266. IN PKEXCEPTION_FRAME ExceptionFrame,
  267. IN PKTRAP_FRAME TrapFrame,
  268. IN KPROCESSOR_MODE PreviousMode,
  269. IN BOOLEAN FirstChance
  270. )
  271. /*++
  272. Routine Description:
  273. This function is called to dispatch an exception to the proper mode and
  274. to cause the exception dispatcher to be called. If the previous mode is
  275. kernel, then the exception dispatcher is called directly to process the
  276. exception. Otherwise the exception record, exception frame, and trap
  277. frame contents are copied to the user mode stack. The contents of the
  278. exception frame and trap are then modified such that when control is
  279. returned, execution will commense in user mode in a routine which will
  280. call the exception dispatcher.
  281. Arguments:
  282. ExceptionRecord - Supplies a pointer to an exception record.
  283. ExceptionFrame - Supplies a pointer to an exception frame. For NT386,
  284. this should be NULL.
  285. TrapFrame - Supplies a pointer to a trap frame.
  286. PreviousMode - Supplies the previous processor mode.
  287. FirstChance - Supplies a boolean value that specifies whether this is
  288. the first (TRUE) or second (FALSE) chance for the exception.
  289. Return Value:
  290. None.
  291. --*/
  292. {
  293. CONTEXT ContextRecord;
  294. EXCEPTION_RECORD ExceptionRecord1;
  295. BOOLEAN ExceptionWasForwarded = FALSE;
  296. PMACHINE_FRAME MachineFrame;
  297. PKPROCESS Process;
  298. PKTHREAD Thread;
  299. ULONG64 UserStack1;
  300. ULONG64 UserStack2;
  301. //
  302. // If the exception is a data misalignment, the previous mode was user,
  303. // this is the first chance for handling the exception, and the current
  304. // thread has enabled automatic alignment fixup, then attempt to emulate
  305. // the unaligned reference.
  306. //
  307. if (ExceptionRecord->ExceptionCode == STATUS_DATATYPE_MISALIGNMENT) {
  308. if (KiHandleAlignmentFault(ExceptionRecord,
  309. ExceptionFrame,
  310. TrapFrame,
  311. PreviousMode,
  312. FirstChance,
  313. &ExceptionWasForwarded) != FALSE ) {
  314. goto Handled2;
  315. }
  316. }
  317. //
  318. // Move machine state from trap and exception frames to a context frame
  319. // and increment the number of exceptions dispatched.
  320. //
  321. KeGetCurrentPrcb()->KeExceptionDispatchCount += 1;
  322. ContextRecord.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
  323. KeContextFromKframes(TrapFrame, ExceptionFrame, &ContextRecord);
  324. //
  325. // If the exception is a break point, then convert the break point to a
  326. // fault.
  327. //
  328. if (ExceptionRecord->ExceptionCode == STATUS_BREAKPOINT) {
  329. ContextRecord.Rip -= 1;
  330. }
  331. //
  332. // Select the method of handling the exception based on the previous mode.
  333. //
  334. if (PreviousMode == KernelMode) {
  335. //
  336. // Previous mode was kernel.
  337. //
  338. // If the kernel debugger is active, then give the kernel debugger
  339. // the first chance to handle the exception. If the kernel debugger
  340. // handles the exception, then continue execution. Otherwise, attempt
  341. // to dispatch the exception to a frame based handler. If a frame
  342. // based handler handles the exception, then continue execution.
  343. //
  344. // If a frame based handler does not handle the exception, give the
  345. // kernel debugger a second chance, if it's present.
  346. //
  347. // If the exception is still unhandled call bugcheck.
  348. //
  349. if (FirstChance != FALSE) {
  350. if ((KiDebugRoutine != NULL) &&
  351. (((KiDebugRoutine)(TrapFrame,
  352. ExceptionFrame,
  353. ExceptionRecord,
  354. &ContextRecord,
  355. PreviousMode,
  356. FALSE)) != FALSE)) {
  357. goto Handled1;
  358. }
  359. //
  360. // Kernel debugger didn't handle exception.
  361. //
  362. // ******fix
  363. //
  364. // If interrupts are disabled, then bugcheck.
  365. //
  366. // ******fix
  367. if (RtlDispatchException(ExceptionRecord, &ContextRecord) != FALSE) {
  368. goto Handled1;
  369. }
  370. }
  371. //
  372. // This is the second chance to handle the exception.
  373. //
  374. if ((KiDebugRoutine != NULL) &&
  375. (((KiDebugRoutine)(TrapFrame,
  376. ExceptionFrame,
  377. ExceptionRecord,
  378. &ContextRecord,
  379. PreviousMode,
  380. TRUE)) != FALSE)) {
  381. goto Handled1;
  382. }
  383. KeBugCheckEx(KMODE_EXCEPTION_NOT_HANDLED,
  384. ExceptionRecord->ExceptionCode,
  385. (ULONG64)ExceptionRecord->ExceptionAddress,
  386. ExceptionRecord->ExceptionInformation[0],
  387. ExceptionRecord->ExceptionInformation[1]);
  388. } else {
  389. //
  390. // Previous mode was user.
  391. //
  392. // If this is the first chance and the current process has a debugger
  393. // port, then send a message to the debugger port and wait for a reply.
  394. // If the debugger handles the exception, then continue execution. Else
  395. // transfer the exception information to the user stack, transition to
  396. // user mode, and attempt to dispatch the exception to a frame based
  397. // handler. If a frame based handler handles the exception, then continue
  398. // execution with the continue system service. Else execute the
  399. // NtRaiseException system service with FirstChance == FALSE, which
  400. // will call this routine a second time to process the exception.
  401. //
  402. // If this is the second chance and the current process has a debugger
  403. // port, then send a message to the debugger port and wait for a reply.
  404. // If the debugger handles the exception, then continue execution. Else
  405. // if the current process has a subsystem port, then send a message to
  406. // the subsystem port and wait for a reply. If the subsystem handles the
  407. // exception, then continue execution. Else terminate the thread.
  408. //
  409. if (FirstChance == TRUE) {
  410. //
  411. // This is the first chance to handle the exception.
  412. //
  413. if (PsGetCurrentProcess()->DebugPort != NULL) {
  414. if ((KiDebugRoutine != NULL) &&
  415. (KdIsThisAKdTrap(ExceptionRecord, &ContextRecord, UserMode) != FALSE)) {
  416. if ((((KiDebugRoutine)(TrapFrame,
  417. ExceptionFrame,
  418. ExceptionRecord,
  419. &ContextRecord,
  420. PreviousMode,
  421. FALSE)) != FALSE)) {
  422. goto Handled1;
  423. }
  424. }
  425. } else {
  426. if ((KiDebugRoutine != NULL) &&
  427. (((KiDebugRoutine)(TrapFrame,
  428. ExceptionFrame,
  429. ExceptionRecord,
  430. &ContextRecord,
  431. PreviousMode,
  432. FALSE)) != FALSE)) {
  433. goto Handled1;
  434. }
  435. }
  436. if ((ExceptionWasForwarded == FALSE) &&
  437. (DbgkForwardException(ExceptionRecord, TRUE, FALSE))) {
  438. goto Handled2;
  439. }
  440. //
  441. // If the user mode thread is executing in 32-bit mode, then
  442. // clear the upper 32-bits of the stack address since they
  443. // may contain garbage.
  444. //
  445. if ((ContextRecord.SegCs & 0xfff8) == KGDT64_R3_CMCODE) {
  446. ContextRecord.Rsp &= 0xffffffff;
  447. }
  448. //
  449. // Transfer exception information to the user stack, transition
  450. // to user mode, and attempt to dispatch the exception to a frame
  451. // based handler.
  452. //
  453. repeat:
  454. try {
  455. //
  456. // Compute address of aligned machine frame, compute address
  457. // of exception record, compute address of context record,
  458. // and probe user stack for writeability.
  459. //
  460. MachineFrame =
  461. (PMACHINE_FRAME)((ContextRecord.Rsp - sizeof(MACHINE_FRAME)) & ~STACK_ROUND);
  462. UserStack1 = (ULONG64)MachineFrame - EXCEPTION_RECORD_LENGTH;
  463. UserStack2 = UserStack1 - CONTEXT_LENGTH;
  464. ProbeForWriteSmallStructure((PVOID)UserStack2,
  465. sizeof(MACHINE_FRAME) + EXCEPTION_RECORD_LENGTH + CONTEXT_LENGTH,
  466. STACK_ALIGN);
  467. //
  468. // Fill in machine frame information.
  469. //
  470. MachineFrame->Rsp = ContextRecord.Rsp;
  471. MachineFrame->Rip = ContextRecord.Rip;
  472. //
  473. // Copy exception record to the user stack.
  474. //
  475. RtlCopyMemory((PVOID)UserStack1,
  476. ExceptionRecord,
  477. sizeof(EXCEPTION_RECORD));
  478. //
  479. // Copy context record to the user stack.
  480. //
  481. RtlCopyMemory((PVOID)UserStack2,
  482. &ContextRecord,
  483. sizeof(CONTEXT));
  484. //
  485. // Set address of exception record, context record, and the
  486. // and the new stack pointer in the current trap frame.
  487. //
  488. ExceptionFrame->Rsi = UserStack1;
  489. ExceptionFrame->Rdi = UserStack2;
  490. TrapFrame->Rsp = UserStack2;
  491. //
  492. // Set the user mode 64-bit code selector.
  493. //
  494. TrapFrame->SegCs = KGDT64_R3_CODE | RPL_MASK;
  495. //
  496. // Set the address of the exception routine that will call the
  497. // exception dispatcher and then return to the trap handler.
  498. // The trap handler will restore the exception and trap frame
  499. // context and continue execution in the routine that will
  500. // call the exception dispatcher.
  501. //
  502. TrapFrame->Rip = (ULONG64)KeUserExceptionDispatcher;
  503. return;
  504. } except (KiCopyInformation(&ExceptionRecord1,
  505. (GetExceptionInformation())->ExceptionRecord)) {
  506. //
  507. // If the exception is a stack overflow, then attempt to
  508. // raise the stack overflow exception. Otherwise, the user's
  509. // stack is not accessible, or is misaligned, and second
  510. // chance processing is performed.
  511. //
  512. if (ExceptionRecord1.ExceptionCode == STATUS_STACK_OVERFLOW) {
  513. ExceptionRecord1.ExceptionAddress = ExceptionRecord->ExceptionAddress;
  514. RtlCopyMemory((PVOID)ExceptionRecord,
  515. &ExceptionRecord1,
  516. sizeof(EXCEPTION_RECORD));
  517. goto repeat;
  518. }
  519. }
  520. }
  521. //
  522. // This is the second chance to handle the exception.
  523. //
  524. if (DbgkForwardException(ExceptionRecord, TRUE, TRUE)) {
  525. goto Handled2;
  526. } else if (DbgkForwardException(ExceptionRecord, FALSE, TRUE)) {
  527. goto Handled2;
  528. } else {
  529. ZwTerminateThread(NtCurrentThread(), ExceptionRecord->ExceptionCode);
  530. KeBugCheckEx(KMODE_EXCEPTION_NOT_HANDLED,
  531. ExceptionRecord->ExceptionCode,
  532. (ULONG64)ExceptionRecord->ExceptionAddress,
  533. ExceptionRecord->ExceptionInformation[0],
  534. ExceptionRecord->ExceptionInformation[1]);
  535. }
  536. }
  537. //
  538. // Move machine state from context frame to trap and exception frames and
  539. // then return to continue execution with the restored state.
  540. //
  541. Handled1:
  542. KeContextToKframes(TrapFrame,
  543. ExceptionFrame,
  544. &ContextRecord,
  545. ContextRecord.ContextFlags,
  546. PreviousMode);
  547. //
  548. // Exception was handled by the debugger or the associated subsystem
  549. // and state was modified, if necessary, using the get state and set
  550. // state capabilities. Therefore the context frame does not need to
  551. // be transfered to the trap and exception frames.
  552. //
  553. Handled2:
  554. return;
  555. }
  556. ULONG
  557. KiCopyInformation (
  558. IN OUT PEXCEPTION_RECORD ExceptionRecord1,
  559. IN PEXCEPTION_RECORD ExceptionRecord2
  560. )
  561. /*++
  562. Routine Description:
  563. This function is called from an exception filter to copy the exception
  564. information from one exception record to another when an exception occurs.
  565. Arguments:
  566. ExceptionRecord1 - Supplies a pointer to the destination exception record.
  567. ExceptionRecord2 - Supplies a pointer to the source exception record.
  568. Return Value:
  569. A value of EXCEPTION_EXECUTE_HANDLER is returned as the function value.
  570. --*/
  571. {
  572. //
  573. // Copy one exception record to another and return value that causes
  574. // an exception handler to be executed.
  575. //
  576. RtlCopyMemory((PVOID)ExceptionRecord1,
  577. (PVOID)ExceptionRecord2,
  578. sizeof(EXCEPTION_RECORD));
  579. return EXCEPTION_EXECUTE_HANDLER;
  580. }
  581. NTSTATUS
  582. KeRaiseUserException (
  583. IN NTSTATUS ExceptionCode
  584. )
  585. /*++
  586. Routine Description:
  587. This function causes an exception to be raised in the calling thread's
  588. user context.
  589. Arguments:
  590. ExceptionCode - Supplies the status value to be raised.
  591. Return Value:
  592. The status value that should be returned by the caller.
  593. --*/
  594. {
  595. PTEB Teb;
  596. PKTHREAD Thread;
  597. PKTRAP_FRAME TrapFrame;
  598. ASSERT(KeGetPreviousMode() == UserMode);
  599. //
  600. // Save the exception code in the TEB and set the return address in the
  601. // trap frame to return to the raise user exception code in user mode.
  602. // This replaces the normal return which would go to the system service
  603. // dispatch stub. The system service dispatch stub is called thus the
  604. // return to the system service call site is on the top of the user stack.
  605. //
  606. Thread = KeGetCurrentThread();
  607. TrapFrame = Thread->TrapFrame;
  608. if (TrapFrame != NULL) {
  609. Teb = (PTEB)Thread->Teb;
  610. try {
  611. Teb->ExceptionCode = ExceptionCode;
  612. } except (EXCEPTION_EXECUTE_HANDLER) {
  613. return ExceptionCode;
  614. }
  615. TrapFrame->Rip = (ULONG64)KeRaiseUserExceptionDispatcher;
  616. }
  617. return ExceptionCode;
  618. }