2 # Low leve IA32 specific debug support functions.
4 # Copyright (c) 2006 - 2008, Intel Corporation. <BR>
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
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.
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)
21 ASM_PFX(StubSize): .long ASM_PFX(InterruptEntryStubEnd) - ASM_PFX(InterruptEntryStub)
22 ASM_PFX(AppEsp): .long 0x11111111 # ?
23 ASM_PFX(DebugEsp): .long 0x22222222 # ?
24 ASM_PFX(ExtraPush): .long 0x33333333 # ?
25 ASM_PFX(ExceptData): .long 0x44444444 # ?
26 ASM_PFX(Eflags): .long 0x55555555 # ?
27 ASM_PFX(OrigVector): .long 0x66666666 # ?
29 #------------------------------------------------------------------------------
35 # Abstract: Returns TRUE if FxStor instructions are supported
37 ASM_GLOBAL ASM_PFX(FxStorSupport)
38 ASM_PFX(FxStorSupport):
40 # cpuid corrupts ebx which must be preserved per the C calling convention
50 #------------------------------------------------------------------------------
53 # DESCRIPTOR * DestDesc,
54 # void (*Vector) (void)
57 # Abstract: Encodes an IDT descriptor with the given physical address
60 ASM_GLOBAL ASM_PFX(Vect2Desc)
68 movw $0x8e00,0x4(%ecx)
74 ASM_GLOBAL ASM_PFX(InterruptEntryStub)
75 ASM_PFX(InterruptEntryStub):
76 mov %esp,0x0 # save stack top
77 mov $0x0,%esp # switch to debugger stack
78 push $0x0 # push vector number - will be modified before installed
79 jmp ASM_PFX(CommonIdtEntry) # jump CommonIdtEntry
80 ASM_GLOBAL ASM_PFX(InterruptEntryStubEnd)
81 ASM_PFX(InterruptEntryStubEnd):
83 #------------------------------------------------------------------------------
86 # Abstract: This code is not a function, but is the common part for all IDT
89 ASM_GLOBAL ASM_PFX(CommonIdtEntry)
90 ASM_PFX(CommonIdtEntry):
92 ## At this point, the stub has saved the current application stack esp into AppEsp
93 ## and switched stacks to the debug stack, where it pushed the vector number
95 ## The application stack looks like this:
98 ## (last application stack entry)
99 ## eflags from interrupted task
100 ## CS from interrupted task
101 ## EIP from interrupted task
102 ## Error code <-------------------- Only present for some exeption types
107 ## The stub switched us to the debug stack and pushed the interrupt number.
109 ## Next, construct the context record. It will be build on the debug stack by
110 ## pushing the registers in the correct order so as to create the context structure
111 ## on the debug stack. The context record must be built from the end back to the
112 ## beginning because the stack grows down...
114 ## For reference, the context record looks like this:
118 ## UINT32 ExceptionData;
119 ## FX_SAVE_STATE_IA32 FxSaveState;
120 ## UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
121 ## UINT32 Cr0, Cr2, Cr3, Cr4;
124 ## UINT32 Gdtr[2], Idtr[2];
126 ## UINT32 Gs, Fs, Es, Ds, Cs, Ss;
127 ## UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;
128 ## } SYSTEM_CONTEXT_IA32; // 32 bit system context record
130 ## UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;
132 ## Save interrupt state eflags register...
135 ## We need to determine if any extra data was pushed by the exception, and if so, save it
136 ## To do this, we check the exception number pushed by the stub, and cache the
137 ## result in a variable since we'll need this again.
140 jne ASM_PFX(CommonIdtEntry+0x20)
142 jmp ASM_PFX(CommonIdtEntry+0xa8)
144 jne ASM_PFX(CommonIdtEntry+0x35)
146 jmp ASM_PFX(CommonIdtEntry+0xa8)
148 jne ASM_PFX(CommonIdtEntry+0x4a)
150 jmp ASM_PFX(CommonIdtEntry+0xa8)
152 jne ASM_PFX(CommonIdtEntry+0x5f)
154 jmp ASM_PFX(CommonIdtEntry+0xa8)
156 jne ASM_PFX(CommonIdtEntry+0x74)
158 jmp ASM_PFX(CommonIdtEntry+0xa8)
160 jne ASM_PFX(CommonIdtEntry+0x89)
162 jmp ASM_PFX(CommonIdtEntry+0xa8)
164 jne ASM_PFX(CommonIdtEntry+0x9e)
166 jmp ASM_PFX(CommonIdtEntry+0xa8)
168 ## If there's some extra data, save it also, and modify the saved AppEsp to effectively
169 ## pop this value off the application's stack.
172 jne ASM_PFX(CommonIdtEntry+0xc8)
178 jmp ASM_PFX(CommonIdtEntry+0xd2)
180 ## The "pushad" above pushed the debug stack esp. Since what we're actually doing
181 ## is building the context record on the debug stack, we need to save the pushed
182 ## debug ESP, and replace it with the application's last stack entry...
187 # application stack has eflags, cs, & eip, so
188 # last actual application stack entry is
189 # 12 bytes into the application stack.
191 ## continue building context record
192 ## UINT32 Gs, Fs, Es, Ds, Cs, Ss; insure high 16 bits of each is zero
196 # CS from application is one entry back in application stack
198 movzwl 0x4(%eax),%eax
210 # Eip from application is on top of application stack
214 ## UINT32 Gdtr[2], Idtr[2];
230 ## Eflags from application is two entries back in application stack
234 ## UINT32 Cr0, Cr1, Cr2, Cr3, Cr4;
235 ## insure FXSAVE/FXRSTOR is enabled in CR4...
236 ## ... while we're at it, make sure DE is also enabled...
249 ## UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
253 ## clear Dr7 while executing debugger itself
259 ## insure all status bits in dr6 are clear...
271 ## FX_SAVE_STATE_IA32 FxSaveState;
274 # IMPORTANT!! The debug stack has been carefully constructed to
275 # insure that esp and edi are 16 byte aligned when we get here.
276 # They MUST be. If they are not, a GP fault will occur.
279 ## UINT32 ExceptionData;
283 # call to C code which will in turn call registered handler
284 # pass in the vector number
289 call ASM_PFX(CommonIdtEntry+0x184)
293 ## UINT32 ExceptionData;
296 ## FX_SAVE_STATE_IA32 FxSaveState;
301 ## UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
311 ## skip restore of dr6. We cleared dr6 during the context save.
316 ## UINT32 Cr0, Cr1, Cr2, Cr3, Cr4;
332 ## UINT32 Gdtr[2], Idtr[2];
333 ## Best not let anyone mess with these particular registers...
339 ## UINT32 SegGs, SegFs, SegEs, SegDs, SegCs, SegSs;
340 ## NOTE - modified segment registers could hang the debugger... We
341 ## could attempt to insulate ourselves against this possibility,
342 ## but that poses risks as well.
353 ## The next stuff to restore is the general purpose registers that were pushed
354 ## using the "pushad" instruction.
356 ## The value of ESP as stored in the context record is the application ESP
357 ## including the 3 entries on the application stack caused by the exception
358 ## itself. It may have been modified by the debug agent, so we need to
359 ## determine if we need to relocate the application stack.
361 mov 0x0,%eax # move the potentially modified AppEsp into ebx
364 je ASM_PFX(CommonIdtEntry+0x202)
366 mov (%eax),%ecx # EIP
368 mov 0x4(%eax),%ecx # CS
370 mov 0x8(%eax),%ecx # EFLAGS
373 mov %ebx,%eax # modify the saved AppEsp to the new AppEsp
375 mov 0x0,%eax # restore the DebugEsp on the debug stack
376 # so our "popad" will not cause a stack switch
380 ## Restore eflags so when we chain, the flags will be exactly as if we were never here.
381 ## We gin up the stack to do an iretd so we can get ALL the flags.
384 and $0xfffffcff,%ebx # special handling for IF and TF
391 ## UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;
394 ## Switch back to application stack
397 ## Jump to original handler
398 ## UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;
400 ## Switch back to application stack
403 ## We're outa here...