2 Commond Debug Agent library implementition. It mainly includes
3 the first C function called by exception/interrupt handlers,
4 read/write debug packet to communication with HOST based on transfer
7 Copyright (c) 2010 - 2013, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php.
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include "DebugAgent.h"
19 #include "Ia32/DebugException.h"
21 CHAR8 mErrorMsgVersionAlert
[] = "\rThe SourceLevelDebugPkg you are using requires a newer version of the Intel(R) UDK Debugger Tool.\r\n";
22 CHAR8 mErrorMsgSendInitPacket
[] = "\rSend INIT break packet and try to connect the HOST (Intel(R) UDK Debugger Tool v1.3) ...\r\n";
23 CHAR8 mErrorMsgConnectOK
[] = "HOST connection is successful!\r\n";
24 CHAR8 mErrorMsgConnectFail
[] = "HOST connection is failed!\r\n";
25 CHAR8 mWarningMsgIngoreBreakpoint
[] = "Ignore break point in SMM for SMI issued during DXE debugging!\r\n";
28 Calculate CRC16 for target data.
30 @param[in] Data The target data.
31 @param[in] DataSize The target data size.
32 @param[in] Crc Initial CRC.
34 @return UINT16 The CRC16 value.
47 for (Index
= 0; Index
< DataSize
; Index
++) {
49 for (BitIndex
= 0; BitIndex
< 8; BitIndex
++) {
50 if ((Crc
& 0x8000) != 0) {
63 Read IDT entry to check if IDT entries are setup by Debug Agent.
65 @retval TRUE IDT entries were setup by Debug Agent.
66 @retval FALSE IDT entries were not setup by Debug Agent.
70 IsDebugAgentInitialzed (
74 UINTN InterruptHandler
;
76 InterruptHandler
= (UINTN
) GetExceptionHandlerInIdtEntry (0);
77 if (InterruptHandler
>= 4 && *(UINT32
*)(InterruptHandler
- 4) == AGENT_HANDLER_SIGNATURE
) {
85 Find and report module image info to HOST.
87 @param[in] AlignSize Image aligned size.
91 FindAndReportModuleImageInfo (
96 EFI_IMAGE_DOS_HEADER
*DosHdr
;
97 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
98 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
103 Pe32Data
= ((UINTN
)mErrorMsgVersionAlert
) & ~(AlignSize
- 1);
104 while (Pe32Data
!= 0) {
105 DosHdr
= (EFI_IMAGE_DOS_HEADER
*) Pe32Data
;
106 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
108 // DOS image header is present, so read the PE header after the DOS image header.
109 // Check if address overflow firstly.
111 if ((MAX_ADDRESS
- (UINTN
)DosHdr
->e_lfanew
) > Pe32Data
) {
112 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)(Pe32Data
+ (UINTN
)(DosHdr
->e_lfanew
));
113 if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
122 // DOS image header is not present, TE header is at the image base.
124 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
125 if ((Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) &&
126 ((Hdr
.Te
->Machine
== IMAGE_FILE_MACHINE_I386
) || Hdr
.Te
->Machine
== IMAGE_FILE_MACHINE_X64
)) {
128 // It's TE image, it TE header and Machine type match
135 // Not found the image base, check the previous aligned address
137 Pe32Data
-= AlignSize
;
140 ImageContext
.ImageAddress
= Pe32Data
;
141 ImageContext
.PdbPointer
= PeCoffLoaderGetPdbPointer ((VOID
*) (UINTN
) ImageContext
.ImageAddress
);
142 PeCoffLoaderRelocateImageExtraAction (&ImageContext
);
146 Trigger one software interrupt to debug agent to handle it.
148 @param[in] Signature Software interrupt signature.
152 TriggerSoftInterrupt (
160 // Save Debug Register State
168 AsmWriteDr0 (SOFT_INTERRUPT_SIGNATURE
);
169 AsmWriteDr1 (Signature
);
172 // Do INT3 to communicate with HOST side
177 // Restore Debug Register State only when Host didn't change it inside exception handler.
178 // Dr registers can only be changed by setting the HW breakpoint.
186 Caculate Mailbox checksum and update the checksum field.
188 @param[in] Mailbox Debug Agent Mailbox pointer.
192 UpdateMailboxChecksum (
193 IN DEBUG_AGENT_MAILBOX
*Mailbox
196 Mailbox
->CheckSum
= CalculateCheckSum8 ((UINT8
*)Mailbox
, sizeof (DEBUG_AGENT_MAILBOX
) - 2);
200 Verify Mailbox checksum.
202 If checksum error, print debug message and run init dead loop.
204 @param[in] Mailbox Debug Agent Mailbox pointer.
208 VerifyMailboxChecksum (
209 IN DEBUG_AGENT_MAILBOX
*Mailbox
214 CheckSum
= CalculateCheckSum8 ((UINT8
*) Mailbox
, sizeof (DEBUG_AGENT_MAILBOX
) - 2);
216 // The checksum updating process may be disturbed by hardware SMI, we need to check CheckSum field
217 // and ToBeCheckSum field to validate the mail box.
219 if (CheckSum
!= Mailbox
->CheckSum
&& CheckSum
!= Mailbox
->ToBeCheckSum
) {
220 DEBUG ((EFI_D_ERROR
, "DebugAgent: Mailbox checksum error, stack or heap crashed!\n"));
221 DEBUG ((EFI_D_ERROR
, "DebugAgent: CheckSum = %x, Mailbox->CheckSum = %x, Mailbox->ToBeCheckSum = %x\n", CheckSum
, Mailbox
->CheckSum
, Mailbox
->ToBeCheckSum
));
227 Update Mailbox content by index.
229 @param[in] Mailbox Debug Agent Mailbox pointer.
230 @param[in] Index Mailbox content index.
231 @param[in] Value Value to be set into Mailbox.
235 UpdateMailboxContent (
236 IN DEBUG_AGENT_MAILBOX
*Mailbox
,
241 AcquireMpSpinLock (&mDebugMpContext
.MailboxSpinLock
);
243 case DEBUG_MAILBOX_DEBUG_FLAG_INDEX
:
244 Mailbox
->ToBeCheckSum
= Mailbox
->CheckSum
+ CalculateSum8 ((UINT8
*)&Mailbox
->DebugFlag
.Uint64
, sizeof(UINT64
))
245 - CalculateSum8 ((UINT8
*)&Value
, sizeof(UINT64
));
246 Mailbox
->DebugFlag
.Uint64
= Value
;
248 case DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX
:
249 Mailbox
->ToBeCheckSum
= Mailbox
->CheckSum
+ CalculateSum8 ((UINT8
*)&Mailbox
->DebugPortHandle
, sizeof(UINTN
))
250 - CalculateSum8 ((UINT8
*)&Value
, sizeof(UINTN
));
251 Mailbox
->DebugPortHandle
= (UINTN
) Value
;
253 case DEBUG_MAILBOX_EXCEPTION_BUFFER_POINTER_INDEX
:
254 Mailbox
->ToBeCheckSum
= Mailbox
->CheckSum
+ CalculateSum8 ((UINT8
*)&Mailbox
->ExceptionBufferPointer
, sizeof(UINTN
))
255 - CalculateSum8 ((UINT8
*)&Value
, sizeof(UINTN
));
256 Mailbox
->ExceptionBufferPointer
= (UINTN
) Value
;
258 case DEBUG_MAILBOX_LAST_ACK
:
259 Mailbox
->ToBeCheckSum
= Mailbox
->CheckSum
+ CalculateSum8 ((UINT8
*)&Mailbox
->LastAck
, sizeof(UINT8
))
260 - CalculateSum8 ((UINT8
*)&Value
, sizeof(UINT8
));
261 Mailbox
->LastAck
= (UINT8
) Value
;
263 case DEBUG_MAILBOX_SEQUENCE_NO_INDEX
:
264 Mailbox
->ToBeCheckSum
= Mailbox
->CheckSum
+ CalculateSum8 ((UINT8
*)&Mailbox
->SequenceNo
, sizeof(UINT8
))
265 - CalculateSum8 ((UINT8
*)&Value
, sizeof(UINT8
));
266 Mailbox
->SequenceNo
= (UINT8
) Value
;
268 case DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX
:
269 Mailbox
->ToBeCheckSum
= Mailbox
->CheckSum
+ CalculateSum8 ((UINT8
*)&Mailbox
->HostSequenceNo
, sizeof(UINT8
))
270 - CalculateSum8 ((UINT8
*)&Value
, sizeof(UINT8
));
271 Mailbox
->HostSequenceNo
= (UINT8
) Value
;
274 UpdateMailboxChecksum (Mailbox
);
275 ReleaseMpSpinLock (&mDebugMpContext
.MailboxSpinLock
);
278 Set debug flag in mailbox.
280 @param[in] FlagMask Debug flag mask value.
281 @param[in] FlagValue Debug flag value.
290 DEBUG_AGENT_MAILBOX
*Mailbox
;
293 Mailbox
= GetMailboxPointer ();
294 Data64
= (Mailbox
->DebugFlag
.Uint64
& ~FlagMask
) |
295 (LShiftU64 ((UINT64
)FlagValue
, LowBitSet64 (FlagMask
)) & FlagMask
);
296 UpdateMailboxContent (Mailbox
, DEBUG_MAILBOX_DEBUG_FLAG_INDEX
, Data64
);
300 Get debug flag in mailbox.
302 @param[in] FlagMask Debug flag mask value.
304 @return Debug flag value.
312 DEBUG_AGENT_MAILBOX
*Mailbox
;
315 Mailbox
= GetMailboxPointer ();
316 DebugFlag
= (UINT32
) RShiftU64 (Mailbox
->DebugFlag
.Uint64
& FlagMask
, LowBitSet64 (FlagMask
));
322 Send a debug message packet to the debug port.
324 @param[in] Buffer The debug message.
325 @param[in] Length The length of debug message.
334 DEBUG_PACKET_HEADER DebugHeader
;
335 DEBUG_PORT_HANDLE Handle
;
337 Handle
= GetDebugPortHandle();
339 DebugHeader
.StartSymbol
= DEBUG_STARTING_SYMBOL_NORMAL
;
340 DebugHeader
.Command
= DEBUG_COMMAND_PRINT_MESSAGE
;
341 DebugHeader
.Length
= sizeof (DEBUG_PACKET_HEADER
) + (UINT8
) Length
;
342 DebugHeader
.SequenceNo
= 0xEE;
344 DebugHeader
.Crc
= CalculateCrc16 (
345 (UINT8
*)Buffer
, Length
,
346 CalculateCrc16 ((UINT8
*)&DebugHeader
, sizeof (DEBUG_PACKET_HEADER
), 0)
349 DebugPortWriteBuffer (Handle
, (UINT8
*)&DebugHeader
, sizeof (DEBUG_PACKET_HEADER
));
350 DebugPortWriteBuffer (Handle
, (UINT8
*)Buffer
, Length
);
354 Prints a debug message to the debug port if the specified error level is enabled.
356 If any bit in ErrorLevel is also set in Mainbox, then print the message specified
357 by Format and the associated variable argument list to the debug port.
359 @param[in] ErrorLevel The error level of the debug message.
360 @param[in] Format Format string for the debug message to print.
361 @param[in] ... Variable argument list whose contents are accessed
362 based on the format string specified by Format.
373 CHAR8 Buffer
[DEBUG_DATA_MAXIMUM_REAL_DATA
];
377 // Check driver debug mask value and global mask
379 if ((ErrorLevel
& GetDebugFlag (DEBUG_AGENT_FLAG_PRINT_ERROR_LEVEL
)) == 0) {
384 // Convert the DEBUG() message to an ASCII String
386 VA_START (Marker
, Format
);
387 AsciiVSPrint (Buffer
, sizeof (Buffer
), Format
, Marker
);
390 SendDebugMsgPacket (Buffer
, AsciiStrLen (Buffer
));
394 Prints a debug message to the debug output device if the specified error level is enabled.
396 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
397 GetDebugPrintErrorLevel (), then print the message specified by Format and the
398 associated variable argument list to the debug output device.
400 If Format is NULL, then ASSERT().
402 @param[in] ErrorLevel The error level of the debug message.
403 @param[in] IsSend Flag of debug message to declare that the data is being sent or being received.
404 @param[in] Data Variable argument list whose contents are accessed
405 @param[in] Length based on the format string specified by Format.
410 DebugAgentDataMsgPrint (
417 CHAR8 Buffer
[DEBUG_DATA_MAXIMUM_REAL_DATA
];
422 // Check driver debug mask value and global mask
424 if ((ErrorLevel
& GetDebugFlag (DEBUG_AGENT_FLAG_PRINT_ERROR_LEVEL
)) == 0) {
430 DestBuffer
+= AsciiSPrint (DestBuffer
, DEBUG_DATA_MAXIMUM_REAL_DATA
, "Sent data [ ");
432 DestBuffer
+= AsciiSPrint (DestBuffer
, DEBUG_DATA_MAXIMUM_REAL_DATA
, "Received data [ ");
437 if (DestBuffer
- Buffer
> DEBUG_DATA_MAXIMUM_REAL_DATA
- 6) {
439 // If there was no enough space in buffer, send out the debug message,
440 // reserving 6 bytes is for the last data and end characters "]\n".
442 SendDebugMsgPacket (Buffer
, DestBuffer
- Buffer
);
445 DestBuffer
+= AsciiSPrint (DestBuffer
, DEBUG_DATA_MAXIMUM_REAL_DATA
- (DestBuffer
- Buffer
), "%02x ", Data
[Index
]);
447 if (Index
>= Length
) {
449 // The last character of debug message has been foramtted in buffer
451 DestBuffer
+= AsciiSPrint(DestBuffer
, DEBUG_DATA_MAXIMUM_REAL_DATA
- (DestBuffer
- Buffer
), "]\n");
452 SendDebugMsgPacket (Buffer
, DestBuffer
- Buffer
);
459 Read remaing debug packet except for the start symbol
461 @param[in] Handle Pointer to Debug Port handle.
462 @param[in, out] DebugHeader Debug header buffer including start symbol.
464 @retval EFI_SUCCESS Read the symbol in BreakSymbol.
465 @retval EFI_CRC_ERROR CRC check fail.
466 @retval EFI_TIMEOUT Timeout occurs when reading debug packet.
470 ReadRemainingBreakPacket (
471 IN DEBUG_PORT_HANDLE Handle
,
472 IN OUT DEBUG_PACKET_HEADER
*DebugHeader
477 // Has received start symbol, try to read the rest part
479 if (DebugPortReadBuffer (Handle
, &DebugHeader
->Command
, sizeof (DEBUG_PACKET_HEADER
) - 1, READ_PACKET_TIMEOUT
) == 0) {
481 // Timeout occur, exit
483 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "Timeout in Debug Timer interrupt\n");
487 Crc
= DebugHeader
->Crc
;
488 DebugHeader
->Crc
= 0;
489 if (CalculateCrc16 ((UINT8
*)DebugHeader
, DebugHeader
->Length
, 0) != Crc
) {
490 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "Debug Timer CRC (%x) against (%x)\n", Crc
, CalculateCrc16 ((UINT8
*) &DebugHeader
, DebugHeader
->Length
, 0));
491 DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE
, FALSE
, (UINT8
*)DebugHeader
, DebugHeader
->Length
);
492 return EFI_CRC_ERROR
;
495 UpdateMailboxContent (GetMailboxPointer(), DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX
, DebugHeader
->SequenceNo
);
500 Check if HOST is attached based on Mailbox.
502 @retval TRUE HOST is attached.
503 @retval FALSE HOST is not attached.
511 return (BOOLEAN
) (GetDebugFlag (DEBUG_AGENT_FLAG_HOST_ATTACHED
) == 1);
515 Set HOST connect flag in Mailbox.
517 @param[in] Attached Attach status.
525 DebugAgentMsgPrint (DEBUG_AGENT_INFO
, "Attach status is %d\n", Attached
);
526 SetDebugFlag (DEBUG_AGENT_FLAG_HOST_ATTACHED
, (UINT32
)Attached
);
530 Set debug setting of Debug Agent in Mailbox.
532 @param DebugSetting Pointer to Debug Setting defined by transfer protocol.
534 @retval RETURN_SUCCESS The setting is set successfully.
535 @retval RETURN_UNSUPPORTED The Key value is not supported.
540 IN DEBUG_DATA_SET_DEBUG_SETTING
*DebugSetting
543 RETURN_STATUS Status
;
545 Status
= RETURN_SUCCESS
;
546 switch (DebugSetting
->Key
) {
547 case DEBUG_AGENT_SETTING_SMM_ENTRY_BREAK
:
548 SetDebugFlag (DEBUG_AGENT_FLAG_BREAK_ON_NEXT_SMI
, DebugSetting
->Value
);
550 case DEBUG_AGENT_SETTING_PRINT_ERROR_LEVEL
:
551 SetDebugFlag (DEBUG_AGENT_FLAG_PRINT_ERROR_LEVEL
, DebugSetting
->Value
);
553 case DEBUG_AGENT_SETTING_BOOT_SCRIPT_ENTRY_BREAK
:
554 SetDebugFlag (DEBUG_AGENT_FLAG_BREAK_BOOT_SCRIPT
, DebugSetting
->Value
);
557 Status
= RETURN_UNSUPPORTED
;
565 @param[in] CpuContext Pointer to saved CPU context.
570 IN DEBUG_CPU_CONTEXT
*CpuContext
573 IA32_EFLAGS32
*Eflags
;
575 Eflags
= (IA32_EFLAGS32
*) &CpuContext
->Eflags
;
581 Exectue Stepping command.
583 @param[in] CpuContext Pointer to saved CPU context.
588 IN DEBUG_CPU_CONTEXT
*CpuContext
591 IA32_EFLAGS32
*Eflags
;
593 Eflags
= (IA32_EFLAGS32
*) &CpuContext
->Eflags
;
599 Set debug register for hardware breakpoint.
601 @param[in] CpuContext Pointer to saved CPU context.
602 @param[in] SetHwBreakpoint Hardware breakpoint to be set.
607 IN DEBUG_CPU_CONTEXT
*CpuContext
,
608 IN DEBUG_DATA_SET_HW_BREAKPOINT
*SetHwBreakpoint
614 RegisterIndex
= SetHwBreakpoint
->Type
.Index
;
619 * ((UINTN
*) &CpuContext
->Dr0
+ RegisterIndex
) = (UINTN
) SetHwBreakpoint
->Address
;
621 Dr7Value
= CpuContext
->Dr7
;
626 Dr7Value
|= 0x3 << (RegisterIndex
* 2);
630 Dr7Value
&= ~(0xf << (16 + RegisterIndex
* 4));
631 Dr7Value
|= (UINTN
) ((SetHwBreakpoint
->Type
.Length
<< 2) | SetHwBreakpoint
->Type
.Access
) << (16 + RegisterIndex
* 4);
637 CpuContext
->Dr7
= Dr7Value
;
641 Clear debug register for hardware breakpoint.
643 @param[in] CpuContext Pointer to saved CPU context.
644 @param[in] ClearHwBreakpoint Hardware breakpoint to be cleared.
649 IN DEBUG_CPU_CONTEXT
*CpuContext
,
650 IN DEBUG_DATA_CLEAR_HW_BREAKPOINT
*ClearHwBreakpoint
653 if ((ClearHwBreakpoint
->IndexMask
& BIT0
) != 0) {
655 CpuContext
->Dr7
&= ~(0x3 << 0);
657 if ((ClearHwBreakpoint
->IndexMask
& BIT1
) != 0) {
659 CpuContext
->Dr7
&= ~(0x3 << 2);
661 if ((ClearHwBreakpoint
->IndexMask
& BIT2
) != 0) {
663 CpuContext
->Dr7
&= ~(0x3 << 4);
665 if ((ClearHwBreakpoint
->IndexMask
& BIT3
) != 0) {
667 CpuContext
->Dr7
&= ~(0x3 << 6);
673 Return the offset of FP / MMX / XMM registers in the FPU saved state by register index.
675 @param[in] Index Register index.
676 @param[out] Width Register width returned.
678 @return Offset in the FPU Save State.
682 ArchReadFxStatOffset (
687 if (Index
< SOFT_DEBUGGER_REGISTER_ST0
) {
689 case SOFT_DEBUGGER_REGISTER_FP_FCW
:
690 *Width
= (UINT8
) sizeof (UINT16
);
691 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE
, Fcw
);
693 case SOFT_DEBUGGER_REGISTER_FP_FSW
:
694 *Width
= (UINT8
) sizeof (UINT16
);
695 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE
, Fsw
);
697 case SOFT_DEBUGGER_REGISTER_FP_FTW
:
698 *Width
= (UINT8
) sizeof (UINT16
);
699 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE
, Ftw
);
701 case SOFT_DEBUGGER_REGISTER_FP_OPCODE
:
702 *Width
= (UINT8
) sizeof (UINT16
);
703 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE
, Opcode
);
705 case SOFT_DEBUGGER_REGISTER_FP_EIP
:
706 *Width
= (UINT8
) sizeof (UINT32
);
707 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE
, Eip
);
709 case SOFT_DEBUGGER_REGISTER_FP_CS
:
710 *Width
= (UINT8
) sizeof (UINT16
);
711 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE
, Cs
);
713 case SOFT_DEBUGGER_REGISTER_FP_DATAOFFSET
:
714 *Width
= (UINT8
) sizeof (UINT32
);
715 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE
, DataOffset
);
717 case SOFT_DEBUGGER_REGISTER_FP_DS
:
718 *Width
= (UINT8
) sizeof (UINT16
);
719 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE
, Ds
);
721 case SOFT_DEBUGGER_REGISTER_FP_MXCSR
:
722 *Width
= (UINT8
) sizeof (UINT32
);
723 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE
, Mxcsr
);
725 case SOFT_DEBUGGER_REGISTER_FP_MXCSR_MASK
:
726 *Width
= (UINT8
) sizeof (UINT32
);
727 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE
, Mxcsr_Mask
);
731 if (Index
<= SOFT_DEBUGGER_REGISTER_ST7
) {
733 } else if (Index
<= SOFT_DEBUGGER_REGISTER_XMM15
) {
740 Index
-= SOFT_DEBUGGER_REGISTER_MM0
- SOFT_DEBUGGER_REGISTER_ST0
;
743 return OFFSET_OF (DEBUG_DATA_FX_SAVE_STATE
, St0Mm0
) + (Index
- SOFT_DEBUGGER_REGISTER_ST0
) * 16;
747 Return the pointer of the register value in the CPU saved context.
749 @param[in] CpuContext Pointer to saved CPU context.
750 @param[in] Index Register index value.
751 @param[out] Width Data width to read.
753 @return The pointer in the CPU saved context.
757 ArchReadRegisterBuffer (
758 IN DEBUG_CPU_CONTEXT
*CpuContext
,
765 if (Index
< SOFT_DEBUGGER_REGISTER_FP_BASE
) {
766 Buffer
= (UINT8
*) CpuContext
+ OFFSET_OF (DEBUG_CPU_CONTEXT
, Dr0
) + Index
* sizeof (UINTN
);
767 *Width
= (UINT8
) sizeof (UINTN
);
770 // FPU/MMX/XMM registers
772 Buffer
= (UINT8
*) CpuContext
+ OFFSET_OF (DEBUG_CPU_CONTEXT
, FxSaveState
) + ArchReadFxStatOffset (Index
, Width
);
779 Send the packet without data to HOST.
781 @param[in] CommandType Type of Command.
782 @param[in] SequenceNo Sequence number.
786 SendPacketWithoutData (
787 IN UINT8 CommandType
,
791 DEBUG_PACKET_HEADER DebugHeader
;
792 DEBUG_PORT_HANDLE Handle
;
794 Handle
= GetDebugPortHandle();
796 DebugHeader
.StartSymbol
= DEBUG_STARTING_SYMBOL_NORMAL
;
797 DebugHeader
.Command
= CommandType
;
798 DebugHeader
.Length
= sizeof (DEBUG_PACKET_HEADER
);
799 DebugHeader
.SequenceNo
= SequenceNo
;
801 DebugHeader
.Crc
= CalculateCrc16 ((UINT8
*)&DebugHeader
, sizeof (DEBUG_PACKET_HEADER
), 0);
803 DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE
, TRUE
, (UINT8
*) &DebugHeader
, DebugHeader
.Length
);
804 DebugPortWriteBuffer (Handle
, (UINT8
*) &DebugHeader
, DebugHeader
.Length
);
808 Send acknowledge packet to HOST.
810 @param[in] AckCommand Type of Acknowledge packet.
819 DEBUG_AGENT_MAILBOX
*Mailbox
;
821 if (AckCommand
!= DEBUG_COMMAND_OK
) {
823 // This is not ACK OK packet
825 DebugAgentMsgPrint (DEBUG_AGENT_ERROR
, "Send ACK(%d)\n", AckCommand
);
827 Mailbox
= GetMailboxPointer();
828 SequenceNo
= Mailbox
->HostSequenceNo
;
829 DebugAgentMsgPrint (DEBUG_AGENT_INFO
, "SendAckPacket: SequenceNo = %x\n", SequenceNo
);
830 SendPacketWithoutData (AckCommand
, SequenceNo
);
831 UpdateMailboxContent (Mailbox
, DEBUG_MAILBOX_LAST_ACK
, AckCommand
);
835 Receive valid packet from HOST.
837 @param[out] InputPacket Buffer to receive packet.
838 @param[out] BreakReceived TRUE means break-in symbol received.
839 FALSE means break-in symbol not received.
840 @param[out] IncompatibilityFlag If IncompatibilityFlag is not NULL, return
841 TRUE: Compatible packet received.
842 FALSE: Incompatible packet received.
843 @param[in] Timeout Time out value to wait for acknowlege from HOST.
844 The unit is microsecond.
845 @param[in] SkipStartSymbol TRUE: Skip time out when reading start symbol.
846 FALSE: Does not Skip time out when reading start symbol.
848 @retval RETURN_SUCCESS A valid package was reveived in InputPacket.
849 @retval RETURN_TIMEOUT Timeout occurs.
854 OUT UINT8
*InputPacket
,
855 OUT BOOLEAN
*BreakReceived
,
856 OUT BOOLEAN
*IncompatibilityFlag
, OPTIONAL
858 IN BOOLEAN SkipStartSymbol
861 DEBUG_PACKET_HEADER
*DebugHeader
;
863 DEBUG_PORT_HANDLE Handle
;
865 UINTN TimeoutForStartSymbol
;
867 Handle
= GetDebugPortHandle();
868 if (SkipStartSymbol
) {
869 TimeoutForStartSymbol
= 0;
871 TimeoutForStartSymbol
= Timeout
;
874 DebugHeader
= (DEBUG_PACKET_HEADER
*) InputPacket
;
877 // Find the valid start symbol
879 Received
= DebugPortReadBuffer (Handle
, &DebugHeader
->StartSymbol
, sizeof (DebugHeader
->StartSymbol
), TimeoutForStartSymbol
);
880 if (Received
< sizeof (DebugHeader
->StartSymbol
)) {
881 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "DebugPortReadBuffer(StartSymbol) timeout\n");
882 return RETURN_TIMEOUT
;
885 if (DebugHeader
->StartSymbol
!= DEBUG_STARTING_SYMBOL_NORMAL
) {
886 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "Invalid start symbol received [%02x]\n", DebugHeader
->StartSymbol
);
891 // Read Package header till field Length
893 Received
= DebugPortReadBuffer (
895 &DebugHeader
->Command
,
896 OFFSET_OF (DEBUG_PACKET_HEADER
, Length
) + sizeof (DebugHeader
->Length
) - sizeof (DebugHeader
->StartSymbol
),
900 DebugAgentMsgPrint (DEBUG_AGENT_ERROR
, "DebugPortReadBuffer(Command) timeout\n");
901 return RETURN_TIMEOUT
;
903 if (DebugHeader
->Length
< sizeof (DEBUG_PACKET_HEADER
)) {
904 if (IncompatibilityFlag
!= NULL
) {
906 // This is one old version debug packet format, set Incompatibility flag
908 *IncompatibilityFlag
= TRUE
;
911 // Skip the bad small packet
917 // Read the payload data include the CRC field
919 Received
= DebugPortReadBuffer (Handle
, &DebugHeader
->SequenceNo
, (UINT8
) (DebugHeader
->Length
- OFFSET_OF (DEBUG_PACKET_HEADER
, SequenceNo
)), Timeout
);
921 DebugAgentMsgPrint (DEBUG_AGENT_ERROR
, "DebugPortReadBuffer(SequenceNo) timeout\n");
922 return RETURN_TIMEOUT
;
925 // Calculate the CRC of Debug Packet
927 Crc
= DebugHeader
->Crc
;
928 DebugHeader
->Crc
= 0;
929 if (Crc
== CalculateCrc16 ((UINT8
*) DebugHeader
, DebugHeader
->Length
, 0)) {
932 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "CRC Error (received CRC is %x)\n", Crc
);
933 DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE
, FALSE
, (UINT8
*) DebugHeader
, DebugHeader
->Length
);
937 DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE
, FALSE
, (UINT8
*) DebugHeader
, DebugHeader
->Length
);
939 return RETURN_SUCCESS
;
943 Receive acknowledge packet OK from HOST in specified time.
945 @param[in] Command The command type issued by TARGET.
946 @param[in] Timeout Time out value to wait for acknowlege from HOST.
947 The unit is microsecond.
948 @param[out] BreakReceived If BreakReceived is not NULL,
949 TRUE is retured if break-in symbol received.
950 FALSE is retured if break-in symbol not received.
951 @param[out] IncompatibilityFlag If IncompatibilityFlag is not NULL, return
952 TRUE: Compatible packet received.
953 FALSE: Incompatible packet received.
955 @retval RETRUEN_SUCCESS Succeed to receive acknowlege packet from HOST,
956 the type of acknowlege packet saved in Ack.
957 @retval RETURN_TIMEOUT Specified timeout value was up.
961 SendCommandAndWaitForAckOK (
964 OUT BOOLEAN
*BreakReceived
, OPTIONAL
965 OUT BOOLEAN
*IncompatibilityFlag OPTIONAL
968 RETURN_STATUS Status
;
969 UINT8 InputPacketBuffer
[DEBUG_DATA_UPPER_LIMIT
];
970 DEBUG_PACKET_HEADER
*DebugHeader
;
972 UINT8 HostSequenceNo
;
976 DebugHeader
= (DEBUG_PACKET_HEADER
*) InputPacketBuffer
;
977 Status
= RETURN_TIMEOUT
;
978 while (RetryCount
> 0) {
979 SequenceNo
= GetMailboxPointer()->SequenceNo
;
980 HostSequenceNo
= GetMailboxPointer()->HostSequenceNo
;
981 SendPacketWithoutData (Command
, SequenceNo
);
982 Status
= ReceivePacket ((UINT8
*) DebugHeader
, BreakReceived
, IncompatibilityFlag
, Timeout
, FALSE
);
983 if (Status
== RETURN_TIMEOUT
) {
984 if (Command
== DEBUG_COMMAND_INIT_BREAK
) {
987 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "TARGET: Timeout when waiting for ACK packet.\n");
991 ASSERT_EFI_ERROR (Status
);
993 // Status == RETURN_SUCCESS
995 if (DebugHeader
->Command
== DEBUG_COMMAND_OK
&& DebugHeader
->SequenceNo
== SequenceNo
) {
999 UpdateMailboxContent (GetMailboxPointer(), DEBUG_MAILBOX_SEQUENCE_NO_INDEX
, ++SequenceNo
);
1002 if (DebugHeader
->Command
== DEBUG_COMMAND_GO
&& (DebugHeader
->SequenceNo
== HostSequenceNo
|| Command
== DEBUG_COMMAND_INIT_BREAK
)) {
1006 if (Command
== DEBUG_COMMAND_INIT_BREAK
) {
1007 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "TARGET: Receive GO() in last boot\n");
1009 SendPacketWithoutData (DEBUG_COMMAND_OK
, DebugHeader
->SequenceNo
);
1013 ASSERT (Command
== DEBUG_COMMAND_INIT_BREAK
);
1018 Get current break cause.
1020 @param[in] Vector Vector value of exception or interrupt.
1021 @param[in] CpuContext Pointer to save CPU context.
1023 @return The type of break cause defined by XXXX
1029 IN DEBUG_CPU_CONTEXT
*CpuContext
1034 Cause
= DEBUG_DATA_BREAK_CAUSE_UNKNOWN
;
1037 case DEBUG_INT1_VECTOR
:
1038 case DEBUG_INT3_VECTOR
:
1040 if (Vector
== DEBUG_INT1_VECTOR
) {
1044 if ((CpuContext
->Dr6
& BIT14
) != 0) {
1045 Cause
= DEBUG_DATA_BREAK_CAUSE_STEPPING
;
1047 // If it's single step, no need to check DR0, to ensure single step work in PeCoffExtraActionLib
1048 // (right after triggering a breakpoint to report image load/unload).
1053 Cause
= DEBUG_DATA_BREAK_CAUSE_HW_BREAKPOINT
;
1059 Cause
= DEBUG_DATA_BREAK_CAUSE_SW_BREAKPOINT
;
1062 switch (CpuContext
->Dr0
) {
1063 case IMAGE_LOAD_SIGNATURE
:
1064 case IMAGE_UNLOAD_SIGNATURE
:
1066 if (CpuContext
->Dr3
== IO_PORT_BREAKPOINT_ADDRESS
) {
1068 Cause
= (UINT8
) ((CpuContext
->Dr0
== IMAGE_LOAD_SIGNATURE
) ?
1069 DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD
: DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD
);
1073 case SOFT_INTERRUPT_SIGNATURE
:
1075 if (CpuContext
->Dr1
== MEMORY_READY_SIGNATURE
) {
1076 Cause
= DEBUG_DATA_BREAK_CAUSE_MEMORY_READY
;
1077 CpuContext
->Dr0
= 0;
1078 } else if (CpuContext
->Dr1
== SYSTEM_RESET_SIGNATURE
) {
1079 Cause
= DEBUG_DATA_BREAK_CAUSE_SYSTEM_RESET
;
1080 CpuContext
->Dr0
= 0;
1091 case DEBUG_TIMER_VECTOR
:
1092 Cause
= DEBUG_DATA_BREAK_CAUSE_USER_HALT
;
1097 if (GetDebugFlag (DEBUG_AGENT_FLAG_STEPPING
) == 1) {
1099 // If stepping command is executing
1101 Cause
= DEBUG_DATA_BREAK_CAUSE_STEPPING
;
1103 Cause
= DEBUG_DATA_BREAK_CAUSE_EXCEPTION
;
1113 Send packet with response data to HOST.
1115 @param[in] Data Pointer to response data buffer.
1116 @param[in] DataSize Size of response data in byte.
1118 @retval RETURN_SUCCESS Response data was sent successfully.
1122 SendDataResponsePacket (
1127 RETURN_STATUS Status
;
1128 DEBUG_PACKET_HEADER
*DebugHeader
;
1130 DEBUG_PACKET_HEADER
*AckDebugHeader
;
1131 UINT8 DebugPacket
[DEBUG_DATA_UPPER_LIMIT
];
1132 UINT8 InputPacketBuffer
[DEBUG_DATA_UPPER_LIMIT
];
1133 DEBUG_PORT_HANDLE Handle
;
1136 Handle
= GetDebugPortHandle();
1138 DebugHeader
= (DEBUG_PACKET_HEADER
*) &DebugPacket
;
1139 DebugHeader
->StartSymbol
= DEBUG_STARTING_SYMBOL_NORMAL
;
1142 SequenceNo
= GetMailboxPointer()->HostSequenceNo
;
1143 if (DataSize
<= DEBUG_DATA_MAXIMUM_REAL_DATA
) {
1145 DebugHeader
->Command
= DEBUG_COMMAND_OK
;
1146 DebugHeader
->Length
= (UINT8
) (DataSize
+ sizeof (DEBUG_PACKET_HEADER
));
1147 DebugHeader
->SequenceNo
= SequenceNo
;
1148 DebugHeader
->Crc
= 0;
1149 CopyMem (DebugHeader
+ 1, Data
, DataSize
);
1153 DebugHeader
->Command
= DEBUG_COMMAND_IN_PROGRESS
;
1154 DebugHeader
->Length
= DEBUG_DATA_MAXIMUM_REAL_DATA
+ sizeof (DEBUG_PACKET_HEADER
);
1155 DebugHeader
->SequenceNo
= SequenceNo
;
1156 DebugHeader
->Crc
= 0;
1157 CopyMem (DebugHeader
+ 1, Data
, DEBUG_DATA_MAXIMUM_REAL_DATA
);
1161 // Calculate and fill the checksum
1163 DebugHeader
->Crc
= CalculateCrc16 ((UINT8
*) DebugHeader
, DebugHeader
->Length
, 0);
1165 DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE
, TRUE
, (UINT8
*) DebugHeader
, DebugHeader
->Length
);
1167 DebugPortWriteBuffer (Handle
, (UINT8
*) DebugHeader
, DebugHeader
->Length
);
1170 Status
= ReceivePacket (InputPacketBuffer
, NULL
, NULL
, READ_PACKET_TIMEOUT
, FALSE
);
1171 if (Status
== RETURN_TIMEOUT
) {
1172 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "TARGET: Timeout in SendDataResponsePacket()\n");
1175 AckDebugHeader
= (DEBUG_PACKET_HEADER
*) InputPacketBuffer
;
1176 SequenceNo
= AckDebugHeader
->SequenceNo
;
1177 if (AckDebugHeader
->Command
== DEBUG_COMMAND_OK
&&
1178 SequenceNo
== DebugHeader
->SequenceNo
&&
1181 // If this is the last packet, return RETURN_SUCCESS.
1183 return RETURN_SUCCESS
;
1185 if ((SequenceNo
== (UINT8
) (DebugHeader
->SequenceNo
+ 1)) && (AckDebugHeader
->Command
== DEBUG_COMMAND_CONTINUE
)) {
1187 // Send the rest packet
1189 Data
+= DEBUG_DATA_MAXIMUM_REAL_DATA
;
1190 DataSize
-= DEBUG_DATA_MAXIMUM_REAL_DATA
;
1191 UpdateMailboxContent (GetMailboxPointer(), DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX
, (UINT8
) SequenceNo
);
1194 if (SequenceNo
>= DebugHeader
->SequenceNo
) {
1195 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "TARGET: Received one old or new command(SequenceNo is %x, last SequenceNo is %x)\n", SequenceNo
, DebugHeader
->SequenceNo
);
1203 Send break cause packet to HOST.
1205 @param[in] Vector Vector value of exception or interrutp.
1206 @param[in] CpuContext Pointer to save CPU context.
1208 @retval RETURN_SUCCESS Response data was sent successfully.
1209 @retval RETURN_DEVICE_ERROR Cannot receive DEBUG_COMMAND_OK from HOST.
1213 SendBreakCausePacket (
1215 IN DEBUG_CPU_CONTEXT
*CpuContext
1218 DEBUG_DATA_RESPONSE_BREAK_CAUSE DebugDataBreakCause
;
1220 DebugDataBreakCause
.StopAddress
= CpuContext
->Eip
;
1221 DebugDataBreakCause
.Cause
= GetBreakCause (Vector
, CpuContext
);
1223 return SendDataResponsePacket ((UINT8
*) &DebugDataBreakCause
, (UINT16
) sizeof (DEBUG_DATA_RESPONSE_BREAK_CAUSE
));
1227 Try to attach the HOST.
1229 Send init break packet to HOST:
1230 If no acknowlege received in specified Timeout, return RETURN_TIMEOUT.
1231 If received acknowlege, check the revision of HOST.
1232 Set Attach Flag if attach successfully.
1234 @param[in] BreakCause Break cause of this break event.
1235 @param[in] Timeout Time out value to wait for acknowlege from HOST.
1236 The unit is microsecond.
1237 @param[out] BreakReceived If BreakReceived is not NULL,
1238 TRUE is retured if break-in symbol received.
1239 FALSE is retured if break-in symbol not received.
1243 IN UINT8 BreakCause
,
1245 OUT BOOLEAN
*BreakReceived
1248 RETURN_STATUS Status
;
1249 DEBUG_PORT_HANDLE Handle
;
1250 BOOLEAN IncompatibilityFlag
;
1252 IncompatibilityFlag
= FALSE
;
1253 Handle
= GetDebugPortHandle();
1256 // Send init break and wait ack in Timeout
1258 DebugPortWriteBuffer (Handle
, (UINT8
*) mErrorMsgSendInitPacket
, AsciiStrLen (mErrorMsgSendInitPacket
));
1259 if (BreakCause
== DEBUG_DATA_BREAK_CAUSE_SYSTEM_RESET
) {
1260 Status
= SendCommandAndWaitForAckOK (DEBUG_COMMAND_INIT_BREAK
, Timeout
, BreakReceived
, &IncompatibilityFlag
);
1262 Status
= SendCommandAndWaitForAckOK (DEBUG_COMMAND_ATTACH_BREAK
, Timeout
, BreakReceived
, &IncompatibilityFlag
);
1264 if (IncompatibilityFlag
) {
1266 // If the incompatible Debug Packet received, the HOST should be running transfer protocol before DEBUG_AGENT_REVISION.
1267 // It could be UDK Debugger for Windows v1.1/v1.2 or for Linux v0.8/v1.2.
1269 DebugPortWriteBuffer (Handle
, (UINT8
*) mErrorMsgVersionAlert
, AsciiStrLen (mErrorMsgVersionAlert
));
1273 if (RETURN_ERROR (Status
)) {
1274 DebugPortWriteBuffer (Handle
, (UINT8
*) mErrorMsgConnectFail
, AsciiStrLen (mErrorMsgConnectFail
));
1276 DebugPortWriteBuffer (Handle
, (UINT8
*) mErrorMsgConnectOK
, AsciiStrLen (mErrorMsgConnectOK
));
1280 SetHostAttached (TRUE
);
1286 Send Break point packet to HOST.
1288 Only the first breaking processor could sent BREAK_POINT packet.
1290 @param[in] BreakCause Break cause of this break event.
1291 @param[in] ProcessorIndex Processor index value.
1292 @param[out] BreakReceived If BreakReceived is not NULL,
1293 TRUE is retured if break-in symbol received.
1294 FALSE is retured if break-in symbol not received.
1298 SendBreakPacketToHost (
1299 IN UINT8 BreakCause
,
1300 IN UINT32 ProcessorIndex
,
1301 OUT BOOLEAN
*BreakReceived
1304 UINT8 InputCharacter
;
1305 DEBUG_PORT_HANDLE Handle
;
1307 Handle
= GetDebugPortHandle();
1309 if (IsHostAttached ()) {
1310 DebugAgentMsgPrint (DEBUG_AGENT_INFO
, "processor[%x]:Send Break Packet to HOST.\n", ProcessorIndex
);
1311 SendCommandAndWaitForAckOK (DEBUG_COMMAND_BREAK_POINT
, READ_PACKET_TIMEOUT
, BreakReceived
, NULL
);
1313 DebugAgentMsgPrint (DEBUG_AGENT_INFO
, "processor[%x]:Try to attach HOST.\n", ProcessorIndex
);
1315 // If HOST is not attached, try to attach it firstly.
1318 // Poll Attach symbols from HOST and ack OK
1321 DebugPortReadBuffer (Handle
, &InputCharacter
, 1, 0);
1322 } while (InputCharacter
!= DEBUG_STARTING_SYMBOL_ATTACH
);
1323 SendAckPacket (DEBUG_COMMAND_OK
);
1326 // Try to attach HOST
1328 while (AttachHost (BreakCause
, 0, NULL
) != RETURN_SUCCESS
);
1334 The main function to process communication with HOST.
1336 It received the command packet from HOST, and sent response data packet to HOST.
1338 @param[in] Vector Vector value of exception or interrutp.
1339 @param[in, out] CpuContext Pointer to saved CPU context.
1340 @param[in] BreakReceived TRUE means break-in symbol received.
1341 FALSE means break-in symbol not received.
1345 CommandCommunication (
1347 IN OUT DEBUG_CPU_CONTEXT
*CpuContext
,
1348 IN BOOLEAN BreakReceived
1351 RETURN_STATUS Status
;
1352 UINT8 InputPacketBuffer
[DEBUG_DATA_UPPER_LIMIT
];
1353 DEBUG_PACKET_HEADER
*DebugHeader
;
1358 DEBUG_DATA_READ_MEMORY
*MemoryRead
;
1359 DEBUG_DATA_WRITE_MEMORY
*MemoryWrite
;
1360 DEBUG_DATA_READ_IO
*IoRead
;
1361 DEBUG_DATA_WRITE_IO
*IoWrite
;
1362 DEBUG_DATA_READ_REGISTER
*RegisterRead
;
1363 DEBUG_DATA_WRITE_REGISTER
*RegisterWrite
;
1364 UINT8
*RegisterBuffer
;
1365 DEBUG_DATA_READ_MSR
*MsrRegisterRead
;
1366 DEBUG_DATA_WRITE_MSR
*MsrRegisterWrite
;
1367 DEBUG_DATA_CPUID
*Cpuid
;
1368 DEBUG_DATA_RESPONSE_CPUID CpuidResponse
;
1369 DEBUG_DATA_SEARCH_SIGNATURE
*SearchSignature
;
1370 DEBUG_DATA_RESPONSE_GET_EXCEPTION Exception
;
1371 DEBUG_DATA_RESPONSE_GET_REVISION DebugAgentRevision
;
1372 DEBUG_DATA_SET_VIEWPOINT
*SetViewPoint
;
1373 BOOLEAN HaltDeferred
;
1374 UINT32 ProcessorIndex
;
1375 DEBUG_PORT_HANDLE Handle
;
1376 DEBUG_AGENT_EXCEPTION_BUFFER AgentExceptionBuffer
;
1377 UINT32 IssuedViewPoint
;
1378 DEBUG_AGENT_MAILBOX
*Mailbox
;
1381 IssuedViewPoint
= 0;
1382 HaltDeferred
= BreakReceived
;
1384 if (MultiProcessorDebugSupport()) {
1385 ProcessorIndex
= GetProcessorIndex ();
1386 SetCpuStopFlagByIndex (ProcessorIndex
, TRUE
);
1387 if (mDebugMpContext
.ViewPointIndex
== ProcessorIndex
) {
1389 // Only the current view processor could set AgentInProgress Flag.
1391 IssuedViewPoint
= ProcessorIndex
;
1395 if (IssuedViewPoint
== ProcessorIndex
) {
1397 // Set AgentInProgress Flag.
1399 SetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS
, 1);
1402 Handle
= GetDebugPortHandle();
1406 if (MultiProcessorDebugSupport()) {
1408 // Check if the current processor is HOST view point
1410 if (mDebugMpContext
.ViewPointIndex
!= ProcessorIndex
) {
1411 if (mDebugMpContext
.RunCommandSet
) {
1413 // If HOST view point sets RUN flag, run GO command to leave
1415 SetCpuStopFlagByIndex (ProcessorIndex
, FALSE
);
1416 CommandGo (CpuContext
);
1420 // Run into loop again
1428 AcquireMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
1430 DebugHeader
=(DEBUG_PACKET_HEADER
*) InputPacketBuffer
;
1432 DebugAgentMsgPrint (DEBUG_AGENT_INFO
, "TARGET: Try to get command from HOST...\n");
1433 Status
= ReceivePacket ((UINT8
*)DebugHeader
, &BreakReceived
, NULL
, READ_PACKET_TIMEOUT
, TRUE
);
1434 if (Status
!= RETURN_SUCCESS
|| (DebugHeader
->Command
& DEBUG_COMMAND_RESPONSE
) != 0) {
1435 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "TARGET: Get command[%x] sequenceno[%x] returned status is [%x] \n", DebugHeader
->Command
, DebugHeader
->SequenceNo
, Status
);
1436 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "TARGET: Get command failed or it's response packet not expected! \n");
1437 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
1441 Mailbox
= GetMailboxPointer ();
1442 if (DebugHeader
->SequenceNo
== Mailbox
->HostSequenceNo
) {
1443 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "TARGET: Receive one old command[%x] agaist command[%x]\n", DebugHeader
->SequenceNo
, Mailbox
->HostSequenceNo
);
1444 SendAckPacket (Mailbox
->LastAck
);
1445 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
1447 } else if (DebugHeader
->SequenceNo
== (UINT8
) (Mailbox
->HostSequenceNo
+ 1)) {
1448 UpdateMailboxContent (Mailbox
, DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX
, (UINT8
) DebugHeader
->SequenceNo
);
1450 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "Receive one invalid comamnd[%x] agaist command[%x]\n", DebugHeader
->SequenceNo
, Mailbox
->HostSequenceNo
);
1451 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
1456 // Save CPU content before executing HOST commond
1458 UpdateMailboxContent (Mailbox
, DEBUG_MAILBOX_EXCEPTION_BUFFER_POINTER_INDEX
, (UINT64
)(UINTN
) &AgentExceptionBuffer
.JumpBuffer
);
1459 if (SetJump (&AgentExceptionBuffer
.JumpBuffer
) != 0) {
1461 // If HOST command failed, continue to wait for HOST's next command
1462 // If needed, agent could send exception info to HOST.
1464 SendAckPacket (DEBUG_COMMAND_ABORT
);
1465 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
1469 DebugAgentMsgPrint (DEBUG_AGENT_INFO
, "Processor[%x]:Received one command(%x)\n", mDebugMpContext
.ViewPointIndex
, DebugHeader
->Command
);
1471 switch (DebugHeader
->Command
) {
1473 case DEBUG_COMMAND_HALT
:
1474 SendAckPacket (DEBUG_COMMAND_HALT_DEFERRED
);
1475 HaltDeferred
= TRUE
;
1476 BreakReceived
= FALSE
;
1477 Status
= RETURN_SUCCESS
;
1480 case DEBUG_COMMAND_RESET
:
1481 SendAckPacket (DEBUG_COMMAND_OK
);
1482 SendAckPacket (DEBUG_COMMAND_OK
);
1483 SendAckPacket (DEBUG_COMMAND_OK
);
1484 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
1488 // Assume system resets in 2 seconds, otherwise send TIMEOUT packet.
1489 // PCD can be used if 2 seconds isn't long enough for some platforms.
1491 MicroSecondDelay (2000000);
1492 UpdateMailboxContent (Mailbox
, DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX
, Mailbox
->HostSequenceNo
+ 1);
1493 SendAckPacket (DEBUG_COMMAND_TIMEOUT
);
1494 SendAckPacket (DEBUG_COMMAND_TIMEOUT
);
1495 SendAckPacket (DEBUG_COMMAND_TIMEOUT
);
1498 case DEBUG_COMMAND_GO
:
1499 CommandGo (CpuContext
);
1501 // Clear Dr0 to avoid to be recognized as IMAGE_LOAD/_UNLOAD again when hitting a breakpoint after GO
1502 // If HOST changed Dr0 before GO, we will not change Dr0 here
1504 Data8
= GetBreakCause (Vector
, CpuContext
);
1505 if (Data8
== DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD
|| Data8
== DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD
) {
1506 CpuContext
->Dr0
= 0;
1509 // Clear Stepping Flag
1511 SetDebugFlag (DEBUG_AGENT_FLAG_STEPPING
, 0);
1513 if (!HaltDeferred
) {
1515 // If no HALT command received when being in-active mode
1517 if (MultiProcessorDebugSupport()) {
1518 Data32
= FindNextPendingBreakCpu ();
1521 // If there are still others processors being in break state,
1522 // send OK packet to HOST to finish this go command
1524 SendAckPacket (DEBUG_COMMAND_OK
);
1527 // Set current view to the next breaking processor
1529 mDebugMpContext
.ViewPointIndex
= Data32
;
1530 mDebugMpContext
.BreakAtCpuIndex
= mDebugMpContext
.ViewPointIndex
;
1531 SetCpuBreakFlagByIndex (mDebugMpContext
.ViewPointIndex
, FALSE
);
1533 // Send break packet to HOST to let HOST break again
1535 SendBreakPacketToHost (DEBUG_DATA_BREAK_CAUSE_UNKNOWN
, mDebugMpContext
.BreakAtCpuIndex
, &BreakReceived
);
1537 // Continue to run into loop to read command packet from HOST
1539 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
1544 // If no else processor break, set stop bitmask,
1545 // and set Running flag for all processors.
1547 SetCpuStopFlagByIndex (ProcessorIndex
, FALSE
);
1548 SetCpuRunningFlag (TRUE
);
1551 // Wait for all processors are in running state
1554 if (IsAllCpuRunning ()) {
1559 // Set BSP to be current view point.
1561 SetDebugViewPoint (mDebugMpContext
.BspIndex
);
1564 // Clear breaking processor index and running flag
1566 mDebugMpContext
.BreakAtCpuIndex
= (UINT32
) (-1);
1567 SetCpuRunningFlag (FALSE
);
1571 // Send OK packet to HOST to finish this go command
1573 SendAckPacket (DEBUG_COMMAND_OK
);
1575 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
1577 if (!IsHostAttached()) {
1578 UpdateMailboxContent (Mailbox
, DEBUG_MAILBOX_SEQUENCE_NO_INDEX
, 0);
1579 UpdateMailboxContent (Mailbox
, DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX
, 0);
1585 // If reveived HALT command, need to defer the GO command
1587 SendAckPacket (DEBUG_COMMAND_HALT_PROCESSED
);
1588 HaltDeferred
= FALSE
;
1590 Vector
= DEBUG_TIMER_VECTOR
;
1594 case DEBUG_COMMAND_BREAK_CAUSE
:
1596 if (MultiProcessorDebugSupport() && ProcessorIndex
!= mDebugMpContext
.BreakAtCpuIndex
) {
1597 Status
= SendBreakCausePacket (DEBUG_TIMER_VECTOR
, CpuContext
);
1600 Status
= SendBreakCausePacket (Vector
, CpuContext
);
1605 case DEBUG_COMMAND_SET_HW_BREAKPOINT
:
1606 SetDebugRegister (CpuContext
, (DEBUG_DATA_SET_HW_BREAKPOINT
*) (DebugHeader
+ 1));
1607 SendAckPacket (DEBUG_COMMAND_OK
);
1610 case DEBUG_COMMAND_CLEAR_HW_BREAKPOINT
:
1611 ClearDebugRegister (CpuContext
, (DEBUG_DATA_CLEAR_HW_BREAKPOINT
*) (DebugHeader
+ 1));
1612 SendAckPacket (DEBUG_COMMAND_OK
);
1615 case DEBUG_COMMAND_SINGLE_STEPPING
:
1616 CommandStepping (CpuContext
);
1618 // Clear Dr0 to avoid to be recognized as IMAGE_LOAD/_UNLOAD again when hitting a breakpoint after GO
1619 // If HOST changed Dr0 before GO, we will not change Dr0 here
1621 Data8
= GetBreakCause (Vector
, CpuContext
);
1622 if (Data8
== DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD
|| Data8
== DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD
) {
1623 CpuContext
->Dr0
= 0;
1626 mDebugMpContext
.BreakAtCpuIndex
= (UINT32
) (-1);
1628 // Set Stepping Flag
1630 SetDebugFlag (DEBUG_AGENT_FLAG_STEPPING
, 1);
1631 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
1633 // Executing stepping command directly without sending ACK packet,
1634 // ACK packet will be sent after stepping done.
1638 case DEBUG_COMMAND_SET_SW_BREAKPOINT
:
1639 Data64
= (UINTN
) (((DEBUG_DATA_SET_SW_BREAKPOINT
*) (DebugHeader
+ 1))->Address
);
1640 Data8
= *(UINT8
*) (UINTN
) Data64
;
1641 *(UINT8
*) (UINTN
) Data64
= DEBUG_SW_BREAKPOINT_SYMBOL
;
1642 Status
= SendDataResponsePacket ((UINT8
*) &Data8
, (UINT16
) sizeof (UINT8
));
1645 case DEBUG_COMMAND_READ_MEMORY
:
1646 MemoryRead
= (DEBUG_DATA_READ_MEMORY
*) (DebugHeader
+ 1);
1647 Status
= SendDataResponsePacket ((UINT8
*) (UINTN
) MemoryRead
->Address
, (UINT16
) (MemoryRead
->Count
* MemoryRead
->Width
));
1650 case DEBUG_COMMAND_WRITE_MEMORY
:
1651 MemoryWrite
= (DEBUG_DATA_WRITE_MEMORY
*) (DebugHeader
+ 1);
1652 CopyMem ((VOID
*) (UINTN
) MemoryWrite
->Address
, &MemoryWrite
->Data
, MemoryWrite
->Count
* MemoryWrite
->Width
);
1653 SendAckPacket (DEBUG_COMMAND_OK
);
1656 case DEBUG_COMMAND_READ_IO
:
1657 IoRead
= (DEBUG_DATA_READ_IO
*) (DebugHeader
+ 1);
1658 switch (IoRead
->Width
) {
1660 Data64
= IoRead8 ((UINTN
) IoRead
->Port
);
1663 Data64
= IoRead16 ((UINTN
) IoRead
->Port
);
1666 Data64
= IoRead32 ((UINTN
) IoRead
->Port
);
1669 Data64
= IoRead64 ((UINTN
) IoRead
->Port
);
1672 Data64
= (UINT64
) -1;
1674 Status
= SendDataResponsePacket ((UINT8
*) &Data64
, IoRead
->Width
);
1677 case DEBUG_COMMAND_WRITE_IO
:
1678 IoWrite
= (DEBUG_DATA_WRITE_IO
*) (DebugHeader
+ 1);
1679 switch (IoWrite
->Width
) {
1681 Data64
= IoWrite8 ((UINTN
) IoWrite
->Port
, *(UINT8
*) &IoWrite
->Data
);
1684 Data64
= IoWrite16 ((UINTN
) IoWrite
->Port
, *(UINT16
*) &IoWrite
->Data
);
1687 Data64
= IoWrite32 ((UINTN
) IoWrite
->Port
, *(UINT32
*) &IoWrite
->Data
);
1690 Data64
= IoWrite64 ((UINTN
) IoWrite
->Port
, *(UINT64
*) &IoWrite
->Data
);
1693 Data64
= (UINT64
) -1;
1695 SendAckPacket (DEBUG_COMMAND_OK
);
1698 case DEBUG_COMMAND_READ_ALL_REGISTERS
:
1699 Status
= SendDataResponsePacket ((UINT8
*) CpuContext
, sizeof (*CpuContext
));
1702 case DEBUG_COMMAND_READ_REGISTER
:
1703 RegisterRead
= (DEBUG_DATA_READ_REGISTER
*) (DebugHeader
+ 1);
1705 if (RegisterRead
->Index
<= SOFT_DEBUGGER_REGISTER_MAX
) {
1706 RegisterBuffer
= ArchReadRegisterBuffer (CpuContext
, RegisterRead
->Index
, &Width
);
1707 Status
= SendDataResponsePacket (RegisterBuffer
, Width
);
1709 Status
= RETURN_UNSUPPORTED
;
1713 case DEBUG_COMMAND_WRITE_REGISTER
:
1714 RegisterWrite
= (DEBUG_DATA_WRITE_REGISTER
*) (DebugHeader
+ 1);
1715 if (RegisterWrite
->Index
<= SOFT_DEBUGGER_REGISTER_MAX
) {
1716 RegisterBuffer
= ArchReadRegisterBuffer (CpuContext
, RegisterWrite
->Index
, &Width
);
1717 ASSERT (Width
== RegisterWrite
->Length
);
1718 CopyMem (RegisterBuffer
, RegisterWrite
->Data
, Width
);
1719 SendAckPacket (DEBUG_COMMAND_OK
);
1721 Status
= RETURN_UNSUPPORTED
;
1725 case DEBUG_COMMAND_ARCH_MODE
:
1726 Data8
= DEBUG_ARCH_SYMBOL
;
1727 Status
= SendDataResponsePacket ((UINT8
*) &Data8
, (UINT16
) sizeof (UINT8
));
1730 case DEBUG_COMMAND_READ_MSR
:
1731 MsrRegisterRead
= (DEBUG_DATA_READ_MSR
*) (DebugHeader
+ 1);
1732 Data64
= AsmReadMsr64 (MsrRegisterRead
->Index
);
1733 Status
= SendDataResponsePacket ((UINT8
*) &Data64
, (UINT16
) sizeof (UINT64
));
1736 case DEBUG_COMMAND_WRITE_MSR
:
1737 MsrRegisterWrite
= (DEBUG_DATA_WRITE_MSR
*) (DebugHeader
+ 1);
1738 AsmWriteMsr64 (MsrRegisterWrite
->Index
, MsrRegisterWrite
->Value
);
1739 SendAckPacket (DEBUG_COMMAND_OK
);
1742 case DEBUG_COMMAND_SET_DEBUG_SETTING
:
1743 Status
= SetDebugSetting ((DEBUG_DATA_SET_DEBUG_SETTING
*)(DebugHeader
+ 1));
1744 if (Status
== RETURN_SUCCESS
) {
1745 SendAckPacket (DEBUG_COMMAND_OK
);
1749 case DEBUG_COMMAND_GET_REVISION
:
1750 DebugAgentRevision
.Revision
= DEBUG_AGENT_REVISION
;
1751 DebugAgentRevision
.Capabilities
= DEBUG_AGENT_CAPABILITIES
;
1752 Status
= SendDataResponsePacket ((UINT8
*) &DebugAgentRevision
, (UINT16
) sizeof (DEBUG_DATA_RESPONSE_GET_REVISION
));
1755 case DEBUG_COMMAND_GET_EXCEPTION
:
1756 Exception
.ExceptionNum
= (UINT8
) Vector
;
1757 Exception
.ExceptionData
= (UINT32
) CpuContext
->ExceptionData
;
1758 Status
= SendDataResponsePacket ((UINT8
*) &Exception
, (UINT16
) sizeof (DEBUG_DATA_RESPONSE_GET_EXCEPTION
));
1761 case DEBUG_COMMAND_SET_VIEWPOINT
:
1762 SetViewPoint
= (DEBUG_DATA_SET_VIEWPOINT
*) (DebugHeader
+ 1);
1763 if (MultiProcessorDebugSupport()) {
1764 if (IsCpuStopped (SetViewPoint
->ViewPoint
)) {
1765 SetDebugViewPoint (SetViewPoint
->ViewPoint
);
1766 SendAckPacket (DEBUG_COMMAND_OK
);
1769 // If CPU is not halted
1771 SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED
);
1773 } else if (SetViewPoint
->ViewPoint
== 0) {
1774 SendAckPacket (DEBUG_COMMAND_OK
);
1777 SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED
);
1782 case DEBUG_COMMAND_GET_VIEWPOINT
:
1783 Data32
= mDebugMpContext
.ViewPointIndex
;
1784 SendDataResponsePacket((UINT8
*) &Data32
, (UINT16
) sizeof (UINT32
));
1787 case DEBUG_COMMAND_MEMORY_READY
:
1788 Data8
= (UINT8
) GetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY
);
1789 SendDataResponsePacket (&Data8
, (UINT16
) sizeof (UINT8
));
1792 case DEBUG_COMMAND_DETACH
:
1793 SetHostAttached (FALSE
);
1794 SendAckPacket (DEBUG_COMMAND_OK
);
1797 case DEBUG_COMMAND_CPUID
:
1798 Cpuid
= (DEBUG_DATA_CPUID
*) (DebugHeader
+ 1);
1800 Cpuid
->Eax
, Cpuid
->Ecx
,
1801 &CpuidResponse
.Eax
, &CpuidResponse
.Ebx
,
1802 &CpuidResponse
.Ecx
, &CpuidResponse
.Edx
1804 SendDataResponsePacket ((UINT8
*) &CpuidResponse
, (UINT16
) sizeof (CpuidResponse
));
1807 case DEBUG_COMMAND_SEARCH_SIGNATURE
:
1808 SearchSignature
= (DEBUG_DATA_SEARCH_SIGNATURE
*) (DebugHeader
+ 1);
1809 if ((SearchSignature
->Alignment
!= 0) &&
1810 (SearchSignature
->Alignment
== GetPowerOfTwo32 (SearchSignature
->Alignment
))
1812 if (SearchSignature
->Positive
) {
1814 Data64
= ALIGN_VALUE ((UINTN
) SearchSignature
->Start
, SearchSignature
->Alignment
);
1815 Data64
<= SearchSignature
->Start
+ SearchSignature
->Count
- SearchSignature
->DataLength
;
1816 Data64
+= SearchSignature
->Alignment
1818 if (CompareMem ((VOID
*) (UINTN
) Data64
, &SearchSignature
->Data
, SearchSignature
->DataLength
) == 0) {
1822 if (Data64
> SearchSignature
->Start
+ SearchSignature
->Count
- SearchSignature
->DataLength
) {
1823 Data64
= (UINT64
) -1;
1827 Data64
= ALIGN_VALUE ((UINTN
) SearchSignature
->Start
- SearchSignature
->Alignment
, SearchSignature
->Alignment
);
1828 Data64
>= SearchSignature
->Start
- SearchSignature
->Count
;
1829 Data64
-= SearchSignature
->Alignment
1831 if (CompareMem ((VOID
*) (UINTN
) Data64
, &SearchSignature
->Data
, SearchSignature
->DataLength
) == 0) {
1835 if (Data64
< SearchSignature
->Start
- SearchSignature
->Count
) {
1836 Data64
= (UINT64
) -1;
1839 SendDataResponsePacket ((UINT8
*) &Data64
, (UINT16
) sizeof (Data64
));
1841 Status
= RETURN_UNSUPPORTED
;
1846 SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED
);
1850 if (Status
== RETURN_UNSUPPORTED
) {
1851 SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED
);
1852 } else if (Status
!= RETURN_SUCCESS
) {
1853 SendAckPacket (DEBUG_COMMAND_ABORT
);
1856 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
1862 C function called in interrupt handler.
1864 @param[in] Vector Vector value of exception or interrutp.
1865 @param[in] CpuContext Pointer to save CPU context.
1872 IN DEBUG_CPU_CONTEXT
*CpuContext
1875 UINT8 InputCharacter
;
1878 BOOLEAN BreakReceived
;
1879 UINT32 ProcessorIndex
;
1880 UINT32 CurrentDebugTimerInitCount
;
1881 DEBUG_PORT_HANDLE Handle
;
1884 UINT32 IssuedViewPoint
;
1885 DEBUG_AGENT_EXCEPTION_BUFFER
*ExceptionBuffer
;
1888 IssuedViewPoint
= 0;
1889 BreakReceived
= FALSE
;
1891 if (mSkipBreakpoint
) {
1893 // If Skip Breakpoint flag is set, means communication is disturbed by hardware SMI, we need to ignore the break points in SMM
1895 if ((Vector
== DEBUG_INT1_VECTOR
) || (Vector
== DEBUG_INT3_VECTOR
)) {
1896 DebugPortWriteBuffer (GetDebugPortHandle(), (UINT8
*) mWarningMsgIngoreBreakpoint
, AsciiStrLen (mWarningMsgIngoreBreakpoint
));
1901 if (MultiProcessorDebugSupport()) {
1902 ProcessorIndex
= GetProcessorIndex ();
1904 // If this processor has alreay halted before, need to check it later
1906 if (IsCpuStopped (ProcessorIndex
)) {
1907 IssuedViewPoint
= ProcessorIndex
;
1911 if (IssuedViewPoint
== ProcessorIndex
&& GetDebugFlag (DEBUG_AGENT_FLAG_STEPPING
) != 1) {
1913 // Check if this exception is issued by Debug Agent itself
1914 // If yes, fill the debug agent exception buffer and LongJump() back to
1915 // the saved CPU content in CommandCommunication()
1917 if (GetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS
) == 1) {
1918 DebugAgentMsgPrint (DEBUG_AGENT_ERROR
, "Debug agent meet one Exception, ExceptionNum is %d.\n", Vector
);
1919 ExceptionBuffer
= (DEBUG_AGENT_EXCEPTION_BUFFER
*) (UINTN
) GetMailboxPointer()->ExceptionBufferPointer
;
1920 ExceptionBuffer
->ExceptionContent
.ExceptionNum
= (UINT8
) Vector
;
1921 ExceptionBuffer
->ExceptionContent
.ExceptionData
= (UINT32
) CpuContext
->ExceptionData
;
1922 LongJump ((BASE_LIBRARY_JUMP_BUFFER
*)(UINTN
)(ExceptionBuffer
), 1);
1926 if (MultiProcessorDebugSupport()) {
1928 // If RUN commmand is executing, wait for it done.
1930 while (mDebugMpContext
.RunCommandSet
) {
1935 Handle
= GetDebugPortHandle();
1936 BreakCause
= GetBreakCause (Vector
, CpuContext
);
1938 case DEBUG_INT1_VECTOR
:
1939 case DEBUG_INT3_VECTOR
:
1940 switch (BreakCause
) {
1941 case DEBUG_DATA_BREAK_CAUSE_SYSTEM_RESET
:
1942 if (AttachHost (BreakCause
, READ_PACKET_TIMEOUT
, &BreakReceived
) != RETURN_SUCCESS
) {
1944 // Try to connect HOST, return if fails
1948 CommandCommunication (Vector
, CpuContext
, BreakReceived
);
1951 case DEBUG_DATA_BREAK_CAUSE_STEPPING
:
1953 // Stepping is finished, send Ack package.
1955 if (MultiProcessorDebugSupport()) {
1956 mDebugMpContext
.BreakAtCpuIndex
= ProcessorIndex
;
1958 SendAckPacket (DEBUG_COMMAND_OK
);
1959 CommandCommunication (Vector
, CpuContext
, BreakReceived
);
1962 case DEBUG_DATA_BREAK_CAUSE_MEMORY_READY
:
1966 SendCommandAndWaitForAckOK (DEBUG_COMMAND_MEMORY_READY
, READ_PACKET_TIMEOUT
, &BreakReceived
, NULL
);
1967 CommandCommunication (Vector
, CpuContext
, BreakReceived
);
1970 case DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD
:
1971 case DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD
:
1973 // Set AL to DEBUG_AGENT_IMAGE_CONTINUE
1975 Al
= ArchReadRegisterBuffer (CpuContext
, SOFT_DEBUGGER_REGISTER_AX
, &Data8
);
1976 *Al
= DEBUG_AGENT_IMAGE_CONTINUE
;
1978 if (!IsHostAttached ()) {
1980 // If HOST is not connected for image load/unload, return
1985 // Continue to run the following common code
1988 case DEBUG_DATA_BREAK_CAUSE_HW_BREAKPOINT
:
1989 case DEBUG_DATA_BREAK_CAUSE_SW_BREAKPOINT
:
1992 // Send Break packet to HOST
1994 AcquireMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
1996 // Only the first breaking processor could send BREAK_POINT to HOST
1998 if (IsFirstBreakProcessor (ProcessorIndex
)) {
1999 SendBreakPacketToHost (BreakCause
, ProcessorIndex
, &BreakReceived
);
2001 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
2003 if (Vector
== DEBUG_INT3_VECTOR
) {
2005 // go back address located "0xCC"
2008 SavedEip
= CpuContext
->Eip
;
2009 CommandCommunication (Vector
, CpuContext
, BreakReceived
);
2010 if ((SavedEip
== CpuContext
->Eip
) &&
2011 (*(UINT8
*) (UINTN
) CpuContext
->Eip
== DEBUG_SW_BREAKPOINT_SYMBOL
)) {
2013 // If this is not a software breakpoint set by HOST,
2019 CommandCommunication (Vector
, CpuContext
, BreakReceived
);
2026 case DEBUG_TIMER_VECTOR
:
2028 AcquireMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
2030 if (MultiProcessorDebugSupport()) {
2031 if (IsBsp (ProcessorIndex
)) {
2033 // If current processor is BSP, check Apic timer's init count if changed,
2034 // it may be re-written when switching BSP.
2035 // If it changed, re-initialize debug timer
2037 CurrentDebugTimerInitCount
= GetApicTimerInitCount ();
2038 if (mDebugMpContext
.DebugTimerInitCount
!= CurrentDebugTimerInitCount
) {
2039 InitializeDebugTimer ();
2043 if (!IsBsp (ProcessorIndex
) || mDebugMpContext
.IpiSentByAp
) {
2044 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
2046 // If current processor is not BSP or this is one IPI sent by AP
2048 if (mDebugMpContext
.BreakAtCpuIndex
!= (UINT32
) (-1)) {
2049 CommandCommunication (Vector
, CpuContext
, FALSE
);
2053 // Clear EOI before exiting interrupt process routine.
2061 // Only BSP could run here
2065 // If there is data in debug port, will check whether it is break(attach/break-in) symbol,
2066 // If yes, go into communication mode with HOST.
2067 // If no, exit interrupt process.
2069 if (DebugReadBreakSymbol (Handle
, &InputCharacter
) == EFI_NOT_FOUND
) {
2073 if ((!IsHostAttached () && (InputCharacter
== DEBUG_STARTING_SYMBOL_ATTACH
)) ||
2074 (IsHostAttached () && (InputCharacter
== DEBUG_COMMAND_HALT
)) ||
2075 (IsHostAttached () && (InputCharacter
== DEBUG_COMMAND_GO
))
2077 DebugAgentMsgPrint (DEBUG_AGENT_VERBOSE
, "Received data [%02x]\n", InputCharacter
);
2079 // Ack OK for break-in symbol
2081 SendAckPacket (DEBUG_COMMAND_OK
);
2084 // If receive GO command in Debug Timer, means HOST may lost ACK packet before.
2086 if (InputCharacter
== DEBUG_COMMAND_GO
) {
2090 if (!IsHostAttached ()) {
2092 // Try to attach HOST, if no ack received after 200ms, return
2094 if (AttachHost (BreakCause
, READ_PACKET_TIMEOUT
, &BreakReceived
) != RETURN_SUCCESS
) {
2099 if (MultiProcessorDebugSupport()) {
2100 if(FindNextPendingBreakCpu () != -1) {
2101 SetCpuBreakFlagByIndex (ProcessorIndex
, TRUE
);
2103 HaltOtherProcessors (ProcessorIndex
);
2106 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
2107 CommandCommunication (Vector
, CpuContext
, BreakReceived
);
2108 AcquireMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
2114 // Clear EOI before exiting interrupt process routine.
2118 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
2123 if (Vector
<= DEBUG_EXCEPT_SIMD
) {
2124 if (BreakCause
== DEBUG_DATA_BREAK_CAUSE_STEPPING
) {
2126 // Stepping is finished, send Ack package.
2128 if (MultiProcessorDebugSupport()) {
2129 mDebugMpContext
.BreakAtCpuIndex
= ProcessorIndex
;
2131 SendAckPacket (DEBUG_COMMAND_OK
);
2134 // Exception occurs, send Break packet to HOST
2136 AcquireMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
2138 // Only the first breaking processor could send BREAK_POINT to HOST
2140 if (IsFirstBreakProcessor (ProcessorIndex
)) {
2141 SendBreakPacketToHost (BreakCause
, ProcessorIndex
, &BreakReceived
);
2143 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
2146 CommandCommunication (Vector
, CpuContext
, BreakReceived
);
2151 if (MultiProcessorDebugSupport()) {
2153 // Clear flag and wait for all processors run here
2155 SetIpiSentByApFlag (FALSE
);
2156 while (mDebugMpContext
.RunCommandSet
) {
2161 // Only current (view) processor could clean up AgentInProgress flag.
2163 if (mDebugMpContext
.ViewPointIndex
== ProcessorIndex
) {
2164 IssuedViewPoint
= mDebugMpContext
.ViewPointIndex
;
2168 if (IssuedViewPoint
== ProcessorIndex
&& GetDebugFlag (DEBUG_AGENT_FLAG_STEPPING
) != 1) {
2170 // If the command is not stepping, clean up AgentInProgress flag
2172 SetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS
, 0);