]> git.proxmox.com Git - mirror_edk2.git/blame - SourceLevelDebugPkg/Library/DebugAgent/DebugAgentCommon/DebugAgent.h
SignedCapsulePkg: Apply uncrustify changes
[mirror_edk2.git] / SourceLevelDebugPkg / Library / DebugAgent / DebugAgentCommon / DebugAgent.h
CommitLineData
18b144ea 1/** @file\r
2 Command header of for Debug Agent library instance.\r
3\r
77695f4d 4 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r
85f7e110 5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
18b144ea 6\r
7**/\r
8\r
9#ifndef _DEBUG_AGENT_H_\r
10#define _DEBUG_AGENT_H_\r
11\r
12#include <Register/LocalApic.h>\r
18b144ea 13#include <Guid/DebugAgentGuid.h>\r
8cc26df4
JF
14#include <Guid/VectorHandoffTable.h>\r
15#include <Ppi/VectorHandoffInfo.h>\r
18b144ea 16#include <Library/BaseLib.h>\r
17#include <Library/BaseMemoryLib.h>\r
18#include <Library/ResetSystemLib.h>\r
19#include <Library/IoLib.h>\r
20#include <Library/HobLib.h>\r
21#include <Library/DebugCommunicationLib.h>\r
22#include <Library/DebugAgentLib.h>\r
23#include <Library/PcdLib.h>\r
24#include <Library/SynchronizationLib.h>\r
25#include <Library/LocalApicLib.h>\r
e2104834 26#include <Library/DebugLib.h>\r
b4ebbaff 27#include <Library/TimerLib.h>\r
93c0bdec 28#include <Library/PrintLib.h>\r
b422b62c 29#include <Library/PeCoffGetEntryPointLib.h>\r
30#include <Library/PeCoffExtraActionLib.h>\r
a742e186 31#include <Register/ArchitecturalMsr.h>\r
18b144ea 32\r
33#include <TransferProtocol.h>\r
34#include <ImageDebugSupport.h>\r
35\r
36#include "DebugMp.h"\r
37#include "DebugTimer.h"\r
38#include "ArchDebugSupport.h"\r
8cc26df4 39#include "DebugException.h"\r
18b144ea 40\r
b422b62c 41//\r
42// These macros may be already defined in DebugAgentLib.h\r
43//\r
44#define DEBUG_AGENT_INIT_PEI 9\r
45#define DEBUG_AGENT_INIT_DXE_LOAD 10\r
46#define DEBUG_AGENT_INIT_DXE_UNLOAD 11\r
47#define DEBUG_AGENT_INIT_THUNK_PEI_IA32TOX64 12\r
48\r
8cc26df4
JF
49#define DEBUG_INT1_VECTOR DEBUG_EXCEPT_DEBUG\r
50#define DEBUG_INT3_VECTOR DEBUG_EXCEPT_BREAKPOINT\r
18b144ea 51#define DEBUG_TIMER_VECTOR 32\r
52#define DEBUG_MAILBOX_VECTOR 33\r
53\r
b422b62c 54//\r
55// Timeout value for reading packet (unit is microsecond)\r
56//\r
57#define READ_PACKET_TIMEOUT (500 * 1000)\r
08021523 58#define DEBUG_TIMER_INTERVAL (100 * 1000)\r
b422b62c 59\r
18b144ea 60#define SOFT_INTERRUPT_SIGNATURE SIGNATURE_32('S','O','F','T')\r
61#define SYSTEM_RESET_SIGNATURE SIGNATURE_32('S','Y','S','R')\r
62#define MEMORY_READY_SIGNATURE SIGNATURE_32('M','E','M','R')\r
63\r
64extern UINTN Exception0Handle;\r
65extern UINTN TimerInterruptHandle;\r
19ee4a90 66extern UINT32 ExceptionStubHeaderSize;\r
b422b62c 67extern BOOLEAN mSkipBreakpoint;\r
8cc26df4
JF
68extern EFI_VECTOR_HANDOFF_INFO mVectorHandoffInfoDebugAgent[];\r
69extern UINTN mVectorHandoffInfoCount;\r
b422b62c 70\r
93c0bdec 71//\r
72// CPU exception information issued by debug agent\r
73//\r
74typedef struct {\r
75 //\r
76 // This field is used to save CPU content before executing HOST command\r
77 //\r
78 BASE_LIBRARY_JUMP_BUFFER JumpBuffer;\r
79 //\r
ff8ad584 80 // This field returns the exception information issued by the HOST command\r
93c0bdec 81 //\r
82 DEBUG_DATA_RESPONSE_GET_EXCEPTION ExceptionContent;\r
83} DEBUG_AGENT_EXCEPTION_BUFFER;\r
18b144ea 84\r
b422b62c 85#define DEBUG_AGENT_FLAG_HOST_ATTACHED BIT0\r
86#define DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS BIT1\r
87#define DEBUG_AGENT_FLAG_MEMORY_READY BIT2\r
88#define DEBUG_AGENT_FLAG_STEPPING BIT3\r
89#define DEBUG_AGENT_FLAG_CHECK_MAILBOX_IN_HOB BIT4\r
0a488765 90#define DEBUG_AGENT_FLAG_INIT_ARCH BIT5|BIT6\r
5d51ba26 91#define DEBUG_AGENT_FLAG_INTERRUPT_FLAG BIT7\r
b422b62c 92#define DEBUG_AGENT_FLAG_BREAK_ON_NEXT_SMI BIT32\r
93#define DEBUG_AGENT_FLAG_PRINT_ERROR_LEVEL (BIT33|BIT34|BIT35|BIT36)\r
94#define DEBUG_AGENT_FLAG_BREAK_BOOT_SCRIPT BIT37\r
95\r
96#define DEBUG_MAILBOX_DEBUG_FLAG_INDEX 1\r
97#define DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX 2\r
98#define DEBUG_MAILBOX_EXCEPTION_BUFFER_POINTER_INDEX 3\r
99#define DEBUG_MAILBOX_LAST_ACK 4\r
100#define DEBUG_MAILBOX_SEQUENCE_NO_INDEX 5\r
101#define DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX 6\r
08021523 102#define DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY 7\r
93c0bdec 103\r
b422b62c 104#pragma pack(1)\r
105typedef union {\r
106 struct {\r
107 //\r
108 // Lower 32 bits to store the status of DebugAgent\r
109 //\r
110 UINT32 HostAttached : 1; // 1: HOST is attached\r
111 UINT32 AgentInProgress : 1; // 1: Debug Agent is communicating with HOST\r
112 UINT32 MemoryReady : 1; // 1: Memory is ready\r
113 UINT32 SteppingFlag : 1; // 1: Agent is running stepping command\r
114 UINT32 CheckMailboxInHob : 1; // 1: Need to check mailbox saved in HOB\r
0a488765 115 UINT32 InitArch : 2; // value of DEBUG_DATA_RESPONSE_ARCH_MODE\r
5d51ba26
JF
116 UINT32 InterruptFlag : 1; // 1: EFLAGS.IF is set\r
117 UINT32 Reserved1 : 24;\r
b422b62c 118 //\r
119 // Higher 32bits to control the behavior of DebugAgent\r
120 //\r
121 UINT32 BreakOnNextSmi : 1; // 1: Break on next SMI\r
122 UINT32 PrintErrorLevel : 4; // Bitmask of print error level for debug message\r
77695f4d 123 UINT32 BreakOnBootScript : 1; // 1: Break before executing boot script\r
b422b62c 124 UINT32 Reserved2 : 26;\r
125 } Bits;\r
126 UINT64 Uint64;\r
93c0bdec 127} DEBUG_AGENT_FLAG;\r
128\r
18b144ea 129typedef struct {\r
130 DEBUG_AGENT_FLAG DebugFlag;\r
131 UINT64 DebugPortHandle;\r
93c0bdec 132 //\r
133 // Pointer to DEBUG_AGENT_EXCEPTION_BUFFER\r
134 //\r
135 UINT64 ExceptionBufferPointer;\r
b422b62c 136 UINT8 LastAck; // The last ack packet type\r
137 UINT8 SequenceNo;\r
138 UINT8 HostSequenceNo;\r
08021523 139 UINT32 DebugTimerFrequency;\r
b422b62c 140 UINT8 CheckSum; // Mailbox checksum\r
141 UINT8 ToBeCheckSum; // To be Mailbox checksum at the next\r
18b144ea 142} DEBUG_AGENT_MAILBOX;\r
143#pragma pack()\r
144\r
b422b62c 145///\r
146/// Byte packed structure for an IA-32 Interrupt Gate Descriptor.\r
147///\r
148typedef union {\r
149 struct {\r
150 UINT32 OffsetLow:16; ///< Offset bits 15..0.\r
151 UINT32 Selector:16; ///< Selector.\r
152 UINT32 Reserved_0:8; ///< Reserved.\r
153 UINT32 GateType:8; ///< Gate Type. See #defines above.\r
154 UINT32 OffsetHigh:16; ///< Offset bits 31..16.\r
155 } Bits;\r
156 UINT64 Uint64;\r
157} IA32_IDT_ENTRY;\r
158\r
159\r
18b144ea 160typedef union {\r
161 struct {\r
162 UINT32 LimitLow : 16;\r
163 UINT32 BaseLow : 16;\r
164 UINT32 BaseMid : 8;\r
165 UINT32 Type : 4;\r
166 UINT32 System : 1;\r
167 UINT32 Dpl : 2;\r
168 UINT32 Present : 1;\r
169 UINT32 LimitHigh : 4;\r
170 UINT32 Software : 1;\r
171 UINT32 Reserved : 1;\r
172 UINT32 DefaultSize : 1;\r
173 UINT32 Granularity : 1;\r
174 UINT32 BaseHigh : 8;\r
175 } Bits;\r
176 UINT64 Uint64;\r
177} IA32_GDT;\r
178\r
18b144ea 179/**\r
180 Initialize IDT entries to support source level debug.\r
181\r
182**/\r
183VOID\r
184InitializeDebugIdt (\r
185 VOID\r
186 );\r
187\r
18b144ea 188/**\r
189 Read register value from saved CPU context.\r
190\r
191 @param[in] CpuContext Pointer to saved CPU context.\r
192 @param[in] Index Register index value.\r
18b144ea 193 @param[in] Width Data width to read.\r
194\r
195 @return The address of register value.\r
196\r
197**/\r
198UINT8 *\r
199ArchReadRegisterBuffer (\r
200 IN DEBUG_CPU_CONTEXT *CpuContext,\r
201 IN UINT8 Index,\r
18b144ea 202 IN UINT8 *Width\r
203 );\r
204\r
205/**\r
206 Send packet with response data to HOST.\r
207\r
d9044ec5
RN
208 @param[in] Data Pointer to response data buffer.\r
209 @param[in] DataSize Size of response data in byte.\r
210 @param[in, out] DebugHeader Pointer to a buffer for creating response packet and receiving ACK packet,\r
211 to minimize the stack usage.\r
18b144ea 212\r
213 @retval RETURN_SUCCESS Response data was sent successfully.\r
214 @retval RETURN_DEVICE_ERROR Cannot receive DEBUG_COMMAND_OK from HOST.\r
215\r
216**/\r
217RETURN_STATUS\r
218SendDataResponsePacket (\r
d9044ec5
RN
219 IN UINT8 *Data,\r
220 IN UINT16 DataSize,\r
221 IN OUT DEBUG_PACKET_HEADER *DebugHeader\r
18b144ea 222 );\r
223\r
224/**\r
93c0bdec 225 Check if HOST is attached based on Mailbox.\r
18b144ea 226\r
93c0bdec 227 @retval TRUE HOST is attached.\r
228 @retval FALSE HOST is not attached.\r
18b144ea 229\r
230**/\r
93c0bdec 231BOOLEAN\r
232IsHostAttached (\r
233 VOID\r
18b144ea 234 );\r
235\r
236/**\r
93c0bdec 237 Get Debug Agent Mailbox pointer.\r
18b144ea 238\r
93c0bdec 239 @return Mailbox pointer.\r
18b144ea 240\r
241**/\r
93c0bdec 242DEBUG_AGENT_MAILBOX *\r
243GetMailboxPointer (\r
244 VOID\r
18b144ea 245 );\r
246\r
247/**\r
93c0bdec 248 Get debug port handle.\r
18b144ea 249\r
93c0bdec 250 @return Debug port handle.\r
18b144ea 251\r
252**/\r
93c0bdec 253DEBUG_PORT_HANDLE\r
254GetDebugPortHandle (\r
255 VOID\r
18b144ea 256 );\r
257\r
258/**\r
93c0bdec 259 Read the Attach/Break-in symbols from the debug port.\r
18b144ea 260\r
93c0bdec 261 @param[in] Handle Pointer to Debug Port handle.\r
262 @param[out] BreakSymbol Returned break symbol.\r
18b144ea 263\r
93c0bdec 264 @retval EFI_SUCCESS Read the symbol in BreakSymbol.\r
265 @retval EFI_NOT_FOUND No read the break symbol.\r
18b144ea 266\r
267**/\r
93c0bdec 268EFI_STATUS\r
269DebugReadBreakSymbol (\r
270 IN DEBUG_PORT_HANDLE Handle,\r
271 OUT UINT8 *BreakSymbol\r
18b144ea 272 );\r
273\r
274/**\r
93c0bdec 275 Prints a debug message to the debug port if the specified error level is enabled.\r
18b144ea 276\r
93c0bdec 277 If any bit in ErrorLevel is also set in Mainbox, then print the message specified\r
278 by Format and the associated variable argument list to the debug port.\r
18b144ea 279\r
93c0bdec 280 @param[in] ErrorLevel The error level of the debug message.\r
281 @param[in] Format Format string for the debug message to print.\r
77695f4d 282 @param[in] ... Variable argument list whose contents are accessed\r
93c0bdec 283 based on the format string specified by Format.\r
18b144ea 284\r
285**/\r
93c0bdec 286VOID\r
287EFIAPI\r
288DebugAgentMsgPrint (\r
289 IN UINT8 ErrorLevel,\r
290 IN CHAR8 *Format,\r
291 ...\r
18b144ea 292 );\r
b422b62c 293\r
294/**\r
295 Trigger one software interrupt to debug agent to handle it.\r
296\r
297 @param[in] Signature Software interrupt signature.\r
298\r
299**/\r
300VOID\r
301TriggerSoftInterrupt (\r
302 IN UINT32 Signature\r
303 );\r
304\r
305/**\r
306 Check if debug agent support multi-processor.\r
307\r
308 @retval TRUE Multi-processor is supported.\r
309 @retval FALSE Multi-processor is not supported.\r
310\r
311**/\r
312BOOLEAN\r
313MultiProcessorDebugSupport (\r
314 VOID\r
315 );\r
316\r
317/**\r
318 Find and report module image info to HOST.\r
77695f4d 319\r
b422b62c 320 @param[in] AlignSize Image aligned size.\r
77695f4d 321\r
b422b62c 322**/\r
77695f4d 323VOID\r
b422b62c 324FindAndReportModuleImageInfo (\r
77695f4d 325 IN UINTN AlignSize\r
b422b62c 326 );\r
327\r
328/**\r
329 Read IDT entry to check if IDT entries are setup by Debug Agent.\r
330\r
331 @retval TRUE IDT entries were setup by Debug Agent.\r
332 @retval FALSE IDT entries were not setup by Debug Agent.\r
333\r
334**/\r
77695f4d 335BOOLEAN\r
b422b62c 336IsDebugAgentInitialzed (\r
337 VOID\r
338 );\r
339\r
340/**\r
12e4c407 341 Calculate Mailbox checksum and update the checksum field.\r
b422b62c 342\r
343 @param[in] Mailbox Debug Agent Mailbox pointer.\r
344\r
345**/\r
346VOID\r
347UpdateMailboxChecksum (\r
348 IN DEBUG_AGENT_MAILBOX *Mailbox\r
349 );\r
350\r
351/**\r
352 Verify Mailbox checksum.\r
353\r
354 If checksum error, print debug message and run init dead loop.\r
355\r
356 @param[in] Mailbox Debug Agent Mailbox pointer.\r
357\r
358**/\r
77695f4d 359VOID\r
b422b62c 360VerifyMailboxChecksum (\r
361 IN DEBUG_AGENT_MAILBOX *Mailbox\r
362 );\r
363\r
364/**\r
365 Set debug flag in mailbox.\r
366\r
367 @param[in] FlagMask Debug flag mask value.\r
368 @param[in] FlagValue Debug flag value.\r
369\r
370**/\r
77695f4d 371VOID\r
b422b62c 372SetDebugFlag (\r
373 IN UINT64 FlagMask,\r
77695f4d 374 IN UINT32 FlagValue\r
b422b62c 375 );\r
376\r
377/**\r
378 Get debug flag in mailbox.\r
379\r
380 @param[in] FlagMask Debug flag mask value.\r
77695f4d 381\r
b422b62c 382 @return Debug flag value.\r
383\r
384**/\r
385UINT32\r
386GetDebugFlag (\r
387 IN UINT64 FlagMask\r
388 );\r
389\r
390/**\r
391 Update Mailbox content by index.\r
392\r
393 @param[in] Mailbox Debug Agent Mailbox pointer.\r
394 @param[in] Index Mailbox content index.\r
395 @param[in] Value Value to be set into mail box.\r
77695f4d 396\r
b422b62c 397**/\r
398VOID\r
77695f4d 399UpdateMailboxContent (\r
b422b62c 400 IN DEBUG_AGENT_MAILBOX *Mailbox,\r
401 IN UINTN Index,\r
402 IN UINT64 Value\r
403 );\r
404\r
405/**\r
406 Retrieve exception handler from IDT table by ExceptionNum.\r
407\r
408 @param[in] ExceptionNum Exception number\r
77695f4d 409\r
b422b62c 410 @return Exception handler\r
411\r
412**/\r
413VOID *\r
414GetExceptionHandlerInIdtEntry (\r
415 IN UINTN ExceptionNum\r
416 );\r
417\r
418/**\r
419 Set exception handler in IDT table by ExceptionNum.\r
420\r
421 @param[in] ExceptionNum Exception number\r
77695f4d 422 @param[in] ExceptionHandler Exception Handler to be set\r
b422b62c 423\r
424**/\r
425VOID\r
426SetExceptionHandlerInIdtEntry (\r
427 IN UINTN ExceptionNum,\r
428 IN VOID *ExceptionHandler\r
429 );\r
430\r
431/**\r
432 Prints a debug message to the debug output device if the specified error level is enabled.\r
433\r
77695f4d
LG
434 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
435 GetDebugPrintErrorLevel (), then print the message specified by Format and the\r
b422b62c 436 associated variable argument list to the debug output device.\r
437\r
438 If Format is NULL, then ASSERT().\r
439\r
440 @param[in] ErrorLevel The error level of the debug message.\r
441 @param[in] IsSend Flag of debug message to declare that the data is being sent or being received.\r
77695f4d 442 @param[in] Data Variable argument list whose contents are accessed\r
b422b62c 443 @param[in] Length based on the format string specified by Format.\r
444\r
445**/\r
446VOID\r
447EFIAPI\r
448DebugAgentDataMsgPrint (\r
449 IN UINT8 ErrorLevel,\r
450 IN BOOLEAN IsSend,\r
451 IN UINT8 *Data,\r
77695f4d 452 IN UINT8 Length\r
b422b62c 453 );\r
454\r
455/**\r
456 Read remaing debug packet except for the start symbol\r
457\r
458 @param[in] Handle Pointer to Debug Port handle.\r
459 @param[in, out] DebugHeader Debug header buffer including start symbol.\r
460\r
461 @retval EFI_SUCCESS Read the symbol in BreakSymbol.\r
462 @retval EFI_CRC_ERROR CRC check fail.\r
463 @retval EFI_TIMEOUT Timeout occurs when reading debug packet.\r
464\r
465**/\r
466EFI_STATUS\r
467ReadRemainingBreakPacket (\r
468 IN DEBUG_PORT_HANDLE Handle,\r
469 IN OUT DEBUG_PACKET_HEADER *DebugHeader\r
470 );\r
471\r
08021523
JF
472/**\r
473 Read data from debug channel and save the data in buffer.\r
474\r
475 Reads NumberOfBytes data bytes from a debug device into the buffer\r
476 specified by Buffer. The number of bytes actually read is returned.\r
477 If the return value is less than NumberOfBytes, then the rest operation failed.\r
478 If NumberOfBytes is zero, then return 0.\r
479\r
480 @param Handle Debug port handle.\r
481 @param Buffer Pointer to the data buffer to store the data read from the debug device.\r
482 @param NumberOfBytes Number of bytes which will be read.\r
483 @param Timeout Timeout value for reading from debug device. It unit is Microsecond.\r
484\r
485 @retval 0 Read data failed, no data is to be read.\r
486 @retval >0 Actual number of bytes read from debug device.\r
487\r
488**/\r
489UINTN\r
490DebugAgentReadBuffer (\r
491 IN DEBUG_PORT_HANDLE Handle,\r
492 IN OUT UINT8 *Buffer,\r
493 IN UINTN NumberOfBytes,\r
494 IN UINTN Timeout\r
495 );\r
496\r
18b144ea 497#endif\r
498\r