[NTOSKRNL]
[reactos.git] / reactos / ntoskrnl / kdbg / amd64 / kdb_help.S
1 #include <reactos/asm.h>
2 #include <ndk/amd64/asm.h>
3
4 .globl KdbEnter
5 KdbEnter:
6
7 /* save flags */
8 pushfq
9 // .pushreg ?
10
11 /* Make room for a KTRAP_FRAME */
12 sub rsp, SIZE_KTRAP_FRAME
13 // .allocstack SIZE_KTRAP_FRAME
14
15 /* Save rbp */
16 mov [rsp + KTRAP_FRAME_Rbp], rbp
17
18 /* Save non-volatile registers */
19 mov [rsp + KTRAP_FRAME_Rbx], rbx
20 mov [rsp + KTRAP_FRAME_Rdi], rdi
21 mov [rsp + KTRAP_FRAME_Rsi], rsi
22
23 /* Save volatile registers */
24 mov [rsp + KTRAP_FRAME_Rax], rax
25 mov [rsp + KTRAP_FRAME_Rcx], rcx
26 mov [rsp + KTRAP_FRAME_Rdx], rdx
27 mov [rsp + KTRAP_FRAME_R8], r8
28 mov [rsp + KTRAP_FRAME_R9], r9
29 mov [rsp + KTRAP_FRAME_R10], r10
30 mov [rsp + KTRAP_FRAME_R11], r11
31
32 /* Save xmm registers */
33 movdqa [rsp + KTRAP_FRAME_Xmm0], xmm0
34 movdqa [rsp + KTRAP_FRAME_Xmm1], xmm1
35 movdqa [rsp + KTRAP_FRAME_Xmm2], xmm2
36 movdqa [rsp + KTRAP_FRAME_Xmm3], xmm3
37 movdqa [rsp + KTRAP_FRAME_Xmm4], xmm4
38 movdqa [rsp + KTRAP_FRAME_Xmm5], xmm5
39
40 /* Save cs and previous mode */
41 mov ax, cs
42 mov [rsp + KTRAP_FRAME_SegCs], ax
43 and ax, 1
44 mov [rsp + KTRAP_FRAME_PreviousMode], al
45
46 /* Save segment selectors */
47 mov ax, ds
48 mov [rsp + KTRAP_FRAME_SegDs], ax
49 mov ax, es
50 mov [rsp + KTRAP_FRAME_SegEs], ax
51 mov ax, fs
52 mov [rsp + KTRAP_FRAME_SegFs], ax
53 mov ax, gs
54 mov [rsp + KTRAP_FRAME_SegGs], ax
55
56 /* Save previous irql */
57 mov rax, cr8
58 mov [rsp + KTRAP_FRAME_PreviousIrql], al
59
60 /* Save debug registers */
61 mov rax, dr0
62 mov [rsp + KTRAP_FRAME_Dr0], rax
63 mov rax, dr1
64 mov [rsp + KTRAP_FRAME_Dr1], rax
65 mov rax, dr2
66 mov [rsp + KTRAP_FRAME_Dr2], rax
67 mov rax, dr3
68 mov [rsp + KTRAP_FRAME_Dr3], rax
69 mov rax, dr6
70 mov [rsp + KTRAP_FRAME_Dr6], rax
71 mov rax, dr7
72 mov [rsp + KTRAP_FRAME_Dr7], rax
73
74 /* Point rbp, where rsp was before */
75 lea rbp, [rsp + SIZE_KTRAP_FRAME]
76 mov [rsp + KTRAP_FRAME_Rsp], rbp
77
78 /* Store the EFLAGS we previously pushed on the stack */
79 mov rax, [rbp + 8]
80 mov [rsp + KTRAP_FRAME_EFlags], rax
81
82 /* Get RIP from the stack */
83 mov rax, [rbp + 16]
84 mov [rsp + KTRAP_FRAME_Rip], rax
85
86 /* Make sure the direction flag is cleared */
87 cld
88
89 /* Clear all breakpoint enables in dr7. */
90 mov rax, dr7
91 and rax, 0xFFFF0000
92 mov dr7, rax
93
94 /* Call KDB */
95 mov byte ptr [rsp + KTRAP_FRAME_P5], 1 /* FirstChance */
96 mov r9, rsp /* Pointer to the trap frame */
97 mov r8, 0 /* Context */
98 mov dl, 0 /* PreviousMode (KernelMode) */
99 mov rcx, 0 /* ExceptionRecord */
100 call KdbEnterDebuggerException
101
102 /* Restore segment selectors */
103 mov ax, [rsp + KTRAP_FRAME_SegDs]
104 mov ds, ax
105 mov ax, [rsp + KTRAP_FRAME_SegEs]
106 mov es, ax
107 mov ax, [rsp + KTRAP_FRAME_SegFs]
108 mov fs, ax
109
110 /* Restore non-volatile registers */
111 mov rbx, [rsp + KTRAP_FRAME_Rbx]
112 mov rdi, [rsp + KTRAP_FRAME_Rdi]
113 mov rsi, [rsp + KTRAP_FRAME_Rsi]
114
115 /* Restore volatile registers */
116 mov rax, [rsp + KTRAP_FRAME_Rax]
117 mov rcx, [rsp + KTRAP_FRAME_Rcx]
118 mov rdx, [rsp + KTRAP_FRAME_Rdx]
119 mov r8, [rsp + KTRAP_FRAME_R8]
120 mov r9, [rsp + KTRAP_FRAME_R9]
121 mov r10, [rsp + KTRAP_FRAME_R10]
122 mov r11, [rsp + KTRAP_FRAME_R11]
123
124 /* Restore RSP */
125 mov rsp, [rsp + KTRAP_FRAME_Rsp]
126
127 /* Restore EFLAGS */
128 popfq
129
130 ret
131
132 .globl KdbpStackSwitchAndCall
133 KdbpStackSwitchAndCall:
134
135 /* Save old stack */
136 mov rax, rsp
137
138 /* Set new stack */
139 mov rsp, rcx
140
141 /* Save old stack on new stack */
142 push rax
143
144 /* Call function */
145 call rdx
146
147 /* Restire old stack */
148 pop rax
149 mov rsp, rax
150
151 /* Return */
152 ret