1 ;------------------------------------------------------------------------------ ;
2 ; Copyright (c) 2012 - 2013, Intel Corporation. All rights reserved.<BR>
3 ; This program and the accompanying materials
4 ; are licensed and made available under the terms and conditions of the BSD License
5 ; which accompanies this distribution. The full text of the license may be found at
6 ; http://opensource.org/licenses/bsd-license.php.
8 ; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 ; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 ; ExceptionHandlerAsm.Asm
17 ; x64 CPU Exception Handler
21 ;------------------------------------------------------------------------------
24 ; CommonExceptionHandler()
26 externdef CommonExceptionHandler:near
28 EXTRN mErrorCodeFlag:DWORD ; Error code flags for exceptions
32 CommonEntryAddr dq CommonInterruptEntry;
38 jmp qword ptr [CommonEntryAddr]
41 jmp qword ptr [CommonEntryAddr]
44 jmp qword ptr [CommonEntryAddr]
47 jmp qword ptr [CommonEntryAddr]
50 jmp qword ptr [CommonEntryAddr]
53 jmp qword ptr [CommonEntryAddr]
56 jmp qword ptr [CommonEntryAddr]
59 jmp qword ptr [CommonEntryAddr]
62 jmp qword ptr [CommonEntryAddr]
65 jmp qword ptr [CommonEntryAddr]
68 jmp qword ptr [CommonEntryAddr]
71 jmp qword ptr [CommonEntryAddr]
74 jmp qword ptr [CommonEntryAddr]
77 jmp qword ptr [CommonEntryAddr]
80 jmp qword ptr [CommonEntryAddr]
83 jmp qword ptr [CommonEntryAddr]
86 jmp qword ptr [CommonEntryAddr]
89 jmp qword ptr [CommonEntryAddr]
92 jmp qword ptr [CommonEntryAddr]
95 jmp qword ptr [CommonEntryAddr]
98 jmp qword ptr [CommonEntryAddr]
101 jmp qword ptr [CommonEntryAddr]
104 jmp qword ptr [CommonEntryAddr]
107 jmp qword ptr [CommonEntryAddr]
110 jmp qword ptr [CommonEntryAddr]
113 jmp qword ptr [CommonEntryAddr]
116 jmp qword ptr [CommonEntryAddr]
119 jmp qword ptr [CommonEntryAddr]
122 jmp qword ptr [CommonEntryAddr]
125 jmp qword ptr [CommonEntryAddr]
128 jmp qword ptr [CommonEntryAddr]
131 jmp qword ptr [CommonEntryAddr]
133 ;CommonInterruptEntrypoint:
134 ;---------------------------------------;
136 ;----------------------------------------------------------------------------;
137 ; The follow algorithm is used for the common interrupt routine.
138 ; Entry from each interrupt with a push eax and eax=interrupt number
140 ;---------------------------------------;
141 ; CommonInterruptEntry ;
142 ;---------------------------------------;
143 ; The follow algorithm is used for the common interrupt routine.
145 CommonInterruptEntry PROC PUBLIC
148 ; All interrupt handlers are invoked through interrupt gates, so
149 ; IF flag automatically cleared at the entry point
152 ; Calculate vector number
154 xchg rcx, [rsp] ; get the return address of call, actually, it is the address of vector number.
155 cmp ecx, 32 ; Intel reserved vector for exceptions?
157 bt mErrorCodeFlag, ecx
163 ; Push a dummy error code on the stack
164 ; to maintain coherent stack map
167 mov qword ptr [rsp + 8], 0
174 ; +---------------------+ <-- 16-byte aligned ensured by processor
176 ; +---------------------+
178 ; +---------------------+
180 ; +---------------------+
182 ; +---------------------+
184 ; +---------------------+
186 ; +---------------------+
187 ; + RCX / Vector Number +
188 ; +---------------------+
190 ; +---------------------+ <-- RBP, 16-byte aligned
195 ; Since here the stack pointer is 16-byte aligned, so
196 ; EFI_FX_SAVE_STATE_X64 of EFI_SYSTEM_CONTEXT_x64
200 ;; UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;
201 ;; UINT64 R8, R9, R10, R11, R12, R13, R14, R15;
211 push qword ptr [rbp + 8] ; RCX
214 push qword ptr [rbp + 48] ; RSP
215 push qword ptr [rbp] ; RBP
219 ;; UINT64 Gs, Fs, Es, Ds, Cs, Ss; insure high 16 bits of each is zero
220 movzx rax, word ptr [rbp + 56]
222 movzx rax, word ptr [rbp + 32]
233 mov [rbp + 8], rcx ; save vector number
236 push qword ptr [rbp + 24]
238 ;; UINT64 Gdtr[2], Idtr[2];
263 push qword ptr [rbp + 40]
265 ;; UINT64 Cr0, Cr1, Cr2, Cr3, Cr4, Cr8;
281 ;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
295 ;; FX_SAVE_STATE_X64 FxSaveState;
298 db 0fh, 0aeh, 07h ;fxsave [rdi]
300 ;; UEFI calling convention for x64 requires that Direction flag in EFLAGs is clear
303 ;; UINT32 ExceptionData;
304 push qword ptr [rbp + 16]
306 ;; Prepare parameter and call
310 ; Per X64 calling convention, allocate maximum parameter stack space
311 ; and make sure RSP is 16-byte aligned
314 mov rax, CommonExceptionHandler
319 ;; UINT64 ExceptionData;
322 ;; FX_SAVE_STATE_X64 FxSaveState;
325 db 0fh, 0aeh, 0Eh ; fxrstor [rsi]
328 ;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
329 ;; Skip restoration of DRx registers to support in-circuit emualators
330 ;; or debuggers set breakpoint in interrupt/exception context
333 ;; UINT64 Cr0, Cr1, Cr2, Cr3, Cr4, Cr8;
336 add rsp, 8 ; not for Cr1
347 pop qword ptr [rbp + 40]
350 ;; UINT64 Gdtr[2], Idtr[2];
351 ;; Best not let anyone mess with these particular registers...
355 pop qword ptr [rbp + 24]
357 ;; UINT64 Gs, Fs, Es, Ds, Cs, Ss;
359 ; mov gs, rax ; not for gs
361 ; mov fs, rax ; not for fs
362 ; (X64 will not use fs and gs, so we do not restore it)
367 pop qword ptr [rbp + 32] ; for cs
368 pop qword ptr [rbp + 56] ; for ss
370 ;; UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;
371 ;; UINT64 R8, R9, R10, R11, R12, R13, R14, R15;
374 add rsp, 8 ; not for rbp
375 pop qword ptr [rbp + 48] ; for rsp
394 CommonInterruptEntry ENDP
396 ;-------------------------------------------------------------------------------------
397 ; GetTemplateAddressMap (&AddressMap);
398 ;-------------------------------------------------------------------------------------
399 ; comments here for definition of address map
400 GetTemplateAddressMap PROC
401 mov rax, offset Exception0Handle
402 mov qword ptr [rcx], rax
403 mov qword ptr [rcx+8h], Exception1Handle - Exception0Handle
405 GetTemplateAddressMap ENDP