]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c
UefiCpuPkg/ExceptionLib: Add CET support.
[mirror_edk2.git] / UefiCpuPkg / Library / CpuExceptionHandlerLib / CpuExceptionCommon.c
CommitLineData
3ff40610 1/** @file\r
e3644786 2 CPU Exception Handler Library common functions.\r
8f07f895 3\r
0d25074c 4 Copyright (c) 2012 - 2019, 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
0d25074c 23CONST UINT32 mErrorCodeFlag = 0x00227d00;\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
30CONST CHAR8 mExceptionReservedStr[] = "Reserved";\r
31CONST 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
0d25074c
JY
52 "#VE - Virtualization",\r
53 "#CP - Control Protection"\r
a51ee144
JF
54};\r
55\r
56#define EXCEPTION_KNOWN_NAME_NUM (sizeof (mExceptionNameStr) / sizeof (CHAR8 *))\r
57\r
58/**\r
59 Get ASCII format string exception name by exception type.\r
60\r
61 @param ExceptionType Exception type.\r
62\r
63 @return ASCII format string exception name.\r
64**/\r
65CONST CHAR8 *\r
66GetExceptionNameStr (\r
67 IN EFI_EXCEPTION_TYPE ExceptionType\r
68 )\r
69{\r
70 if ((UINTN) ExceptionType < EXCEPTION_KNOWN_NAME_NUM) {\r
71 return mExceptionNameStr[ExceptionType];\r
72 } else {\r
73 return mExceptionReservedStr;\r
74 }\r
75}\r
76\r
8f07f895 77/**\r
78 Prints a message to the serial port.\r
79\r
80 @param Format Format string for the message to print.\r
a51ee144 81 @param ... Variable argument list whose contents are accessed\r
8f07f895 82 based on the format string specified by Format.\r
83\r
84**/\r
85VOID\r
86EFIAPI\r
87InternalPrintMessage (\r
88 IN CONST CHAR8 *Format,\r
89 ...\r
90 )\r
91{\r
92 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
93 VA_LIST Marker;\r
94\r
95 //\r
96 // Convert the message to an ASCII String\r
97 //\r
98 VA_START (Marker, Format);\r
99 AsciiVSPrint (Buffer, sizeof (Buffer), Format, Marker);\r
100 VA_END (Marker);\r
101\r
102 //\r
a51ee144 103 // Send the print string to a Serial Port\r
8f07f895 104 //\r
105 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));\r
106}\r
107\r
108/**\r
109 Find and display image base address and return image base and its entry point.\r
dd563742 110\r
a9c7ab95 111 @param CurrentEip Current instruction pointer.\r
dd563742 112\r
8f07f895 113**/\r
dd563742 114VOID\r
1b2f7b3e
JF
115DumpModuleImageInfo (\r
116 IN UINTN CurrentEip\r
8f07f895 117 )\r
118{\r
1b2f7b3e 119 EFI_STATUS Status;\r
8f07f895 120 UINTN Pe32Data;\r
8f07f895 121 VOID *PdbPointer;\r
1b2f7b3e 122 VOID *EntryPoint;\r
8f07f895 123\r
9e981317 124 Pe32Data = PeCoffSearchImageBase (CurrentEip);\r
1b2f7b3e
JF
125 if (Pe32Data == 0) {\r
126 InternalPrintMessage ("!!!! Can't find image information. !!!!\n");\r
127 } else {\r
8f07f895 128 //\r
1b2f7b3e 129 // Find Image Base entry point\r
a51ee144 130 //\r
1b2f7b3e
JF
131 Status = PeCoffLoaderGetEntryPoint ((VOID *) Pe32Data, &EntryPoint);\r
132 if (EFI_ERROR (Status)) {\r
133 EntryPoint = NULL;\r
134 }\r
bb207f6c 135 InternalPrintMessage ("!!!! Find image based on IP(0x%x) ", CurrentEip);\r
8f07f895 136 PdbPointer = PeCoffLoaderGetPdbPointer ((VOID *) Pe32Data);\r
137 if (PdbPointer != NULL) {\r
138 InternalPrintMessage ("%a", PdbPointer);\r
139 } else {\r
140 InternalPrintMessage ("(No PDB) " );\r
141 }\r
1b2f7b3e
JF
142 InternalPrintMessage (\r
143 " (ImageBase=%016lp, EntryPoint=%016p) !!!!\n",\r
144 (VOID *) Pe32Data,\r
145 EntryPoint\r
146 );\r
8f07f895 147 }\r
8f07f895 148}\r
149\r
e41aad15
JF
150/**\r
151 Read and save reserved vector information\r
a51ee144 152\r
e41aad15
JF
153 @param[in] VectorInfo Pointer to reserved vector list.\r
154 @param[out] ReservedVector Pointer to reserved vector data buffer.\r
155 @param[in] VectorCount Vector number to be updated.\r
a51ee144 156\r
e41aad15
JF
157 @return EFI_SUCCESS Read and save vector info successfully.\r
158 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r
159\r
160**/\r
161EFI_STATUS\r
162ReadAndVerifyVectorInfo (\r
163 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo,\r
164 OUT RESERVED_VECTORS_DATA *ReservedVector,\r
165 IN UINTN VectorCount\r
166 )\r
167{\r
168 while (VectorInfo->Attribute != EFI_VECTOR_HANDOFF_LAST_ENTRY) {\r
169 if (VectorInfo->Attribute > EFI_VECTOR_HANDOFF_HOOK_AFTER) {\r
170 //\r
171 // If vector attrubute is invalid\r
172 //\r
173 return EFI_INVALID_PARAMETER;\r
174 }\r
175 if (VectorInfo->VectorNumber < VectorCount) {\r
176 ReservedVector[VectorInfo->VectorNumber].Attribute = VectorInfo->Attribute;\r
177 }\r
178 VectorInfo ++;\r
179 }\r
180 return EFI_SUCCESS;\r
7367cc6c 181}\r