]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / UefiCpuPkg / Library / CpuExceptionHandlerLib / CpuExceptionCommon.c
1 /** @file
2 CPU Exception Handler Library common functions.
3
4 Copyright (c) 2012 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "CpuExceptionCommon.h"
10
11 //
12 // Error code flag indicating whether or not an error code will be
13 // pushed on the stack if an exception occurs.
14 //
15 // 1 means an error code will be pushed, otherwise 0
16 //
17 CONST UINT32 mErrorCodeFlag = 0x20227d00;
18
19 //
20 // Define the maximum message length
21 //
22 #define MAX_DEBUG_MESSAGE_LENGTH 0x100
23
24 CONST CHAR8 mExceptionReservedStr[] = "Reserved";
25 CONST CHAR8 *mExceptionNameStr[] = {
26 "#DE - Divide Error",
27 "#DB - Debug",
28 "NMI Interrupt",
29 "#BP - Breakpoint",
30 "#OF - Overflow",
31 "#BR - BOUND Range Exceeded",
32 "#UD - Invalid Opcode",
33 "#NM - Device Not Available",
34 "#DF - Double Fault",
35 "Coprocessor Segment Overrun",
36 "#TS - Invalid TSS",
37 "#NP - Segment Not Present",
38 "#SS - Stack Fault Fault",
39 "#GP - General Protection",
40 "#PF - Page-Fault",
41 "Reserved",
42 "#MF - x87 FPU Floating-Point Error",
43 "#AC - Alignment Check",
44 "#MC - Machine-Check",
45 "#XM - SIMD floating-point",
46 "#VE - Virtualization",
47 "#CP - Control Protection",
48 "Reserved",
49 "Reserved",
50 "Reserved",
51 "Reserved",
52 "Reserved",
53 "Reserved",
54 "Reserved",
55 "#VC - VMM Communication",
56 };
57
58 #define EXCEPTION_KNOWN_NAME_NUM (sizeof (mExceptionNameStr) / sizeof (CHAR8 *))
59
60 /**
61 Get ASCII format string exception name by exception type.
62
63 @param ExceptionType Exception type.
64
65 @return ASCII format string exception name.
66 **/
67 CONST CHAR8 *
68 GetExceptionNameStr (
69 IN EFI_EXCEPTION_TYPE ExceptionType
70 )
71 {
72 if ((UINTN)ExceptionType < EXCEPTION_KNOWN_NAME_NUM) {
73 return mExceptionNameStr[ExceptionType];
74 } else {
75 return mExceptionReservedStr;
76 }
77 }
78
79 /**
80 Prints a message to the serial port.
81
82 @param Format Format string for the message to print.
83 @param ... Variable argument list whose contents are accessed
84 based on the format string specified by Format.
85
86 **/
87 VOID
88 EFIAPI
89 InternalPrintMessage (
90 IN CONST CHAR8 *Format,
91 ...
92 )
93 {
94 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];
95 VA_LIST Marker;
96
97 //
98 // Convert the message to an ASCII String
99 //
100 VA_START (Marker, Format);
101 AsciiVSPrint (Buffer, sizeof (Buffer), Format, Marker);
102 VA_END (Marker);
103
104 //
105 // Send the print string to a Serial Port
106 //
107 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));
108 }
109
110 /**
111 Find and display image base address and return image base and its entry point.
112
113 @param CurrentEip Current instruction pointer.
114
115 **/
116 VOID
117 DumpModuleImageInfo (
118 IN UINTN CurrentEip
119 )
120 {
121 EFI_STATUS Status;
122 UINTN Pe32Data;
123 VOID *PdbPointer;
124 VOID *EntryPoint;
125
126 Pe32Data = PeCoffSearchImageBase (CurrentEip);
127 if (Pe32Data == 0) {
128 InternalPrintMessage ("!!!! Can't find image information. !!!!\n");
129 } else {
130 //
131 // Find Image Base entry point
132 //
133 Status = PeCoffLoaderGetEntryPoint ((VOID *)Pe32Data, &EntryPoint);
134 if (EFI_ERROR (Status)) {
135 EntryPoint = NULL;
136 }
137
138 InternalPrintMessage ("!!!! Find image based on IP(0x%x) ", CurrentEip);
139 PdbPointer = PeCoffLoaderGetPdbPointer ((VOID *)Pe32Data);
140 if (PdbPointer != NULL) {
141 InternalPrintMessage ("%a", PdbPointer);
142 } else {
143 InternalPrintMessage ("(No PDB) ");
144 }
145
146 InternalPrintMessage (
147 " (ImageBase=%016lp, EntryPoint=%016p) !!!!\n",
148 (VOID *)Pe32Data,
149 EntryPoint
150 );
151 }
152 }
153
154 /**
155 Read and save reserved vector information
156
157 @param[in] VectorInfo Pointer to reserved vector list.
158 @param[out] ReservedVector Pointer to reserved vector data buffer.
159 @param[in] VectorCount Vector number to be updated.
160
161 @return EFI_SUCCESS Read and save vector info successfully.
162 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
163
164 **/
165 EFI_STATUS
166 ReadAndVerifyVectorInfo (
167 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo,
168 OUT RESERVED_VECTORS_DATA *ReservedVector,
169 IN UINTN VectorCount
170 )
171 {
172 while (VectorInfo->Attribute != EFI_VECTOR_HANDOFF_LAST_ENTRY) {
173 if (VectorInfo->Attribute > EFI_VECTOR_HANDOFF_HOOK_AFTER) {
174 //
175 // If vector attrubute is invalid
176 //
177 return EFI_INVALID_PARAMETER;
178 }
179
180 if (VectorInfo->VectorNumber < VectorCount) {
181 ReservedVector[VectorInfo->VectorNumber].Attribute = VectorInfo->Attribute;
182 }
183
184 VectorInfo++;
185 }
186
187 return EFI_SUCCESS;
188 }