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, strict qword 0 ; mov rax, ASM_PFX(CommonInterruptEntry)
42 HookAfterStubHeaderBegin:
45 db 0 ; 0 will be fixed
47 mov rax, strict qword 0 ; mov rax, HookAfterStubHeaderEnd
50 HookAfterStubHeaderEnd:
52 and sp, 0xfff0 ; make sure 16-byte aligned for exception context
53 sub rsp, 0x18 ; reserve room for filling exception data later
56 bt [ASM_PFX(mErrorCodeFlag)], ecx
58 push qword [rsp] ; push additional rcx to make stack alignment
60 xchg rcx, [rsp] ; restore rcx, save Exception Number in stack
61 push qword [rax] ; push rax into stack to keep code consistence
63 ;---------------------------------------;
64 ; CommonInterruptEntry ;
65 ;---------------------------------------;
66 ; The follow algorithm is used for the common interrupt routine.
67 ; Entry from each interrupt with a push eax and eax=interrupt number
68 ; Stack frame would be as follows as specified in IA32 manuals:
70 ; +---------------------+ <-- 16-byte aligned ensured by processor
72 ; +---------------------+
74 ; +---------------------+
76 ; +---------------------+
78 ; +---------------------+
80 ; +---------------------+
82 ; +---------------------+
84 ; +---------------------+
86 ; +---------------------+ <-- RBP, 16-byte aligned
87 ; The follow algorithm is used for the common interrupt routine.
88 global ASM_PFX(CommonInterruptEntry)
89 ASM_PFX(CommonInterruptEntry):
93 ; All interrupt handlers are invoked through interrupt gates, so
94 ; IF flag automatically cleared at the entry point
96 xchg rcx, [rsp] ; Save rcx into stack and save vector number into rcx
98 cmp ecx, 32 ; Intel reserved vector for exceptions?
100 bt [ASM_PFX(mErrorCodeFlag)], ecx
106 ; Push a dummy error code on the stack
107 ; to maintain coherent stack map
110 mov qword [rsp + 8], 0
114 push 0 ; clear EXCEPTION_HANDLER_CONTEXT.OldIdtHandler
115 push 0 ; clear EXCEPTION_HANDLER_CONTEXT.ExceptionDataFlag
119 ; +---------------------+ <-- 16-byte aligned ensured by processor
121 ; +---------------------+
123 ; +---------------------+
125 ; +---------------------+
127 ; +---------------------+
129 ; +---------------------+
131 ; +---------------------+
132 ; + RCX / Vector Number +
133 ; +---------------------+
135 ; +---------------------+ <-- RBP, 16-byte aligned
139 ; Since here the stack pointer is 16-byte aligned, so
140 ; EFI_FX_SAVE_STATE_X64 of EFI_SYSTEM_CONTEXT_x64
144 ;; UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;
145 ;; UINT64 R8, R9, R10, R11, R12, R13, R14, R15;
155 push qword [rbp + 8] ; RCX
158 push qword [rbp + 48] ; RSP
159 push qword [rbp] ; RBP
163 ;; UINT64 Gs, Fs, Es, Ds, Cs, Ss; insure high 16 bits of each is zero
164 movzx rax, word [rbp + 56]
166 movzx rax, word [rbp + 32]
177 mov [rbp + 8], rcx ; save vector number
180 push qword [rbp + 24]
182 ;; UINT64 Gdtr[2], Idtr[2];
188 mov rax, qword [rsp + 2]
190 mov word [rsp + 8], bx
197 mov rax, qword [rsp + 2]
199 mov word [rsp + 8], bx
209 push qword [rbp + 40]
211 ;; UINT64 Cr0, Cr1, Cr2, Cr3, Cr4, Cr8;
227 ;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
241 ;; FX_SAVE_STATE_X64 FxSaveState;
244 db 0xf, 0xae, 0x7 ;fxsave [rdi]
246 ;; UEFI calling convention for x64 requires that Direction flag in EFLAGs is clear
249 ;; UINT32 ExceptionData;
250 push qword [rbp + 16]
252 ;; Prepare parameter and call
256 ; Per X64 calling convention, allocate maximum parameter stack space
257 ; and make sure RSP is 16-byte aligned
260 call 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 lea rax, [AsmIdtVectorBegin]
370 mov qword [rcx + 0x8], (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
371 lea rax, [HookAfterStubHeaderBegin]
372 mov qword [rcx + 0x10], rax
374 ; Fix up CommonInterruptEntry address
375 lea rax, [ASM_PFX(CommonInterruptEntry)]
376 lea rcx, [AsmIdtVectorBegin]
378 mov qword [rcx + (JmpAbsoluteAddress - 8 - HookAfterStubHeaderBegin)], rax
379 add rcx, (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
381 ; Fix up HookAfterStubHeaderEnd
382 lea rax, [HookAfterStubHeaderEnd]
383 lea rcx, [JmpAbsoluteAddress]
384 mov qword [rcx - 8], rax
388 ;-------------------------------------------------------------------------------------
389 ; AsmVectorNumFixup (*NewVectorAddr, VectorNum, *OldVectorAddr);
390 ;-------------------------------------------------------------------------------------
391 global ASM_PFX(AsmVectorNumFixup)
392 ASM_PFX(AsmVectorNumFixup):
394 mov [rcx + (@VectorNum - HookAfterStubHeaderBegin)], al