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