2 x64 CPU Exception Hanlder.
4 Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "CpuExceptionCommon.h"
18 Internal function to setup CPU exception handlers.
22 InternalSetupCpuExceptionHandlers (
26 IA32_DESCRIPTOR IdtDescriptor
;
28 EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap
;
30 IA32_IDT_GATE_DESCRIPTOR
*IdtEntry
;
32 UINTN InterruptHandler
;
35 // Read IDT descriptor and calculate IDT size
37 AsmReadIdtr (&IdtDescriptor
);
38 IdtSize
= (IdtDescriptor
.Limit
+ 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR
);
39 if (IdtSize
> CPU_EXCEPTION_NUM
) {
41 // CPU exeption library only setup CPU_EXCEPTION_NUM exception handler at most
43 IdtSize
= CPU_EXCEPTION_NUM
;
47 // Use current CS as the segment selector of interrupt gate in IDT
49 CodeSegment
= AsmReadCs ();
50 IdtEntry
= (IA32_IDT_GATE_DESCRIPTOR
*) IdtDescriptor
.Base
;
52 GetTemplateAddressMap (&TemplateMap
);
54 for (Index
= 0; Index
< IdtSize
; Index
++) {
55 InterruptHandler
= TemplateMap
.ExceptionStart
+ Index
* TemplateMap
.ExceptionStubHeaderSize
;
56 IdtEntry
[Index
].Bits
.OffsetLow
= (UINT16
)(UINTN
)InterruptHandler
;
57 IdtEntry
[Index
].Bits
.OffsetHigh
= (UINT16
)((UINTN
)InterruptHandler
>> 16);
58 IdtEntry
[Index
].Bits
.OffsetUpper
= (UINT32
)((UINTN
)InterruptHandler
>> 32);
59 IdtEntry
[Index
].Bits
.Selector
= CodeSegment
;
60 IdtEntry
[Index
].Bits
.GateType
= IA32_IDT_GATE_TYPE_INTERRUPT_32
;
65 Common exception handler.
67 @param ExceptionType Exception type.
68 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
72 IN UINTN ExceptionType
,
73 IN EFI_SYSTEM_CONTEXT SystemContext
79 InternalPrintMessage (
80 "!!!! X64 Exception Type - %016lx CPU Apic ID - %08x !!!!\n",
84 InternalPrintMessage (
85 "RIP - %016lx, CS - %016lx, RFLAGS - %016lx\n",
86 SystemContext
.SystemContextX64
->Rip
,
87 SystemContext
.SystemContextX64
->Cs
,
88 SystemContext
.SystemContextX64
->Rflags
90 if (mErrorCodeFlag
& (1 << ExceptionType
)) {
91 InternalPrintMessage (
92 "ExceptionData - %016lx\n",
93 SystemContext
.SystemContextX64
->ExceptionData
96 InternalPrintMessage (
97 "RAX - %016lx, RCX - %016lx, RDX - %016lx\n",
98 SystemContext
.SystemContextX64
->Rax
,
99 SystemContext
.SystemContextX64
->Rcx
,
100 SystemContext
.SystemContextX64
->Rdx
102 InternalPrintMessage (
103 "RBX - %016lx, RSP - %016lx, RBP - %016lx\n",
104 SystemContext
.SystemContextX64
->Rbx
,
105 SystemContext
.SystemContextX64
->Rsp
,
106 SystemContext
.SystemContextX64
->Rbp
108 InternalPrintMessage (
109 "RSI - %016lx, RDI - %016lx\n",
110 SystemContext
.SystemContextX64
->Rsi
,
111 SystemContext
.SystemContextX64
->Rdi
113 InternalPrintMessage (
114 "R8 - %016lx, R9 - %016lx, R10 - %016lx\n",
115 SystemContext
.SystemContextX64
->R8
,
116 SystemContext
.SystemContextX64
->R9
,
117 SystemContext
.SystemContextX64
->R10
119 InternalPrintMessage (
120 "R11 - %016lx, R12 - %016lx, R13 - %016lx\n",
121 SystemContext
.SystemContextX64
->R11
,
122 SystemContext
.SystemContextX64
->R12
,
123 SystemContext
.SystemContextX64
->R13
125 InternalPrintMessage (
126 "R14 - %016lx, R15 - %016lx\n",
127 SystemContext
.SystemContextX64
->R14
,
128 SystemContext
.SystemContextX64
->R15
130 InternalPrintMessage (
131 "DS - %016lx, ES - %016lx, FS - %016lx\n",
132 SystemContext
.SystemContextX64
->Ds
,
133 SystemContext
.SystemContextX64
->Es
,
134 SystemContext
.SystemContextX64
->Fs
136 InternalPrintMessage (
137 "GS - %016lx, SS - %016lx\n",
138 SystemContext
.SystemContextX64
->Gs
,
139 SystemContext
.SystemContextX64
->Ss
141 InternalPrintMessage (
142 "CR0 - %016lx, CR2 - %016lx, CR3 - %016lx\n",
143 SystemContext
.SystemContextX64
->Cr0
,
144 SystemContext
.SystemContextX64
->Cr2
,
145 SystemContext
.SystemContextX64
->Cr3
147 InternalPrintMessage (
148 "CR4 - %016lx, CR8 - %016lx\n",
149 SystemContext
.SystemContextX64
->Cr4
,
150 SystemContext
.SystemContextX64
->Cr8
152 InternalPrintMessage (
153 "DR0 - %016lx, DR1 - %016lx, DR2 - %016lx\n",
154 SystemContext
.SystemContextX64
->Dr0
,
155 SystemContext
.SystemContextX64
->Dr1
,
156 SystemContext
.SystemContextX64
->Dr2
158 InternalPrintMessage (
159 "DR3 - %016lx, DR6 - %016lx, DR7 - %016lx\n",
160 SystemContext
.SystemContextX64
->Dr3
,
161 SystemContext
.SystemContextX64
->Dr6
,
162 SystemContext
.SystemContextX64
->Dr7
164 InternalPrintMessage (
165 "GDTR - %016lx %016lx, LDTR - %016lx\n",
166 SystemContext
.SystemContextX64
->Gdtr
[0],
167 SystemContext
.SystemContextX64
->Gdtr
[1],
168 SystemContext
.SystemContextX64
->Ldtr
170 InternalPrintMessage (
171 "IDTR - %016lx %016lx, TR - %016lx\n",
172 SystemContext
.SystemContextX64
->Idtr
[0],
173 SystemContext
.SystemContextX64
->Idtr
[1],
174 SystemContext
.SystemContextX64
->Tr
176 InternalPrintMessage (
177 "FXSAVE_STATE - %016lx\n",
178 &SystemContext
.SystemContextX64
->FxSaveState
182 // Find module image base and module entry point by RIP
184 ImageBase
= FindModuleImageBase (SystemContext
.SystemContextX64
->Rip
, &EntryPoint
);
185 if (ImageBase
!= 0) {
186 InternalPrintMessage (
187 " (ImageBase=%016lx, EntryPoint=%016lx) !!!!\n",