Sync with trunk (48237)
[reactos.git] / ntoskrnl / kdbg / amd64 / kdb.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/kdbg/amd64/kdb.c
5 * PURPOSE: Kernel Debugger
6 * PROGRAMMERS:
7 */
8
9
10 /* INCLUDES ******************************************************************/
11
12 #include <ntoskrnl.h>
13 #define NDEBUG
14 #include <debug.h>
15
16 ULONG
17 NTAPI
18 KiEspFromTrapFrame(IN PKTRAP_FRAME TrapFrame)
19 {
20 return TrapFrame->Rsp;
21 }
22
23 VOID
24 NTAPI
25 KiEspToTrapFrame(IN PKTRAP_FRAME TrapFrame,
26 IN ULONG_PTR Esp)
27 {
28 KIRQL OldIrql;
29 ULONG Previous;
30
31 /* Raise to APC_LEVEL if needed */
32 OldIrql = KeGetCurrentIrql();
33 if (OldIrql < APC_LEVEL) KeRaiseIrql(APC_LEVEL, &OldIrql);
34
35 /* Get the old ESP */
36 Previous = KiEspFromTrapFrame(TrapFrame);
37
38 /* Check if this is user-mode */
39 if ((TrapFrame->SegCs & MODE_MASK))
40 {
41 /* Write it directly */
42 TrapFrame->Rsp = Esp;
43 }
44 else
45 {
46 /* Don't allow ESP to be lowered, this is illegal */
47 if (Esp < Previous) KeBugCheckEx(SET_OF_INVALID_CONTEXT,
48 Esp,
49 Previous,
50 (ULONG_PTR)TrapFrame,
51 0);
52
53 /* Create an edit frame, check if it was alrady */
54 if (!(TrapFrame->SegCs & FRAME_EDITED))
55 {
56 /* Update the value */
57 TrapFrame->Rsp = Esp;
58 }
59 else
60 {
61 /* Check if ESP changed */
62 if (Previous != Esp)
63 {
64 /* Save CS */
65 TrapFrame->SegCs &= ~FRAME_EDITED;
66
67 /* Save ESP */
68 TrapFrame->Rsp = Esp;
69 }
70 }
71 }
72
73 /* Restore IRQL */
74 if (OldIrql < APC_LEVEL) KeLowerIrql(OldIrql);
75
76 }
77
78 ULONG
79 NTAPI
80 KiSsFromTrapFrame(IN PKTRAP_FRAME TrapFrame)
81 {
82 if (TrapFrame->SegCs & MODE_MASK)
83 {
84 /* User mode, return the User SS */
85 return TrapFrame->SegSs | RPL_MASK;
86 }
87 else
88 {
89 /* Kernel mode */
90 return KGDT64_SYS_TSS;
91 }
92 }
93
94 VOID
95 NTAPI
96 KiSsToTrapFrame(IN PKTRAP_FRAME TrapFrame,
97 IN ULONG Ss)
98 {
99 /* Remove the high-bits */
100 Ss &= 0xFFFF;
101
102 if (TrapFrame->SegCs & MODE_MASK)
103 {
104 /* Usermode, save the User SS */
105 TrapFrame->SegSs = Ss | RPL_MASK;
106 }
107
108 }
109
110
111