1 ;******************************************************************************
3 ;* Copyright (c) 2006, Intel Corporation
4 ;* All rights reserved. This program and the accompanying materials
5 ;* are licensed and made available under the terms and conditions of the BSD License
6 ;* which accompanies this distribution. The full text of the license may be found at
7 ;* http://opensource.org/licenses/bsd-license.php
9 ;* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 ;* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 ;******************************************************************************
17 EXCPT32_DIVIDE_ERROR EQU 0
20 EXCPT32_BREAKPOINT EQU 3
21 EXCPT32_OVERFLOW EQU 4
23 EXCPT32_INVALID_OPCODE EQU 6
24 EXCPT32_DOUBLE_FAULT EQU 8
25 EXCPT32_INVALID_TSS EQU 10
26 EXCPT32_SEG_NOT_PRESENT EQU 11
27 EXCPT32_STACK_FAULT EQU 12
28 EXCPT32_GP_FAULT EQU 13
29 EXCPT32_PAGE_FAULT EQU 14
30 EXCPT32_FP_ERROR EQU 16
31 EXCPT32_ALIGNMENT_CHECK EQU 17
32 EXCPT32_MACHINE_CHECK EQU 18
35 FXSTOR_FLAG EQU 01000000h ; bit cpuid 24 of feature flags
37 ;; The FXSTOR and FXRSTOR commands are used for saving and restoring the x87,
38 ;; MMX, SSE, SSE2, etc registers. The initialization of the debugsupport driver
39 ;; MUST check the CPUID feature flags to see that these instructions are available
40 ;; and fail to init if they are not.
44 db 0fh, 0aeh, 00000111y ; mod = 00, reg/op = 000, r/m = 111 = [edi]
49 db 0fh, 0aeh, 00001110y ; mod = 00, reg/op = 001, r/m = 110 = [esi]
53 public OrigVector, InterruptEntryStub, StubSize, CommonIdtEntry, FxStorSupport
55 StubSize dd InterruptEntryStubEnd - InterruptEntryStub
56 AppEsp dd 11111111h ; ?
57 DebugEsp dd 22222222h ; ?
58 ExtraPush dd 33333333h ; ?
59 ExceptData dd 44444444h ; ?
60 Eflags dd 55555555h ; ?
61 OrigVector dd 66666666h ; ?
63 ;; The declarations below define the memory region that will be used for the debug stack.
64 ;; The context record will be built by pushing register values onto this stack.
65 ;; It is imparitive that alignment be carefully managed, since the FXSTOR and
66 ;; FXRSTOR instructions will GP fault if their memory operand is not 16 byte aligned.
68 ;; The stub will switch stacks from the application stack to the debuger stack
69 ;; and pushes the exception number.
71 ;; Then we building the context record on the stack. Since the stack grows down,
72 ;; we push the fields of the context record from the back to the front. There
73 ;; are 132 bytes of stack used prior allocating the 512 bytes of stack to be
74 ;; used as the memory buffer for the fxstor instruction. Therefore address of
75 ;; the buffer used for the FXSTOR instruction is &Eax - 132 - 512, which
76 ;; must be 16 byte aligned.
78 ;; We carefully locate the stack to make this happen.
80 ;; For reference, the context structure looks like this:
82 ;; UINT32 ExceptionData;
83 ;; FX_SAVE_STATE_IA32 FxSaveState; // 512 bytes, must be 16 byte aligned
84 ;; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
85 ;; UINT32 Cr0, Cr1, Cr2, Cr3, Cr4;
88 ;; UINT32 Gdtr[2], Idtr[2];
90 ;; UINT32 Gs, Fs, Es, Ds, Cs, Ss;
91 ;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;
92 ;; } SYSTEM_CONTEXT_IA32; // 32 bit system context record
96 DebugStackEnd db "DbgStkEnd >>>>>>" ;; 16 byte long string - must be 16 bytes to preserve alignment
97 dd 1ffdh dup (000000000h) ;; 32K should be enough stack
98 ;; This allocation is coocked to insure
99 ;; that the the buffer for the FXSTORE instruction
100 ;; will be 16 byte aligned also.
102 ExceptionNumber dd ? ;; first entry will be the vector number pushed by the stub
104 DebugStackBegin db "<<<< DbgStkBegin" ;; initial debug ESP == DebugStackBegin, set in stub
108 externdef InterruptDistrubutionHub:near
110 ;------------------------------------------------------------------------------
116 ; Abstract: Returns TRUE if FxStor instructions are supported
118 FxStorSupport PROC C PUBLIC
121 ; cpuid corrupts ebx which must be preserved per the C calling convention
134 ;------------------------------------------------------------------------------
140 ; Abstract: Returns physical address of IDTR
142 GetIdtr PROC C PUBLIC
146 mov eax, DWORD PTR IdtrBuf + 2
151 ;------------------------------------------------------------------------------
153 ; WriteInterruptFlag (
157 ; Abstract: Programs interrupt flag to the requested state and returns previous
160 WriteInterruptFlag PROC C PUBLIC State:DWORD
174 WriteInterruptFlag ENDP
178 ;------------------------------------------------------------------------------
181 ; DESCRIPTOR * DestDesc,
182 ; void (*Vector) (void)
185 ; Abstract: Encodes an IDT descriptor with the given physical address
187 Vect2Desc PROC C PUBLIC DestPtr:DWORD, Vector:DWORD
191 mov word ptr [ecx], ax ; write bits 15..0 of offset
193 mov word ptr [ecx+2], dx ; SYS_CODE_SEL from GDT
194 mov word ptr [ecx+4], 0e00h OR 8000h ; type = 386 interrupt gate, present
196 mov word ptr [ecx+6], ax ; write bits 31..16 of offset
204 ;------------------------------------------------------------------------------
207 ; Abstract: This code is not a function, but is a small piece of code that is
208 ; copied and fixed up once for each IDT entry that is hooked.
211 mov AppEsp, esp ; save stack top
212 mov esp, offset DebugStackBegin ; switch to debugger stack
213 push 0 ; push vector number - will be modified before installed
215 dd 0 ; fixed up to relative address of CommonIdtEntry
216 InterruptEntryStubEnd:
220 ;------------------------------------------------------------------------------
223 ; Abstract: This code is not a function, but is the common part for all IDT
228 ;; At this point, the stub has saved the current application stack esp into AppEsp
229 ;; and switched stacks to the debug stack, where it pushed the vector number
231 ;; The application stack looks like this:
234 ;; (last application stack entry)
235 ;; eflags from interrupted task
236 ;; CS from interrupted task
237 ;; EIP from interrupted task
238 ;; Error code <-------------------- Only present for some exeption types
243 ;; The stub switched us to the debug stack and pushed the interrupt number.
245 ;; Next, construct the context record. It will be build on the debug stack by
246 ;; pushing the registers in the correct order so as to create the context structure
247 ;; on the debug stack. The context record must be built from the end back to the
248 ;; beginning because the stack grows down...
250 ;; For reference, the context record looks like this:
254 ;; UINT32 ExceptionData;
255 ;; FX_SAVE_STATE_IA32 FxSaveState;
256 ;; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
257 ;; UINT32 Cr0, Cr2, Cr3, Cr4;
260 ;; UINT32 Gdtr[2], Idtr[2];
262 ;; UINT32 Gs, Fs, Es, Ds, Cs, Ss;
263 ;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;
264 ;; } SYSTEM_CONTEXT_IA32; // 32 bit system context record
266 ;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;
269 ;; Save interrupt state eflags register...
272 mov dword ptr Eflags, eax
274 ;; We need to determine if any extra data was pushed by the exception, and if so, save it
275 ;; To do this, we check the exception number pushed by the stub, and cache the
276 ;; result in a variable since we'll need this again.
277 .IF ExceptionNumber == EXCPT32_DOUBLE_FAULT
279 .ELSEIF ExceptionNumber == EXCPT32_INVALID_TSS
281 .ELSEIF ExceptionNumber == EXCPT32_SEG_NOT_PRESENT
283 .ELSEIF ExceptionNumber == EXCPT32_STACK_FAULT
285 .ELSEIF ExceptionNumber == EXCPT32_GP_FAULT
287 .ELSEIF ExceptionNumber == EXCPT32_PAGE_FAULT
289 .ELSEIF ExceptionNumber == EXCPT32_ALIGNMENT_CHECK
295 ;; If there's some extra data, save it also, and modify the saved AppEsp to effectively
296 ;; pop this value off the application's stack.
307 ;; The "pushad" above pushed the debug stack esp. Since what we're actually doing
308 ;; is building the context record on the debug stack, we need to save the pushed
309 ;; debug ESP, and replace it with the application's last stack entry...
314 ; application stack has eflags, cs, & eip, so
315 ; last actual application stack entry is
316 ; 12 bytes into the application stack.
319 ;; continue building context record
320 ;; UINT32 Gs, Fs, Es, Ds, Cs, Ss; insure high 16 bits of each is zero
324 ; CS from application is one entry back in application stack
326 movzx eax, word ptr [eax + 4]
339 ; Eip from application is on top of application stack
343 ;; UINT32 Gdtr[2], Idtr[2];
359 ;; Eflags from application is two entries back in application stack
361 push dword ptr [eax + 8]
363 ;; UINT32 Cr0, Cr1, Cr2, Cr3, Cr4;
364 ;; insure FXSAVE/FXRSTOR is enabled in CR4...
365 ;; ... while we're at it, make sure DE is also enabled...
378 ;; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
381 ;; clear Dr7 while executing debugger itself
387 ;; insure all status bits in dr6 are clear...
400 ;; FX_SAVE_STATE_IA32 FxSaveState;
403 ; IMPORTANT!! The debug stack has been carefully constructed to
404 ; insure that esp and edi are 16 byte aligned when we get here.
405 ; They MUST be. If they are not, a GP fault will occur.
408 ;; UINT32 ExceptionData;
412 ; call to C code which will in turn call registered handler
413 ; pass in the vector number
416 mov eax, ExceptionNumber
418 call InterruptDistrubutionHub
422 ;; UINT32 ExceptionData;
425 ;; FX_SAVE_STATE_IA32 FxSaveState;
430 ;; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
439 ;; skip restore of dr6. We cleared dr6 during the context save.
444 ;; UINT32 Cr0, Cr1, Cr2, Cr3, Cr4;
457 pop dword ptr [eax + 8]
460 ;; UINT32 Gdtr[2], Idtr[2];
461 ;; Best not let anyone mess with these particular registers...
467 ;; UINT32 SegGs, SegFs, SegEs, SegDs, SegCs, SegSs;
468 ;; NOTE - modified segment registers could hang the debugger... We
469 ;; could attempt to insulate ourselves against this possibility,
470 ;; but that poses risks as well.
480 ;; The next stuff to restore is the general purpose registers that were pushed
481 ;; using the "pushad" instruction.
483 ;; The value of ESP as stored in the context record is the application ESP
484 ;; including the 3 entries on the application stack caused by the exception
485 ;; itself. It may have been modified by the debug agent, so we need to
486 ;; determine if we need to relocate the application stack.
488 mov ebx, [esp + 12] ; move the potentially modified AppEsp into ebx
498 mov ecx, [eax + 4] ; CS
501 mov ecx, [eax + 8] ; EFLAGS
504 mov eax, ebx ; modify the saved AppEsp to the new AppEsp
507 mov eax, DebugEsp ; restore the DebugEsp on the debug stack
508 ; so our "popad" will not cause a stack switch
511 cmp ExceptionNumber, 068h
516 ;; Restore eflags so when we chain, the flags will be exactly as if we were never here.
517 ;; We gin up the stack to do an iretd so we can get ALL the flags.
520 and ebx, NOT 300h ; special handling for IF and TF
527 ;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;
530 ;; Switch back to application stack
533 ;; Jump to original handler
537 ;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;
540 ;; Switch back to application stack
543 ;; We're outa here...