1 ;------------------------------------------------------------------------------ ;
2 ; Copyright (c) 2012 - 2014, Intel Corporation. All rights reserved.<BR>
3 ; This program and the accompanying materials
4 ; are licensed and made available under the terms and conditions of the BSD License
5 ; which accompanies this distribution. The full text of the license may be found at
6 ; http://opensource.org/licenses/bsd-license.php.
8 ; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 ; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 ; ExceptionHandlerAsm.Asm
17 ; x64 CPU Exception Handler
21 ;------------------------------------------------------------------------------
24 ; CommonExceptionHandler()
27 extern ASM_PFX(mErrorCodeFlag) ; Error code flags for exceptions
28 extern ASM_PFX(mDoFarReturnFlag) ; Do far return flag
29 extern ASM_PFX(CommonExceptionHandler)
40 db 0x6a ; push #VectorNum
41 db ($ - AsmIdtVectorBegin) / ((AsmIdtVectorEnd - AsmIdtVectorBegin) / 32) ; VectorNum
43 mov rax, ASM_PFX(CommonInterruptEntry)
48 HookAfterStubHeaderBegin:
51 db 0 ; 0 will be fixed
53 mov rax, HookAfterStubHeaderEnd
55 HookAfterStubHeaderEnd:
57 and sp, 0xfff0 ; make sure 16-byte aligned for exception context
58 sub rsp, 0x18 ; reserve room for filling exception data later
61 bt [ASM_PFX(mErrorCodeFlag)], ecx
63 push qword [rsp] ; push additional rcx to make stack alignment
65 xchg rcx, [rsp] ; restore rcx, save Exception Number in stack
66 push qword [rax] ; push rax into stack to keep code consistence
68 ;---------------------------------------;
69 ; CommonInterruptEntry ;
70 ;---------------------------------------;
71 ; The follow algorithm is used for the common interrupt routine.
72 ; Entry from each interrupt with a push eax and eax=interrupt number
73 ; Stack frame would be as follows as specified in IA32 manuals:
75 ; +---------------------+ <-- 16-byte aligned ensured by processor
77 ; +---------------------+
79 ; +---------------------+
81 ; +---------------------+
83 ; +---------------------+
85 ; +---------------------+
87 ; +---------------------+
89 ; +---------------------+
91 ; +---------------------+ <-- RBP, 16-byte aligned
92 ; The follow algorithm is used for the common interrupt routine.
93 global ASM_PFX(CommonInterruptEntry)
94 ASM_PFX(CommonInterruptEntry):
98 ; All interrupt handlers are invoked through interrupt gates, so
99 ; IF flag automatically cleared at the entry point
101 xchg rcx, [rsp] ; Save rcx into stack and save vector number into rcx
103 cmp ecx, 32 ; Intel reserved vector for exceptions?
105 bt [ASM_PFX(mErrorCodeFlag)], ecx
111 ; Push a dummy error code on the stack
112 ; to maintain coherent stack map
115 mov qword [rsp + 8], 0
119 push 0 ; clear EXCEPTION_HANDLER_CONTEXT.OldIdtHandler
120 push 0 ; clear EXCEPTION_HANDLER_CONTEXT.ExceptionDataFlag
124 ; +---------------------+ <-- 16-byte aligned ensured by processor
126 ; +---------------------+
128 ; +---------------------+
130 ; +---------------------+
132 ; +---------------------+
134 ; +---------------------+
136 ; +---------------------+
137 ; + RCX / Vector Number +
138 ; +---------------------+
140 ; +---------------------+ <-- RBP, 16-byte aligned
144 ; Since here the stack pointer is 16-byte aligned, so
145 ; EFI_FX_SAVE_STATE_X64 of EFI_SYSTEM_CONTEXT_x64
149 ;; UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;
150 ;; UINT64 R8, R9, R10, R11, R12, R13, R14, R15;
160 push qword [rbp + 8] ; RCX
163 push qword [rbp + 48] ; RSP
164 push qword [rbp] ; RBP
168 ;; UINT64 Gs, Fs, Es, Ds, Cs, Ss; insure high 16 bits of each is zero
169 movzx rax, word [rbp + 56]
171 movzx rax, word [rbp + 32]
182 mov [rbp + 8], rcx ; save vector number
185 push qword [rbp + 24]
187 ;; UINT64 Gdtr[2], Idtr[2];
212 push qword [rbp + 40]
214 ;; UINT64 Cr0, Cr1, Cr2, Cr3, Cr4, Cr8;
230 ;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
244 ;; FX_SAVE_STATE_X64 FxSaveState;
247 db 0xf, 0xae, 0x7 ;fxsave [rdi]
249 ;; UEFI calling convention for x64 requires that Direction flag in EFLAGs is clear
252 ;; UINT32 ExceptionData;
253 push qword [rbp + 16]
255 ;; Prepare parameter and call
259 ; Per X64 calling convention, allocate maximum parameter stack space
260 ; and make sure RSP is 16-byte aligned
263 mov rax, ASM_PFX(CommonExceptionHandler)
268 ;; UINT64 ExceptionData;
271 ;; FX_SAVE_STATE_X64 FxSaveState;
274 db 0xf, 0xae, 0xE ; fxrstor [rsi]
277 ;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
278 ;; Skip restoration of DRx registers to support in-circuit emualators
279 ;; or debuggers set breakpoint in interrupt/exception context
282 ;; UINT64 Cr0, Cr1, Cr2, Cr3, Cr4, Cr8;
285 add rsp, 8 ; not for Cr1
299 ;; UINT64 Gdtr[2], Idtr[2];
300 ;; Best not let anyone mess with these particular registers...
306 ;; UINT64 Gs, Fs, Es, Ds, Cs, Ss;
308 ; mov gs, rax ; not for gs
310 ; mov fs, rax ; not for fs
311 ; (X64 will not use fs and gs, so we do not restore it)
316 pop qword [rbp + 32] ; for cs
317 pop qword [rbp + 56] ; for ss
319 ;; UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;
320 ;; UINT64 R8, R9, R10, R11, R12, R13, R14, R15;
323 add rsp, 8 ; not for rbp
324 pop qword [rbp + 48] ; for rsp
341 cmp qword [rsp - 32], 0 ; check EXCEPTION_HANDLER_CONTEXT.OldIdtHandler
343 cmp qword [rsp - 40], 1 ; check EXCEPTION_HANDLER_CONTEXT.ExceptionDataFlag
351 cmp qword [ASM_PFX(mDoFarReturnFlag)], 0 ; Check if need to do far return instead of IRET
354 mov rax, rsp ; save old RSP to rax
355 mov rsp, [rsp + 0x20]
356 push qword [rax + 0x10] ; save CS in new location
357 push qword [rax + 0x8] ; save EIP in new location
358 push qword [rax + 0x18] ; save EFLAGS in new location
359 mov rax, [rax] ; restore rax
360 popfq ; restore EFLAGS
361 DB 0x48 ; prefix to composite "retq" with next "retf"
366 ;-------------------------------------------------------------------------------------
367 ; GetTemplateAddressMap (&AddressMap);
368 ;-------------------------------------------------------------------------------------
369 ; comments here for definition of address map
370 global ASM_PFX(AsmGetTemplateAddressMap)
371 ASM_PFX(AsmGetTemplateAddressMap):
372 mov rax, AsmIdtVectorBegin
374 mov qword [rcx + 0x8], (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
375 mov rax, HookAfterStubHeaderBegin
376 mov qword [rcx + 0x10], rax
379 ;-------------------------------------------------------------------------------------
380 ; AsmVectorNumFixup (*NewVectorAddr, VectorNum, *OldVectorAddr);
381 ;-------------------------------------------------------------------------------------
382 global ASM_PFX(AsmVectorNumFixup)
383 ASM_PFX(AsmVectorNumFixup):
385 mov [rcx + (@VectorNum - HookAfterStubHeaderBegin)], al