]>
Commit | Line | Data |
---|---|---|
8f07f895 | 1 | /** @file\r |
e3644786 | 2 | CPU Exception Handler Library common functions.\r |
8f07f895 | 3 | \r |
396fe30a | 4 | Copyright (c) 2012 - 2016, Intel Corporation. All rights reserved.<BR>\r |
8f07f895 | 5 | This program and the accompanying materials\r |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | **/\r | |
14 | \r | |
15 | #include "CpuExceptionCommon.h"\r | |
16 | \r | |
17 | //\r | |
e41aad15 | 18 | // Error code flag indicating whether or not an error code will be\r |
8f07f895 | 19 | // pushed on the stack if an exception occurs.\r |
20 | //\r | |
21 | // 1 means an error code will be pushed, otherwise 0\r | |
22 | //\r | |
396fe30a | 23 | CONST UINT32 mErrorCodeFlag = 0x00027d00;\r |
8f07f895 | 24 | \r |
25 | //\r | |
a51ee144 | 26 | // Define the maximum message length\r |
8f07f895 | 27 | //\r |
28 | #define MAX_DEBUG_MESSAGE_LENGTH 0x100\r | |
29 | \r | |
a51ee144 JF |
30 | CONST CHAR8 mExceptionReservedStr[] = "Reserved";\r |
31 | CONST CHAR8 *mExceptionNameStr[] = {\r | |
32 | "#DE - Divide Error",\r | |
33 | "#DB - Debug",\r | |
34 | "NMI Interrupt",\r | |
35 | "#BP - Breakpoint",\r | |
36 | "#OF - Overflow",\r | |
37 | "#BR - BOUND Range Exceeded",\r | |
38 | "#UD - Invalid Opcode",\r | |
39 | "#NM - Device Not Available",\r | |
40 | "#DF - Double Fault",\r | |
41 | "Coprocessor Segment Overrun",\r | |
42 | "#TS - Invalid TSS",\r | |
43 | "#NP - Segment Not Present",\r | |
44 | "#SS - Stack Fault Fault",\r | |
45 | "#GP - General Protection",\r | |
46 | "#PF - Page-Fault",\r | |
47 | "Reserved",\r | |
48 | "#MF - x87 FPU Floating-Point Error",\r | |
49 | "#AC - Alignment Check",\r | |
50 | "#MC - Machine-Check",\r | |
51 | "#XM - SIMD floating-point",\r | |
52 | "#VE - Virtualization"\r | |
53 | };\r | |
54 | \r | |
55 | #define EXCEPTION_KNOWN_NAME_NUM (sizeof (mExceptionNameStr) / sizeof (CHAR8 *))\r | |
56 | \r | |
57 | /**\r | |
58 | Get ASCII format string exception name by exception type.\r | |
59 | \r | |
60 | @param ExceptionType Exception type.\r | |
61 | \r | |
62 | @return ASCII format string exception name.\r | |
63 | **/\r | |
64 | CONST CHAR8 *\r | |
65 | GetExceptionNameStr (\r | |
66 | IN EFI_EXCEPTION_TYPE ExceptionType\r | |
67 | )\r | |
68 | {\r | |
69 | if ((UINTN) ExceptionType < EXCEPTION_KNOWN_NAME_NUM) {\r | |
70 | return mExceptionNameStr[ExceptionType];\r | |
71 | } else {\r | |
72 | return mExceptionReservedStr;\r | |
73 | }\r | |
74 | }\r | |
75 | \r | |
8f07f895 | 76 | /**\r |
77 | Prints a message to the serial port.\r | |
78 | \r | |
79 | @param Format Format string for the message to print.\r | |
a51ee144 | 80 | @param ... Variable argument list whose contents are accessed\r |
8f07f895 | 81 | based on the format string specified by Format.\r |
82 | \r | |
83 | **/\r | |
84 | VOID\r | |
85 | EFIAPI\r | |
86 | InternalPrintMessage (\r | |
87 | IN CONST CHAR8 *Format,\r | |
88 | ...\r | |
89 | )\r | |
90 | {\r | |
91 | CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r | |
92 | VA_LIST Marker;\r | |
93 | \r | |
94 | //\r | |
95 | // Convert the message to an ASCII String\r | |
96 | //\r | |
97 | VA_START (Marker, Format);\r | |
98 | AsciiVSPrint (Buffer, sizeof (Buffer), Format, Marker);\r | |
99 | VA_END (Marker);\r | |
100 | \r | |
101 | //\r | |
a51ee144 | 102 | // Send the print string to a Serial Port\r |
8f07f895 | 103 | //\r |
104 | SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));\r | |
105 | }\r | |
106 | \r | |
107 | /**\r | |
108 | Find and display image base address and return image base and its entry point.\r | |
a51ee144 | 109 | \r |
a9c7ab95 | 110 | @param CurrentEip Current instruction pointer.\r |
111 | @param EntryPoint Return module entry point if module header is found.\r | |
a51ee144 | 112 | \r |
a9c7ab95 | 113 | @return !0 Image base address.\r |
114 | @return 0 Image header cannot be found.\r | |
8f07f895 | 115 | **/\r |
a51ee144 | 116 | UINTN\r |
8f07f895 | 117 | FindModuleImageBase (\r |
118 | IN UINTN CurrentEip,\r | |
119 | OUT UINTN *EntryPoint\r | |
120 | )\r | |
121 | {\r | |
122 | UINTN Pe32Data;\r | |
123 | EFI_IMAGE_DOS_HEADER *DosHdr;\r | |
124 | EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r | |
125 | VOID *PdbPointer;\r | |
126 | \r | |
127 | //\r | |
128 | // Find Image Base\r | |
129 | //\r | |
130 | Pe32Data = CurrentEip & ~(mImageAlignSize - 1);\r | |
131 | while (Pe32Data != 0) {\r | |
132 | DosHdr = (EFI_IMAGE_DOS_HEADER *) Pe32Data;\r | |
133 | if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r | |
134 | //\r | |
135 | // DOS image header is present, so read the PE header after the DOS image header.\r | |
136 | //\r | |
137 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)(Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));\r | |
9e2364ef JF |
138 | //\r |
139 | // Make sure PE header address does not overflow and is less than the initial address.\r | |
140 | //\r | |
141 | if (((UINTN)Hdr.Pe32 > Pe32Data) && ((UINTN)Hdr.Pe32 < CurrentEip)) {\r | |
142 | if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r | |
143 | //\r | |
144 | // It's PE image.\r | |
145 | //\r | |
146 | InternalPrintMessage ("!!!! Find PE image ");\r | |
147 | *EntryPoint = (UINTN)Pe32Data + (UINTN)(Hdr.Pe32->OptionalHeader.AddressOfEntryPoint & 0x0ffffffff);\r | |
148 | break;\r | |
149 | }\r | |
8f07f895 | 150 | }\r |
151 | } else {\r | |
152 | //\r | |
153 | // DOS image header is not present, TE header is at the image base.\r | |
154 | //\r | |
155 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r | |
156 | if ((Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) &&\r | |
157 | ((Hdr.Te->Machine == IMAGE_FILE_MACHINE_I386) || Hdr.Te->Machine == IMAGE_FILE_MACHINE_X64)) {\r | |
158 | //\r | |
159 | // It's TE image, it TE header and Machine type match\r | |
160 | //\r | |
161 | InternalPrintMessage ("!!!! Find TE image ");\r | |
162 | *EntryPoint = (UINTN)Pe32Data + (UINTN)(Hdr.Te->AddressOfEntryPoint & 0x0ffffffff) + sizeof(EFI_TE_IMAGE_HEADER) - Hdr.Te->StrippedSize;\r | |
163 | break;\r | |
164 | }\r | |
165 | }\r | |
166 | \r | |
167 | //\r | |
168 | // Not found the image base, check the previous aligned address\r | |
a51ee144 | 169 | //\r |
8f07f895 | 170 | Pe32Data -= mImageAlignSize;\r |
171 | }\r | |
172 | \r | |
173 | if (Pe32Data != 0) {\r | |
174 | PdbPointer = PeCoffLoaderGetPdbPointer ((VOID *) Pe32Data);\r | |
175 | if (PdbPointer != NULL) {\r | |
176 | InternalPrintMessage ("%a", PdbPointer);\r | |
177 | } else {\r | |
178 | InternalPrintMessage ("(No PDB) " );\r | |
179 | }\r | |
180 | } else {\r | |
181 | InternalPrintMessage ("!!!! Can't find image information. !!!!\n");\r | |
182 | }\r | |
183 | \r | |
184 | return Pe32Data;\r | |
185 | }\r | |
186 | \r | |
e41aad15 JF |
187 | /**\r |
188 | Read and save reserved vector information\r | |
a51ee144 | 189 | \r |
e41aad15 JF |
190 | @param[in] VectorInfo Pointer to reserved vector list.\r |
191 | @param[out] ReservedVector Pointer to reserved vector data buffer.\r | |
192 | @param[in] VectorCount Vector number to be updated.\r | |
a51ee144 | 193 | \r |
e41aad15 JF |
194 | @return EFI_SUCCESS Read and save vector info successfully.\r |
195 | @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r | |
196 | \r | |
197 | **/\r | |
198 | EFI_STATUS\r | |
199 | ReadAndVerifyVectorInfo (\r | |
200 | IN EFI_VECTOR_HANDOFF_INFO *VectorInfo,\r | |
201 | OUT RESERVED_VECTORS_DATA *ReservedVector,\r | |
202 | IN UINTN VectorCount\r | |
203 | )\r | |
204 | {\r | |
205 | while (VectorInfo->Attribute != EFI_VECTOR_HANDOFF_LAST_ENTRY) {\r | |
206 | if (VectorInfo->Attribute > EFI_VECTOR_HANDOFF_HOOK_AFTER) {\r | |
207 | //\r | |
208 | // If vector attrubute is invalid\r | |
209 | //\r | |
210 | return EFI_INVALID_PARAMETER;\r | |
211 | }\r | |
212 | if (VectorInfo->VectorNumber < VectorCount) {\r | |
213 | ReservedVector[VectorInfo->VectorNumber].Attribute = VectorInfo->Attribute;\r | |
214 | }\r | |
215 | VectorInfo ++;\r | |
216 | }\r | |
217 | return EFI_SUCCESS;\r | |
218 | } |