X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=SourceLevelDebugPkg%2FLibrary%2FDebugAgent%2FDebugAgentCommon%2FDebugAgent.c;h=f156fe24db2d3578e2766d96cc147b84a01ef80e;hp=fc40112b54e9285fe7e381f918ed24d2a4073023;hb=9e981317be20ab85bb68a670e79735f9685a3348;hpb=08021523f8a581437b07986d2c1876a7b6f0d282 diff --git a/SourceLevelDebugPkg/Library/DebugAgent/DebugAgentCommon/DebugAgent.c b/SourceLevelDebugPkg/Library/DebugAgent/DebugAgentCommon/DebugAgent.c index fc40112b54..f156fe24db 100644 --- a/SourceLevelDebugPkg/Library/DebugAgent/DebugAgentCommon/DebugAgent.c +++ b/SourceLevelDebugPkg/Library/DebugAgent/DebugAgentCommon/DebugAgent.c @@ -4,7 +4,7 @@ read/write debug packet to communication with HOST based on transfer protocol. - Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.
+ Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -19,7 +19,7 @@ #include "Ia32/DebugException.h" GLOBAL_REMOVE_IF_UNREFERENCED CHAR8 mErrorMsgVersionAlert[] = "\rThe SourceLevelDebugPkg you are using requires a newer version of the Intel(R) UDK Debugger Tool.\r\n"; -GLOBAL_REMOVE_IF_UNREFERENCED CHAR8 mErrorMsgSendInitPacket[] = "\rSend INIT break packet and try to connect the HOST (Intel(R) UDK Debugger Tool v1.4) ...\r\n"; +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"; GLOBAL_REMOVE_IF_UNREFERENCED CHAR8 mErrorMsgConnectOK[] = "HOST connection is successful!\r\n"; GLOBAL_REMOVE_IF_UNREFERENCED CHAR8 mErrorMsgConnectFail[] = "HOST connection is failed!\r\n"; GLOBAL_REMOVE_IF_UNREFERENCED CHAR8 mWarningMsgIngoreBreakpoint[] = "Ignore break point in SMM for SMI issued during DXE debugging!\r\n"; @@ -201,55 +201,17 @@ FindAndReportModuleImageInfo ( ) { UINTN Pe32Data; - EFI_IMAGE_DOS_HEADER *DosHdr; - EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr; PE_COFF_LOADER_IMAGE_CONTEXT ImageContext; // // Find Image Base // - Pe32Data = ((UINTN)mErrorMsgVersionAlert) & ~(AlignSize - 1); - while (Pe32Data != 0) { - DosHdr = (EFI_IMAGE_DOS_HEADER *) Pe32Data; - if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) { - // - // DOS image header is present, so read the PE header after the DOS image header. - // - Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)(Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff)); - // - // Make sure PE header address does not overflow and is less than the initial address. - // - if (((UINTN)Hdr.Pe32 > Pe32Data) && ((UINTN)Hdr.Pe32 < (UINTN)mErrorMsgVersionAlert)) { - if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) { - // - // It's PE image. - // - break; - } - } - } else { - // - // DOS image header is not present, TE header is at the image base. - // - Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data; - if ((Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) && - ((Hdr.Te->Machine == IMAGE_FILE_MACHINE_I386) || Hdr.Te->Machine == IMAGE_FILE_MACHINE_X64)) { - // - // It's TE image, it TE header and Machine type match - // - break; - } - } - - // - // Not found the image base, check the previous aligned address - // - Pe32Data -= AlignSize; + Pe32Data = PeCoffSearchImageBase ((UINTN) mErrorMsgVersionAlert); + if (Pe32Data != 0) { + ImageContext.ImageAddress = Pe32Data; + ImageContext.PdbPointer = PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) ImageContext.ImageAddress); + PeCoffLoaderRelocateImageExtraAction (&ImageContext); } - - ImageContext.ImageAddress = Pe32Data; - ImageContext.PdbPointer = PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) ImageContext.ImageAddress); - PeCoffLoaderRelocateImageExtraAction (&ImageContext); } /** @@ -381,7 +343,7 @@ UpdateMailboxContent ( Mailbox->HostSequenceNo = (UINT8) Value; break; case DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY: - Mailbox->ToBeCheckSum = Mailbox->CheckSum + CalculateSum8 ((UINT8 *)&Mailbox->HostSequenceNo, sizeof(UINT32)) + Mailbox->ToBeCheckSum = Mailbox->CheckSum + CalculateSum8 ((UINT8 *)&Mailbox->DebugTimerFrequency, sizeof(UINT32)) - CalculateSum8 ((UINT8 *)&Value, sizeof(UINT32)); Mailbox->DebugTimerFrequency = (UINT32) Value; break; @@ -785,7 +747,7 @@ CommandGo ( } /** - Exectue Stepping command. + Execute Stepping command. @param[in] CpuContext Pointer to saved CPU context. @@ -800,6 +762,39 @@ CommandStepping ( Eflags = (IA32_EFLAGS32 *) &CpuContext->Eflags; Eflags->Bits.TF = 1; Eflags->Bits.RF = 1; + // + // Save and clear EFLAGS.IF to avoid interrupt happen when executing Stepping + // + SetDebugFlag (DEBUG_AGENT_FLAG_INTERRUPT_FLAG, Eflags->Bits.IF); + Eflags->Bits.IF = 0; + // + // Set Stepping Flag + // + SetDebugFlag (DEBUG_AGENT_FLAG_STEPPING, 1); +} + +/** + Do some cleanup after Stepping command done. + + @param[in] CpuContext Pointer to saved CPU context. + +**/ +VOID +CommandSteppingCleanup ( + IN DEBUG_CPU_CONTEXT *CpuContext + ) +{ + IA32_EFLAGS32 *Eflags; + + Eflags = (IA32_EFLAGS32 *) &CpuContext->Eflags; + // + // Restore EFLAGS.IF + // + Eflags->Bits.IF = GetDebugFlag (DEBUG_AGENT_FLAG_INTERRUPT_FLAG); + // + // Clear Stepping flag + // + SetDebugFlag (DEBUG_AGENT_FLAG_STEPPING, 0); } /** @@ -1302,8 +1297,12 @@ GetBreakCause ( if ((CpuContext->Dr6 & BIT14) != 0) { Cause = DEBUG_DATA_BREAK_CAUSE_STEPPING; // - // If it's single step, no need to check DR0, to ensure single step work in PeCoffExtraActionLib - // (right after triggering a breakpoint to report image load/unload). + // DR6.BIT14 Indicates (when set) that the debug exception was + // triggered by the single step execution mode. + // The single-step mode is the highest priority debug exception. + // This is single step, no need to check DR0, to ensure single step + // work in PeCoffExtraActionLib (right after triggering a breakpoint + // to report image load/unload). // return Cause; @@ -1434,31 +1433,32 @@ CopyMemByWidth ( 2. Compute the CRC of the compressed data buffer; 3. Compress the data and send to the debug channel. + @param[in] Handle The debug channel handle to send the compressed data buffer. @param[in] Data The data buffer. @param[in] Length The length of the data buffer. + @param[in] Send TRUE to send the compressed data buffer. @param[out] CompressedLength Return the length of the compressed data buffer. It may be larger than the Length in some cases. @param[out] CompressedCrc Return the CRC of the compressed data buffer. - @param[in] Handle The debug channel handle to send the compressed data buffer. **/ VOID -CompressDataThenSend ( +CompressData ( + IN DEBUG_PORT_HANDLE Handle, IN UINT8 *Data, IN UINT8 Length, + IN BOOLEAN Send, OUT UINTN *CompressedLength, OPTIONAL - OUT UINT16 *CompressedCrc, OPTIONAL - IN DEBUG_PORT_HANDLE Handle OPTIONAL + OUT UINT16 *CompressedCrc OPTIONAL ) { - UINTN Index; - UINT8 LastChar; - UINT8 LastCharCount; - UINT8 CurrentChar; - UINTN CompressedIndex; + UINTN Index; + UINT8 LastChar; + UINT8 LastCharCount; + UINT8 CurrentChar; + UINTN CompressedIndex; ASSERT (Length > 0); - - LastChar = Data[0] + 1; // Just ensure it's different from the first byte. + LastChar = Data[0] + 1; // Just ensure it's different from the first byte. LastCharCount = 0; for (Index = 0, CompressedIndex = 0; Index <= Length; Index++) { @@ -1473,7 +1473,7 @@ CompressDataThenSend ( if (CompressedCrc != NULL) { *CompressedCrc = CalculateCrc16 (&LastChar, 1, *CompressedCrc); } - if (Handle != NULL) { + if (Send) { DebugPortWriteBuffer (Handle, &LastChar, 1); } @@ -1485,7 +1485,7 @@ CompressDataThenSend ( *CompressedCrc = CalculateCrc16 (&LastChar, 1, *CompressedCrc); *CompressedCrc = CalculateCrc16 (&LastCharCount, 1, *CompressedCrc); } - if (Handle != NULL) { + if (Send) { DebugPortWriteBuffer (Handle, &LastChar, 1); DebugPortWriteBuffer (Handle, &LastChar, 1); DebugPortWriteBuffer (Handle, &LastCharCount, 1); @@ -1568,11 +1568,12 @@ ReadMemoryAndSendResponsePacket ( // // Get the compressed data size without modifying the packet. // - CompressDataThenSend ( + CompressData ( + Handle, (UINT8 *) (DebugHeader + 1), CurrentDataSize, + FALSE, &CompressedDataSize, - NULL, NULL ); } else { @@ -1585,12 +1586,13 @@ ReadMemoryAndSendResponsePacket ( // Compute the CRC of the packet head without modifying the packet. // DebugHeader->Crc = CalculateCrc16 ((UINT8 *) DebugHeader, sizeof (DEBUG_PACKET_HEADER), 0); - CompressDataThenSend ( + CompressData ( + Handle, (UINT8 *) (DebugHeader + 1), CurrentDataSize, + FALSE, NULL, - &DebugHeader->Crc, - NULL + &DebugHeader->Crc ); // // Send out the packet head. @@ -1599,12 +1601,13 @@ ReadMemoryAndSendResponsePacket ( // // Compress and send out the packet data. // - CompressDataThenSend ( + CompressData ( + Handle, (UINT8 *) (DebugHeader + 1), CurrentDataSize, + TRUE, NULL, - NULL, - Handle + NULL ); } else { @@ -1818,7 +1821,6 @@ CommandCommunication ( DEBUG_DATA_SET_VIEWPOINT *SetViewPoint; BOOLEAN HaltDeferred; UINT32 ProcessorIndex; - DEBUG_PORT_HANDLE Handle; DEBUG_AGENT_EXCEPTION_BUFFER AgentExceptionBuffer; UINT32 IssuedViewPoint; DEBUG_AGENT_MAILBOX *Mailbox; @@ -1846,8 +1848,6 @@ CommandCommunication ( SetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS, 1); } - Handle = GetDebugPortHandle(); - while (TRUE) { if (MultiProcessorDebugSupport()) { @@ -1952,10 +1952,6 @@ CommandCommunication ( if (Data8 == DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD || Data8 == DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD) { CpuContext->Dr0 = 0; } - // - // Clear Stepping Flag - // - SetDebugFlag (DEBUG_AGENT_FLAG_STEPPING, 0); if (!HaltDeferred) { // @@ -2070,10 +2066,6 @@ CommandCommunication ( } mDebugMpContext.BreakAtCpuIndex = (UINT32) (-1); - // - // Set Stepping Flag - // - SetDebugFlag (DEBUG_AGENT_FLAG_STEPPING, 1); ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock); // // Executing stepping command directly without sending ACK packet, @@ -2367,9 +2359,16 @@ InterruptProcess ( // Check if this exception is issued by Debug Agent itself // If yes, fill the debug agent exception buffer and LongJump() back to // the saved CPU content in CommandCommunication() + // If exception is issued when executing Stepping, will be handled in + // exception handle procedure. // if (GetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS) == 1) { - DebugAgentMsgPrint (DEBUG_AGENT_ERROR, "Debug agent meet one Exception, ExceptionNum is %d, EIP = 0x%x.\n", Vector, (UINTN)CpuContext->Eip); + DebugAgentMsgPrint ( + DEBUG_AGENT_ERROR, + "Debug agent meet one Exception, ExceptionNum is %d, EIP = 0x%x.\n", + Vector, + (UINTN)CpuContext->Eip + ); ExceptionBuffer = (DEBUG_AGENT_EXCEPTION_BUFFER *) (UINTN) GetMailboxPointer()->ExceptionBufferPointer; ExceptionBuffer->ExceptionContent.ExceptionNum = (UINT8) Vector; ExceptionBuffer->ExceptionContent.ExceptionData = (UINT32) CpuContext->ExceptionData; @@ -2409,6 +2408,10 @@ InterruptProcess ( if (MultiProcessorDebugSupport()) { mDebugMpContext.BreakAtCpuIndex = ProcessorIndex; } + // + // Clear Stepping Flag and restore EFLAGS.IF + // + CommandSteppingCleanup (CpuContext); SendAckPacket (DEBUG_COMMAND_OK); CommandCommunication (Vector, CpuContext, BreakReceived); break; @@ -2490,7 +2493,8 @@ InterruptProcess ( // CurrentDebugTimerInitCount = GetApicTimerInitCount (); if (mDebugMpContext.DebugTimerInitCount != CurrentDebugTimerInitCount) { - InitializeDebugTimer (NULL); + InitializeDebugTimer (NULL, FALSE); + SaveAndSetDebugTimerInterrupt (TRUE); } } @@ -2575,13 +2579,24 @@ InterruptProcess ( default: if (Vector <= DEBUG_EXCEPT_SIMD) { + DebugAgentMsgPrint ( + DEBUG_AGENT_ERROR, + "Exception happened, ExceptionNum is %d, EIP = 0x%x.\n", + Vector, + (UINTN) CpuContext->Eip + ); if (BreakCause == DEBUG_DATA_BREAK_CAUSE_STEPPING) { // - // Stepping is finished, send Ack package. + // If exception happened when executing Stepping, send Ack package. + // HOST consider Stepping command was finished. // if (MultiProcessorDebugSupport()) { mDebugMpContext.BreakAtCpuIndex = ProcessorIndex; } + // + // Clear Stepping flag and restore EFLAGS.IF + // + CommandSteppingCleanup (CpuContext); SendAckPacket (DEBUG_COMMAND_OK); } else { //