]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandler.c
f2c39eb193e3d676ae89ca39de8b15bd4a0d85d1
[mirror_edk2.git] / UefiCpuPkg / Library / CpuExceptionHandlerLib / Ia32 / ArchExceptionHandler.c
1 /** @file
2 IA32 CPU Exception Handler functons.
3
4 Copyright (c) 2012 - 2017, 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
9
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.
12
13 **/
14
15 #include "CpuExceptionCommon.h"
16
17 /**
18 Return address map of exception handler template so that C code can generate
19 exception tables.
20
21 @param IdtEntry Pointer to IDT entry to be updated.
22 @param InterruptHandler IDT handler value.
23
24 **/
25 VOID
26 ArchUpdateIdtEntry (
27 IN IA32_IDT_GATE_DESCRIPTOR *IdtEntry,
28 IN UINTN InterruptHandler
29 )
30 {
31 IdtEntry->Bits.OffsetLow = (UINT16)(UINTN)InterruptHandler;
32 IdtEntry->Bits.OffsetHigh = (UINT16)((UINTN)InterruptHandler >> 16);
33 IdtEntry->Bits.GateType = IA32_IDT_GATE_TYPE_INTERRUPT_32;
34 }
35
36 /**
37 Read IDT handler value from IDT entry.
38
39 @param IdtEntry Pointer to IDT entry to be read.
40
41 **/
42 UINTN
43 ArchGetIdtHandler (
44 IN IA32_IDT_GATE_DESCRIPTOR *IdtEntry
45 )
46 {
47 return (UINTN)IdtEntry->Bits.OffsetLow + (((UINTN)IdtEntry->Bits.OffsetHigh) << 16);
48 }
49
50 /**
51 Save CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.
52
53 @param[in] ExceptionType Exception type.
54 @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT.
55 @param[in] ExceptionHandlerData Pointer to exception handler data.
56 **/
57 VOID
58 ArchSaveExceptionContext (
59 IN UINTN ExceptionType,
60 IN EFI_SYSTEM_CONTEXT SystemContext,
61 IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData
62 )
63 {
64 IA32_EFLAGS32 Eflags;
65 RESERVED_VECTORS_DATA *ReservedVectors;
66
67 ReservedVectors = ExceptionHandlerData->ReservedVectors;
68 //
69 // Save Exception context in global variable
70 //
71 ReservedVectors[ExceptionType].OldFlags = SystemContext.SystemContextIa32->Eflags;
72 ReservedVectors[ExceptionType].OldCs = SystemContext.SystemContextIa32->Cs;
73 ReservedVectors[ExceptionType].OldIp = SystemContext.SystemContextIa32->Eip;
74 ReservedVectors[ExceptionType].ExceptionData = SystemContext.SystemContextIa32->ExceptionData;
75 //
76 // Clear IF flag to avoid old IDT handler enable interrupt by IRET
77 //
78 Eflags.UintN = SystemContext.SystemContextIa32->Eflags;
79 Eflags.Bits.IF = 0;
80 SystemContext.SystemContextIa32->Eflags = Eflags.UintN;
81 //
82 // Modify the EIP in stack, then old IDT handler will return to the stub code
83 //
84 SystemContext.SystemContextIa32->Eip = (UINTN) ReservedVectors[ExceptionType].HookAfterStubHeaderCode;
85 }
86
87 /**
88 Restore CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.
89
90 @param[in] ExceptionType Exception type.
91 @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT.
92 @param[in] ExceptionHandlerData Pointer to exception handler data.
93 **/
94 VOID
95 ArchRestoreExceptionContext (
96 IN UINTN ExceptionType,
97 IN EFI_SYSTEM_CONTEXT SystemContext,
98 IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData
99 )
100 {
101 RESERVED_VECTORS_DATA *ReservedVectors;
102
103 ReservedVectors = ExceptionHandlerData->ReservedVectors;
104 SystemContext.SystemContextIa32->Eflags = ReservedVectors[ExceptionType].OldFlags;
105 SystemContext.SystemContextIa32->Cs = ReservedVectors[ExceptionType].OldCs;
106 SystemContext.SystemContextIa32->Eip = ReservedVectors[ExceptionType].OldIp;
107 SystemContext.SystemContextIa32->ExceptionData = ReservedVectors[ExceptionType].ExceptionData;
108 }
109
110 /**
111 Display processor context.
112
113 @param[in] ExceptionType Exception type.
114 @param[in] SystemContext Processor context to be display.
115 **/
116 VOID
117 EFIAPI
118 DumpCpuContext (
119 IN EFI_EXCEPTION_TYPE ExceptionType,
120 IN EFI_SYSTEM_CONTEXT SystemContext
121 )
122 {
123 InternalPrintMessage (
124 "!!!! IA32 Exception Type - %02x(%a) CPU Apic ID - %08x !!!!\n",
125 ExceptionType,
126 GetExceptionNameStr (ExceptionType),
127 GetApicId ()
128 );
129 if ((mErrorCodeFlag & (1 << ExceptionType)) != 0) {
130 InternalPrintMessage (
131 "ExceptionData - %08x",
132 SystemContext.SystemContextIa32->ExceptionData
133 );
134 if (ExceptionType == EXCEPT_IA32_PAGE_FAULT) {
135 InternalPrintMessage (
136 " I:%x R:%x U:%x W:%x P:%x PK:%x S:%x",
137 (SystemContext.SystemContextIa32->ExceptionData & IA32_PF_EC_ID) != 0,
138 (SystemContext.SystemContextIa32->ExceptionData & IA32_PF_EC_RSVD) != 0,
139 (SystemContext.SystemContextIa32->ExceptionData & IA32_PF_EC_US) != 0,
140 (SystemContext.SystemContextIa32->ExceptionData & IA32_PF_EC_WR) != 0,
141 (SystemContext.SystemContextIa32->ExceptionData & IA32_PF_EC_P) != 0,
142 (SystemContext.SystemContextIa32->ExceptionData & IA32_PF_EC_PK) != 0,
143 (SystemContext.SystemContextIa32->ExceptionData & IA32_PF_EC_SGX) != 0
144 );
145 }
146 InternalPrintMessage ("\n");
147 }
148 InternalPrintMessage (
149 "EIP - %08x, CS - %08x, EFLAGS - %08x\n",
150 SystemContext.SystemContextIa32->Eip,
151 SystemContext.SystemContextIa32->Cs,
152 SystemContext.SystemContextIa32->Eflags
153 );
154 InternalPrintMessage (
155 "EAX - %08x, ECX - %08x, EDX - %08x, EBX - %08x\n",
156 SystemContext.SystemContextIa32->Eax,
157 SystemContext.SystemContextIa32->Ecx,
158 SystemContext.SystemContextIa32->Edx,
159 SystemContext.SystemContextIa32->Ebx
160 );
161 InternalPrintMessage (
162 "ESP - %08x, EBP - %08x, ESI - %08x, EDI - %08x\n",
163 SystemContext.SystemContextIa32->Esp,
164 SystemContext.SystemContextIa32->Ebp,
165 SystemContext.SystemContextIa32->Esi,
166 SystemContext.SystemContextIa32->Edi
167 );
168 InternalPrintMessage (
169 "DS - %08x, ES - %08x, FS - %08x, GS - %08x, SS - %08x\n",
170 SystemContext.SystemContextIa32->Ds,
171 SystemContext.SystemContextIa32->Es,
172 SystemContext.SystemContextIa32->Fs,
173 SystemContext.SystemContextIa32->Gs,
174 SystemContext.SystemContextIa32->Ss
175 );
176 InternalPrintMessage (
177 "CR0 - %08x, CR2 - %08x, CR3 - %08x, CR4 - %08x\n",
178 SystemContext.SystemContextIa32->Cr0,
179 SystemContext.SystemContextIa32->Cr2,
180 SystemContext.SystemContextIa32->Cr3,
181 SystemContext.SystemContextIa32->Cr4
182 );
183 InternalPrintMessage (
184 "DR0 - %08x, DR1 - %08x, DR2 - %08x, DR3 - %08x\n",
185 SystemContext.SystemContextIa32->Dr0,
186 SystemContext.SystemContextIa32->Dr1,
187 SystemContext.SystemContextIa32->Dr2,
188 SystemContext.SystemContextIa32->Dr3
189 );
190 InternalPrintMessage (
191 "DR6 - %08x, DR7 - %08x\n",
192 SystemContext.SystemContextIa32->Dr6,
193 SystemContext.SystemContextIa32->Dr7
194 );
195 InternalPrintMessage (
196 "GDTR - %08x %08x, IDTR - %08x %08x\n",
197 SystemContext.SystemContextIa32->Gdtr[0],
198 SystemContext.SystemContextIa32->Gdtr[1],
199 SystemContext.SystemContextIa32->Idtr[0],
200 SystemContext.SystemContextIa32->Idtr[1]
201 );
202 InternalPrintMessage (
203 "LDTR - %08x, TR - %08x\n",
204 SystemContext.SystemContextIa32->Ldtr,
205 SystemContext.SystemContextIa32->Tr
206 );
207 InternalPrintMessage (
208 "FXSAVE_STATE - %08x\n",
209 &SystemContext.SystemContextIa32->FxSaveState
210 );
211 }
212
213 /**
214 Display CPU information.
215
216 @param ExceptionType Exception type.
217 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
218 **/
219 VOID
220 DumpImageAndCpuContent (
221 IN EFI_EXCEPTION_TYPE ExceptionType,
222 IN EFI_SYSTEM_CONTEXT SystemContext
223 )
224 {
225 DumpCpuContext (ExceptionType, SystemContext);
226 //
227 // Dump module image base and module entry point by EIP
228 //
229 DumpModuleImageInfo (SystemContext.SystemContextIa32->Eip);
230 }