]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandler.c
Fix meta file issue.
[mirror_edk2.git] / UefiCpuPkg / Library / CpuExceptionHandlerLib / Ia32 / ArchExceptionHandler.c
1 /** @file
2 IA32 CPU Exception Hanlder functons.
3
4 Copyright (c) 2012 - 2013, 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 ExceptionType Exception type.
54 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
55
56 **/
57 VOID
58 ArchSaveExceptionContext (
59 IN UINTN ExceptionType,
60 IN EFI_SYSTEM_CONTEXT SystemContext
61 )
62 {
63 IA32_EFLAGS32 Eflags;
64 //
65 // Save Exception context in global variable
66 //
67 mReservedVectors[ExceptionType].OldFlags = SystemContext.SystemContextIa32->Eflags;
68 mReservedVectors[ExceptionType].OldCs = SystemContext.SystemContextIa32->Cs;
69 mReservedVectors[ExceptionType].OldIp = SystemContext.SystemContextIa32->Eip;
70 mReservedVectors[ExceptionType].ExceptionData = SystemContext.SystemContextIa32->ExceptionData;
71 //
72 // Clear IF flag to avoid old IDT handler enable interrupt by IRET
73 //
74 Eflags.UintN = SystemContext.SystemContextIa32->Eflags;
75 Eflags.Bits.IF = 0;
76 SystemContext.SystemContextIa32->Eflags = Eflags.UintN;
77 //
78 // Modify the EIP in stack, then old IDT handler will return to the stub code
79 //
80 SystemContext.SystemContextIa32->Eip = (UINTN) mReservedVectors[ExceptionType].HookAfterStubHeaderCode;
81 }
82
83 /**
84 Restore CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.
85
86 @param ExceptionType Exception type.
87 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
88 **/
89 VOID
90 ArchRestoreExceptionContext (
91 IN UINTN ExceptionType,
92 IN EFI_SYSTEM_CONTEXT SystemContext
93 )
94 {
95 SystemContext.SystemContextIa32->Eflags = mReservedVectors[ExceptionType].OldFlags;
96 SystemContext.SystemContextIa32->Cs = mReservedVectors[ExceptionType].OldCs;
97 SystemContext.SystemContextIa32->Eip = mReservedVectors[ExceptionType].OldIp;
98 SystemContext.SystemContextIa32->ExceptionData = mReservedVectors[ExceptionType].ExceptionData;
99 }
100
101 /**
102 Display CPU information.
103
104 @param ExceptionType Exception type.
105 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
106 **/
107 VOID
108 DumpCpuContent (
109 IN EFI_EXCEPTION_TYPE ExceptionType,
110 IN EFI_SYSTEM_CONTEXT SystemContext
111 )
112 {
113 UINTN ImageBase;
114 UINTN EntryPoint;
115
116 InternalPrintMessage (
117 "!!!! IA32 Exception Type - %08x CPU Apic ID - %08x !!!!\n",
118 ExceptionType,
119 GetApicId ()
120 );
121 InternalPrintMessage (
122 "EIP - %08x, CS - %08x, EFLAGS - %08x\n",
123 SystemContext.SystemContextIa32->Eip,
124 SystemContext.SystemContextIa32->Cs,
125 SystemContext.SystemContextIa32->Eflags
126 );
127 if ((mErrorCodeFlag & (1 << ExceptionType)) != 0) {
128 InternalPrintMessage (
129 "ExceptionData - %08x\n",
130 SystemContext.SystemContextIa32->ExceptionData
131 );
132 }
133 InternalPrintMessage (
134 "EAX - %08x, ECX - %08x, EDX - %08x, EBX - %08x\n",
135 SystemContext.SystemContextIa32->Eax,
136 SystemContext.SystemContextIa32->Ecx,
137 SystemContext.SystemContextIa32->Edx,
138 SystemContext.SystemContextIa32->Ebx
139 );
140 InternalPrintMessage (
141 "ESP - %08x, EBP - %08x, ESI - %08x, EDI - %08x\n",
142 SystemContext.SystemContextIa32->Esp,
143 SystemContext.SystemContextIa32->Ebp,
144 SystemContext.SystemContextIa32->Esi,
145 SystemContext.SystemContextIa32->Edi
146 );
147 InternalPrintMessage (
148 "DS - %08x, ES - %08x, FS - %08x, GS - %08x, SS - %08x\n",
149 SystemContext.SystemContextIa32->Ds,
150 SystemContext.SystemContextIa32->Es,
151 SystemContext.SystemContextIa32->Fs,
152 SystemContext.SystemContextIa32->Gs,
153 SystemContext.SystemContextIa32->Ss
154 );
155 InternalPrintMessage (
156 "CR0 - %08x, CR2 - %08x, CR3 - %08x, CR4 - %08x\n",
157 SystemContext.SystemContextIa32->Cr0,
158 SystemContext.SystemContextIa32->Cr2,
159 SystemContext.SystemContextIa32->Cr3,
160 SystemContext.SystemContextIa32->Cr4
161 );
162 InternalPrintMessage (
163 "DR0 - %08x, DR1 - %08x, DR2 - %08x, DR3 - %08x\n",
164 SystemContext.SystemContextIa32->Dr0,
165 SystemContext.SystemContextIa32->Dr1,
166 SystemContext.SystemContextIa32->Dr2,
167 SystemContext.SystemContextIa32->Dr3
168 );
169 InternalPrintMessage (
170 "DR6 - %08x, DR7 - %08x\n",
171 SystemContext.SystemContextIa32->Dr6,
172 SystemContext.SystemContextIa32->Dr7
173 );
174 InternalPrintMessage (
175 "GDTR - %08x %08x, IDTR - %08x %08x\n",
176 SystemContext.SystemContextIa32->Gdtr[0],
177 SystemContext.SystemContextIa32->Gdtr[1],
178 SystemContext.SystemContextIa32->Idtr[0],
179 SystemContext.SystemContextIa32->Idtr[1]
180 );
181 InternalPrintMessage (
182 "LDTR - %08x, TR - %08x\n",
183 SystemContext.SystemContextIa32->Ldtr,
184 SystemContext.SystemContextIa32->Tr
185 );
186 InternalPrintMessage (
187 "FXSAVE_STATE - %08x\n",
188 &SystemContext.SystemContextIa32->FxSaveState
189 );
190
191 //
192 // Find module image base and module entry point by RIP
193 //
194 ImageBase = FindModuleImageBase (SystemContext.SystemContextIa32->Eip, &EntryPoint);
195 if (ImageBase != 0) {
196 InternalPrintMessage (
197 " (ImageBase=%08x, EntryPoint=%08x) !!!!\n",
198 ImageBase,
199 EntryPoint
200 );
201 }
202 }