]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/PlatformDebugLibIoPort/DebugLib.c
OvmfPkg: Apply uncrustify changes
[mirror_edk2.git] / OvmfPkg / Library / PlatformDebugLibIoPort / DebugLib.c
1 /** @file
2 Base Debug library instance for hypervisor debug port.
3 It uses PrintLib to send debug messages to a fixed I/O port.
4
5 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
6 Copyright (c) 2012, Red Hat, Inc.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include <Base.h>
12 #include <Library/DebugLib.h>
13 #include <Library/BaseLib.h>
14 #include <Library/IoLib.h>
15 #include <Library/PrintLib.h>
16 #include <Library/PcdLib.h>
17 #include <Library/BaseMemoryLib.h>
18 #include <Library/DebugPrintErrorLevelLib.h>
19 #include "DebugLibDetect.h"
20
21 //
22 // Define the maximum debug and assert message length that this library supports
23 //
24 #define MAX_DEBUG_MESSAGE_LENGTH 0x100
25
26 //
27 // VA_LIST can not initialize to NULL for all compiler, so we use this to
28 // indicate a null VA_LIST
29 //
30 VA_LIST mVaListNull;
31
32 /**
33 Prints a debug message to the debug output device if the specified error level is enabled.
34
35 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
36 GetDebugPrintErrorLevel (), then print the message specified by Format and the
37 associated variable argument list to the debug output device.
38
39 If Format is NULL, then ASSERT().
40
41 @param ErrorLevel The error level of the debug message.
42 @param Format Format string for the debug message to print.
43 @param ... Variable argument list whose contents are accessed
44 based on the format string specified by Format.
45
46 **/
47 VOID
48 EFIAPI
49 DebugPrint (
50 IN UINTN ErrorLevel,
51 IN CONST CHAR8 *Format,
52 ...
53 )
54 {
55 VA_LIST Marker;
56
57 VA_START (Marker, Format);
58 DebugVPrint (ErrorLevel, Format, Marker);
59 VA_END (Marker);
60 }
61
62 /**
63 Prints a debug message to the debug output device if the specified
64 error level is enabled base on Null-terminated format string and a
65 VA_LIST argument list or a BASE_LIST argument list.
66
67 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
68 GetDebugPrintErrorLevel (), then print the message specified by Format and
69 the associated variable argument list to the debug output device.
70
71 If Format is NULL, then ASSERT().
72
73 @param ErrorLevel The error level of the debug message.
74 @param Format Format string for the debug message to print.
75 @param VaListMarker VA_LIST marker for the variable argument list.
76 @param BaseListMarker BASE_LIST marker for the variable argument list.
77
78 **/
79 VOID
80 DebugPrintMarker (
81 IN UINTN ErrorLevel,
82 IN CONST CHAR8 *Format,
83 IN VA_LIST VaListMarker,
84 IN BASE_LIST BaseListMarker
85 )
86 {
87 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];
88 UINTN Length;
89
90 //
91 // If Format is NULL, then ASSERT().
92 //
93 ASSERT (Format != NULL);
94
95 //
96 // Check if the global mask disables this message or the device is inactive
97 //
98 if (((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) ||
99 !PlatformDebugLibIoPortFound ())
100 {
101 return;
102 }
103
104 //
105 // Convert the DEBUG() message to an ASCII String
106 //
107 if (BaseListMarker == NULL) {
108 Length = AsciiVSPrint (Buffer, sizeof (Buffer), Format, VaListMarker);
109 } else {
110 Length = AsciiBSPrint (Buffer, sizeof (Buffer), Format, BaseListMarker);
111 }
112
113 //
114 // Send the print string to the debug I/O port
115 //
116 IoWriteFifo8 (PcdGet16 (PcdDebugIoPort), Length, Buffer);
117 }
118
119 /**
120 Prints a debug message to the debug output device if the specified
121 error level is enabled.
122
123 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
124 GetDebugPrintErrorLevel (), then print the message specified by Format and
125 the associated variable argument list to the debug output device.
126
127 If Format is NULL, then ASSERT().
128
129 @param ErrorLevel The error level of the debug message.
130 @param Format Format string for the debug message to print.
131 @param VaListMarker VA_LIST marker for the variable argument list.
132
133 **/
134 VOID
135 EFIAPI
136 DebugVPrint (
137 IN UINTN ErrorLevel,
138 IN CONST CHAR8 *Format,
139 IN VA_LIST VaListMarker
140 )
141 {
142 DebugPrintMarker (ErrorLevel, Format, VaListMarker, NULL);
143 }
144
145 /**
146 Prints a debug message to the debug output device if the specified
147 error level is enabled.
148 This function use BASE_LIST which would provide a more compatible
149 service than VA_LIST.
150
151 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
152 GetDebugPrintErrorLevel (), then print the message specified by Format and
153 the associated variable argument list to the debug output device.
154
155 If Format is NULL, then ASSERT().
156
157 @param ErrorLevel The error level of the debug message.
158 @param Format Format string for the debug message to print.
159 @param BaseListMarker BASE_LIST marker for the variable argument list.
160
161 **/
162 VOID
163 EFIAPI
164 DebugBPrint (
165 IN UINTN ErrorLevel,
166 IN CONST CHAR8 *Format,
167 IN BASE_LIST BaseListMarker
168 )
169 {
170 DebugPrintMarker (ErrorLevel, Format, mVaListNull, BaseListMarker);
171 }
172
173 /**
174 Prints an assert message containing a filename, line number, and description.
175 This may be followed by a breakpoint or a dead loop.
176
177 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
178 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
179 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
180 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
181 CpuDeadLoop() is called. If neither of these bits are set, then this function
182 returns immediately after the message is printed to the debug output device.
183 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
184 processing another DebugAssert(), then DebugAssert() must return immediately.
185
186 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
187 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
188
189 @param FileName The pointer to the name of the source file that generated the assert condition.
190 @param LineNumber The line number in the source file that generated the assert condition
191 @param Description The pointer to the description of the assert condition.
192
193 **/
194 VOID
195 EFIAPI
196 DebugAssert (
197 IN CONST CHAR8 *FileName,
198 IN UINTN LineNumber,
199 IN CONST CHAR8 *Description
200 )
201 {
202 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];
203 UINTN Length;
204
205 //
206 // Generate the ASSERT() message in Ascii format
207 //
208 Length = AsciiSPrint (
209 Buffer,
210 sizeof Buffer,
211 "ASSERT %a(%Lu): %a\n",
212 FileName,
213 (UINT64)LineNumber,
214 Description
215 );
216
217 //
218 // Send the print string to the debug I/O port, if present
219 //
220 if (PlatformDebugLibIoPortFound ()) {
221 IoWriteFifo8 (PcdGet16 (PcdDebugIoPort), Length, Buffer);
222 }
223
224 //
225 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
226 //
227 if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {
228 CpuBreakpoint ();
229 } else if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {
230 CpuDeadLoop ();
231 }
232 }
233
234 /**
235 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
236
237 This function fills Length bytes of Buffer with the value specified by
238 PcdDebugClearMemoryValue, and returns Buffer.
239
240 If Buffer is NULL, then ASSERT().
241 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
242
243 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
244 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
245
246 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
247
248 **/
249 VOID *
250 EFIAPI
251 DebugClearMemory (
252 OUT VOID *Buffer,
253 IN UINTN Length
254 )
255 {
256 //
257 // If Buffer is NULL, then ASSERT().
258 //
259 ASSERT (Buffer != NULL);
260
261 //
262 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer
263 //
264 return SetMem (Buffer, Length, PcdGet8 (PcdDebugClearMemoryValue));
265 }
266
267 /**
268 Returns TRUE if ASSERT() macros are enabled.
269
270 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
271 PcdDebugProperyMask is set. Otherwise FALSE is returned.
272
273 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
274 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
275
276 **/
277 BOOLEAN
278 EFIAPI
279 DebugAssertEnabled (
280 VOID
281 )
282 {
283 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);
284 }
285
286 /**
287 Returns TRUE if DEBUG() macros are enabled.
288
289 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
290 PcdDebugProperyMask is set. Otherwise FALSE is returned.
291
292 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
293 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
294
295 **/
296 BOOLEAN
297 EFIAPI
298 DebugPrintEnabled (
299 VOID
300 )
301 {
302 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);
303 }
304
305 /**
306 Returns TRUE if DEBUG_CODE() macros are enabled.
307
308 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
309 PcdDebugProperyMask is set. Otherwise FALSE is returned.
310
311 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
312 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
313
314 **/
315 BOOLEAN
316 EFIAPI
317 DebugCodeEnabled (
318 VOID
319 )
320 {
321 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);
322 }
323
324 /**
325 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
326
327 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
328 PcdDebugProperyMask is set. Otherwise FALSE is returned.
329
330 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
331 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
332
333 **/
334 BOOLEAN
335 EFIAPI
336 DebugClearMemoryEnabled (
337 VOID
338 )
339 {
340 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);
341 }
342
343 /**
344 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
345
346 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
347
348 @retval TRUE Current ErrorLevel is supported.
349 @retval FALSE Current ErrorLevel is not supported.
350
351 **/
352 BOOLEAN
353 EFIAPI
354 DebugPrintLevelEnabled (
355 IN CONST UINTN ErrorLevel
356 )
357 {
358 return (BOOLEAN)((ErrorLevel & PcdGet32 (PcdFixedDebugPrintErrorLevel)) != 0);
359 }