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 ;------------------------------------------------------------------------------
19 ; CommonExceptionHandler()
22 %define VC_EXCEPTION 29
24 extern ASM_PFX(mErrorCodeFlag) ; Error code flags for exceptions
25 extern ASM_PFX(mDoFarReturnFlag) ; Do far return flag
26 extern ASM_PFX(CommonExceptionHandler)
27 extern ASM_PFX(FeaturePcdGet (PcdCpuSmmStackGuard))
38 db 0x6a ; push #VectorNum
39 db ($ - AsmIdtVectorBegin) / ((AsmIdtVectorEnd - AsmIdtVectorBegin) / 32) ; VectorNum
41 mov rax, strict qword 0 ; mov rax, ASM_PFX(CommonInterruptEntry)
46 HookAfterStubHeaderBegin:
49 db 0 ; 0 will be fixed
51 mov rax, strict qword 0 ; mov rax, HookAfterStubHeaderEnd
54 HookAfterStubHeaderEnd:
56 and sp, 0xfff0 ; make sure 16-byte aligned for exception context
57 sub rsp, 0x18 ; reserve room for filling exception data later
60 bt [ASM_PFX(mErrorCodeFlag)], ecx
62 push qword [rsp] ; push additional rcx to make stack alignment
64 xchg rcx, [rsp] ; restore rcx, save Exception Number in stack
65 push qword [rax] ; push rax into stack to keep code consistence
67 ;---------------------------------------;
68 ; CommonInterruptEntry ;
69 ;---------------------------------------;
70 ; The follow algorithm is used for the common interrupt routine.
71 ; Entry from each interrupt with a push eax and eax=interrupt number
72 ; Stack frame would be as follows as specified in IA32 manuals:
74 ; +---------------------+ <-- 16-byte aligned ensured by processor
76 ; +---------------------+
78 ; +---------------------+
80 ; +---------------------+
82 ; +---------------------+
84 ; +---------------------+
86 ; +---------------------+
88 ; +---------------------+
90 ; +---------------------+ <-- RBP, 16-byte aligned
91 ; The follow algorithm is used for the common interrupt routine.
92 global ASM_PFX(CommonInterruptEntry)
93 ASM_PFX(CommonInterruptEntry):
97 ; All interrupt handlers are invoked through interrupt gates, so
98 ; IF flag automatically cleared at the entry point
100 xchg rcx, [rsp] ; Save rcx into stack and save vector number into rcx
102 cmp ecx, 32 ; Intel reserved vector for exceptions?
104 bt [ASM_PFX(mErrorCodeFlag)], ecx
110 ; Push a dummy error code on the stack
111 ; to maintain coherent stack map
114 mov qword [rsp + 8], 0
118 push 0 ; clear EXCEPTION_HANDLER_CONTEXT.OldIdtHandler
119 push 0 ; clear EXCEPTION_HANDLER_CONTEXT.ExceptionDataFlag
123 ; +---------------------+ <-- 16-byte aligned ensured by processor
125 ; +---------------------+
127 ; +---------------------+
129 ; +---------------------+
131 ; +---------------------+
133 ; +---------------------+
135 ; +---------------------+
136 ; + RCX / Vector Number +
137 ; +---------------------+
139 ; +---------------------+ <-- RBP, 16-byte aligned
143 ; Since here the stack pointer is 16-byte aligned, so
144 ; EFI_FX_SAVE_STATE_X64 of EFI_SYSTEM_CONTEXT_x64
148 ;; UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;
149 ;; UINT64 R8, R9, R10, R11, R12, R13, R14, R15;
159 push qword [rbp + 8] ; RCX
162 push qword [rbp + 48] ; RSP
163 push qword [rbp] ; RBP
167 ;; UINT64 Gs, Fs, Es, Ds, Cs, Ss; insure high 16 bits of each is zero
168 movzx rax, word [rbp + 56]
170 movzx rax, word [rbp + 32]
181 mov [rbp + 8], rcx ; save vector number
184 push qword [rbp + 24]
186 ;; UINT64 Gdtr[2], Idtr[2];
192 mov rax, qword [rsp + 2]
194 mov word [rsp + 8], bx
201 mov rax, qword [rsp + 2]
203 mov word [rsp + 8], bx
213 push qword [rbp + 40]
215 ;; UINT64 Cr0, Cr1, Cr2, Cr3, Cr4, Cr8;
231 ;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
232 cmp qword [rbp + 8], VC_EXCEPTION
233 je VcDebugRegs ; For SEV-ES (#VC) Debug registers ignored
250 ;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7 are skipped for #VC to avoid exception recursion
260 ;; FX_SAVE_STATE_X64 FxSaveState;
263 db 0xf, 0xae, 0x7 ;fxsave [rdi]
265 ;; UEFI calling convention for x64 requires that Direction flag in EFLAGs is clear
268 ;; UINT32 ExceptionData;
269 push qword [rbp + 16]
271 ;; Prepare parameter and call
275 ; Per X64 calling convention, allocate maximum parameter stack space
276 ; and make sure RSP is 16-byte aligned
279 call ASM_PFX(CommonExceptionHandler)
283 ;; UINT64 ExceptionData;
286 ;; FX_SAVE_STATE_X64 FxSaveState;
289 db 0xf, 0xae, 0xE ; fxrstor [rsi]
292 ;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
293 ;; Skip restoration of DRx registers to support in-circuit emualators
294 ;; or debuggers set breakpoint in interrupt/exception context
297 ;; UINT64 Cr0, Cr1, Cr2, Cr3, Cr4, Cr8;
300 add rsp, 8 ; not for Cr1
314 ;; UINT64 Gdtr[2], Idtr[2];
315 ;; Best not let anyone mess with these particular registers...
321 ;; UINT64 Gs, Fs, Es, Ds, Cs, Ss;
323 ; mov gs, rax ; not for gs
325 ; mov fs, rax ; not for fs
326 ; (X64 will not use fs and gs, so we do not restore it)
331 pop qword [rbp + 32] ; for cs
332 pop qword [rbp + 56] ; for ss
334 ;; UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;
335 ;; UINT64 R8, R9, R10, R11, R12, R13, R14, R15;
338 add rsp, 8 ; not for rbp
339 pop qword [rbp + 48] ; for rsp
356 cmp qword [rsp - 32], 0 ; check EXCEPTION_HANDLER_CONTEXT.OldIdtHandler
358 cmp qword [rsp - 40], 1 ; check EXCEPTION_HANDLER_CONTEXT.ExceptionDataFlag
366 cmp qword [ASM_PFX(mDoFarReturnFlag)], 0 ; Check if need to do far return instead of IRET
369 mov rax, rsp ; save old RSP to rax
370 mov rsp, [rsp + 0x20]
371 push qword [rax + 0x10] ; save CS in new location
372 push qword [rax + 0x8] ; save EIP in new location
373 push qword [rax + 0x18] ; save EFLAGS in new location
374 mov rax, [rax] ; restore rax
375 popfq ; restore EFLAGS
377 ; The follow algorithm is used for clear shadow stack token busy bit.
378 ; The comment is based on the sample shadow stack.
379 ; The sample shadow stack layout :
381 ; +-------------------------+
382 ; 0xFD0 | FREE | it is 0xFD8|0x02|(LMA & CS.L), after SAVEPREVSSP.
383 ; +-------------------------+
385 ; +-------------------------+
387 ; +-------------------------+
389 ; +-------------------------+
390 ; 0xFF0 | 0xFF0 | BUSY | BUSY flag cleared after CLRSSBSY
391 ; +-------------------------+
392 ; 0xFF8 | 0xFD8|0x02|(LMA & CS.L) |
393 ; +-------------------------+
394 ; Instructions for Intel Control Flow Enforcement Technology (CET) are supported since NASM version 2.15.01.
395 push rax ; SSP should be 0xFD8 at this point
396 cmp byte [dword ASM_PFX(FeaturePcdGet (PcdCpuSmmStackGuard))], 0
399 and rax, 0x800000 ; check if CET is enabled
401 mov rax, 0x04 ; advance past cs:lip:prevssp;supervisor shadow stack token
402 INCSSP_RAX ; After this SSP should be 0xFF8
403 SAVEPREVSSP ; now the shadow stack restore token will be created at 0xFD0
404 READSSP_RAX ; Read new SSP, SSP should be 0x1000
407 CLRSSBSY_RAX ; Clear token at 0xFF0, SSP should be 0 after this
409 RSTORSSP_RAX ; Restore to token at 0xFD0, new SSP will be 0xFD0
411 mov rax, 0x01 ; Pop off the new save token created
412 INCSSP_RAX ; SSP should be 0xFD8 now
414 pop rax ; restore rax
416 DB 0x48 ; prefix to composite "retq" with next "retf"
421 ;-------------------------------------------------------------------------------------
422 ; GetTemplateAddressMap (&AddressMap);
423 ;-------------------------------------------------------------------------------------
424 ; comments here for definition of address map
425 global ASM_PFX(AsmGetTemplateAddressMap)
426 ASM_PFX(AsmGetTemplateAddressMap):
427 lea rax, [AsmIdtVectorBegin]
429 mov qword [rcx + 0x8], (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
430 lea rax, [HookAfterStubHeaderBegin]
431 mov qword [rcx + 0x10], rax
433 ; Fix up CommonInterruptEntry address
434 lea rax, [ASM_PFX(CommonInterruptEntry)]
435 lea rcx, [AsmIdtVectorBegin]
437 mov qword [rcx + (JmpAbsoluteAddress - 8 - HookAfterStubHeaderBegin)], rax
438 add rcx, (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
440 ; Fix up HookAfterStubHeaderEnd
441 lea rax, [HookAfterStubHeaderEnd]
442 lea rcx, [JmpAbsoluteAddress]
443 mov qword [rcx - 8], rax
447 ;-------------------------------------------------------------------------------------
448 ; AsmVectorNumFixup (*NewVectorAddr, VectorNum, *OldVectorAddr);
449 ;-------------------------------------------------------------------------------------
450 global ASM_PFX(AsmVectorNumFixup)
451 ASM_PFX(AsmVectorNumFixup):
453 mov [rcx + (@VectorNum - HookAfterStubHeaderBegin)], al