]> git.proxmox.com Git - mirror_edk2.git/blob - SourceLevelDebugPkg/Library/DebugAgent/DebugAgentCommon/X64/AsmFuncs.nasm
b1019e017bc4697fa1aa0411573d01b66a76b4ca
[mirror_edk2.git] / SourceLevelDebugPkg / Library / DebugAgent / DebugAgentCommon / X64 / AsmFuncs.nasm
1 ;------------------------------------------------------------------------------
2 ;
3 ; Copyright (c) 2016 - 2022, Intel Corporation. All rights reserved.<BR>
4 ; SPDX-License-Identifier: BSD-2-Clause-Patent
5 ;
6 ; Module Name:
7 ;
8 ; AsmFuncs.nasm
9 ;
10 ; Abstract:
11 ;
12 ; Debug interrupt handle functions.
13 ;
14 ;------------------------------------------------------------------------------
15
16 #include "DebugException.h"
17
18 SECTION .data
19
20 extern ASM_PFX(InterruptProcess)
21 global ASM_PFX(Exception0Handle)
22 global ASM_PFX(TimerInterruptHandle)
23 global ASM_PFX(ExceptionStubHeaderSize)
24
25 %macro AGENT_HANDLER_SIGNATURE 0
26 db 0x41, 0x47, 0x54, 0x48 ; SIGNATURE_32('A','G','T','H')
27 %endmacro
28
29 ASM_PFX(ExceptionStubHeaderSize): dd Exception1Handle - ASM_PFX(Exception0Handle) ;
30 CommonEntryAddr: dq CommonEntry ;
31
32 DEFAULT REL
33 SECTION .text
34
35 AGENT_HANDLER_SIGNATURE
36 ASM_PFX(Exception0Handle):
37 cli
38 push rcx
39 mov rcx, dword 0
40 jmp qword [CommonEntryAddr]
41 AGENT_HANDLER_SIGNATURE
42 Exception1Handle:
43 cli
44 push rcx
45 mov rcx, dword 1
46 jmp qword [CommonEntryAddr]
47 AGENT_HANDLER_SIGNATURE
48 Exception2Handle:
49 cli
50 push rcx
51 mov rcx, dword 2
52 jmp qword [CommonEntryAddr]
53 AGENT_HANDLER_SIGNATURE
54 Exception3Handle:
55 cli
56 push rcx
57 mov rcx, dword 3
58 jmp qword [CommonEntryAddr]
59 AGENT_HANDLER_SIGNATURE
60 Exception4Handle:
61 cli
62 push rcx
63 mov rcx, dword 4
64 jmp qword [CommonEntryAddr]
65 AGENT_HANDLER_SIGNATURE
66 Exception5Handle:
67 cli
68 push rcx
69 mov rcx, dword 5
70 jmp qword [CommonEntryAddr]
71 AGENT_HANDLER_SIGNATURE
72 Exception6Handle:
73 cli
74 push rcx
75 mov rcx, dword 6
76 jmp qword [CommonEntryAddr]
77 AGENT_HANDLER_SIGNATURE
78 Exception7Handle:
79 cli
80 push rcx
81 mov rcx, dword 7
82 jmp qword [CommonEntryAddr]
83 AGENT_HANDLER_SIGNATURE
84 Exception8Handle:
85 cli
86 push rcx
87 mov rcx, dword 8
88 jmp qword [CommonEntryAddr]
89 AGENT_HANDLER_SIGNATURE
90 Exception9Handle:
91 cli
92 push rcx
93 mov rcx, dword 9
94 jmp qword [CommonEntryAddr]
95 AGENT_HANDLER_SIGNATURE
96 Exception10Handle:
97 cli
98 push rcx
99 mov rcx, dword 10
100 jmp qword [CommonEntryAddr]
101 AGENT_HANDLER_SIGNATURE
102 Exception11Handle:
103 cli
104 push rcx
105 mov rcx, dword 11
106 jmp qword [CommonEntryAddr]
107 AGENT_HANDLER_SIGNATURE
108 Exception12Handle:
109 cli
110 push rcx
111 mov rcx, dword 12
112 jmp qword [CommonEntryAddr]
113 AGENT_HANDLER_SIGNATURE
114 Exception13Handle:
115 cli
116 push rcx
117 mov rcx, dword 13
118 jmp qword [CommonEntryAddr]
119 AGENT_HANDLER_SIGNATURE
120 Exception14Handle:
121 cli
122 push rcx
123 mov rcx, dword 14
124 jmp qword [CommonEntryAddr]
125 AGENT_HANDLER_SIGNATURE
126 Exception15Handle:
127 cli
128 push rcx
129 mov rcx, dword 15
130 jmp qword [CommonEntryAddr]
131 AGENT_HANDLER_SIGNATURE
132 Exception16Handle:
133 cli
134 push rcx
135 mov rcx, dword 16
136 jmp qword [CommonEntryAddr]
137 AGENT_HANDLER_SIGNATURE
138 Exception17Handle:
139 cli
140 push rcx
141 mov rcx, dword 17
142 jmp qword [CommonEntryAddr]
143 AGENT_HANDLER_SIGNATURE
144 Exception18Handle:
145 cli
146 push rcx
147 mov rcx, dword 18
148 jmp qword [CommonEntryAddr]
149 AGENT_HANDLER_SIGNATURE
150 Exception19Handle:
151 cli
152 push rcx
153 mov rcx, dword 19
154 jmp qword [CommonEntryAddr]
155 AGENT_HANDLER_SIGNATURE
156 ASM_PFX(TimerInterruptHandle):
157 cli
158 push rcx
159 mov rcx, dword 32
160 jmp qword [CommonEntryAddr]
161
162 CommonEntry:
163 ; We need to determine if any extra data was pushed by the exception
164 cmp rcx, DEBUG_EXCEPT_DOUBLE_FAULT
165 je NoExtrPush
166 cmp rcx, DEBUG_EXCEPT_INVALID_TSS
167 je NoExtrPush
168 cmp rcx, DEBUG_EXCEPT_SEG_NOT_PRESENT
169 je NoExtrPush
170 cmp rcx, DEBUG_EXCEPT_STACK_FAULT
171 je NoExtrPush
172 cmp rcx, DEBUG_EXCEPT_GP_FAULT
173 je NoExtrPush
174 cmp rcx, DEBUG_EXCEPT_PAGE_FAULT
175 je NoExtrPush
176 cmp rcx, DEBUG_EXCEPT_ALIGNMENT_CHECK
177 je NoExtrPush
178
179 push qword [rsp]
180 mov qword [rsp + 8], 0
181
182 NoExtrPush:
183 push rbp
184 mov rbp, rsp
185
186 ; store UINT64 r8, r9, r10, r11, r12, r13, r14, r15;
187 push r15
188 push r14
189 push r13
190 push r12
191 push r11
192 push r10
193 push r9
194 push r8
195
196 mov r8, cr8
197 push r8
198
199 ; store UINT64 Rdi, Rsi, Rbp, Rsp, Rdx, Rcx, Rbx, Rax;
200 push rax
201 push rbx
202 push qword [rbp + 8] ; original rcx
203 push rdx
204 push qword [rbp + 6 * 8] ; original rsp
205 push qword [rbp] ; original rbp
206 push rsi
207 push rdi
208
209 ;; UINT32 Cr0, Cr1, Cr2, Cr3, Cr4;
210 ;; insure FXSAVE/FXRSTOR is enabled in CR4...
211 ;; ... while we're at it, make sure DE is also enabled...
212 mov rax, cr4
213 or rax, 0x208
214 mov cr4, rax
215 push rax
216 mov rax, cr3
217 push rax
218 mov rax, cr2
219 push rax
220 push 0
221 mov rax, cr0
222 push rax
223
224 xor rax, rax
225 mov rax, Ss
226 push rax
227 mov rax, Cs
228 push rax
229 mov rax, Ds
230 push rax
231 mov rax, Es
232 push rax
233 mov rax, Fs
234 push rax
235 mov rax, Gs
236 push rax
237
238 ;; EIP
239 mov rax, [rbp + 8 * 3] ; EIP
240 push rax
241
242 ;; UINT64 Gdtr[2], Idtr[2];
243 sub rsp, 16
244 sidt [rsp]
245 sub rsp, 16
246 sgdt [rsp]
247
248 ;; UINT64 Ldtr, Tr;
249 xor rax, rax
250 str ax
251 push rax
252 sldt ax
253 push rax
254
255 ;; EFlags
256 mov rax, [rbp + 8 * 5]
257 push rax
258
259 ;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
260 mov rax, dr7
261 push rax
262
263 ;; clear Dr7 while executing debugger itself
264 xor rax, rax
265 mov dr7, rax
266
267 ;; Dr6
268 mov rax, dr6
269 push rax
270
271 ;; insure all status bits in dr6 are clear...
272 xor rax, rax
273 mov dr6, rax
274
275 mov rax, dr3
276 push rax
277 mov rax, dr2
278 push rax
279 mov rax, dr1
280 push rax
281 mov rax, dr0
282 push rax
283
284 ;; Clear Direction Flag
285 cld
286
287 sub rsp, 512
288 mov rdi, rsp
289 ;; Clear the buffer
290 xor rax, rax
291 push rcx
292 mov rcx, dword 64 ;= 512 / 8
293 rep stosq
294 pop rcx
295 mov rdi, rsp
296 fxsave [rdi]
297
298 ;; save the exception data
299 push qword [rbp + 16]
300
301 ; call the C interrupt process function
302 mov rdx, rsp ; Structure
303 mov r15, rcx ; save vector in r15
304
305 ;
306 ; Per X64 calling convention, allocate maximum parameter stack space
307 ; and make sure RSP is 16-byte aligned
308 ;
309 sub rsp, 32 + 8
310 call ASM_PFX(InterruptProcess)
311 add rsp, 32 + 8
312
313 ;; skip the exception data
314 add rsp, 8
315
316 mov rsi, rsp
317 fxrstor [rsi]
318 add rsp, 512
319
320 ;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
321 pop rax
322 mov dr0, rax
323 pop rax
324 mov dr1, rax
325 pop rax
326 mov dr2, rax
327 pop rax
328 mov dr3, rax
329 ;; skip restore of dr6. We cleared dr6 during the context save.
330 add rsp, 8
331 pop rax
332 mov dr7, rax
333
334 ;; set EFlags
335 pop qword [rbp + 8 * 5]
336
337 ;; UINT64 Ldtr, Tr;
338 ;; UINT64 Gdtr[2], Idtr[2];
339 ;; Best not let anyone mess with these particular registers...
340 add rsp, 24 * 2
341
342 ;; UINT64 Eip;
343 pop qword [rbp + 8 * 3] ; set EIP in stack
344
345 ;; UINT64 Gs, Fs, Es, Ds, Cs, Ss;
346 ;; NOTE - modified segment registers could hang the debugger... We
347 ;; could attempt to insulate ourselves against this possibility,
348 ;; but that poses risks as well.
349 ;;
350 pop rax
351 pop rax
352 pop rax
353 mov es, rax
354 pop rax
355 mov ds, rax
356 pop qword [rbp + 8 * 4] ; Set CS in stack
357 pop rax
358 mov ss, rax
359
360 ;; UINT64 Cr0, Cr1, Cr2, Cr3, Cr4;
361 pop rax
362 mov cr0, rax
363 add rsp, 8 ; skip for Cr1
364 pop rax
365 mov cr2, rax
366 pop rax
367 mov cr3, rax
368 pop rax
369 mov cr4, rax
370
371 ;; restore general register
372 pop rdi
373 pop rsi
374 add rsp, 8 ; skip rbp
375 add rsp, 8 ; skip rsp
376 pop rdx
377 pop rcx
378 pop rbx
379 pop rax
380
381 pop r8
382 mov cr8, r8
383
384 ; store UINT64 r8, r9, r10, r11, r12, r13, r14, r15;
385 pop r8
386 pop r9
387 pop r10
388 pop r11
389 pop r12
390 pop r13
391 pop r14
392 pop r15
393
394 mov rsp, rbp
395 pop rbp
396 add rsp, 16 ; skip rcx and error code
397
398 iretq
399