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