]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/DebugSupportDxe/X64/AsmFuncs.S
Remove ".intel_syntax", convert MASM to GAS.
[mirror_edk2.git] / MdeModulePkg / Universal / DebugSupportDxe / X64 / AsmFuncs.S
1 #/**@file
2 # Low leve x64 specific debug support functions.
3 #
4 # Copyright (c) 2006 - 2008, Intel Corporation
5 # All rights reserved. This program and the accompanying materials
6 # are licensed and made available under the terms and conditions of the BSD License
7 # which accompanies this distribution. The full text of the license may be found at
8 # http://opensource.org/licenses/bsd-license.php
9 #
10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 #
13 #**/
14
15 ASM_GLOBAL ASM_PFX(OrigVector)
16 ASM_GLOBAL ASM_PFX(InterruptEntryStub)
17 ASM_GLOBAL ASM_PFX(StubSize)
18 ASM_GLOBAL ASM_PFX(CommonIdtEntry)
19 ASM_GLOBAL ASM_PFX(FxStorSupport)
20
21 .data
22
23 ASM_PFX(StubSize): .long ASM_PFX(InterruptEntryStubEnd) - ASM_PFX(InterruptEntryStub)
24 ASM_PFX(AppRsp): .long 0x11111111 # ?
25 .long 0x11111111 # ?
26 ASM_PFX(DebugRsp): .long 0x22222222 # ?
27 .long 0x22222222 # ?
28 ASM_PFX(ExtraPush): .long 0x33333333 # ?
29 .long 0x33333333 # ?
30 ASM_PFX(ExceptData): .long 0x44444444 # ?
31 .long 0x44444444 # ?
32 ASM_PFX(Rflags): .long 0x55555555 # ?
33 .long 0x55555555 # ?
34 ASM_PFX(OrigVector): .long 0x66666666 # ?
35 .long 0x66666666 # ?
36
37 ## The declarations below define the memory region that will be used for the debug stack.
38 ## The context record will be built by pushing register values onto this stack.
39 ## It is imparitive that alignment be carefully managed, since the FXSTOR and
40 ## FXRSTOR instructions will GP fault if their memory operand is not 16 byte aligned.
41 ##
42 ## The stub will switch stacks from the application stack to the debuger stack
43 ## and pushes the exception number.
44 ##
45 ## Then we building the context record on the stack. Since the stack grows down,
46 ## we push the fields of the context record from the back to the front. There
47 ## are 336 bytes of stack used prior allocating the 512 bytes of stack to be
48 ## used as the memory buffer for the fxstor instruction. Therefore address of
49 ## the buffer used for the FXSTOR instruction is &Eax - 336 - 512, which
50 ## must be 16 byte aligned.
51 ##
52 ## We carefully locate the stack to make this happen.
53 ##
54 ## For reference, the context structure looks like this:
55 ## struct {
56 ## UINT64 ExceptionData;
57 ## FX_SAVE_STATE_X64 FxSaveState; // 512 bytes, must be 16 byte aligned
58 ## UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
59 ## UINT64 Cr0, Cr1, Cr2, Cr3, Cr4, Cr8;
60 ## UINT64 RFlags;
61 ## UINT64 Ldtr, Tr;
62 ## UINT64 Gdtr[2], Idtr[2];
63 ## UINT64 Rip;
64 ## UINT64 Gs, Fs, Es, Ds, Cs, Ss;
65 ## UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;
66 ## UINT64 R8, R9, R10, R11, R12, R13, R14, R15;
67 ## } SYSTEM_CONTEXT_X64; // 64 bit system context record
68
69 .align 16
70 DebugStackEnd : .ascii "DbgStkEnd >>>>>>" # 16 byte long string - must be 16 bytes to preserve alignment
71 .rept 0x1ffc
72 .long 0x00000000
73 .endr
74 # 32K should be enough stack
75 # This allocation is coocked to insure
76 # that the the buffer for the FXSTORE instruction
77 # will be 16 byte aligned also.
78 #
79 ASM_PFX(ExceptionNumber): .long 0x77777777 # first entry will be the vector number pushed by the stub
80 .long 0x77777777 # ?
81
82 DebugStackBegin : .ascii "<<<< DbgStkBegin" # initial debug ESP == DebugStackBegin, set in stub
83
84
85 .text
86
87 #------------------------------------------------------------------------------
88 # BOOLEAN
89 # FxStorSupport (
90 # void
91 # )
92 #
93 # Abstract: Returns TRUE if FxStor instructions are supported
94 #
95 ASM_GLOBAL ASM_PFX(FxStorSupport)
96 ASM_PFX(FxStorSupport):
97 #
98 # cpuid corrupts rbx which must be preserved per the C calling convention
99 #
100 pushq %rbx
101 movq $1, %rax
102 cpuid
103 movl %edx, %eax
104 andq $0x01000000, %rax
105 shrq $24, %rax
106 popq %rbx
107 ret
108 #------------------------------------------------------------------------------
109 # void
110 # Vect2Desc (
111 # IA32_IDT_GATE_DESCRIPTOR * DestDesc, // rcx
112 # void (*Vector) (void) // rdx
113 # )
114 #
115 # Abstract: Encodes an IDT descriptor with the given physical address
116 #
117 ASM_GLOBAL ASM_PFX(Vect2Desc)
118 ASM_PFX(Vect2Desc):
119 movq %rdx, %rax
120 movw %ax, (%rcx) # write bits 15..0 of offset
121 movw %cs, %dx
122 movw %dx, 2(%rcx) # SYS_CODE_SEL from GDT
123 movw $(0x0e00 | 0x8000), 4(%rcx) # type = 386 interrupt gate, present
124 shrq $16, %rax
125 movw %ax, 6(%rcx) # write bits 31..16 of offset
126 shrq $16, %rax
127 movl %eax, 8(%rcx) # write bits 63..32 of offset
128
129 ret
130
131 #------------------------------------------------------------------------------
132 # InterruptEntryStub
133 #
134 # Abstract: This code is not a function, but is a small piece of code that is
135 # copied and fixed up once for each IDT entry that is hooked.
136 #
137 ASM_GLOBAL ASM_PFX(InterruptEntryStub)
138 ASM_PFX(InterruptEntryStub):
139
140 pushq $0 # push vector number - will be modified before installed
141 jmp ASM_PFX(CommonIdtEntry)
142
143 ASM_GLOBAL ASM_PFX(InterruptEntryStubEnd)
144 ASM_PFX(InterruptEntryStubEnd):
145
146 #------------------------------------------------------------------------------
147 # CommonIdtEntry
148 #
149 # Abstract: This code is not a function, but is the common part for all IDT
150 # vectors.
151 #
152 ASM_GLOBAL ASM_PFX(CommonIdtEntry)
153 ##
154 ## At this point, the stub has saved the current application stack esp into AppRsp
155 ## and switched stacks to the debug stack, where it pushed the vector number
156 ##
157 ## The application stack looks like this:
158 ##
159 ## ...
160 ## (last application stack entry)
161 ## [16 bytes alignment, do not care it]
162 ## SS from interrupted task
163 ## RSP from interrupted task
164 ## rflags from interrupted task
165 ## CS from interrupted task
166 ## RIP from interrupted task
167 ## Error code <-------------------- Only present for some exeption types
168 ##
169 ## Vector Number <----------------- pushed in our IDT Entry
170 ##
171
172
173 ## The stub switched us to the debug stack and pushed the interrupt number.
174 ##
175 ## Next, construct the context record. It will be build on the debug stack by
176 ## pushing the registers in the correct order so as to create the context structure
177 ## on the debug stack. The context record must be built from the end back to the
178 ## beginning because the stack grows down...
179 #
180 ## For reference, the context record looks like this:
181 ##
182 ## typedef
183 ## struct {
184 ## UINT64 ExceptionData;
185 ## FX_SAVE_STATE_X64 FxSaveState;
186 ## UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
187 ## UINT64 Cr0, Cr2, Cr3, Cr4, Cr8;
188 ## UINT64 RFlags;
189 ## UINT64 Ldtr, Tr;
190 ## UINT64 Gdtr[2], Idtr[2];
191 ## UINT64 Rip;
192 ## UINT64 Gs, Fs, Es, Ds, Cs, Ss;
193 ## UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;
194 ## UINT64 R8, R9, R10, R11, R12, R13, R14, R15;
195 ## } SYSTEM_CONTEXT_X64; // 64
196 ASM_PFX(CommonIdtEntry):
197 ## NOTE: we save rsp here to prevent compiler put rip reference cause error AppRsp
198 pushq %rax
199 movq (8)(%rsp), %rax # save vector number
200 movq %rax, ASM_PFX(ExceptionNumber) # save vector number
201 popq %rax
202 addq $8, %rsp # pop vector number
203 movq %rsp, ASM_PFX(AppRsp) # save stack top
204 movq DebugStackBegin, %rsp # switch to debugger stack
205 subq $8, %rsp # leave space for vector number
206 ## UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;
207 ## UINT64 R8, R9, R10, R11, R12, R13, R14, R15;
208 pushq %r15
209 pushq %r14
210 pushq %r13
211 pushq %r12
212 pushq %r11
213 pushq %r10
214 pushq %r9
215 pushq %r8
216 pushq %rax
217 pushq %rcx
218 pushq %rdx
219 pushq %rbx
220 pushq %rsp
221 pushq %rbp
222 pushq %rsi
223 pushq %rdi
224 ## Save interrupt state rflags register...
225 pushfq
226 popq %rax
227 movq %rax, ASM_PFX(Rflags)
228 ## We need to determine if any extra data was pushed by the exception, and if so, save it
229 ## To do this, we check the exception number pushed by the stub, and cache the
230 ## result in a variable since we'll need this again.
231 cmpl $0, ASM_PFX(ExceptionNumber)
232 jz ExtraPushOne
233 cmpl $10, ASM_PFX(ExceptionNumber)
234 jz ExtraPushOne
235 cmpl $11, ASM_PFX(ExceptionNumber)
236 jz ExtraPushOne
237 cmpl $12, ASM_PFX(ExceptionNumber)
238 jz ExtraPushOne
239 cmpl $13, ASM_PFX(ExceptionNumber)
240 jz ExtraPushOne
241 cmpl $14, ASM_PFX(ExceptionNumber)
242 jz ExtraPushOne
243 cmpl $17, ASM_PFX(ExceptionNumber)
244 jz ExtraPushOne
245 movl $0, ASM_PFX(ExtraPush)
246 movl $0, ASM_PFX(ExceptData)
247 jmp ExtraPushDone
248 ExtraPushOne:
249 movl $1, ASM_PFX(ExtraPush)
250
251 ## If there's some extra data, save it also, and modify the saved AppRsp to effectively
252 ## pop this value off the application's stack.
253 movq ASM_PFX(AppRsp), %rax
254 movq (%rax), %rbx
255 movq %rbx, ASM_PFX(ExceptData)
256 addq $8, %rax
257 movq %rax, ASM_PFX(AppRsp)
258
259 ExtraPushDone:
260
261 ## The "push" above pushed the debug stack rsp. Since what we're actually doing
262 ## is building the context record on the debug stack, we need to save the pushed
263 ## debug RSP, and replace it with the application's last stack entry...
264 movq 24(%rsp), %rax
265 movq %rax, ASM_PFX(DebugRsp)
266 movq ASM_PFX(AppRsp), %rax
267 addq $40, %rax
268 # application stack has ss, rsp, rflags, cs, & rip, so
269 # last actual application stack entry is 40 bytes
270 # into the application stack.
271 movq %rax, 24(%rsp)
272
273 ## continue building context record
274 ## UINT64 Gs, Fs, Es, Ds, Cs, Ss; insure high 16 bits of each is zero
275 movq %ss, %rax
276 pushq %rax
277 # CS from application is one entry back in application stack
278 movq ASM_PFX(AppRsp), %rax
279 movzxw 8(%rax), %rax
280 pushq %rax
281
282 movq %ds, %rax
283 pushq %rax
284 movq %es, %rax
285 pushq %rax
286 movq %fs, %rax
287 pushq %rax
288 movq %gs, %rax
289 pushq %rax
290 ## UINT64 Rip;
291 # Rip from application is on top of application stack
292 movq ASM_PFX(AppRsp), %rax
293 pushq (%rax)
294 ## UINT64 Gdtr[2], Idtr[2];
295 push $0
296 push $0
297 sidtq (%rsp)
298 push $0
299 push $0
300 sgdtq (%rsp)
301
302 ## UINT64 Ldtr, Tr;
303 xorq %rax, %rax
304 str %ax
305 pushq %rax
306 sldt %ax
307 pushq %rax
308
309 ## UINT64 RFlags;
310 ## Rflags from application is two entries back in application stack
311 movq ASM_PFX(AppRsp), %rax
312 pushq 16(%rax)
313 ## UINT64 Cr0, Cr1, Cr2, Cr3, Cr4, Cr8;
314 ## insure FXSAVE/FXRSTOR is enabled in CR4...
315 ## ... while we're at it, make sure DE is also enabled...
316 movq %cr8, %rax
317 pushq %rax
318 movq %cr4, %rax
319 orq $0x208, %rax
320 movq %rax, %cr4
321 pushq %rax
322 movq %cr3, %rax
323 pushq %rax
324 movq %cr2, %rax
325 pushq %rax
326 push $0
327 movq %cr0, %rax
328 pushq %rax
329 ## UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
330 movq %dr7, %rax
331 pushq %rax
332 ## clear Dr7 while executing debugger itself
333 xorq %rax, %rax
334 movq %rax, %dr7
335
336 movq %dr6, %rax
337 pushq %rax
338 ## insure all status bits in dr6 are clear...
339 xorq %rax, %rax
340 movq %rax, %dr6
341
342 movq %dr3, %rax
343 pushq %rax
344 movq %dr2, %rax
345 pushq %rax
346 movq %dr1, %rax
347 pushq %rax
348 movq %dr0, %rax
349 pushq %rax
350
351 ## FX_SAVE_STATE_X64 FxSaveState;
352 subq $512, %rsp
353 movq %rsp, %rdi
354 # IMPORTANT!! The debug stack has been carefully constructed to
355 # insure that rsp and rdi are 16 byte aligned when we get here.
356 # They MUST be. If they are not, a GP fault will occur.
357
358 # FXSTOR_RDI
359 fxsave (%rdi)
360
361 ## UINT64 ExceptionData;
362 movq ASM_PFX(ExceptData), %rax
363 pushq %rax
364
365 # call to C code which will in turn call registered handler
366 # pass in the vector number
367 movq %rsp, %rdx
368 movq ASM_PFX(ExceptionNumber), %rcx
369 subq $40, %rsp
370 call ASM_PFX(InterruptDistrubutionHub)
371 addq $40, %rsp
372 # restore context...
373 ## UINT64 ExceptionData;
374 addq $8, %rsp
375
376 ## FX_SAVE_STATE_X64 FxSaveState;
377 movq %rsp, %rsi
378
379 # FXRSTOR_RSI
380 fxrstor (%rsi)
381
382 addq $512, %rsp
383
384 ## UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
385 popq %rax
386 movq %rax, %dr0
387 popq %rax
388 movq %rax, %dr1
389 popq %rax
390 movq %rax, %dr2
391 popq %rax
392 movq %rax, %dr3
393
394 ## skip restore of dr6. We cleared dr6 during the context save.
395 addq $8, %rsp
396 popq %rax
397 movq %rax, %dr7
398
399 ## UINT64 Cr0, Cr1, Cr2, Cr3, Cr4, Cr8;
400 popq %rax
401 movq %rax, %cr0
402 addq $8, %rsp
403 popq %rax
404 movq %rax, %cr2
405 popq %rax
406 movq %rax, %cr3
407 popq %rax
408 movq %rax, %cr4
409 popq %rax
410 movq %rax, %cr8
411 ## UINT64 RFlags;
412 movq ASM_PFX(AppRsp), %rax
413 popq 16(%rax)
414 ## UINT64 Ldtr, Tr;
415 ## UINT64 Gdtr[2], Idtr[2];
416 ## Best not let anyone mess with these particular registers...
417 addq $48, %rsp
418 ## UINT64 Rip;
419 popq (%rax)
420
421 ## UINT64 Gs, Fs, Es, Ds, Cs, Ss;
422 ## NOTE - modified segment registers could hang the debugger... We
423 ## could attempt to insulate ourselves against this possibility,
424 ## but that poses risks as well.
425 ##
426
427 popq %rax
428 # movq %rax, %gs
429 popq %rax
430 # movq %rax, %fs
431 popq %rax
432 movq %rax, %es
433 popq %rax
434 movq %rax, %ds
435 movq ASM_PFX(AppRsp), %rax
436 popq 8(%rax)
437 popq %rax
438 movq %rax, %ss
439 ## The next stuff to restore is the general purpose registers that were pushed
440 ## using the "push" instruction.
441 ##
442 ## The value of RSP as stored in the context record is the application RSP
443 ## including the 5 entries on the application stack caused by the exception
444 ## itself. It may have been modified by the debug agent, so we need to
445 ## determine if we need to relocate the application stack.
446
447 movq 24(%rsp), %rbx # move the potentially modified AppRsp into rbx
448 movq ASM_PFX(AppRsp), %rax
449 addq $40, %rax
450 cmpq %rax, %rbx
451 je NoAppStackMove
452
453 movq ASM_PFX(AppRsp), %rax
454 movq (%rax), %rcx # RIP
455 movq %rcx, (%rbx)
456
457 movq 8(%rax), %rcx # CS
458 movq %rcx, 8(%rbx)
459
460 movq 16(%rax), %rcx # RFLAGS
461 movq %rcx, 16(%rbx)
462
463 movq 24(%rax), %rcx # RSP
464 movq %rcx, 24(%rbx)
465
466 movq 32(%rax), %rcx # SS
467 movq %rcx, 32(%rbx)
468
469 movq %rbx, %rax # modify the saved AppRsp to the new AppRsp
470 movq %rax, ASM_PFX(AppRsp)
471 NoAppStackMove:
472 movq ASM_PFX(DebugRsp), %rax # restore the DebugRsp on the debug stack
473 # so our "pop" will not cause a stack switch
474 movq %rax, 24(%rsp)
475
476 cmpl $0x068, ASM_PFX(ExceptionNumber)
477 jne NoChain
478
479 Chain:
480
481 ## Restore rflags so when we chain, the flags will be exactly as if we were never here.
482 ## We gin up the stack to do an iretq so we can get ALL the flags.
483 movq ASM_PFX(AppRsp), %rax
484 movq 40(%rax), %rbx
485 pushq %rbx
486 movq %ss, %rax
487 pushq %rax
488 movq %rsp, %rax
489 addq $16, %rax
490 pushq %rax
491 movq ASM_PFX(AppRsp), %rax
492 movq 16(%rax), %rbx
493 andq $0xfffffffffffffcff, %rbx # special handling for IF and TF
494 pushq %rbx
495 movq %cs, %rax
496 pushq %rax
497 movq PhonyIretq, %rax
498 pushq %rax
499 iretq
500 PhonyIretq:
501
502 ## UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;
503 ## UINT64 R8, R9, R10, R11, R12, R13, R14, R15;
504 popq %rdi
505 popq %rsi
506 popq %rbp
507 popq %rsp
508 popq %rbx
509 popq %rdx
510 popq %rcx
511 popq %rax
512 popq %r8
513 popq %r9
514 popq %r10
515 popq %r11
516 popq %r12
517 popq %r13
518 popq %r14
519 popq %r15
520
521 ## Switch back to application stack
522 movq ASM_PFX(AppRsp), %rsp
523 ## Jump to original handler
524 jmp ASM_PFX(OrigVector)
525 NoChain:
526 ## UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;
527 ## UINT64 R8, R9, R10, R11, R12, R13, R14, R15;
528 popq %rdi
529 popq %rsi
530 popq %rbp
531 popq %rsp
532 popq %rbx
533 popq %rdx
534 popq %rcx
535 popq %rax
536 popq %r8
537 popq %r9
538 popq %r10
539 popq %r11
540 popq %r12
541 popq %r13
542 popq %r14
543 popq %r15
544
545 ## Switch back to application stack
546 movq ASM_PFX(AppRsp), %rsp
547
548 ## We're outa here...
549 iret