1 ;------------------------------------------------------------------------------ ;
2 ; Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>
3 ; SPDX-License-Identifier: BSD-2-Clause-Patent
7 ; ExceptionHandlerAsm.Asm
11 ; x64 CPU Exception Handler
15 ;------------------------------------------------------------------------------
18 ; CommonExceptionHandler()
21 extern ASM_PFX(mErrorCodeFlag) ; Error code flags for exceptions
22 extern ASM_PFX(mDoFarReturnFlag) ; Do far return flag
23 extern ASM_PFX(CommonExceptionHandler)
34 db 0x6a ; push #VectorNum
35 db ($ - AsmIdtVectorBegin) / ((AsmIdtVectorEnd - AsmIdtVectorBegin) / 32) ; VectorNum
37 mov rax, ASM_PFX(CommonInterruptEntry)
42 HookAfterStubHeaderBegin:
45 db 0 ; 0 will be fixed
47 mov rax, HookAfterStubHeaderEnd
49 HookAfterStubHeaderEnd:
51 and sp, 0xfff0 ; make sure 16-byte aligned for exception context
52 sub rsp, 0x18 ; reserve room for filling exception data later
55 bt [ASM_PFX(mErrorCodeFlag)], ecx
57 push qword [rsp] ; push additional rcx to make stack alignment
59 xchg rcx, [rsp] ; restore rcx, save Exception Number in stack
60 push qword [rax] ; push rax into stack to keep code consistence
62 ;---------------------------------------;
63 ; CommonInterruptEntry ;
64 ;---------------------------------------;
65 ; The follow algorithm is used for the common interrupt routine.
66 ; Entry from each interrupt with a push eax and eax=interrupt number
67 ; Stack frame would be as follows as specified in IA32 manuals:
69 ; +---------------------+ <-- 16-byte aligned ensured by processor
71 ; +---------------------+
73 ; +---------------------+
75 ; +---------------------+
77 ; +---------------------+
79 ; +---------------------+
81 ; +---------------------+
83 ; +---------------------+
85 ; +---------------------+ <-- RBP, 16-byte aligned
86 ; The follow algorithm is used for the common interrupt routine.
87 global ASM_PFX(CommonInterruptEntry)
88 ASM_PFX(CommonInterruptEntry):
92 ; All interrupt handlers are invoked through interrupt gates, so
93 ; IF flag automatically cleared at the entry point
95 xchg rcx, [rsp] ; Save rcx into stack and save vector number into rcx
97 cmp ecx, 32 ; Intel reserved vector for exceptions?
99 bt [ASM_PFX(mErrorCodeFlag)], ecx
105 ; Push a dummy error code on the stack
106 ; to maintain coherent stack map
109 mov qword [rsp + 8], 0
113 push 0 ; clear EXCEPTION_HANDLER_CONTEXT.OldIdtHandler
114 push 0 ; clear EXCEPTION_HANDLER_CONTEXT.ExceptionDataFlag
118 ; +---------------------+ <-- 16-byte aligned ensured by processor
120 ; +---------------------+
122 ; +---------------------+
124 ; +---------------------+
126 ; +---------------------+
128 ; +---------------------+
130 ; +---------------------+
131 ; + RCX / Vector Number +
132 ; +---------------------+
134 ; +---------------------+ <-- RBP, 16-byte aligned
138 ; Since here the stack pointer is 16-byte aligned, so
139 ; EFI_FX_SAVE_STATE_X64 of EFI_SYSTEM_CONTEXT_x64
143 ;; UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;
144 ;; UINT64 R8, R9, R10, R11, R12, R13, R14, R15;
154 push qword [rbp + 8] ; RCX
157 push qword [rbp + 48] ; RSP
158 push qword [rbp] ; RBP
162 ;; UINT64 Gs, Fs, Es, Ds, Cs, Ss; insure high 16 bits of each is zero
163 movzx rax, word [rbp + 56]
165 movzx rax, word [rbp + 32]
176 mov [rbp + 8], rcx ; save vector number
179 push qword [rbp + 24]
181 ;; UINT64 Gdtr[2], Idtr[2];
187 mov rax, qword [rsp + 2]
189 mov word [rsp + 8], bx
196 mov rax, qword [rsp + 2]
198 mov word [rsp + 8], bx
208 push qword [rbp + 40]
210 ;; UINT64 Cr0, Cr1, Cr2, Cr3, Cr4, Cr8;
226 ;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
240 ;; FX_SAVE_STATE_X64 FxSaveState;
243 db 0xf, 0xae, 0x7 ;fxsave [rdi]
245 ;; UEFI calling convention for x64 requires that Direction flag in EFLAGs is clear
248 ;; UINT32 ExceptionData;
249 push qword [rbp + 16]
251 ;; Prepare parameter and call
255 ; Per X64 calling convention, allocate maximum parameter stack space
256 ; and make sure RSP is 16-byte aligned
259 mov rax, ASM_PFX(CommonExceptionHandler)
264 ;; UINT64 ExceptionData;
267 ;; FX_SAVE_STATE_X64 FxSaveState;
270 db 0xf, 0xae, 0xE ; fxrstor [rsi]
273 ;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
274 ;; Skip restoration of DRx registers to support in-circuit emualators
275 ;; or debuggers set breakpoint in interrupt/exception context
278 ;; UINT64 Cr0, Cr1, Cr2, Cr3, Cr4, Cr8;
281 add rsp, 8 ; not for Cr1
295 ;; UINT64 Gdtr[2], Idtr[2];
296 ;; Best not let anyone mess with these particular registers...
302 ;; UINT64 Gs, Fs, Es, Ds, Cs, Ss;
304 ; mov gs, rax ; not for gs
306 ; mov fs, rax ; not for fs
307 ; (X64 will not use fs and gs, so we do not restore it)
312 pop qword [rbp + 32] ; for cs
313 pop qword [rbp + 56] ; for ss
315 ;; UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;
316 ;; UINT64 R8, R9, R10, R11, R12, R13, R14, R15;
319 add rsp, 8 ; not for rbp
320 pop qword [rbp + 48] ; for rsp
337 cmp qword [rsp - 32], 0 ; check EXCEPTION_HANDLER_CONTEXT.OldIdtHandler
339 cmp qword [rsp - 40], 1 ; check EXCEPTION_HANDLER_CONTEXT.ExceptionDataFlag
347 cmp qword [ASM_PFX(mDoFarReturnFlag)], 0 ; Check if need to do far return instead of IRET
350 mov rax, rsp ; save old RSP to rax
351 mov rsp, [rsp + 0x20]
352 push qword [rax + 0x10] ; save CS in new location
353 push qword [rax + 0x8] ; save EIP in new location
354 push qword [rax + 0x18] ; save EFLAGS in new location
355 mov rax, [rax] ; restore rax
356 popfq ; restore EFLAGS
357 DB 0x48 ; prefix to composite "retq" with next "retf"
362 ;-------------------------------------------------------------------------------------
363 ; GetTemplateAddressMap (&AddressMap);
364 ;-------------------------------------------------------------------------------------
365 ; comments here for definition of address map
366 global ASM_PFX(AsmGetTemplateAddressMap)
367 ASM_PFX(AsmGetTemplateAddressMap):
368 mov rax, AsmIdtVectorBegin
370 mov qword [rcx + 0x8], (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
371 mov rax, HookAfterStubHeaderBegin
372 mov qword [rcx + 0x10], rax
375 ;-------------------------------------------------------------------------------------
376 ; AsmVectorNumFixup (*NewVectorAddr, VectorNum, *OldVectorAddr);
377 ;-------------------------------------------------------------------------------------
378 global ASM_PFX(AsmVectorNumFixup)
379 ASM_PFX(AsmVectorNumFixup):
381 mov [rcx + (@VectorNum - HookAfterStubHeaderBegin)], al