1 ;------------------------------------------------------------------------------ ;
2 ; Copyright (c) 2012 - 2022, 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 %define VC_EXCEPTION 29
23 extern ASM_PFX(mErrorCodeFlag) ; Error code flags for exceptions
24 extern ASM_PFX(mDoFarReturnFlag) ; Do far return flag
25 extern ASM_PFX(CommonExceptionHandler)
39 mov rax, ASM_PFX(CommonInterruptEntry)
41 %assign Vector Vector+1
45 HookAfterStubHeaderBegin:
48 db 0 ; 0 will be fixed
50 mov rax, HookAfterStubHeaderEnd
52 HookAfterStubHeaderEnd:
54 and sp, 0xfff0 ; make sure 16-byte aligned for exception context
55 sub rsp, 0x18 ; reserve room for filling exception data later
58 bt [ASM_PFX(mErrorCodeFlag)], ecx
60 push qword [rsp] ; push additional rcx to make stack alignment
62 xchg rcx, [rsp] ; restore rcx, save Exception Number in stack
63 push qword [rax] ; push rax into stack to keep code consistence
65 ;---------------------------------------;
66 ; CommonInterruptEntry ;
67 ;---------------------------------------;
68 ; The follow algorithm is used for the common interrupt routine.
69 ; Entry from each interrupt with a push eax and eax=interrupt number
70 ; Stack frame would be as follows as specified in IA32 manuals:
72 ; +---------------------+ <-- 16-byte aligned ensured by processor
74 ; +---------------------+
76 ; +---------------------+
78 ; +---------------------+
80 ; +---------------------+
82 ; +---------------------+
84 ; +---------------------+
86 ; +---------------------+
88 ; +---------------------+ <-- RBP, 16-byte aligned
89 ; The follow algorithm is used for the common interrupt routine.
90 global ASM_PFX(CommonInterruptEntry)
91 ASM_PFX(CommonInterruptEntry):
95 ; All interrupt handlers are invoked through interrupt gates, so
96 ; IF flag automatically cleared at the entry point
98 xchg rcx, [rsp] ; Save rcx into stack and save vector number into rcx
100 cmp ecx, 32 ; Intel reserved vector for exceptions?
102 bt [ASM_PFX(mErrorCodeFlag)], ecx
108 ; Push a dummy error code on the stack
109 ; to maintain coherent stack map
112 mov qword [rsp + 8], 0
116 push 0 ; clear EXCEPTION_HANDLER_CONTEXT.OldIdtHandler
117 push 0 ; clear EXCEPTION_HANDLER_CONTEXT.ExceptionDataFlag
121 ; +---------------------+ <-- 16-byte aligned ensured by processor
123 ; +---------------------+
125 ; +---------------------+
127 ; +---------------------+
129 ; +---------------------+
131 ; +---------------------+
133 ; +---------------------+
134 ; + RCX / Vector Number +
135 ; +---------------------+
137 ; +---------------------+ <-- RBP, 16-byte aligned
141 ; Since here the stack pointer is 16-byte aligned, so
142 ; EFI_FX_SAVE_STATE_X64 of EFI_SYSTEM_CONTEXT_x64
146 ;; UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;
147 ;; UINT64 R8, R9, R10, R11, R12, R13, R14, R15;
157 push qword [rbp + 8] ; RCX
160 push qword [rbp + 48] ; RSP
161 push qword [rbp] ; RBP
165 ;; UINT64 Gs, Fs, Es, Ds, Cs, Ss; insure high 16 bits of each is zero
166 movzx rax, word [rbp + 56]
168 movzx rax, word [rbp + 32]
179 mov [rbp + 8], rcx ; save vector number
182 push qword [rbp + 24]
184 ;; UINT64 Gdtr[2], Idtr[2];
190 mov rax, qword [rsp + 2]
192 mov word [rsp + 8], bx
199 mov rax, qword [rsp + 2]
201 mov word [rsp + 8], bx
211 push qword [rbp + 40]
213 ;; UINT64 Cr0, Cr1, Cr2, Cr3, Cr4, Cr8;
229 ;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
230 cmp qword [rbp + 8], VC_EXCEPTION
231 je VcDebugRegs ; For SEV-ES (#VC) Debug registers ignored
248 ;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7 are skipped for #VC to avoid exception recursion
258 ;; FX_SAVE_STATE_X64 FxSaveState;
263 ;; UEFI calling convention for x64 requires that Direction flag in EFLAGs is clear
266 ;; UINT32 ExceptionData;
267 push qword [rbp + 16]
269 ;; Prepare parameter and call
273 ; Per X64 calling convention, allocate maximum parameter stack space
274 ; and make sure RSP is 16-byte aligned
277 mov rax, ASM_PFX(CommonExceptionHandler)
282 ;; UINT64 ExceptionData;
285 ;; FX_SAVE_STATE_X64 FxSaveState;
291 ;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
292 ;; Skip restoration of DRx registers to support in-circuit emualators
293 ;; or debuggers set breakpoint in interrupt/exception context
296 ;; UINT64 Cr0, Cr1, Cr2, Cr3, Cr4, Cr8;
299 add rsp, 8 ; not for Cr1
313 ;; UINT64 Gdtr[2], Idtr[2];
314 ;; Best not let anyone mess with these particular registers...
320 ;; UINT64 Gs, Fs, Es, Ds, Cs, Ss;
322 ; mov gs, rax ; not for gs
324 ; mov fs, rax ; not for fs
325 ; (X64 will not use fs and gs, so we do not restore it)
330 pop qword [rbp + 32] ; for cs
331 pop qword [rbp + 56] ; for ss
333 ;; UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;
334 ;; UINT64 R8, R9, R10, R11, R12, R13, R14, R15;
337 add rsp, 8 ; not for rbp
338 pop qword [rbp + 48] ; for rsp
355 cmp qword [rsp - 32], 0 ; check EXCEPTION_HANDLER_CONTEXT.OldIdtHandler
357 cmp qword [rsp - 40], 1 ; check EXCEPTION_HANDLER_CONTEXT.ExceptionDataFlag
365 cmp qword [ASM_PFX(mDoFarReturnFlag)], 0 ; Check if need to do far return instead of IRET
368 mov rax, rsp ; save old RSP to rax
369 mov rsp, [rsp + 0x20]
370 push qword [rax + 0x10] ; save CS in new location
371 push qword [rax + 0x8] ; save EIP in new location
372 push qword [rax + 0x18] ; save EFLAGS in new location
373 mov rax, [rax] ; restore rax
374 popfq ; restore EFLAGS
379 ;-------------------------------------------------------------------------------------
380 ; GetTemplateAddressMap (&AddressMap);
381 ;-------------------------------------------------------------------------------------
382 ; comments here for definition of address map
383 global ASM_PFX(AsmGetTemplateAddressMap)
384 ASM_PFX(AsmGetTemplateAddressMap):
385 mov rax, AsmIdtVectorBegin
387 mov qword [rcx + 0x8], (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
388 mov rax, HookAfterStubHeaderBegin
389 mov qword [rcx + 0x10], rax
392 ;-------------------------------------------------------------------------------------
393 ; AsmVectorNumFixup (*NewVectorAddr, VectorNum, *OldVectorAddr);
394 ;-------------------------------------------------------------------------------------
395 global ASM_PFX(AsmVectorNumFixup)
396 ASM_PFX(AsmVectorNumFixup):
398 mov [rcx + (@VectorNum - HookAfterStubHeaderBegin)], al