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 - 2017, 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 GLOBAL_REMOVE_IF_UNREFERENCED CHAR8 mErrorMsgVersionAlert
[] = "\rThe SourceLevelDebugPkg you are using requires a newer version of the Intel(R) UDK Debugger Tool.\r\n";
22 GLOBAL_REMOVE_IF_UNREFERENCED CHAR8 mErrorMsgSendInitPacket
[] = "\rSend INIT break packet and try to connect the HOST (Intel(R) UDK Debugger Tool v1.5) ...\r\n";
23 GLOBAL_REMOVE_IF_UNREFERENCED CHAR8 mErrorMsgConnectOK
[] = "HOST connection is successful!\r\n";
24 GLOBAL_REMOVE_IF_UNREFERENCED CHAR8 mErrorMsgConnectFail
[] = "HOST connection is failed!\r\n";
25 GLOBAL_REMOVE_IF_UNREFERENCED CHAR8 mWarningMsgIngoreBreakpoint
[] = "Ignore break point in SMM for SMI issued during DXE debugging!\r\n";
28 // Vector Handoff Info list used by Debug Agent for persist
30 GLOBAL_REMOVE_IF_UNREFERENCED EFI_VECTOR_HANDOFF_INFO mVectorHandoffInfoDebugAgent
[] = {
32 DEBUG_EXCEPT_DIVIDE_ERROR
, // Vector 0
33 EFI_VECTOR_HANDOFF_HOOK_BEFORE
,
37 DEBUG_EXCEPT_DEBUG
, // Vector 1
38 EFI_VECTOR_HANDOFF_DO_NOT_HOOK
,
42 DEBUG_EXCEPT_NMI
, // Vector 2
43 EFI_VECTOR_HANDOFF_HOOK_BEFORE
,
47 DEBUG_EXCEPT_BREAKPOINT
, // Vector 3
48 EFI_VECTOR_HANDOFF_DO_NOT_HOOK
,
52 DEBUG_EXCEPT_OVERFLOW
, // Vector 4
53 EFI_VECTOR_HANDOFF_HOOK_BEFORE
,
57 DEBUG_EXCEPT_BOUND
, // Vector 5
58 EFI_VECTOR_HANDOFF_HOOK_BEFORE
,
62 DEBUG_EXCEPT_INVALID_OPCODE
, // Vector 6
63 EFI_VECTOR_HANDOFF_HOOK_BEFORE
,
67 DEBUG_EXCEPT_DOUBLE_FAULT
, // Vector 8
68 EFI_VECTOR_HANDOFF_HOOK_BEFORE
,
72 DEBUG_EXCEPT_INVALID_TSS
, // Vector 10
73 EFI_VECTOR_HANDOFF_HOOK_BEFORE
,
77 DEBUG_EXCEPT_SEG_NOT_PRESENT
, // Vector 11
78 EFI_VECTOR_HANDOFF_HOOK_BEFORE
,
82 DEBUG_EXCEPT_STACK_FAULT
, // Vector 12
83 EFI_VECTOR_HANDOFF_HOOK_BEFORE
,
87 DEBUG_EXCEPT_GP_FAULT
, // Vector 13
88 EFI_VECTOR_HANDOFF_HOOK_BEFORE
,
92 DEBUG_EXCEPT_PAGE_FAULT
, // Vector 14
93 EFI_VECTOR_HANDOFF_HOOK_BEFORE
,
97 DEBUG_EXCEPT_FP_ERROR
, // Vector 16
98 EFI_VECTOR_HANDOFF_HOOK_BEFORE
,
102 DEBUG_EXCEPT_ALIGNMENT_CHECK
, // Vector 17
103 EFI_VECTOR_HANDOFF_HOOK_BEFORE
,
107 DEBUG_EXCEPT_MACHINE_CHECK
, // Vector 18
108 EFI_VECTOR_HANDOFF_HOOK_BEFORE
,
112 DEBUG_EXCEPT_SIMD
, // Vector 19
113 EFI_VECTOR_HANDOFF_HOOK_BEFORE
,
117 DEBUG_TIMER_VECTOR
, // Vector 32
118 EFI_VECTOR_HANDOFF_DO_NOT_HOOK
,
122 DEBUG_MAILBOX_VECTOR
, // Vector 33
123 EFI_VECTOR_HANDOFF_DO_NOT_HOOK
,
128 EFI_VECTOR_HANDOFF_LAST_ENTRY
,
133 GLOBAL_REMOVE_IF_UNREFERENCED UINTN mVectorHandoffInfoCount
= sizeof (mVectorHandoffInfoDebugAgent
) / sizeof (EFI_VECTOR_HANDOFF_INFO
);
136 Calculate CRC16 for target data.
138 @param[in] Data The target data.
139 @param[in] DataSize The target data size.
140 @param[in] Crc Initial CRC.
142 @return UINT16 The CRC16 value.
155 for (Index
= 0; Index
< DataSize
; Index
++) {
156 Crc
^= (UINT16
)Data
[Index
];
157 for (BitIndex
= 0; BitIndex
< 8; BitIndex
++) {
158 if ((Crc
& 0x8000) != 0) {
171 Read IDT entry to check if IDT entries are setup by Debug Agent.
173 @retval TRUE IDT entries were setup by Debug Agent.
174 @retval FALSE IDT entries were not setup by Debug Agent.
178 IsDebugAgentInitialzed (
182 UINTN InterruptHandler
;
184 InterruptHandler
= (UINTN
) GetExceptionHandlerInIdtEntry (0);
185 if (InterruptHandler
>= 4 && *(UINT32
*)(InterruptHandler
- 4) == AGENT_HANDLER_SIGNATURE
) {
193 Find and report module image info to HOST.
195 @param[in] AlignSize Image aligned size.
199 FindAndReportModuleImageInfo (
204 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
209 Pe32Data
= PeCoffSearchImageBase ((UINTN
) mErrorMsgVersionAlert
);
211 ImageContext
.ImageAddress
= Pe32Data
;
212 ImageContext
.PdbPointer
= PeCoffLoaderGetPdbPointer ((VOID
*) (UINTN
) ImageContext
.ImageAddress
);
213 PeCoffLoaderRelocateImageExtraAction (&ImageContext
);
218 Trigger one software interrupt to debug agent to handle it.
220 @param[in] Signature Software interrupt signature.
224 TriggerSoftInterrupt (
232 // Save Debug Register State
240 AsmWriteDr0 (SOFT_INTERRUPT_SIGNATURE
);
241 AsmWriteDr1 (Signature
);
244 // Do INT3 to communicate with HOST side
249 // Restore Debug Register State only when Host didn't change it inside exception handler.
250 // Dr registers can only be changed by setting the HW breakpoint.
258 Calculate Mailbox checksum and update the checksum field.
260 @param[in] Mailbox Debug Agent Mailbox pointer.
264 UpdateMailboxChecksum (
265 IN DEBUG_AGENT_MAILBOX
*Mailbox
268 Mailbox
->CheckSum
= CalculateCheckSum8 ((UINT8
*)Mailbox
, sizeof (DEBUG_AGENT_MAILBOX
) - 2);
272 Verify Mailbox checksum.
274 If checksum error, print debug message and run init dead loop.
276 @param[in] Mailbox Debug Agent Mailbox pointer.
280 VerifyMailboxChecksum (
281 IN DEBUG_AGENT_MAILBOX
*Mailbox
286 CheckSum
= CalculateCheckSum8 ((UINT8
*) Mailbox
, sizeof (DEBUG_AGENT_MAILBOX
) - 2);
288 // The checksum updating process may be disturbed by hardware SMI, we need to check CheckSum field
289 // and ToBeCheckSum field to validate the mail box.
291 if (CheckSum
!= Mailbox
->CheckSum
&& CheckSum
!= Mailbox
->ToBeCheckSum
) {
292 DEBUG ((EFI_D_ERROR
, "DebugAgent: Mailbox checksum error, stack or heap crashed!\n"));
293 DEBUG ((EFI_D_ERROR
, "DebugAgent: CheckSum = %x, Mailbox->CheckSum = %x, Mailbox->ToBeCheckSum = %x\n", CheckSum
, Mailbox
->CheckSum
, Mailbox
->ToBeCheckSum
));
299 Update Mailbox content by index.
301 @param[in] Mailbox Debug Agent Mailbox pointer.
302 @param[in] Index Mailbox content index.
303 @param[in] Value Value to be set into Mailbox.
307 UpdateMailboxContent (
308 IN DEBUG_AGENT_MAILBOX
*Mailbox
,
313 AcquireMpSpinLock (&mDebugMpContext
.MailboxSpinLock
);
315 case DEBUG_MAILBOX_DEBUG_FLAG_INDEX
:
316 Mailbox
->ToBeCheckSum
= Mailbox
->CheckSum
+ CalculateSum8 ((UINT8
*)&Mailbox
->DebugFlag
.Uint64
, sizeof(UINT64
))
317 - CalculateSum8 ((UINT8
*)&Value
, sizeof(UINT64
));
318 Mailbox
->DebugFlag
.Uint64
= Value
;
320 case DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX
:
321 Mailbox
->ToBeCheckSum
= Mailbox
->CheckSum
+ CalculateSum8 ((UINT8
*)&Mailbox
->DebugPortHandle
, sizeof(UINTN
))
322 - CalculateSum8 ((UINT8
*)&Value
, sizeof(UINTN
));
323 Mailbox
->DebugPortHandle
= (UINTN
) Value
;
325 case DEBUG_MAILBOX_EXCEPTION_BUFFER_POINTER_INDEX
:
326 Mailbox
->ToBeCheckSum
= Mailbox
->CheckSum
+ CalculateSum8 ((UINT8
*)&Mailbox
->ExceptionBufferPointer
, sizeof(UINTN
))
327 - CalculateSum8 ((UINT8
*)&Value
, sizeof(UINTN
));
328 Mailbox
->ExceptionBufferPointer
= (UINTN
) Value
;
330 case DEBUG_MAILBOX_LAST_ACK
:
331 Mailbox
->ToBeCheckSum
= Mailbox
->CheckSum
+ CalculateSum8 ((UINT8
*)&Mailbox
->LastAck
, sizeof(UINT8
))
332 - CalculateSum8 ((UINT8
*)&Value
, sizeof(UINT8
));
333 Mailbox
->LastAck
= (UINT8
) Value
;
335 case DEBUG_MAILBOX_SEQUENCE_NO_INDEX
:
336 Mailbox
->ToBeCheckSum
= Mailbox
->CheckSum
+ CalculateSum8 ((UINT8
*)&Mailbox
->SequenceNo
, sizeof(UINT8
))
337 - CalculateSum8 ((UINT8
*)&Value
, sizeof(UINT8
));
338 Mailbox
->SequenceNo
= (UINT8
) Value
;
340 case DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX
:
341 Mailbox
->ToBeCheckSum
= Mailbox
->CheckSum
+ CalculateSum8 ((UINT8
*)&Mailbox
->HostSequenceNo
, sizeof(UINT8
))
342 - CalculateSum8 ((UINT8
*)&Value
, sizeof(UINT8
));
343 Mailbox
->HostSequenceNo
= (UINT8
) Value
;
345 case DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY
:
346 Mailbox
->ToBeCheckSum
= Mailbox
->CheckSum
+ CalculateSum8 ((UINT8
*)&Mailbox
->DebugTimerFrequency
, sizeof(UINT32
))
347 - CalculateSum8 ((UINT8
*)&Value
, sizeof(UINT32
));
348 Mailbox
->DebugTimerFrequency
= (UINT32
) Value
;
351 UpdateMailboxChecksum (Mailbox
);
352 ReleaseMpSpinLock (&mDebugMpContext
.MailboxSpinLock
);
356 Read data from debug device and save the data in buffer.
358 Reads NumberOfBytes data bytes from a debug device into the buffer
359 specified by Buffer. The number of bytes actually read is returned.
360 If the return value is less than NumberOfBytes, then the rest operation failed.
361 If NumberOfBytes is zero, then return 0.
363 @param Handle Debug port handle.
364 @param Buffer Pointer to the data buffer to store the data read from the debug device.
365 @param NumberOfBytes Number of bytes which will be read.
366 @param Timeout Timeout value for reading from debug device. It unit is Microsecond.
368 @retval 0 Read data failed, no data is to be read.
369 @retval >0 Actual number of bytes read from debug device.
373 DebugAgentReadBuffer (
374 IN DEBUG_PORT_HANDLE Handle
,
376 IN UINTN NumberOfBytes
,
382 UINT32 TimeoutTicker
;
384 UINT32 TimerFrequency
;
390 TimerFrequency
= GetMailboxPointer()->DebugTimerFrequency
;
391 TimerCycle
= GetApicTimerInitCount ();
394 Begin
= GetApicTimerCurrentCount ();
395 TimeoutTicker
= (UINT32
) DivU64x32 (
402 TimerRound
= (UINT32
) DivU64x32Remainder (TimeoutTicker
, TimerCycle
/ 2, &TimeoutTicker
);
405 while (Index
< NumberOfBytes
) {
406 if (DebugPortPollBuffer (Handle
)) {
407 DebugPortReadBuffer (Handle
, Buffer
+ Index
, 1, 0);
412 if (TimerRound
== 0) {
413 if (IsDebugTimerTimeout (TimerCycle
, Begin
, TimeoutTicker
)) {
415 // If time out occurs.
420 if (IsDebugTimerTimeout (TimerCycle
, Begin
, TimerCycle
/ 2)) {
422 Begin
= GetApicTimerCurrentCount ();
432 Set debug flag in mailbox.
434 @param[in] FlagMask Debug flag mask value.
435 @param[in] FlagValue Debug flag value.
444 DEBUG_AGENT_MAILBOX
*Mailbox
;
447 Mailbox
= GetMailboxPointer ();
448 Data64
= (Mailbox
->DebugFlag
.Uint64
& ~FlagMask
) |
449 (LShiftU64 ((UINT64
)FlagValue
, LowBitSet64 (FlagMask
)) & FlagMask
);
450 UpdateMailboxContent (Mailbox
, DEBUG_MAILBOX_DEBUG_FLAG_INDEX
, Data64
);
454 Get debug flag in mailbox.
456 @param[in] FlagMask Debug flag mask value.
458 @return Debug flag value.
466 DEBUG_AGENT_MAILBOX
*Mailbox
;
469 Mailbox
= GetMailboxPointer ();
470 DebugFlag
= (UINT32
) RShiftU64 (Mailbox
->DebugFlag
.Uint64
& FlagMask
, LowBitSet64 (FlagMask
));
476 Send a debug message packet to the debug port.
478 @param[in] Buffer The debug message.
479 @param[in] Length The length of debug message.
488 DEBUG_PACKET_HEADER DebugHeader
;
489 DEBUG_PORT_HANDLE Handle
;
491 Handle
= GetDebugPortHandle();
493 DebugHeader
.StartSymbol
= DEBUG_STARTING_SYMBOL_NORMAL
;
494 DebugHeader
.Command
= DEBUG_COMMAND_PRINT_MESSAGE
;
495 DebugHeader
.Length
= sizeof (DEBUG_PACKET_HEADER
) + (UINT8
) Length
;
496 DebugHeader
.SequenceNo
= 0xEE;
498 DebugHeader
.Crc
= CalculateCrc16 (
499 (UINT8
*)Buffer
, Length
,
500 CalculateCrc16 ((UINT8
*)&DebugHeader
, sizeof (DEBUG_PACKET_HEADER
), 0)
503 DebugPortWriteBuffer (Handle
, (UINT8
*)&DebugHeader
, sizeof (DEBUG_PACKET_HEADER
));
504 DebugPortWriteBuffer (Handle
, (UINT8
*)Buffer
, Length
);
508 Prints a debug message to the debug port if the specified error level is enabled.
510 If any bit in ErrorLevel is also set in Mainbox, then print the message specified
511 by Format and the associated variable argument list to the debug port.
513 @param[in] ErrorLevel The error level of the debug message.
514 @param[in] Format Format string for the debug message to print.
515 @param[in] ... Variable argument list whose contents are accessed
516 based on the format string specified by Format.
527 CHAR8 Buffer
[DEBUG_DATA_MAXIMUM_REAL_DATA
];
531 // Check driver debug mask value and global mask
533 if ((ErrorLevel
& GetDebugFlag (DEBUG_AGENT_FLAG_PRINT_ERROR_LEVEL
)) == 0) {
538 // Convert the DEBUG() message to an ASCII String
540 VA_START (Marker
, Format
);
541 AsciiVSPrint (Buffer
, sizeof (Buffer
), Format
, Marker
);
544 SendDebugMsgPacket (Buffer
, AsciiStrLen (Buffer
));
548 Prints a debug message to the debug output device if the specified error level is enabled.
550 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
551 GetDebugPrintErrorLevel (), then print the message specified by Format and the
552 associated variable argument list to the debug output device.
554 If Format is NULL, then ASSERT().
556 @param[in] ErrorLevel The error level of the debug message.
557 @param[in] IsSend Flag of debug message to declare that the data is being sent or being received.
558 @param[in] Data Variable argument list whose contents are accessed
559 @param[in] Length based on the format string specified by Format.
564 DebugAgentDataMsgPrint (
571 CHAR8 Buffer
[DEBUG_DATA_MAXIMUM_REAL_DATA
];
576 // Check driver debug mask value and global mask
578 if ((ErrorLevel
& GetDebugFlag (DEBUG_AGENT_FLAG_PRINT_ERROR_LEVEL
)) == 0) {
584 DestBuffer
+= AsciiSPrint (DestBuffer
, DEBUG_DATA_MAXIMUM_REAL_DATA
, "Sent data [ ");
586 DestBuffer
+= AsciiSPrint (DestBuffer
, DEBUG_DATA_MAXIMUM_REAL_DATA
, "Received data [ ");
591 if (DestBuffer
- Buffer
> DEBUG_DATA_MAXIMUM_REAL_DATA
- 6) {
593 // If there was no enough space in buffer, send out the debug message,
594 // reserving 6 bytes is for the last data and end characters "]\n".
596 SendDebugMsgPacket (Buffer
, DestBuffer
- Buffer
);
599 DestBuffer
+= AsciiSPrint (DestBuffer
, DEBUG_DATA_MAXIMUM_REAL_DATA
- (DestBuffer
- Buffer
), "%02x ", Data
[Index
]);
601 if (Index
>= Length
) {
603 // The last character of debug message has been foramtted in buffer
605 DestBuffer
+= AsciiSPrint(DestBuffer
, DEBUG_DATA_MAXIMUM_REAL_DATA
- (DestBuffer
- Buffer
), "]\n");
606 SendDebugMsgPacket (Buffer
, DestBuffer
- Buffer
);
613 Read remaing debug packet except for the start symbol
615 @param[in] Handle Pointer to Debug Port handle.
616 @param[in, out] DebugHeader Debug header buffer including start symbol.
618 @retval EFI_SUCCESS Read the symbol in BreakSymbol.
619 @retval EFI_CRC_ERROR CRC check fail.
620 @retval EFI_TIMEOUT Timeout occurs when reading debug packet.
621 @retval EFI_DEVICE_ERROR Receive the old or responsed packet.
625 ReadRemainingBreakPacket (
626 IN DEBUG_PORT_HANDLE Handle
,
627 IN OUT DEBUG_PACKET_HEADER
*DebugHeader
631 DEBUG_AGENT_MAILBOX
*Mailbox
;
634 // Has received start symbol, try to read the rest part
636 if (DebugAgentReadBuffer (Handle
, (UINT8
*)DebugHeader
+ OFFSET_OF (DEBUG_PACKET_HEADER
, Command
), sizeof (DEBUG_PACKET_HEADER
) - OFFSET_OF (DEBUG_PACKET_HEADER
, Command
), READ_PACKET_TIMEOUT
) == 0) {
638 // Timeout occur, exit
640 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "Timeout in Debug Timer interrupt\n");
644 Crc
= DebugHeader
->Crc
;
645 DebugHeader
->Crc
= 0;
646 if (CalculateCrc16 ((UINT8
*)DebugHeader
, DebugHeader
->Length
, 0) != Crc
) {
647 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "Debug Timer CRC (%x) against (%x)\n", Crc
, CalculateCrc16 ((UINT8
*) &DebugHeader
, DebugHeader
->Length
, 0));
648 DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE
, FALSE
, (UINT8
*)DebugHeader
, DebugHeader
->Length
);
649 return EFI_CRC_ERROR
;
651 Mailbox
= GetMailboxPointer();
652 if (IS_REQUEST (DebugHeader
)) {
653 if (DebugHeader
->SequenceNo
== (UINT8
) (Mailbox
->HostSequenceNo
+ 1)) {
655 // Only updagte HostSequenceNo for new command packet
657 UpdateMailboxContent (Mailbox
, DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX
, DebugHeader
->SequenceNo
);
660 if (DebugHeader
->SequenceNo
== Mailbox
->HostSequenceNo
) {
665 return EFI_DEVICE_ERROR
;
669 Check if HOST is attached based on Mailbox.
671 @retval TRUE HOST is attached.
672 @retval FALSE HOST is not attached.
680 return (BOOLEAN
) (GetDebugFlag (DEBUG_AGENT_FLAG_HOST_ATTACHED
) == 1);
684 Set HOST connect flag in Mailbox.
686 @param[in] Attached Attach status.
694 DebugAgentMsgPrint (DEBUG_AGENT_INFO
, "Attach status is %d\n", Attached
);
695 SetDebugFlag (DEBUG_AGENT_FLAG_HOST_ATTACHED
, (UINT32
)Attached
);
699 Set debug setting of Debug Agent in Mailbox.
701 @param DebugSetting Pointer to Debug Setting defined by transfer protocol.
703 @retval RETURN_SUCCESS The setting is set successfully.
704 @retval RETURN_UNSUPPORTED The Key value is not supported.
709 IN DEBUG_DATA_SET_DEBUG_SETTING
*DebugSetting
712 RETURN_STATUS Status
;
714 Status
= RETURN_SUCCESS
;
715 switch (DebugSetting
->Key
) {
716 case DEBUG_AGENT_SETTING_SMM_ENTRY_BREAK
:
717 SetDebugFlag (DEBUG_AGENT_FLAG_BREAK_ON_NEXT_SMI
, DebugSetting
->Value
);
719 case DEBUG_AGENT_SETTING_PRINT_ERROR_LEVEL
:
720 SetDebugFlag (DEBUG_AGENT_FLAG_PRINT_ERROR_LEVEL
, DebugSetting
->Value
);
722 case DEBUG_AGENT_SETTING_BOOT_SCRIPT_ENTRY_BREAK
:
723 SetDebugFlag (DEBUG_AGENT_FLAG_BREAK_BOOT_SCRIPT
, DebugSetting
->Value
);
726 Status
= RETURN_UNSUPPORTED
;
734 @param[in] CpuContext Pointer to saved CPU context.
739 IN DEBUG_CPU_CONTEXT
*CpuContext
742 IA32_EFLAGS32
*Eflags
;
744 Eflags
= (IA32_EFLAGS32
*) &CpuContext
->Eflags
;
750 Execute Stepping command.
752 @param[in] CpuContext Pointer to saved CPU context.
757 IN DEBUG_CPU_CONTEXT
*CpuContext
760 IA32_EFLAGS32
*Eflags
;
762 Eflags
= (IA32_EFLAGS32
*) &CpuContext
->Eflags
;
766 // Save and clear EFLAGS.IF to avoid interrupt happen when executing Stepping
768 SetDebugFlag (DEBUG_AGENT_FLAG_INTERRUPT_FLAG
, Eflags
->Bits
.IF
);
773 SetDebugFlag (DEBUG_AGENT_FLAG_STEPPING
, 1);
777 Do some cleanup after Stepping command done.
779 @param[in] CpuContext Pointer to saved CPU context.
783 CommandSteppingCleanup (
784 IN DEBUG_CPU_CONTEXT
*CpuContext
787 IA32_EFLAGS32
*Eflags
;
789 Eflags
= (IA32_EFLAGS32
*) &CpuContext
->Eflags
;
793 Eflags
->Bits
.IF
= GetDebugFlag (DEBUG_AGENT_FLAG_INTERRUPT_FLAG
);
795 // Clear Stepping flag
797 SetDebugFlag (DEBUG_AGENT_FLAG_STEPPING
, 0);
801 Set debug register for hardware breakpoint.
803 @param[in] CpuContext Pointer to saved CPU context.
804 @param[in] SetHwBreakpoint Hardware breakpoint to be set.
809 IN DEBUG_CPU_CONTEXT
*CpuContext
,
810 IN DEBUG_DATA_SET_HW_BREAKPOINT
*SetHwBreakpoint
816 RegisterIndex
= SetHwBreakpoint
->Type
.Index
;
821 * ((UINTN
*) &CpuContext
->Dr0
+ RegisterIndex
) = (UINTN
) SetHwBreakpoint
->Address
;
823 Dr7Value
= CpuContext
->Dr7
;
828 Dr7Value
|= (UINTN
) (0x3 << (RegisterIndex
* 2));
832 Dr7Value
&= (UINTN
) (~(0xf << (16 + RegisterIndex
* 4)));
833 Dr7Value
|= (UINTN
) ((SetHwBreakpoint
->Type
.Length
<< 2) | SetHwBreakpoint
->Type
.Access
) << (16 + RegisterIndex
* 4);
839 CpuContext
->Dr7
= Dr7Value
;
843 Clear debug register for hardware breakpoint.
845 @param[in] CpuContext Pointer to saved CPU context.
846 @param[in] ClearHwBreakpoint Hardware breakpoint to be cleared.
851 IN DEBUG_CPU_CONTEXT
*CpuContext
,
852 IN DEBUG_DATA_CLEAR_HW_BREAKPOINT
*ClearHwBreakpoint
855 if ((ClearHwBreakpoint
->IndexMask
& BIT0
) != 0) {
857 CpuContext
->Dr7
&= (UINTN
)(~(0x3 << 0));
859 if ((ClearHwBreakpoint
->IndexMask
& BIT1
) != 0) {
861 CpuContext
->Dr7
&= (UINTN
)(~(0x3 << 2));
863 if ((ClearHwBreakpoint
->IndexMask
& BIT2
) != 0) {
865 CpuContext
->Dr7
&= (UINTN
)(~(0x3 << 4));
867 if ((ClearHwBreakpoint
->IndexMask
& BIT3
) != 0) {
869 CpuContext
->Dr7
&= (UINTN
)(~(0x3 << 6));
875 Return the offset of FP / MMX / XMM registers in the FPU saved state by register index.
877 @param[in] Index Register index.
878 @param[out] Width Register width returned.
880 @return Offset in the FPU Save State.
884 ArchReadFxStatOffset (
889 if (Index
< SOFT_DEBUGGER_REGISTER_ST0
) {
891 case SOFT_DEBUGGER_REGISTER_FP_FCW
:
892 *Width
= (UINT8
) sizeof (UINT16
);
893 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE
, Fcw
);
895 case SOFT_DEBUGGER_REGISTER_FP_FSW
:
896 *Width
= (UINT8
) sizeof (UINT16
);
897 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE
, Fsw
);
899 case SOFT_DEBUGGER_REGISTER_FP_FTW
:
900 *Width
= (UINT8
) sizeof (UINT16
);
901 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE
, Ftw
);
903 case SOFT_DEBUGGER_REGISTER_FP_OPCODE
:
904 *Width
= (UINT8
) sizeof (UINT16
);
905 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE
, Opcode
);
907 case SOFT_DEBUGGER_REGISTER_FP_EIP
:
908 *Width
= (UINT8
) sizeof (UINT32
);
909 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE
, Eip
);
911 case SOFT_DEBUGGER_REGISTER_FP_CS
:
912 *Width
= (UINT8
) sizeof (UINT16
);
913 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE
, Cs
);
915 case SOFT_DEBUGGER_REGISTER_FP_DATAOFFSET
:
916 *Width
= (UINT8
) sizeof (UINT32
);
917 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE
, DataOffset
);
919 case SOFT_DEBUGGER_REGISTER_FP_DS
:
920 *Width
= (UINT8
) sizeof (UINT16
);
921 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE
, Ds
);
923 case SOFT_DEBUGGER_REGISTER_FP_MXCSR
:
924 *Width
= (UINT8
) sizeof (UINT32
);
925 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE
, Mxcsr
);
927 case SOFT_DEBUGGER_REGISTER_FP_MXCSR_MASK
:
928 *Width
= (UINT8
) sizeof (UINT32
);
929 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE
, Mxcsr_Mask
);
933 if (Index
<= SOFT_DEBUGGER_REGISTER_ST7
) {
935 } else if (Index
<= SOFT_DEBUGGER_REGISTER_XMM15
) {
942 Index
-= SOFT_DEBUGGER_REGISTER_MM0
- SOFT_DEBUGGER_REGISTER_ST0
;
945 return OFFSET_OF (DEBUG_DATA_FX_SAVE_STATE
, St0Mm0
) + (Index
- SOFT_DEBUGGER_REGISTER_ST0
) * 16;
949 Return the pointer of the register value in the CPU saved context.
951 @param[in] CpuContext Pointer to saved CPU context.
952 @param[in] Index Register index value.
953 @param[out] Width Data width to read.
955 @return The pointer in the CPU saved context.
959 ArchReadRegisterBuffer (
960 IN DEBUG_CPU_CONTEXT
*CpuContext
,
967 if (Index
< SOFT_DEBUGGER_REGISTER_FP_BASE
) {
968 Buffer
= (UINT8
*) CpuContext
+ OFFSET_OF (DEBUG_CPU_CONTEXT
, Dr0
) + Index
* sizeof (UINTN
);
969 *Width
= (UINT8
) sizeof (UINTN
);
972 // FPU/MMX/XMM registers
974 Buffer
= (UINT8
*) CpuContext
+ OFFSET_OF (DEBUG_CPU_CONTEXT
, FxSaveState
) + ArchReadFxStatOffset (Index
, Width
);
981 Send the packet without data to HOST.
983 @param[in] CommandType Type of Command.
984 @param[in] SequenceNo Sequence number.
988 SendPacketWithoutData (
989 IN UINT8 CommandType
,
993 DEBUG_PACKET_HEADER DebugHeader
;
994 DEBUG_PORT_HANDLE Handle
;
996 Handle
= GetDebugPortHandle();
998 DebugHeader
.StartSymbol
= DEBUG_STARTING_SYMBOL_NORMAL
;
999 DebugHeader
.Command
= CommandType
;
1000 DebugHeader
.Length
= sizeof (DEBUG_PACKET_HEADER
);
1001 DebugHeader
.SequenceNo
= SequenceNo
;
1002 DebugHeader
.Crc
= 0;
1003 DebugHeader
.Crc
= CalculateCrc16 ((UINT8
*)&DebugHeader
, sizeof (DEBUG_PACKET_HEADER
), 0);
1005 DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE
, TRUE
, (UINT8
*) &DebugHeader
, DebugHeader
.Length
);
1006 DebugPortWriteBuffer (Handle
, (UINT8
*) &DebugHeader
, DebugHeader
.Length
);
1010 Send acknowledge packet to HOST.
1012 @param[in] AckCommand Type of Acknowledge packet.
1021 DEBUG_AGENT_MAILBOX
*Mailbox
;
1023 if (AckCommand
!= DEBUG_COMMAND_OK
) {
1025 // This is not ACK OK packet
1027 DebugAgentMsgPrint (DEBUG_AGENT_ERROR
, "Send ACK(%d)\n", AckCommand
);
1029 Mailbox
= GetMailboxPointer();
1030 SequenceNo
= Mailbox
->HostSequenceNo
;
1031 DebugAgentMsgPrint (DEBUG_AGENT_INFO
, "SendAckPacket: SequenceNo = %x\n", SequenceNo
);
1032 SendPacketWithoutData (AckCommand
, SequenceNo
);
1033 UpdateMailboxContent (Mailbox
, DEBUG_MAILBOX_LAST_ACK
, AckCommand
);
1037 Decompress the Data in place.
1039 @param[in, out] Data The compressed data buffer.
1040 The buffer is assumed large enough to hold the uncompressed data.
1041 @param[in] Length The length of the compressed data buffer.
1043 @return The length of the uncompressed data buffer.
1046 DecompressDataInPlace (
1053 UINTN LastCharCount
;
1056 LastChar
= (UINT16
) -1;
1058 for (Index
= 0; Index
< Length
; Index
++) {
1059 CurrentChar
= Data
[Index
];
1060 if (LastCharCount
== 2) {
1062 CopyMem (&Data
[Index
+ CurrentChar
], &Data
[Index
+ 1], Length
- Index
- 1);
1063 SetMem (&Data
[Index
], CurrentChar
, (UINT8
) LastChar
);
1064 LastChar
= (UINT16
) -1;
1065 Index
+= CurrentChar
- 1;
1066 Length
+= CurrentChar
- 1;
1068 if (LastChar
!= CurrentChar
) {
1072 LastChar
= CurrentChar
;
1076 ASSERT (Length
<= DEBUG_DATA_MAXIMUM_REAL_DATA
);
1078 return (UINT8
) Length
;
1082 Receive valid packet from HOST.
1084 @param[out] InputPacket Buffer to receive packet.
1085 @param[out] BreakReceived TRUE means break-in symbol received.
1086 FALSE means break-in symbol not received.
1087 @param[out] IncompatibilityFlag If IncompatibilityFlag is not NULL, return
1088 TRUE: Compatible packet received.
1089 FALSE: Incompatible packet received.
1090 @param[in] Timeout Time out value to wait for acknowlege from HOST.
1091 The unit is microsecond.
1092 @param[in] SkipStartSymbol TRUE: Skip time out when reading start symbol.
1093 FALSE: Does not Skip time out when reading start symbol.
1095 @retval RETURN_SUCCESS A valid package was reveived in InputPacket.
1096 @retval RETURN_TIMEOUT Timeout occurs.
1101 OUT UINT8
*InputPacket
,
1102 OUT BOOLEAN
*BreakReceived
,
1103 OUT BOOLEAN
*IncompatibilityFlag
, OPTIONAL
1105 IN BOOLEAN SkipStartSymbol
1108 DEBUG_PACKET_HEADER
*DebugHeader
;
1110 DEBUG_PORT_HANDLE Handle
;
1112 UINTN TimeoutForStartSymbol
;
1114 Handle
= GetDebugPortHandle();
1115 if (SkipStartSymbol
) {
1116 TimeoutForStartSymbol
= 0;
1118 TimeoutForStartSymbol
= Timeout
;
1121 DebugHeader
= (DEBUG_PACKET_HEADER
*) InputPacket
;
1124 // Find the valid start symbol
1126 Received
= DebugAgentReadBuffer (Handle
, &DebugHeader
->StartSymbol
, sizeof (DebugHeader
->StartSymbol
), TimeoutForStartSymbol
);
1127 if (Received
< sizeof (DebugHeader
->StartSymbol
)) {
1128 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "DebugAgentReadBuffer(StartSymbol) timeout\n");
1129 return RETURN_TIMEOUT
;
1132 if ((DebugHeader
->StartSymbol
!= DEBUG_STARTING_SYMBOL_NORMAL
) && (DebugHeader
->StartSymbol
!= DEBUG_STARTING_SYMBOL_COMPRESS
)) {
1133 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "Invalid start symbol received [%02x]\n", DebugHeader
->StartSymbol
);
1138 // Read Package header till field Length
1140 Received
= DebugAgentReadBuffer (
1142 (UINT8
*) DebugHeader
+ OFFSET_OF (DEBUG_PACKET_HEADER
, Command
),
1143 OFFSET_OF (DEBUG_PACKET_HEADER
, Length
) + sizeof (DebugHeader
->Length
) - sizeof (DebugHeader
->StartSymbol
),
1146 if (Received
== 0) {
1147 DebugAgentMsgPrint (DEBUG_AGENT_ERROR
, "DebugAgentReadBuffer(Command) timeout\n");
1148 return RETURN_TIMEOUT
;
1150 if (DebugHeader
->Length
< sizeof (DEBUG_PACKET_HEADER
)) {
1151 if (IncompatibilityFlag
!= NULL
) {
1153 // This is one old version debug packet format, set Incompatibility flag
1155 *IncompatibilityFlag
= TRUE
;
1158 // Skip the bad small packet
1164 // Read the payload data include the CRC field
1166 Received
= DebugAgentReadBuffer (Handle
, &DebugHeader
->SequenceNo
, (UINT8
) (DebugHeader
->Length
- OFFSET_OF (DEBUG_PACKET_HEADER
, SequenceNo
)), Timeout
);
1167 if (Received
== 0) {
1168 DebugAgentMsgPrint (DEBUG_AGENT_ERROR
, "DebugAgentReadBuffer(SequenceNo) timeout\n");
1169 return RETURN_TIMEOUT
;
1172 // Calculate the CRC of Debug Packet
1174 Crc
= DebugHeader
->Crc
;
1175 DebugHeader
->Crc
= 0;
1176 if (Crc
== CalculateCrc16 ((UINT8
*) DebugHeader
, DebugHeader
->Length
, 0)) {
1179 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "CRC Error (received CRC is %x)\n", Crc
);
1180 DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE
, FALSE
, (UINT8
*) DebugHeader
, DebugHeader
->Length
);
1184 DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE
, FALSE
, (UINT8
*) DebugHeader
, DebugHeader
->Length
);
1186 if (DebugHeader
->StartSymbol
== DEBUG_STARTING_SYMBOL_COMPRESS
) {
1187 DebugHeader
->StartSymbol
= DEBUG_STARTING_SYMBOL_NORMAL
;
1188 DebugHeader
->Length
= DecompressDataInPlace (
1189 (UINT8
*) (DebugHeader
+ 1), DebugHeader
->Length
- sizeof (DEBUG_PACKET_HEADER
)
1190 ) + sizeof (DEBUG_PACKET_HEADER
);
1192 return RETURN_SUCCESS
;
1196 Receive acknowledge packet OK from HOST in specified time.
1198 @param[in] Command The command type issued by TARGET.
1199 @param[in] Timeout Time out value to wait for acknowlege from HOST.
1200 The unit is microsecond.
1201 @param[out] BreakReceived If BreakReceived is not NULL,
1202 TRUE is retured if break-in symbol received.
1203 FALSE is retured if break-in symbol not received.
1204 @param[out] IncompatibilityFlag If IncompatibilityFlag is not NULL, return
1205 TRUE: Compatible packet received.
1206 FALSE: Incompatible packet received.
1208 @retval RETRUEN_SUCCESS Succeed to receive acknowlege packet from HOST,
1209 the type of acknowlege packet saved in Ack.
1210 @retval RETURN_TIMEOUT Specified timeout value was up.
1214 SendCommandAndWaitForAckOK (
1217 OUT BOOLEAN
*BreakReceived
, OPTIONAL
1218 OUT BOOLEAN
*IncompatibilityFlag OPTIONAL
1221 RETURN_STATUS Status
;
1222 UINT8 InputPacketBuffer
[DEBUG_DATA_UPPER_LIMIT
];
1223 DEBUG_PACKET_HEADER
*DebugHeader
;
1225 UINT8 HostSequenceNo
;
1229 DebugHeader
= (DEBUG_PACKET_HEADER
*) InputPacketBuffer
;
1230 Status
= RETURN_TIMEOUT
;
1231 while (RetryCount
> 0) {
1232 SequenceNo
= GetMailboxPointer()->SequenceNo
;
1233 HostSequenceNo
= GetMailboxPointer()->HostSequenceNo
;
1234 SendPacketWithoutData (Command
, SequenceNo
);
1235 Status
= ReceivePacket ((UINT8
*) DebugHeader
, BreakReceived
, IncompatibilityFlag
, Timeout
, FALSE
);
1236 if (Status
== RETURN_TIMEOUT
) {
1237 if (Command
== DEBUG_COMMAND_INIT_BREAK
) {
1240 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "TARGET: Timeout when waiting for ACK packet.\n");
1244 ASSERT_EFI_ERROR (Status
);
1246 // Status == RETURN_SUCCESS
1248 if (DebugHeader
->Command
== DEBUG_COMMAND_OK
&& DebugHeader
->SequenceNo
== SequenceNo
) {
1252 UpdateMailboxContent (GetMailboxPointer(), DEBUG_MAILBOX_SEQUENCE_NO_INDEX
, ++SequenceNo
);
1255 if (DebugHeader
->Command
== DEBUG_COMMAND_GO
&& (DebugHeader
->SequenceNo
== HostSequenceNo
|| Command
== DEBUG_COMMAND_INIT_BREAK
)) {
1259 if (Command
== DEBUG_COMMAND_INIT_BREAK
) {
1260 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "TARGET: Receive GO() in last boot\n");
1262 SendPacketWithoutData (DEBUG_COMMAND_OK
, DebugHeader
->SequenceNo
);
1266 ASSERT (Command
== DEBUG_COMMAND_INIT_BREAK
);
1271 Get current break cause.
1273 @param[in] Vector Vector value of exception or interrupt.
1274 @param[in] CpuContext Pointer to save CPU context.
1276 @return The type of break cause defined by XXXX
1282 IN DEBUG_CPU_CONTEXT
*CpuContext
1287 Cause
= DEBUG_DATA_BREAK_CAUSE_UNKNOWN
;
1290 case DEBUG_INT1_VECTOR
:
1291 case DEBUG_INT3_VECTOR
:
1293 if (Vector
== DEBUG_INT1_VECTOR
) {
1297 if ((CpuContext
->Dr6
& BIT14
) != 0) {
1298 Cause
= DEBUG_DATA_BREAK_CAUSE_STEPPING
;
1300 // DR6.BIT14 Indicates (when set) that the debug exception was
1301 // triggered by the single step execution mode.
1302 // The single-step mode is the highest priority debug exception.
1303 // This is single step, no need to check DR0, to ensure single step
1304 // work in PeCoffExtraActionLib (right after triggering a breakpoint
1305 // to report image load/unload).
1310 Cause
= DEBUG_DATA_BREAK_CAUSE_HW_BREAKPOINT
;
1316 Cause
= DEBUG_DATA_BREAK_CAUSE_SW_BREAKPOINT
;
1319 switch (CpuContext
->Dr0
) {
1320 case IMAGE_LOAD_SIGNATURE
:
1321 case IMAGE_UNLOAD_SIGNATURE
:
1323 if (CpuContext
->Dr3
== IO_PORT_BREAKPOINT_ADDRESS
) {
1325 Cause
= (UINT8
) ((CpuContext
->Dr0
== IMAGE_LOAD_SIGNATURE
) ?
1326 DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD
: DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD
);
1330 case SOFT_INTERRUPT_SIGNATURE
:
1332 if (CpuContext
->Dr1
== MEMORY_READY_SIGNATURE
) {
1333 Cause
= DEBUG_DATA_BREAK_CAUSE_MEMORY_READY
;
1334 CpuContext
->Dr0
= 0;
1335 } else if (CpuContext
->Dr1
== SYSTEM_RESET_SIGNATURE
) {
1336 Cause
= DEBUG_DATA_BREAK_CAUSE_SYSTEM_RESET
;
1337 CpuContext
->Dr0
= 0;
1348 case DEBUG_TIMER_VECTOR
:
1349 Cause
= DEBUG_DATA_BREAK_CAUSE_USER_HALT
;
1354 if (GetDebugFlag (DEBUG_AGENT_FLAG_STEPPING
) == 1) {
1356 // If stepping command is executing
1358 Cause
= DEBUG_DATA_BREAK_CAUSE_STEPPING
;
1360 Cause
= DEBUG_DATA_BREAK_CAUSE_EXCEPTION
;
1370 Copy memory from source to destination with specified width.
1372 @param[out] Dest A pointer to the destination buffer of the memory copy.
1373 @param[in] Src A pointer to the source buffer of the memory copy.
1374 @param[in] Count The number of data with specified width to copy from source to destination.
1375 @param[in] Width Data width in byte.
1396 // Copy memory from tail to avoid memory overlap
1398 Destination
= Dest
+ (Count
- 1) * Width
;
1399 Source
= Src
+ (Count
- 1) * Width
;
1403 while (Count
-- != 0) {
1406 *(UINT8
*) Destination
= MmioRead8 ((UINTN
) Source
);
1409 *(UINT16
*) Destination
= MmioRead16 ((UINTN
) Source
);
1412 *(UINT32
*) Destination
= MmioRead32 ((UINTN
) Source
);
1415 *(UINT64
*) Destination
= MmioRead64 ((UINTN
) Source
);
1421 Destination
+= Step
;
1426 Compress the data buffer but do not modify the original buffer.
1428 The compressed data is directly send to the debug channel.
1429 Compressing in place doesn't work because the data may become larger
1430 during compressing phase. ("3 3 ..." --> "3 3 0 ...")
1431 The routine is expected to be called three times:
1432 1. Compute the length of the compressed data buffer;
1433 2. Compute the CRC of the compressed data buffer;
1434 3. Compress the data and send to the debug channel.
1436 @param[in] Handle The debug channel handle to send the compressed data buffer.
1437 @param[in] Data The data buffer.
1438 @param[in] Length The length of the data buffer.
1439 @param[in] Send TRUE to send the compressed data buffer.
1440 @param[out] CompressedLength Return the length of the compressed data buffer.
1441 It may be larger than the Length in some cases.
1442 @param[out] CompressedCrc Return the CRC of the compressed data buffer.
1446 IN DEBUG_PORT_HANDLE Handle
,
1450 OUT UINTN
*CompressedLength
, OPTIONAL
1451 OUT UINT16
*CompressedCrc OPTIONAL
1456 UINT8 LastCharCount
;
1458 UINTN CompressedIndex
;
1460 ASSERT (Length
> 0);
1461 LastChar
= Data
[0] + 1; // Just ensure it's different from the first byte.
1464 for (Index
= 0, CompressedIndex
= 0; Index
<= Length
; Index
++) {
1465 if (Index
< Length
) {
1466 CurrentChar
= Data
[Index
];
1468 CurrentChar
= (UINT8
) LastChar
+ 1; // just ensure it's different from LastChar
1470 if (LastChar
!= CurrentChar
) {
1471 if (LastCharCount
== 1) {
1473 if (CompressedCrc
!= NULL
) {
1474 *CompressedCrc
= CalculateCrc16 (&LastChar
, 1, *CompressedCrc
);
1477 DebugPortWriteBuffer (Handle
, &LastChar
, 1);
1480 } else if (LastCharCount
>= 2) {
1481 CompressedIndex
+= 3;
1483 if (CompressedCrc
!= NULL
) {
1484 *CompressedCrc
= CalculateCrc16 (&LastChar
, 1, *CompressedCrc
);
1485 *CompressedCrc
= CalculateCrc16 (&LastChar
, 1, *CompressedCrc
);
1486 *CompressedCrc
= CalculateCrc16 (&LastCharCount
, 1, *CompressedCrc
);
1489 DebugPortWriteBuffer (Handle
, &LastChar
, 1);
1490 DebugPortWriteBuffer (Handle
, &LastChar
, 1);
1491 DebugPortWriteBuffer (Handle
, &LastCharCount
, 1);
1497 LastChar
= CurrentChar
;
1500 if (CompressedLength
!= NULL
) {
1501 *CompressedLength
= CompressedIndex
;
1506 Read memory with speicifed width and send packet with response data to HOST.
1508 @param[in] Data Pointer to response data buffer.
1509 @param[in] Count The number of data with specified Width.
1510 @param[in] Width Data width in byte.
1511 @param[in] DebugHeader Pointer to a buffer for creating response packet and receiving ACK packet,
1512 to minimize the stack usage.
1514 @retval RETURN_SUCCESS Response data was sent successfully.
1518 ReadMemoryAndSendResponsePacket (
1522 IN DEBUG_PACKET_HEADER
*DebugHeader
1525 RETURN_STATUS Status
;
1527 DEBUG_PORT_HANDLE Handle
;
1529 UINTN RemainingDataSize
;
1530 UINT8 CurrentDataSize
;
1531 UINTN CompressedDataSize
;
1533 Handle
= GetDebugPortHandle();
1535 RemainingDataSize
= Count
* Width
;
1537 SequenceNo
= GetMailboxPointer()->HostSequenceNo
;
1538 if (RemainingDataSize
<= DEBUG_DATA_MAXIMUM_REAL_DATA
) {
1540 // If the remaining data is less one real packet size, this is the last data packet
1542 CurrentDataSize
= (UINT8
) RemainingDataSize
;
1544 DebugHeader
->Command
= DEBUG_COMMAND_OK
;
1547 // Data is too larger to be sent in one packet, calculate the actual data size could
1548 // be sent in one Maximum data packet
1550 CurrentDataSize
= (DEBUG_DATA_MAXIMUM_REAL_DATA
/ Width
) * Width
;
1552 DebugHeader
->Command
= DEBUG_COMMAND_IN_PROGRESS
;
1555 // Construct the rest Debug header
1557 DebugHeader
->StartSymbol
= DEBUG_STARTING_SYMBOL_NORMAL
;
1558 DebugHeader
->Length
= CurrentDataSize
+ sizeof (DEBUG_PACKET_HEADER
);
1559 DebugHeader
->SequenceNo
= SequenceNo
;
1560 DebugHeader
->Crc
= 0;
1561 CopyMemByWidth ((UINT8
*) (DebugHeader
+ 1), Data
, CurrentDataSize
/ Width
, Width
);
1564 // Compression/decompression support was added since revision 0.4.
1565 // Revision 0.3 shouldn't compress the packet.
1567 if (PcdGet32(PcdTransferProtocolRevision
) >= DEBUG_AGENT_REVISION_04
) {
1569 // Get the compressed data size without modifying the packet.
1573 (UINT8
*) (DebugHeader
+ 1),
1576 &CompressedDataSize
,
1580 CompressedDataSize
= CurrentDataSize
;
1582 if (CompressedDataSize
< CurrentDataSize
) {
1583 DebugHeader
->Length
= (UINT8
) CompressedDataSize
+ sizeof (DEBUG_PACKET_HEADER
);
1584 DebugHeader
->StartSymbol
= DEBUG_STARTING_SYMBOL_COMPRESS
;
1586 // Compute the CRC of the packet head without modifying the packet.
1588 DebugHeader
->Crc
= CalculateCrc16 ((UINT8
*) DebugHeader
, sizeof (DEBUG_PACKET_HEADER
), 0);
1591 (UINT8
*) (DebugHeader
+ 1),
1598 // Send out the packet head.
1600 DebugPortWriteBuffer (Handle
, (UINT8
*) DebugHeader
, sizeof (DEBUG_PACKET_HEADER
));
1602 // Compress and send out the packet data.
1606 (UINT8
*) (DebugHeader
+ 1),
1615 // Calculate and fill the checksum, DebugHeader->Crc should be 0 before invoking CalculateCrc16 ()
1617 DebugHeader
->Crc
= CalculateCrc16 ((UINT8
*) DebugHeader
, DebugHeader
->Length
, 0);
1619 DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE
, TRUE
, (UINT8
*) DebugHeader
, DebugHeader
->Length
);
1621 DebugPortWriteBuffer (Handle
, (UINT8
*) DebugHeader
, DebugHeader
->Length
);
1625 Status
= ReceivePacket ((UINT8
*) DebugHeader
, NULL
, NULL
, READ_PACKET_TIMEOUT
, FALSE
);
1626 if (Status
== RETURN_TIMEOUT
) {
1627 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "TARGET: Timeout in SendDataResponsePacket()\n");
1630 if ((DebugHeader
->Command
== DEBUG_COMMAND_OK
) && (DebugHeader
->SequenceNo
== SequenceNo
) && LastPacket
) {
1632 // If this is the last packet, return RETURN_SUCCESS.
1634 return RETURN_SUCCESS
;
1636 if ((DebugHeader
->Command
== DEBUG_COMMAND_CONTINUE
) && (DebugHeader
->SequenceNo
== (UINT8
) (SequenceNo
+ 1))) {
1638 // Calculate the rest data size
1640 Data
+= CurrentDataSize
;
1641 RemainingDataSize
-= CurrentDataSize
;
1642 UpdateMailboxContent (GetMailboxPointer(), DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX
, DebugHeader
->SequenceNo
);
1645 if (DebugHeader
->SequenceNo
>= SequenceNo
) {
1646 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "TARGET: Received one old or new command(SequenceNo is %x, last SequenceNo is %x)\n", SequenceNo
, DebugHeader
->SequenceNo
);
1654 Send packet with response data to HOST.
1656 @param[in] Data Pointer to response data buffer.
1657 @param[in] DataSize Size of response data in byte.
1658 @param[in, out] DebugHeader Pointer to a buffer for creating response packet and receiving ACK packet,
1659 to minimize the stack usage.
1661 @retval RETURN_SUCCESS Response data was sent successfully.
1665 SendDataResponsePacket (
1668 IN OUT DEBUG_PACKET_HEADER
*DebugHeader
1671 return ReadMemoryAndSendResponsePacket (Data
, DataSize
, 1, DebugHeader
);
1675 Try to attach the HOST.
1677 Send init break packet to HOST:
1678 If no acknowlege received in specified Timeout, return RETURN_TIMEOUT.
1679 If received acknowlege, check the revision of HOST.
1680 Set Attach Flag if attach successfully.
1682 @param[in] BreakCause Break cause of this break event.
1683 @param[in] Timeout Time out value to wait for acknowlege from HOST.
1684 The unit is microsecond.
1685 @param[out] BreakReceived If BreakReceived is not NULL,
1686 TRUE is retured if break-in symbol received.
1687 FALSE is retured if break-in symbol not received.
1691 IN UINT8 BreakCause
,
1693 OUT BOOLEAN
*BreakReceived
1696 RETURN_STATUS Status
;
1697 DEBUG_PORT_HANDLE Handle
;
1698 BOOLEAN IncompatibilityFlag
;
1700 IncompatibilityFlag
= FALSE
;
1701 Handle
= GetDebugPortHandle();
1704 // Send init break and wait ack in Timeout
1706 DebugPortWriteBuffer (Handle
, (UINT8
*) mErrorMsgSendInitPacket
, AsciiStrLen (mErrorMsgSendInitPacket
));
1707 if (BreakCause
== DEBUG_DATA_BREAK_CAUSE_SYSTEM_RESET
) {
1708 Status
= SendCommandAndWaitForAckOK (DEBUG_COMMAND_INIT_BREAK
, Timeout
, BreakReceived
, &IncompatibilityFlag
);
1710 Status
= SendCommandAndWaitForAckOK (DEBUG_COMMAND_ATTACH_BREAK
, Timeout
, BreakReceived
, &IncompatibilityFlag
);
1712 if (IncompatibilityFlag
) {
1714 // If the incompatible Debug Packet received, the HOST should be running transfer protocol before PcdTransferProtocolRevision.
1715 // It could be UDK Debugger for Windows v1.1/v1.2 or for Linux v0.8/v1.2.
1717 DebugPortWriteBuffer (Handle
, (UINT8
*) mErrorMsgVersionAlert
, AsciiStrLen (mErrorMsgVersionAlert
));
1721 if (RETURN_ERROR (Status
)) {
1722 DebugPortWriteBuffer (Handle
, (UINT8
*) mErrorMsgConnectFail
, AsciiStrLen (mErrorMsgConnectFail
));
1724 DebugPortWriteBuffer (Handle
, (UINT8
*) mErrorMsgConnectOK
, AsciiStrLen (mErrorMsgConnectOK
));
1728 SetHostAttached (TRUE
);
1734 Send Break point packet to HOST.
1736 Only the first breaking processor could sent BREAK_POINT packet.
1738 @param[in] BreakCause Break cause of this break event.
1739 @param[in] ProcessorIndex Processor index value.
1740 @param[out] BreakReceived If BreakReceived is not NULL,
1741 TRUE is retured if break-in symbol received.
1742 FALSE is retured if break-in symbol not received.
1746 SendBreakPacketToHost (
1747 IN UINT8 BreakCause
,
1748 IN UINT32 ProcessorIndex
,
1749 OUT BOOLEAN
*BreakReceived
1752 UINT8 InputCharacter
;
1753 DEBUG_PORT_HANDLE Handle
;
1755 Handle
= GetDebugPortHandle();
1757 if (IsHostAttached ()) {
1758 DebugAgentMsgPrint (DEBUG_AGENT_INFO
, "processor[%x]:Send Break Packet to HOST.\n", ProcessorIndex
);
1759 SendCommandAndWaitForAckOK (DEBUG_COMMAND_BREAK_POINT
, READ_PACKET_TIMEOUT
, BreakReceived
, NULL
);
1761 DebugAgentMsgPrint (DEBUG_AGENT_INFO
, "processor[%x]:Try to attach HOST.\n", ProcessorIndex
);
1763 // If HOST is not attached, try to attach it firstly.
1766 // Poll Attach symbols from HOST and ack OK
1769 DebugAgentReadBuffer (Handle
, &InputCharacter
, 1, 0);
1770 } while (InputCharacter
!= DEBUG_STARTING_SYMBOL_ATTACH
);
1771 SendAckPacket (DEBUG_COMMAND_OK
);
1774 // Try to attach HOST
1776 while (AttachHost (BreakCause
, 0, NULL
) != RETURN_SUCCESS
);
1782 The main function to process communication with HOST.
1784 It received the command packet from HOST, and sent response data packet to HOST.
1786 @param[in] Vector Vector value of exception or interrutp.
1787 @param[in, out] CpuContext Pointer to saved CPU context.
1788 @param[in] BreakReceived TRUE means break-in symbol received.
1789 FALSE means break-in symbol not received.
1793 CommandCommunication (
1795 IN OUT DEBUG_CPU_CONTEXT
*CpuContext
,
1796 IN BOOLEAN BreakReceived
1799 RETURN_STATUS Status
;
1800 UINT8 InputPacketBuffer
[DEBUG_DATA_UPPER_LIMIT
+ sizeof (UINT64
) - 1];
1801 DEBUG_PACKET_HEADER
*DebugHeader
;
1806 DEBUG_DATA_READ_MEMORY
*MemoryRead
;
1807 DEBUG_DATA_WRITE_MEMORY
*MemoryWrite
;
1808 DEBUG_DATA_READ_IO
*IoRead
;
1809 DEBUG_DATA_WRITE_IO
*IoWrite
;
1810 DEBUG_DATA_READ_REGISTER
*RegisterRead
;
1811 DEBUG_DATA_WRITE_REGISTER
*RegisterWrite
;
1812 UINT8
*RegisterBuffer
;
1813 DEBUG_DATA_READ_MSR
*MsrRegisterRead
;
1814 DEBUG_DATA_WRITE_MSR
*MsrRegisterWrite
;
1815 DEBUG_DATA_CPUID
*Cpuid
;
1816 DEBUG_DATA_RESPONSE_BREAK_CAUSE BreakCause
;
1817 DEBUG_DATA_RESPONSE_CPUID CpuidResponse
;
1818 DEBUG_DATA_SEARCH_SIGNATURE
*SearchSignature
;
1819 DEBUG_DATA_RESPONSE_GET_EXCEPTION Exception
;
1820 DEBUG_DATA_RESPONSE_GET_REVISION DebugAgentRevision
;
1821 DEBUG_DATA_SET_VIEWPOINT
*SetViewPoint
;
1822 BOOLEAN HaltDeferred
;
1823 UINT32 ProcessorIndex
;
1824 DEBUG_AGENT_EXCEPTION_BUFFER AgentExceptionBuffer
;
1825 UINT32 IssuedViewPoint
;
1826 DEBUG_AGENT_MAILBOX
*Mailbox
;
1827 UINT8
*AlignedDataPtr
;
1830 IssuedViewPoint
= 0;
1831 HaltDeferred
= BreakReceived
;
1833 if (MultiProcessorDebugSupport()) {
1834 ProcessorIndex
= GetProcessorIndex ();
1835 SetCpuStopFlagByIndex (ProcessorIndex
, TRUE
);
1836 if (mDebugMpContext
.ViewPointIndex
== ProcessorIndex
) {
1838 // Only the current view processor could set AgentInProgress Flag.
1840 IssuedViewPoint
= ProcessorIndex
;
1844 if (IssuedViewPoint
== ProcessorIndex
) {
1846 // Set AgentInProgress Flag.
1848 SetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS
, 1);
1853 if (MultiProcessorDebugSupport()) {
1855 // Check if the current processor is HOST view point
1857 if (mDebugMpContext
.ViewPointIndex
!= ProcessorIndex
) {
1858 if (mDebugMpContext
.RunCommandSet
) {
1860 // If HOST view point sets RUN flag, run GO command to leave
1862 SetCpuStopFlagByIndex (ProcessorIndex
, FALSE
);
1863 CommandGo (CpuContext
);
1867 // Run into loop again
1875 AcquireMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
1877 DebugHeader
=(DEBUG_PACKET_HEADER
*) InputPacketBuffer
;
1879 DebugAgentMsgPrint (DEBUG_AGENT_INFO
, "TARGET: Try to get command from HOST...\n");
1880 Status
= ReceivePacket ((UINT8
*) DebugHeader
, &BreakReceived
, NULL
, READ_PACKET_TIMEOUT
, TRUE
);
1881 if (Status
!= RETURN_SUCCESS
|| !IS_REQUEST (DebugHeader
)) {
1882 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "TARGET: Get command[%x] sequenceno[%x] returned status is [%x] \n", DebugHeader
->Command
, DebugHeader
->SequenceNo
, Status
);
1883 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "TARGET: Get command failed or it's response packet not expected! \n");
1884 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
1888 Mailbox
= GetMailboxPointer ();
1889 if (DebugHeader
->SequenceNo
== Mailbox
->HostSequenceNo
) {
1890 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "TARGET: Receive one old command[%x] agaist command[%x]\n", DebugHeader
->SequenceNo
, Mailbox
->HostSequenceNo
);
1891 SendAckPacket (Mailbox
->LastAck
);
1892 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
1894 } else if (DebugHeader
->SequenceNo
== (UINT8
) (Mailbox
->HostSequenceNo
+ 1)) {
1895 UpdateMailboxContent (Mailbox
, DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX
, (UINT8
) DebugHeader
->SequenceNo
);
1897 DebugAgentMsgPrint (DEBUG_AGENT_WARNING
, "Receive one invalid comamnd[%x] agaist command[%x]\n", DebugHeader
->SequenceNo
, Mailbox
->HostSequenceNo
);
1898 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
1903 // Save CPU content before executing HOST commond
1905 UpdateMailboxContent (Mailbox
, DEBUG_MAILBOX_EXCEPTION_BUFFER_POINTER_INDEX
, (UINT64
)(UINTN
) &AgentExceptionBuffer
.JumpBuffer
);
1906 if (SetJump (&AgentExceptionBuffer
.JumpBuffer
) != 0) {
1908 // If HOST command failed, continue to wait for HOST's next command
1909 // If needed, agent could send exception info to HOST.
1911 SendAckPacket (DEBUG_COMMAND_ABORT
);
1912 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
1916 DebugAgentMsgPrint (DEBUG_AGENT_INFO
, "Processor[%x]:Received one command(%x)\n", mDebugMpContext
.ViewPointIndex
, DebugHeader
->Command
);
1918 switch (DebugHeader
->Command
) {
1920 case DEBUG_COMMAND_HALT
:
1921 SendAckPacket (DEBUG_COMMAND_HALT_DEFERRED
);
1922 HaltDeferred
= TRUE
;
1923 BreakReceived
= FALSE
;
1924 Status
= RETURN_SUCCESS
;
1927 case DEBUG_COMMAND_RESET
:
1928 SendAckPacket (DEBUG_COMMAND_OK
);
1929 SendAckPacket (DEBUG_COMMAND_OK
);
1930 SendAckPacket (DEBUG_COMMAND_OK
);
1931 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
1935 // Assume system resets in 2 seconds, otherwise send TIMEOUT packet.
1936 // PCD can be used if 2 seconds isn't long enough for some platforms.
1938 MicroSecondDelay (2000000);
1939 UpdateMailboxContent (Mailbox
, DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX
, Mailbox
->HostSequenceNo
+ 1);
1940 SendAckPacket (DEBUG_COMMAND_TIMEOUT
);
1941 SendAckPacket (DEBUG_COMMAND_TIMEOUT
);
1942 SendAckPacket (DEBUG_COMMAND_TIMEOUT
);
1945 case DEBUG_COMMAND_GO
:
1946 CommandGo (CpuContext
);
1948 // Clear Dr0 to avoid to be recognized as IMAGE_LOAD/_UNLOAD again when hitting a breakpoint after GO
1949 // If HOST changed Dr0 before GO, we will not change Dr0 here
1951 Data8
= GetBreakCause (Vector
, CpuContext
);
1952 if (Data8
== DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD
|| Data8
== DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD
) {
1953 CpuContext
->Dr0
= 0;
1956 if (!HaltDeferred
) {
1958 // If no HALT command received when being in-active mode
1960 if (MultiProcessorDebugSupport()) {
1961 Data32
= FindNextPendingBreakCpu ();
1964 // If there are still others processors being in break state,
1965 // send OK packet to HOST to finish this go command
1967 SendAckPacket (DEBUG_COMMAND_OK
);
1970 // Set current view to the next breaking processor
1972 mDebugMpContext
.ViewPointIndex
= Data32
;
1973 mDebugMpContext
.BreakAtCpuIndex
= mDebugMpContext
.ViewPointIndex
;
1974 SetCpuBreakFlagByIndex (mDebugMpContext
.ViewPointIndex
, FALSE
);
1976 // Send break packet to HOST to let HOST break again
1978 SendBreakPacketToHost (DEBUG_DATA_BREAK_CAUSE_UNKNOWN
, mDebugMpContext
.BreakAtCpuIndex
, &BreakReceived
);
1980 // Continue to run into loop to read command packet from HOST
1982 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
1987 // If no else processor break, set stop bitmask,
1988 // and set Running flag for all processors.
1990 SetCpuStopFlagByIndex (ProcessorIndex
, FALSE
);
1991 SetCpuRunningFlag (TRUE
);
1994 // Wait for all processors are in running state
1997 if (IsAllCpuRunning ()) {
2002 // Set BSP to be current view point.
2004 SetDebugViewPoint (mDebugMpContext
.BspIndex
);
2007 // Clear breaking processor index and running flag
2009 mDebugMpContext
.BreakAtCpuIndex
= (UINT32
) (-1);
2010 SetCpuRunningFlag (FALSE
);
2014 // Send OK packet to HOST to finish this go command
2016 SendAckPacket (DEBUG_COMMAND_OK
);
2018 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
2020 if (!IsHostAttached()) {
2021 UpdateMailboxContent (Mailbox
, DEBUG_MAILBOX_SEQUENCE_NO_INDEX
, 0);
2022 UpdateMailboxContent (Mailbox
, DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX
, 0);
2028 // If reveived HALT command, need to defer the GO command
2030 SendAckPacket (DEBUG_COMMAND_HALT_PROCESSED
);
2031 HaltDeferred
= FALSE
;
2033 Vector
= DEBUG_TIMER_VECTOR
;
2037 case DEBUG_COMMAND_BREAK_CAUSE
:
2038 BreakCause
.StopAddress
= CpuContext
->Eip
;
2039 if (MultiProcessorDebugSupport() && ProcessorIndex
!= mDebugMpContext
.BreakAtCpuIndex
) {
2040 BreakCause
.Cause
= GetBreakCause (DEBUG_TIMER_VECTOR
, CpuContext
);
2042 BreakCause
.Cause
= GetBreakCause (Vector
, CpuContext
);
2044 SendDataResponsePacket ((UINT8
*) &BreakCause
, (UINT16
) sizeof (DEBUG_DATA_RESPONSE_BREAK_CAUSE
), DebugHeader
);
2047 case DEBUG_COMMAND_SET_HW_BREAKPOINT
:
2048 SetDebugRegister (CpuContext
, (DEBUG_DATA_SET_HW_BREAKPOINT
*) (DebugHeader
+ 1));
2049 SendAckPacket (DEBUG_COMMAND_OK
);
2052 case DEBUG_COMMAND_CLEAR_HW_BREAKPOINT
:
2053 ClearDebugRegister (CpuContext
, (DEBUG_DATA_CLEAR_HW_BREAKPOINT
*) (DebugHeader
+ 1));
2054 SendAckPacket (DEBUG_COMMAND_OK
);
2057 case DEBUG_COMMAND_SINGLE_STEPPING
:
2058 CommandStepping (CpuContext
);
2060 // Clear Dr0 to avoid to be recognized as IMAGE_LOAD/_UNLOAD again when hitting a breakpoint after GO
2061 // If HOST changed Dr0 before GO, we will not change Dr0 here
2063 Data8
= GetBreakCause (Vector
, CpuContext
);
2064 if (Data8
== DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD
|| Data8
== DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD
) {
2065 CpuContext
->Dr0
= 0;
2068 mDebugMpContext
.BreakAtCpuIndex
= (UINT32
) (-1);
2069 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
2071 // Executing stepping command directly without sending ACK packet,
2072 // ACK packet will be sent after stepping done.
2076 case DEBUG_COMMAND_SET_SW_BREAKPOINT
:
2077 Data64
= (UINTN
) (((DEBUG_DATA_SET_SW_BREAKPOINT
*) (DebugHeader
+ 1))->Address
);
2078 Data8
= *(UINT8
*) (UINTN
) Data64
;
2079 *(UINT8
*) (UINTN
) Data64
= DEBUG_SW_BREAKPOINT_SYMBOL
;
2080 Status
= SendDataResponsePacket ((UINT8
*) &Data8
, (UINT16
) sizeof (UINT8
), DebugHeader
);
2083 case DEBUG_COMMAND_READ_MEMORY
:
2084 MemoryRead
= (DEBUG_DATA_READ_MEMORY
*) (DebugHeader
+ 1);
2085 Status
= ReadMemoryAndSendResponsePacket ((UINT8
*) (UINTN
) MemoryRead
->Address
, MemoryRead
->Count
, MemoryRead
->Width
, DebugHeader
);
2088 case DEBUG_COMMAND_WRITE_MEMORY
:
2089 MemoryWrite
= (DEBUG_DATA_WRITE_MEMORY
*) (DebugHeader
+ 1);
2091 // Copy data into one memory with 8-byte alignment address
2093 AlignedDataPtr
= ALIGN_POINTER ((UINT8
*) &MemoryWrite
->Data
, sizeof (UINT64
));
2094 if (AlignedDataPtr
!= (UINT8
*) &MemoryWrite
->Data
) {
2095 CopyMem (AlignedDataPtr
, (UINT8
*) &MemoryWrite
->Data
, MemoryWrite
->Count
* MemoryWrite
->Width
);
2097 CopyMemByWidth ((UINT8
*) (UINTN
) MemoryWrite
->Address
, AlignedDataPtr
, MemoryWrite
->Count
, MemoryWrite
->Width
);
2098 SendAckPacket (DEBUG_COMMAND_OK
);
2101 case DEBUG_COMMAND_READ_IO
:
2102 IoRead
= (DEBUG_DATA_READ_IO
*) (DebugHeader
+ 1);
2103 switch (IoRead
->Width
) {
2105 Data64
= IoRead8 ((UINTN
) IoRead
->Port
);
2108 Data64
= IoRead16 ((UINTN
) IoRead
->Port
);
2111 Data64
= IoRead32 ((UINTN
) IoRead
->Port
);
2114 Data64
= IoRead64 ((UINTN
) IoRead
->Port
);
2117 Data64
= (UINT64
) -1;
2119 Status
= SendDataResponsePacket ((UINT8
*) &Data64
, IoRead
->Width
, DebugHeader
);
2122 case DEBUG_COMMAND_WRITE_IO
:
2123 IoWrite
= (DEBUG_DATA_WRITE_IO
*) (DebugHeader
+ 1);
2124 switch (IoWrite
->Width
) {
2126 Data64
= IoWrite8 ((UINTN
) IoWrite
->Port
, *(UINT8
*) &IoWrite
->Data
);
2129 Data64
= IoWrite16 ((UINTN
) IoWrite
->Port
, *(UINT16
*) &IoWrite
->Data
);
2132 Data64
= IoWrite32 ((UINTN
) IoWrite
->Port
, *(UINT32
*) &IoWrite
->Data
);
2135 Data64
= IoWrite64 ((UINTN
) IoWrite
->Port
, *(UINT64
*) &IoWrite
->Data
);
2138 Data64
= (UINT64
) -1;
2140 SendAckPacket (DEBUG_COMMAND_OK
);
2143 case DEBUG_COMMAND_READ_ALL_REGISTERS
:
2144 Status
= SendDataResponsePacket ((UINT8
*) CpuContext
, sizeof (*CpuContext
), DebugHeader
);
2147 case DEBUG_COMMAND_READ_REGISTER
:
2148 RegisterRead
= (DEBUG_DATA_READ_REGISTER
*) (DebugHeader
+ 1);
2150 if (RegisterRead
->Index
<= SOFT_DEBUGGER_REGISTER_MAX
) {
2151 RegisterBuffer
= ArchReadRegisterBuffer (CpuContext
, RegisterRead
->Index
, &Width
);
2152 Status
= SendDataResponsePacket (RegisterBuffer
, Width
, DebugHeader
);
2154 Status
= RETURN_UNSUPPORTED
;
2158 case DEBUG_COMMAND_WRITE_REGISTER
:
2159 RegisterWrite
= (DEBUG_DATA_WRITE_REGISTER
*) (DebugHeader
+ 1);
2160 if (RegisterWrite
->Index
<= SOFT_DEBUGGER_REGISTER_MAX
) {
2161 RegisterBuffer
= ArchReadRegisterBuffer (CpuContext
, RegisterWrite
->Index
, &Width
);
2162 ASSERT (Width
== RegisterWrite
->Length
);
2163 CopyMem (RegisterBuffer
, RegisterWrite
->Data
, Width
);
2164 SendAckPacket (DEBUG_COMMAND_OK
);
2166 Status
= RETURN_UNSUPPORTED
;
2170 case DEBUG_COMMAND_ARCH_MODE
:
2171 Data8
= DEBUG_ARCH_SYMBOL
;
2172 Status
= SendDataResponsePacket ((UINT8
*) &Data8
, (UINT16
) sizeof (UINT8
), DebugHeader
);
2175 case DEBUG_COMMAND_READ_MSR
:
2176 MsrRegisterRead
= (DEBUG_DATA_READ_MSR
*) (DebugHeader
+ 1);
2177 Data64
= AsmReadMsr64 (MsrRegisterRead
->Index
);
2178 Status
= SendDataResponsePacket ((UINT8
*) &Data64
, (UINT16
) sizeof (UINT64
), DebugHeader
);
2181 case DEBUG_COMMAND_WRITE_MSR
:
2182 MsrRegisterWrite
= (DEBUG_DATA_WRITE_MSR
*) (DebugHeader
+ 1);
2183 AsmWriteMsr64 (MsrRegisterWrite
->Index
, MsrRegisterWrite
->Value
);
2184 SendAckPacket (DEBUG_COMMAND_OK
);
2187 case DEBUG_COMMAND_SET_DEBUG_SETTING
:
2188 Status
= SetDebugSetting ((DEBUG_DATA_SET_DEBUG_SETTING
*)(DebugHeader
+ 1));
2189 if (Status
== RETURN_SUCCESS
) {
2190 SendAckPacket (DEBUG_COMMAND_OK
);
2194 case DEBUG_COMMAND_GET_REVISION
:
2195 DebugAgentRevision
.Revision
= PcdGet32(PcdTransferProtocolRevision
);
2196 DebugAgentRevision
.Capabilities
= DEBUG_AGENT_CAPABILITIES
;
2197 Status
= SendDataResponsePacket ((UINT8
*) &DebugAgentRevision
, (UINT16
) sizeof (DEBUG_DATA_RESPONSE_GET_REVISION
), DebugHeader
);
2200 case DEBUG_COMMAND_GET_EXCEPTION
:
2201 Exception
.ExceptionNum
= (UINT8
) Vector
;
2202 Exception
.ExceptionData
= (UINT32
) CpuContext
->ExceptionData
;
2203 Status
= SendDataResponsePacket ((UINT8
*) &Exception
, (UINT16
) sizeof (DEBUG_DATA_RESPONSE_GET_EXCEPTION
), DebugHeader
);
2206 case DEBUG_COMMAND_SET_VIEWPOINT
:
2207 SetViewPoint
= (DEBUG_DATA_SET_VIEWPOINT
*) (DebugHeader
+ 1);
2208 if (MultiProcessorDebugSupport()) {
2209 if (IsCpuStopped (SetViewPoint
->ViewPoint
)) {
2210 SetDebugViewPoint (SetViewPoint
->ViewPoint
);
2211 SendAckPacket (DEBUG_COMMAND_OK
);
2214 // If CPU is not halted
2216 SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED
);
2218 } else if (SetViewPoint
->ViewPoint
== 0) {
2219 SendAckPacket (DEBUG_COMMAND_OK
);
2222 SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED
);
2227 case DEBUG_COMMAND_GET_VIEWPOINT
:
2228 Data32
= mDebugMpContext
.ViewPointIndex
;
2229 SendDataResponsePacket((UINT8
*) &Data32
, (UINT16
) sizeof (UINT32
), DebugHeader
);
2232 case DEBUG_COMMAND_MEMORY_READY
:
2233 Data8
= (UINT8
) GetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY
);
2234 SendDataResponsePacket (&Data8
, (UINT16
) sizeof (UINT8
), DebugHeader
);
2237 case DEBUG_COMMAND_DETACH
:
2238 SetHostAttached (FALSE
);
2239 SendAckPacket (DEBUG_COMMAND_OK
);
2242 case DEBUG_COMMAND_CPUID
:
2243 Cpuid
= (DEBUG_DATA_CPUID
*) (DebugHeader
+ 1);
2245 Cpuid
->Eax
, Cpuid
->Ecx
,
2246 &CpuidResponse
.Eax
, &CpuidResponse
.Ebx
,
2247 &CpuidResponse
.Ecx
, &CpuidResponse
.Edx
2249 SendDataResponsePacket ((UINT8
*) &CpuidResponse
, (UINT16
) sizeof (CpuidResponse
), DebugHeader
);
2252 case DEBUG_COMMAND_SEARCH_SIGNATURE
:
2253 SearchSignature
= (DEBUG_DATA_SEARCH_SIGNATURE
*) (DebugHeader
+ 1);
2254 if ((SearchSignature
->Alignment
!= 0) &&
2255 (SearchSignature
->Alignment
== GetPowerOfTwo32 (SearchSignature
->Alignment
))
2257 if (SearchSignature
->Positive
) {
2259 Data64
= ALIGN_VALUE ((UINTN
) SearchSignature
->Start
, SearchSignature
->Alignment
);
2260 Data64
<= SearchSignature
->Start
+ SearchSignature
->Count
- SearchSignature
->DataLength
;
2261 Data64
+= SearchSignature
->Alignment
2263 if (CompareMem ((VOID
*) (UINTN
) Data64
, &SearchSignature
->Data
, SearchSignature
->DataLength
) == 0) {
2267 if (Data64
> SearchSignature
->Start
+ SearchSignature
->Count
- SearchSignature
->DataLength
) {
2268 Data64
= (UINT64
) -1;
2272 Data64
= ALIGN_VALUE ((UINTN
) SearchSignature
->Start
- SearchSignature
->Alignment
, SearchSignature
->Alignment
);
2273 Data64
>= SearchSignature
->Start
- SearchSignature
->Count
;
2274 Data64
-= SearchSignature
->Alignment
2276 if (CompareMem ((VOID
*) (UINTN
) Data64
, &SearchSignature
->Data
, SearchSignature
->DataLength
) == 0) {
2280 if (Data64
< SearchSignature
->Start
- SearchSignature
->Count
) {
2281 Data64
= (UINT64
) -1;
2284 SendDataResponsePacket ((UINT8
*) &Data64
, (UINT16
) sizeof (Data64
), DebugHeader
);
2286 Status
= RETURN_UNSUPPORTED
;
2291 SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED
);
2295 if (Status
== RETURN_UNSUPPORTED
) {
2296 SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED
);
2297 } else if (Status
!= RETURN_SUCCESS
) {
2298 SendAckPacket (DEBUG_COMMAND_ABORT
);
2301 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
2307 C function called in interrupt handler.
2309 @param[in] Vector Vector value of exception or interrutp.
2310 @param[in] CpuContext Pointer to save CPU context.
2317 IN DEBUG_CPU_CONTEXT
*CpuContext
2320 UINT8 InputCharacter
;
2323 BOOLEAN BreakReceived
;
2324 UINT32 ProcessorIndex
;
2325 UINT32 CurrentDebugTimerInitCount
;
2326 DEBUG_PORT_HANDLE Handle
;
2329 UINT32 IssuedViewPoint
;
2330 DEBUG_AGENT_EXCEPTION_BUFFER
*ExceptionBuffer
;
2334 IssuedViewPoint
= 0;
2335 BreakReceived
= FALSE
;
2337 if (mSkipBreakpoint
) {
2339 // If Skip Breakpoint flag is set, means communication is disturbed by hardware SMI, we need to ignore the break points in SMM
2341 if ((Vector
== DEBUG_INT1_VECTOR
) || (Vector
== DEBUG_INT3_VECTOR
)) {
2342 DebugPortWriteBuffer (GetDebugPortHandle(), (UINT8
*) mWarningMsgIngoreBreakpoint
, AsciiStrLen (mWarningMsgIngoreBreakpoint
));
2347 if (MultiProcessorDebugSupport()) {
2348 ProcessorIndex
= GetProcessorIndex ();
2350 // If this processor has alreay halted before, need to check it later
2352 if (IsCpuStopped (ProcessorIndex
)) {
2353 IssuedViewPoint
= ProcessorIndex
;
2357 if (IssuedViewPoint
== ProcessorIndex
&& GetDebugFlag (DEBUG_AGENT_FLAG_STEPPING
) != 1) {
2359 // Check if this exception is issued by Debug Agent itself
2360 // If yes, fill the debug agent exception buffer and LongJump() back to
2361 // the saved CPU content in CommandCommunication()
2362 // If exception is issued when executing Stepping, will be handled in
2363 // exception handle procedure.
2365 if (GetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS
) == 1) {
2366 DebugAgentMsgPrint (
2368 "Debug agent meet one Exception, ExceptionNum is %d, EIP = 0x%x.\n",
2370 (UINTN
)CpuContext
->Eip
2372 ExceptionBuffer
= (DEBUG_AGENT_EXCEPTION_BUFFER
*) (UINTN
) GetMailboxPointer()->ExceptionBufferPointer
;
2373 ExceptionBuffer
->ExceptionContent
.ExceptionNum
= (UINT8
) Vector
;
2374 ExceptionBuffer
->ExceptionContent
.ExceptionData
= (UINT32
) CpuContext
->ExceptionData
;
2375 LongJump ((BASE_LIBRARY_JUMP_BUFFER
*)(UINTN
)(ExceptionBuffer
), 1);
2379 if (MultiProcessorDebugSupport()) {
2381 // If RUN commmand is executing, wait for it done.
2383 while (mDebugMpContext
.RunCommandSet
) {
2388 Handle
= GetDebugPortHandle();
2389 BreakCause
= GetBreakCause (Vector
, CpuContext
);
2391 case DEBUG_INT1_VECTOR
:
2392 case DEBUG_INT3_VECTOR
:
2393 switch (BreakCause
) {
2394 case DEBUG_DATA_BREAK_CAUSE_SYSTEM_RESET
:
2395 if (AttachHost (BreakCause
, READ_PACKET_TIMEOUT
, &BreakReceived
) != RETURN_SUCCESS
) {
2397 // Try to connect HOST, return if fails
2401 CommandCommunication (Vector
, CpuContext
, BreakReceived
);
2404 case DEBUG_DATA_BREAK_CAUSE_STEPPING
:
2406 // Stepping is finished, send Ack package.
2408 if (MultiProcessorDebugSupport()) {
2409 mDebugMpContext
.BreakAtCpuIndex
= ProcessorIndex
;
2412 // Clear Stepping Flag and restore EFLAGS.IF
2414 CommandSteppingCleanup (CpuContext
);
2415 SendAckPacket (DEBUG_COMMAND_OK
);
2416 CommandCommunication (Vector
, CpuContext
, BreakReceived
);
2419 case DEBUG_DATA_BREAK_CAUSE_MEMORY_READY
:
2423 SendCommandAndWaitForAckOK (DEBUG_COMMAND_MEMORY_READY
, READ_PACKET_TIMEOUT
, &BreakReceived
, NULL
);
2424 CommandCommunication (Vector
, CpuContext
, BreakReceived
);
2427 case DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD
:
2428 case DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD
:
2430 // Set AL to DEBUG_AGENT_IMAGE_CONTINUE
2432 Al
= ArchReadRegisterBuffer (CpuContext
, SOFT_DEBUGGER_REGISTER_AX
, &Data8
);
2433 *Al
= DEBUG_AGENT_IMAGE_CONTINUE
;
2435 if (!IsHostAttached ()) {
2437 // If HOST is not connected for image load/unload, return
2442 // Continue to run the following common code
2445 case DEBUG_DATA_BREAK_CAUSE_HW_BREAKPOINT
:
2446 case DEBUG_DATA_BREAK_CAUSE_SW_BREAKPOINT
:
2449 // Send Break packet to HOST
2451 AcquireMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
2453 // Only the first breaking processor could send BREAK_POINT to HOST
2455 if (IsFirstBreakProcessor (ProcessorIndex
)) {
2456 SendBreakPacketToHost (BreakCause
, ProcessorIndex
, &BreakReceived
);
2458 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
2460 if (Vector
== DEBUG_INT3_VECTOR
) {
2462 // go back address located "0xCC"
2465 SavedEip
= CpuContext
->Eip
;
2466 CommandCommunication (Vector
, CpuContext
, BreakReceived
);
2467 if ((SavedEip
== CpuContext
->Eip
) &&
2468 (*(UINT8
*) (UINTN
) CpuContext
->Eip
== DEBUG_SW_BREAKPOINT_SYMBOL
)) {
2470 // If this is not a software breakpoint set by HOST,
2476 CommandCommunication (Vector
, CpuContext
, BreakReceived
);
2483 case DEBUG_TIMER_VECTOR
:
2485 AcquireMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
2487 if (MultiProcessorDebugSupport()) {
2488 if (IsBsp (ProcessorIndex
)) {
2490 // If current processor is BSP, check Apic timer's init count if changed,
2491 // it may be re-written when switching BSP.
2492 // If it changed, re-initialize debug timer
2494 CurrentDebugTimerInitCount
= GetApicTimerInitCount ();
2495 if (mDebugMpContext
.DebugTimerInitCount
!= CurrentDebugTimerInitCount
) {
2496 InitializeDebugTimer (NULL
, FALSE
);
2497 SaveAndSetDebugTimerInterrupt (TRUE
);
2501 if (!IsBsp (ProcessorIndex
) || mDebugMpContext
.IpiSentByAp
) {
2502 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
2504 // If current processor is not BSP or this is one IPI sent by AP
2506 if (mDebugMpContext
.BreakAtCpuIndex
!= (UINT32
) (-1)) {
2507 CommandCommunication (Vector
, CpuContext
, FALSE
);
2511 // Clear EOI before exiting interrupt process routine.
2519 // Only BSP could run here
2523 // If there is data in debug port, will check whether it is break(attach/break-in) symbol,
2524 // If yes, go into communication mode with HOST.
2525 // If no, exit interrupt process.
2527 if (DebugReadBreakSymbol (Handle
, &InputCharacter
) == EFI_NOT_FOUND
) {
2531 if ((!IsHostAttached () && (InputCharacter
== DEBUG_STARTING_SYMBOL_ATTACH
)) ||
2532 (IsHostAttached () && (InputCharacter
== DEBUG_COMMAND_HALT
)) ||
2533 (IsHostAttached () && (InputCharacter
== DEBUG_COMMAND_GO
))
2535 DebugAgentMsgPrint (DEBUG_AGENT_VERBOSE
, "Received data [%02x]\n", InputCharacter
);
2537 // Ack OK for break-in symbol
2539 SendAckPacket (DEBUG_COMMAND_OK
);
2542 // If receive GO command in Debug Timer, means HOST may lost ACK packet before.
2544 if (InputCharacter
== DEBUG_COMMAND_GO
) {
2548 if (!IsHostAttached ()) {
2550 // Try to attach HOST, if no ack received after 200ms, return
2552 if (AttachHost (BreakCause
, READ_PACKET_TIMEOUT
, &BreakReceived
) != RETURN_SUCCESS
) {
2557 if (MultiProcessorDebugSupport()) {
2558 if(FindNextPendingBreakCpu () != -1) {
2559 SetCpuBreakFlagByIndex (ProcessorIndex
, TRUE
);
2561 HaltOtherProcessors (ProcessorIndex
);
2564 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
2565 CommandCommunication (Vector
, CpuContext
, BreakReceived
);
2566 AcquireMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
2572 // Clear EOI before exiting interrupt process routine.
2576 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
2581 if (Vector
<= DEBUG_EXCEPT_SIMD
) {
2582 DebugAgentMsgPrint (
2584 "Exception happened, ExceptionNum is %d, EIP = 0x%x.\n",
2586 (UINTN
) CpuContext
->Eip
2588 if (BreakCause
== DEBUG_DATA_BREAK_CAUSE_STEPPING
) {
2590 // If exception happened when executing Stepping, send Ack package.
2591 // HOST consider Stepping command was finished.
2593 if (MultiProcessorDebugSupport()) {
2594 mDebugMpContext
.BreakAtCpuIndex
= ProcessorIndex
;
2597 // Clear Stepping flag and restore EFLAGS.IF
2599 CommandSteppingCleanup (CpuContext
);
2600 SendAckPacket (DEBUG_COMMAND_OK
);
2603 // Exception occurs, send Break packet to HOST
2605 AcquireMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
2607 // Only the first breaking processor could send BREAK_POINT to HOST
2609 if (IsFirstBreakProcessor (ProcessorIndex
)) {
2610 SendBreakPacketToHost (BreakCause
, ProcessorIndex
, &BreakReceived
);
2612 ReleaseMpSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
2615 CommandCommunication (Vector
, CpuContext
, BreakReceived
);
2620 if (MultiProcessorDebugSupport()) {
2622 // Clear flag and wait for all processors run here
2624 SetIpiSentByApFlag (FALSE
);
2625 while (mDebugMpContext
.RunCommandSet
) {
2630 // Only current (view) processor could clean up AgentInProgress flag.
2632 if (mDebugMpContext
.ViewPointIndex
== ProcessorIndex
) {
2633 IssuedViewPoint
= mDebugMpContext
.ViewPointIndex
;
2637 if (IssuedViewPoint
== ProcessorIndex
&& GetDebugFlag (DEBUG_AGENT_FLAG_STEPPING
) != 1) {
2639 // If the command is not stepping, clean up AgentInProgress flag
2641 SetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS
, 0);