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 %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)
36 db 0x6a ; push #VectorNum
37 db ($ - AsmIdtVectorBegin) / ((AsmIdtVectorEnd - AsmIdtVectorBegin) / 32) ; VectorNum
39 mov rax, strict qword 0 ; mov rax, ASM_PFX(CommonInterruptEntry)
44 HookAfterStubHeaderBegin:
47 db 0 ; 0 will be fixed
49 mov rax, strict qword 0 ; 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;
261 db 0xf, 0xae, 0x7 ;fxsave [rdi]
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 call ASM_PFX(CommonExceptionHandler)
281 ;; UINT64 ExceptionData;
284 ;; FX_SAVE_STATE_X64 FxSaveState;
287 db 0xf, 0xae, 0xE ; fxrstor [rsi]
290 ;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
291 ;; Skip restoration of DRx registers to support in-circuit emualators
292 ;; or debuggers set breakpoint in interrupt/exception context
295 ;; UINT64 Cr0, Cr1, Cr2, Cr3, Cr4, Cr8;
298 add rsp, 8 ; not for Cr1
312 ;; UINT64 Gdtr[2], Idtr[2];
313 ;; Best not let anyone mess with these particular registers...
319 ;; UINT64 Gs, Fs, Es, Ds, Cs, Ss;
321 ; mov gs, rax ; not for gs
323 ; mov fs, rax ; not for fs
324 ; (X64 will not use fs and gs, so we do not restore it)
329 pop qword [rbp + 32] ; for cs
330 pop qword [rbp + 56] ; for ss
332 ;; UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;
333 ;; UINT64 R8, R9, R10, R11, R12, R13, R14, R15;
336 add rsp, 8 ; not for rbp
337 pop qword [rbp + 48] ; for rsp
354 cmp qword [rsp - 32], 0 ; check EXCEPTION_HANDLER_CONTEXT.OldIdtHandler
356 cmp qword [rsp - 40], 1 ; check EXCEPTION_HANDLER_CONTEXT.ExceptionDataFlag
364 cmp qword [ASM_PFX(mDoFarReturnFlag)], 0 ; Check if need to do far return instead of IRET
367 mov rax, rsp ; save old RSP to rax
368 mov rsp, [rsp + 0x20]
369 push qword [rax + 0x10] ; save CS in new location
370 push qword [rax + 0x8] ; save EIP in new location
371 push qword [rax + 0x18] ; save EFLAGS in new location
372 mov rax, [rax] ; restore rax
373 popfq ; restore EFLAGS
374 DB 0x48 ; prefix to composite "retq" with next "retf"
379 ;-------------------------------------------------------------------------------------
380 ; GetTemplateAddressMap (&AddressMap);
381 ;-------------------------------------------------------------------------------------
382 ; comments here for definition of address map
383 global ASM_PFX(AsmGetTemplateAddressMap)
384 ASM_PFX(AsmGetTemplateAddressMap):
385 lea rax, [AsmIdtVectorBegin]
387 mov qword [rcx + 0x8], (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
388 lea rax, [HookAfterStubHeaderBegin]
389 mov qword [rcx + 0x10], rax
391 ; Fix up CommonInterruptEntry address
392 lea rax, [ASM_PFX(CommonInterruptEntry)]
393 lea rcx, [AsmIdtVectorBegin]
395 mov qword [rcx + (JmpAbsoluteAddress - 8 - HookAfterStubHeaderBegin)], rax
396 add rcx, (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
398 ; Fix up HookAfterStubHeaderEnd
399 lea rax, [HookAfterStubHeaderEnd]
400 lea rcx, [JmpAbsoluteAddress]
401 mov qword [rcx - 8], rax
405 ;-------------------------------------------------------------------------------------
406 ; AsmVectorNumFixup (*NewVectorAddr, VectorNum, *OldVectorAddr);
407 ;-------------------------------------------------------------------------------------
408 global ASM_PFX(AsmVectorNumFixup)
409 ASM_PFX(AsmVectorNumFixup):
411 mov [rcx + (@VectorNum - HookAfterStubHeaderBegin)], al