]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFsp2Pkg/Library/BaseFspDebugLibSerialPort/DebugLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / IntelFsp2Pkg / Library / BaseFspDebugLibSerialPort / DebugLib.c
CommitLineData
cf1d4549
JY
1/** @file\r
2\r
446be24d 3 Copyright (c) 2014 - 2019, Intel Corporation. All rights reserved.<BR>\r
9672cd30 4 SPDX-License-Identifier: BSD-2-Clause-Patent\r
cf1d4549
JY
5\r
6**/\r
7\r
8#include <Base.h>\r
9#include <Library/DebugLib.h>\r
10#include <Library/BaseLib.h>\r
11#include <Library/PrintLib.h>\r
12#include <Library/PcdLib.h>\r
13#include <Library/BaseMemoryLib.h>\r
14#include <Library/SerialPortLib.h>\r
15#include <Library/DebugDeviceLib.h>\r
16#include <Library/DebugPrintErrorLevelLib.h>\r
17\r
18//\r
19// Define the maximum debug and assert message length that this library supports\r
20//\r
21#define MAX_DEBUG_MESSAGE_LENGTH 0x100\r
22\r
23CONST CHAR8 *mHexTable = "0123456789ABCDEF";\r
24\r
446be24d
BB
25//\r
26// VA_LIST can not initialize to NULL for all compiler, so we use this to\r
27// indicate a null VA_LIST\r
28//\r
111f2228 29VA_LIST mVaListNull;\r
446be24d 30\r
cf1d4549
JY
31/**\r
32 Get stack frame pointer of function call.\r
33\r
34 @return StackFramePointer stack frame pointer of function call.\r
35**/\r
0531f613 36UINTN *\r
cf1d4549
JY
37EFIAPI\r
38GetStackFramePointer (\r
39 VOID\r
40 );\r
41\r
cf1d4549
JY
42/**\r
43 Prints a debug message to the debug output device if the specified error level is enabled.\r
44\r
45 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
46 GetDebugPrintErrorLevel (), then print the message specified by Format and the\r
47 associated variable argument list to the debug output device.\r
48\r
49 If Format is NULL, then ASSERT().\r
50\r
51 @param ErrorLevel The error level of the debug message.\r
52 @param Format Format string for the debug message to print.\r
53 @param ... Variable argument list whose contents are accessed\r
54 based on the format string specified by Format.\r
55\r
56**/\r
57VOID\r
58EFIAPI\r
59DebugPrint (\r
60 IN UINTN ErrorLevel,\r
61 IN CONST CHAR8 *Format,\r
62 ...\r
63 )\r
446be24d 64{\r
111f2228 65 VA_LIST Marker;\r
446be24d
BB
66\r
67 VA_START (Marker, Format);\r
68 DebugVPrint (ErrorLevel, Format, Marker);\r
69 VA_END (Marker);\r
70}\r
71\r
72/**\r
73 Prints a debug message to the debug output device if the specified\r
74 error level is enabled base on Null-terminated format string and a\r
75 VA_LIST argument list or a BASE_LIST argument list.\r
76\r
77 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
78 GetDebugPrintErrorLevel (), then print the message specified by Format and\r
79 the associated variable argument list to the debug output device.\r
80\r
81 If Format is NULL, then ASSERT().\r
82\r
83 @param ErrorLevel The error level of the debug message.\r
84 @param Format Format string for the debug message to print.\r
85 @param VaListMarker VA_LIST marker for the variable argument list.\r
86 @param BaseListMarker BASE_LIST marker for the variable argument list.\r
87\r
88**/\r
89VOID\r
90DebugPrintMarker (\r
111f2228
MK
91 IN UINTN ErrorLevel,\r
92 IN CONST CHAR8 *Format,\r
93 IN VA_LIST VaListMarker,\r
94 IN BASE_LIST BaseListMarker\r
446be24d 95 )\r
cf1d4549 96{\r
111f2228 97 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
cf1d4549
JY
98\r
99 //\r
100 // If Format is NULL, then ASSERT().\r
101 //\r
102 if (!GetDebugPrintDeviceEnable ()) {\r
103 return;\r
104 }\r
105\r
106 //\r
107 // Check driver debug mask value and global mask\r
108 //\r
109 if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {\r
110 return;\r
111 }\r
112\r
113 //\r
114 // If Format is NULL, then ASSERT().\r
115 //\r
116 ASSERT (Format != NULL);\r
117\r
118 //\r
119 // Convert the DEBUG() message to an ASCII String\r
120 //\r
446be24d
BB
121 if (BaseListMarker == NULL) {\r
122 AsciiVSPrint (Buffer, sizeof (Buffer), Format, VaListMarker);\r
123 } else {\r
124 AsciiBSPrint (Buffer, sizeof (Buffer), Format, BaseListMarker);\r
125 }\r
cf1d4549
JY
126\r
127 //\r
128 // Send the print string to a Serial Port\r
129 //\r
130 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));\r
131}\r
132\r
446be24d
BB
133/**\r
134 Prints a debug message to the debug output device if the specified\r
135 error level is enabled.\r
136\r
137 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
138 GetDebugPrintErrorLevel (), then print the message specified by Format and\r
139 the associated variable argument list to the debug output device.\r
140\r
141 If Format is NULL, then ASSERT().\r
142\r
143 @param ErrorLevel The error level of the debug message.\r
144 @param Format Format string for the debug message to print.\r
145 @param VaListMarker VA_LIST marker for the variable argument list.\r
146\r
147**/\r
148VOID\r
149EFIAPI\r
150DebugVPrint (\r
111f2228
MK
151 IN UINTN ErrorLevel,\r
152 IN CONST CHAR8 *Format,\r
153 IN VA_LIST VaListMarker\r
446be24d
BB
154 )\r
155{\r
156 DebugPrintMarker (ErrorLevel, Format, VaListMarker, NULL);\r
157}\r
158\r
159/**\r
160 Prints a debug message to the debug output device if the specified\r
161 error level is enabled.\r
162 This function use BASE_LIST which would provide a more compatible\r
163 service than VA_LIST.\r
164\r
165 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
166 GetDebugPrintErrorLevel (), then print the message specified by Format and\r
167 the associated variable argument list to the debug output device.\r
168\r
169 If Format is NULL, then ASSERT().\r
170\r
171 @param ErrorLevel The error level of the debug message.\r
172 @param Format Format string for the debug message to print.\r
173 @param BaseListMarker BASE_LIST marker for the variable argument list.\r
174\r
175**/\r
176VOID\r
177EFIAPI\r
178DebugBPrint (\r
111f2228
MK
179 IN UINTN ErrorLevel,\r
180 IN CONST CHAR8 *Format,\r
181 IN BASE_LIST BaseListMarker\r
446be24d
BB
182 )\r
183{\r
184 DebugPrintMarker (ErrorLevel, Format, mVaListNull, BaseListMarker);\r
185}\r
186\r
cf1d4549 187/**\r
91cc60ba 188 Convert an UINT32 value into HEX string specified by Buffer.\r
cf1d4549
JY
189\r
190 @param Value The HEX value to convert to string\r
191 @param Buffer The pointer to the target buffer to be filled with HEX string\r
192\r
193**/\r
194VOID\r
195FillHex (\r
629709a5
MK
196 UINTN Value,\r
197 CHAR8 *Buffer\r
cf1d4549
JY
198 )\r
199{\r
200 INTN Idx;\r
111f2228 201\r
0531f613 202 for (Idx = (sizeof (UINTN) * 2) - 1; Idx >= 0; Idx--) {\r
cf1d4549 203 Buffer[Idx] = mHexTable[Value & 0x0F];\r
111f2228 204 Value >>= 4;\r
cf1d4549
JY
205 }\r
206}\r
207\r
208/**\r
209 Prints an assert message containing a filename, line number, and description.\r
210 This may be followed by a breakpoint or a dead loop.\r
211\r
212 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"\r
213 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of\r
91cc60ba
AC
214 PcdDebugPropertyMask is set then CpuBreakpoint() is called. Otherwise, if\r
215 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugPropertyMask is set then\r
cf1d4549
JY
216 CpuDeadLoop() is called. If neither of these bits are set, then this function\r
217 returns immediately after the message is printed to the debug output device.\r
218 DebugAssert() must actively prevent recursion. If DebugAssert() is called while\r
219 processing another DebugAssert(), then DebugAssert() must return immediately.\r
220\r
221 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.\r
222 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.\r
223\r
224**/\r
225VOID\r
226DebugAssertInternal (\r
227 VOID\r
228 )\r
229{\r
629709a5
MK
230 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
231 UINTN *Frame;\r
cf1d4549 232\r
0531f613 233 Frame = (UINTN *)GetStackFramePointer ();\r
cf1d4549
JY
234\r
235 //\r
236 // Generate the ASSERT() message in Ascii format\r
237 //\r
0531f613
KT
238 if (sizeof (UINTN) == sizeof (UINT32)) {\r
239 AsciiStrnCpyS (\r
240 Buffer,\r
241 sizeof (Buffer) / sizeof (CHAR8),\r
242 "-> EBP:0x00000000 EIP:0x00000000\n",\r
243 sizeof (Buffer) / sizeof (CHAR8) - 1\r
244 );\r
245 } else {\r
246 AsciiStrnCpyS (\r
247 Buffer,\r
248 sizeof (Buffer) / sizeof (CHAR8),\r
249 "-> RBP:0x0000000000000000 RIP:0x0000000000000000\n",\r
250 sizeof (Buffer) / sizeof (CHAR8) - 1\r
251 );\r
252 }\r
629709a5 253\r
cf1d4549
JY
254 SerialPortWrite ((UINT8 *)"ASSERT DUMP:\n", 13);\r
255 while (Frame != NULL) {\r
0531f613
KT
256 FillHex ((UINTN)Frame, Buffer + 9);\r
257 FillHex (Frame[1], Buffer + 9 + (sizeof (UINTN) * 2) + 8);\r
cf1d4549 258 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));\r
0531f613
KT
259 if ((Frame[0] > (UINTN)Frame) && (Frame[0] < (UINTN)Frame + 0x00100000)) {\r
260 Frame = (UINTN *)Frame[0];\r
cf1d4549
JY
261 } else {\r
262 Frame = NULL;\r
263 }\r
264 }\r
265\r
266 //\r
267 // Dead loop\r
268 //\r
269 CpuDeadLoop ();\r
270}\r
271\r
272/**\r
273 Prints an assert message containing a filename, line number, and description.\r
274 This may be followed by a breakpoint or a dead loop.\r
275\r
276 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"\r
277 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of\r
91cc60ba
AC
278 PcdDebugPropertyMask is set then CpuBreakpoint() is called. Otherwise, if\r
279 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugPropertyMask is set then\r
cf1d4549
JY
280 CpuDeadLoop() is called. If neither of these bits are set, then this function\r
281 returns immediately after the message is printed to the debug output device.\r
282 DebugAssert() must actively prevent recursion. If DebugAssert() is called while\r
283 processing another DebugAssert(), then DebugAssert() must return immediately.\r
284\r
285 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.\r
286 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.\r
287\r
288 @param FileName The pointer to the name of the source file that generated the assert condition.\r
289 @param LineNumber The line number in the source file that generated the assert condition\r
290 @param Description The pointer to the description of the assert condition.\r
291\r
292**/\r
293VOID\r
294EFIAPI\r
295DebugAssert (\r
296 IN CONST CHAR8 *FileName,\r
297 IN UINTN LineNumber,\r
298 IN CONST CHAR8 *Description\r
299 )\r
300{\r
301 DebugAssertInternal ();\r
302}\r
303\r
cf1d4549
JY
304/**\r
305 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.\r
306\r
307 This function fills Length bytes of Buffer with the value specified by\r
308 PcdDebugClearMemoryValue, and returns Buffer.\r
309\r
310 If Buffer is NULL, then ASSERT().\r
311 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
312\r
313 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.\r
314 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.\r
315\r
316 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.\r
317\r
318**/\r
319VOID *\r
320EFIAPI\r
321DebugClearMemory (\r
322 OUT VOID *Buffer,\r
323 IN UINTN Length\r
324 )\r
325{\r
326 return Buffer;\r
327}\r
328\r
cf1d4549
JY
329/**\r
330 Returns TRUE if ASSERT() macros are enabled.\r
331\r
332 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of\r
91cc60ba 333 PcdDebugPropertyMask is set. Otherwise FALSE is returned.\r
cf1d4549 334\r
91cc60ba
AC
335 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugPropertyMask is set.\r
336 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugPropertyMask is clear.\r
cf1d4549
JY
337\r
338**/\r
339BOOLEAN\r
340EFIAPI\r
341DebugAssertEnabled (\r
342 VOID\r
343 )\r
344{\r
111f2228 345 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);\r
cf1d4549
JY
346}\r
347\r
cf1d4549
JY
348/**\r
349 Returns TRUE if DEBUG() macros are enabled.\r
350\r
351 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of\r
91cc60ba 352 PcdDebugPropertyMask is set. Otherwise FALSE is returned.\r
cf1d4549 353\r
91cc60ba
AC
354 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugPropertyMask is set.\r
355 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugPropertyMask is clear.\r
cf1d4549
JY
356\r
357**/\r
358BOOLEAN\r
359EFIAPI\r
360DebugPrintEnabled (\r
361 VOID\r
362 )\r
363{\r
111f2228 364 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);\r
cf1d4549
JY
365}\r
366\r
367/**\r
368 Returns TRUE if DEBUG_CODE() macros are enabled.\r
369\r
370 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of\r
91cc60ba 371 PcdDebugPropertyMask is set. Otherwise FALSE is returned.\r
cf1d4549 372\r
91cc60ba
AC
373 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugPropertyMask is set.\r
374 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugPropertyMask is clear.\r
cf1d4549
JY
375\r
376**/\r
377BOOLEAN\r
378EFIAPI\r
379DebugCodeEnabled (\r
380 VOID\r
381 )\r
382{\r
111f2228 383 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);\r
cf1d4549
JY
384}\r
385\r
cf1d4549
JY
386/**\r
387 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.\r
388\r
389 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of\r
91cc60ba 390 PcdDebugPropertyMask is set. Otherwise FALSE is returned.\r
cf1d4549 391\r
91cc60ba
AC
392 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugPropertyMask is set.\r
393 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugPropertyMask is clear.\r
cf1d4549
JY
394\r
395**/\r
396BOOLEAN\r
397EFIAPI\r
398DebugClearMemoryEnabled (\r
399 VOID\r
400 )\r
401{\r
111f2228 402 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);\r
cf1d4549
JY
403}\r
404\r
405/**\r
406 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
407\r
408 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
409\r
410 @retval TRUE Current ErrorLevel is supported.\r
411 @retval FALSE Current ErrorLevel is not supported.\r
412\r
413**/\r
414BOOLEAN\r
415EFIAPI\r
416DebugPrintLevelEnabled (\r
111f2228 417 IN CONST UINTN ErrorLevel\r
cf1d4549
JY
418 )\r
419{\r
111f2228 420 return (BOOLEAN)((ErrorLevel & PcdGet32 (PcdFixedDebugPrintErrorLevel)) != 0);\r
cf1d4549 421}\r