1 ;------------------------------------------------------------------------------ ;
2 ; Copyright (c) 2012 - 2018, 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, strict qword 0 ; mov rax, ASM_PFX(CommonInterruptEntry)
48 HookAfterStubHeaderBegin:
51 db 0 ; 0 will be fixed
53 mov rax, strict qword 0 ; mov rax, HookAfterStubHeaderEnd
56 HookAfterStubHeaderEnd:
58 and sp, 0xfff0 ; make sure 16-byte aligned for exception context
59 sub rsp, 0x18 ; reserve room for filling exception data later
62 bt [ASM_PFX(mErrorCodeFlag)], ecx
64 push qword [rsp] ; push additional rcx to make stack alignment
66 xchg rcx, [rsp] ; restore rcx, save Exception Number in stack
67 push qword [rax] ; push rax into stack to keep code consistence
69 ;---------------------------------------;
70 ; CommonInterruptEntry ;
71 ;---------------------------------------;
72 ; The follow algorithm is used for the common interrupt routine.
73 ; Entry from each interrupt with a push eax and eax=interrupt number
74 ; Stack frame would be as follows as specified in IA32 manuals:
76 ; +---------------------+ <-- 16-byte aligned ensured by processor
78 ; +---------------------+
80 ; +---------------------+
82 ; +---------------------+
84 ; +---------------------+
86 ; +---------------------+
88 ; +---------------------+
90 ; +---------------------+
92 ; +---------------------+ <-- RBP, 16-byte aligned
93 ; The follow algorithm is used for the common interrupt routine.
94 global ASM_PFX(CommonInterruptEntry)
95 ASM_PFX(CommonInterruptEntry):
99 ; All interrupt handlers are invoked through interrupt gates, so
100 ; IF flag automatically cleared at the entry point
102 xchg rcx, [rsp] ; Save rcx into stack and save vector number into rcx
104 cmp ecx, 32 ; Intel reserved vector for exceptions?
106 bt [ASM_PFX(mErrorCodeFlag)], ecx
112 ; Push a dummy error code on the stack
113 ; to maintain coherent stack map
116 mov qword [rsp + 8], 0
120 push 0 ; clear EXCEPTION_HANDLER_CONTEXT.OldIdtHandler
121 push 0 ; clear EXCEPTION_HANDLER_CONTEXT.ExceptionDataFlag
125 ; +---------------------+ <-- 16-byte aligned ensured by processor
127 ; +---------------------+
129 ; +---------------------+
131 ; +---------------------+
133 ; +---------------------+
135 ; +---------------------+
137 ; +---------------------+
138 ; + RCX / Vector Number +
139 ; +---------------------+
141 ; +---------------------+ <-- RBP, 16-byte aligned
145 ; Since here the stack pointer is 16-byte aligned, so
146 ; EFI_FX_SAVE_STATE_X64 of EFI_SYSTEM_CONTEXT_x64
150 ;; UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;
151 ;; UINT64 R8, R9, R10, R11, R12, R13, R14, R15;
161 push qword [rbp + 8] ; RCX
164 push qword [rbp + 48] ; RSP
165 push qword [rbp] ; RBP
169 ;; UINT64 Gs, Fs, Es, Ds, Cs, Ss; insure high 16 bits of each is zero
170 movzx rax, word [rbp + 56]
172 movzx rax, word [rbp + 32]
183 mov [rbp + 8], rcx ; save vector number
186 push qword [rbp + 24]
188 ;; UINT64 Gdtr[2], Idtr[2];
213 push qword [rbp + 40]
215 ;; UINT64 Cr0, Cr1, Cr2, Cr3, Cr4, Cr8;
231 ;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
245 ;; FX_SAVE_STATE_X64 FxSaveState;
248 db 0xf, 0xae, 0x7 ;fxsave [rdi]
250 ;; UEFI calling convention for x64 requires that Direction flag in EFLAGs is clear
253 ;; UINT32 ExceptionData;
254 push qword [rbp + 16]
256 ;; Prepare parameter and call
260 ; Per X64 calling convention, allocate maximum parameter stack space
261 ; and make sure RSP is 16-byte aligned
264 call 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 lea rax, [AsmIdtVectorBegin]
374 mov qword [rcx + 0x8], (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
375 lea rax, [HookAfterStubHeaderBegin]
376 mov qword [rcx + 0x10], rax
378 ; Fix up CommonInterruptEntry address
379 lea rax, [ASM_PFX(CommonInterruptEntry)]
380 lea rcx, [AsmIdtVectorBegin]
382 mov qword [rcx + (JmpAbsoluteAddress - 8 - HookAfterStubHeaderBegin)], rax
383 add rcx, (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
385 ; Fix up HookAfterStubHeaderEnd
386 lea rax, [HookAfterStubHeaderEnd]
387 lea rcx, [JmpAbsoluteAddress]
388 mov qword [rcx - 8], rax
392 ;-------------------------------------------------------------------------------------
393 ; AsmVectorNumFixup (*NewVectorAddr, VectorNum, *OldVectorAddr);
394 ;-------------------------------------------------------------------------------------
395 global ASM_PFX(AsmVectorNumFixup)
396 ASM_PFX(AsmVectorNumFixup):
398 mov [rcx + (@VectorNum - HookAfterStubHeaderBegin)], al