]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SourceLevelDebugPkg/Library/DebugAgent/DebugAgentCommon/DebugAgent.c
SourceLevelDebugPkg: Fix Serial Port connection cannot be setup
[mirror_edk2.git] / SourceLevelDebugPkg / Library / DebugAgent / DebugAgentCommon / DebugAgent.c
index 42b73f60ea0029bed59a6ff633c27bed01c1fb82..10dd302694bb19d74c0df114207c4c52ec01a014 100644 (file)
@@ -785,7 +785,7 @@ CommandGo (
 }\r
 \r
 /**\r
-  Exectue Stepping command.\r
+  Execute Stepping command.\r
 \r
   @param[in] CpuContext        Pointer to saved CPU context.\r
 \r
@@ -800,6 +800,39 @@ CommandStepping (
   Eflags = (IA32_EFLAGS32 *) &CpuContext->Eflags;\r
   Eflags->Bits.TF = 1;\r
   Eflags->Bits.RF = 1;\r
+  //\r
+  // Save and clear EFLAGS.IF to avoid interrupt happen when executing Stepping\r
+  //\r
+  SetDebugFlag (DEBUG_AGENT_FLAG_INTERRUPT_FLAG, Eflags->Bits.IF);\r
+  Eflags->Bits.IF = 0;\r
+  //\r
+  // Set Stepping Flag\r
+  //\r
+  SetDebugFlag (DEBUG_AGENT_FLAG_STEPPING, 1);\r
+}\r
+\r
+/**\r
+  Do some cleanup after Stepping command done.\r
+\r
+  @param[in] CpuContext        Pointer to saved CPU context.\r
+\r
+**/\r
+VOID\r
+CommandSteppingCleanup (\r
+  IN DEBUG_CPU_CONTEXT          *CpuContext\r
+  )\r
+{\r
+  IA32_EFLAGS32                *Eflags;\r
+\r
+  Eflags = (IA32_EFLAGS32 *) &CpuContext->Eflags;\r
+  //\r
+  // Restore EFLAGS.IF\r
+  //\r
+  Eflags->Bits.IF = GetDebugFlag (DEBUG_AGENT_FLAG_INTERRUPT_FLAG);\r
+  //\r
+  // Clear Stepping flag\r
+  //\r
+  SetDebugFlag (DEBUG_AGENT_FLAG_STEPPING, 0);\r
 }\r
 \r
 /**\r
@@ -1302,8 +1335,12 @@ GetBreakCause (
       if ((CpuContext->Dr6 & BIT14) != 0) {\r
         Cause = DEBUG_DATA_BREAK_CAUSE_STEPPING;\r
         //\r
-        // If it's single step, no need to check DR0, to ensure single step work in PeCoffExtraActionLib\r
-        // (right after triggering a breakpoint to report image load/unload).\r
+        // DR6.BIT14 Indicates (when set) that the debug exception was\r
+        // triggered by the single step execution mode.\r
+        // The single-step mode is the highest priority debug exception.\r
+        // This is single step, no need to check DR0, to ensure single step\r
+        // work in PeCoffExtraActionLib (right after triggering a breakpoint\r
+        // to report image load/unload).\r
         //\r
         return Cause;\r
 \r
@@ -1434,31 +1471,32 @@ CopyMemByWidth (
   2. Compute the CRC of the compressed data buffer;\r
   3. Compress the data and send to the debug channel.\r
 \r
+  @param[in]  Handle           The debug channel handle to send the compressed data buffer.\r
   @param[in]  Data             The data buffer.\r
   @param[in]  Length           The length of the data buffer.\r
+  @param[in]  Send             TRUE to send the compressed data buffer.\r
   @param[out] CompressedLength Return the length of the compressed data buffer.\r
                                It may be larger than the Length in some cases.\r
   @param[out] CompressedCrc    Return the CRC of the compressed data buffer.\r
-  @param[in]  Handle           The debug channel handle to send the compressed data buffer.\r
 **/\r
 VOID\r
-CompressDataThenSend (\r
+CompressData (\r
+  IN  DEBUG_PORT_HANDLE Handle,\r
   IN  UINT8             *Data,\r
   IN  UINT8             Length,\r
+  IN  BOOLEAN           Send,\r
   OUT UINTN             *CompressedLength,  OPTIONAL\r
-  OUT UINT16            *CompressedCrc,     OPTIONAL\r
-  IN  DEBUG_PORT_HANDLE Handle              OPTIONAL\r
+  OUT UINT16            *CompressedCrc      OPTIONAL\r
   )\r
 {\r
-  UINTN  Index;\r
-  UINT8  LastChar;\r
-  UINT8  LastCharCount;\r
-  UINT8  CurrentChar;\r
-  UINTN  CompressedIndex;\r
+  UINTN                 Index;\r
+  UINT8                 LastChar;\r
+  UINT8                 LastCharCount;\r
+  UINT8                 CurrentChar;\r
+  UINTN                 CompressedIndex;\r
 \r
   ASSERT (Length > 0);\r
-\r
-  LastChar = Data[0] + 1; // Just ensure it's different from the first byte.\r
+  LastChar      = Data[0] + 1; // Just ensure it's different from the first byte.\r
   LastCharCount = 0;\r
 \r
   for (Index = 0, CompressedIndex = 0; Index <= Length; Index++) {\r
@@ -1473,7 +1511,7 @@ CompressDataThenSend (
         if (CompressedCrc != NULL) {\r
           *CompressedCrc = CalculateCrc16 (&LastChar, 1, *CompressedCrc);\r
         }\r
-        if (Handle != NULL) {\r
+        if (Send) {\r
           DebugPortWriteBuffer (Handle, &LastChar, 1);\r
         }\r
         \r
@@ -1485,7 +1523,7 @@ CompressDataThenSend (
           *CompressedCrc = CalculateCrc16 (&LastChar, 1, *CompressedCrc);\r
           *CompressedCrc = CalculateCrc16 (&LastCharCount, 1, *CompressedCrc);\r
         }\r
-        if (Handle != NULL) {\r
+        if (Send) {\r
           DebugPortWriteBuffer (Handle, &LastChar, 1);\r
           DebugPortWriteBuffer (Handle, &LastChar, 1);\r
           DebugPortWriteBuffer (Handle, &LastCharCount, 1);\r
@@ -1568,11 +1606,12 @@ ReadMemoryAndSendResponsePacket (
       //\r
       // Get the compressed data size without modifying the packet.\r
       //\r
-      CompressDataThenSend (\r
+      CompressData (\r
+        Handle,\r
         (UINT8 *) (DebugHeader + 1),\r
         CurrentDataSize,\r
+        FALSE,\r
         &CompressedDataSize,\r
-        NULL,\r
         NULL\r
         );\r
     } else {\r
@@ -1585,12 +1624,13 @@ ReadMemoryAndSendResponsePacket (
       // Compute the CRC of the packet head without modifying the packet.\r
       //\r
       DebugHeader->Crc = CalculateCrc16 ((UINT8 *) DebugHeader, sizeof (DEBUG_PACKET_HEADER), 0);\r
-      CompressDataThenSend (\r
+      CompressData (\r
+        Handle,\r
         (UINT8 *) (DebugHeader + 1),\r
         CurrentDataSize,\r
+        FALSE,\r
         NULL,\r
-        &DebugHeader->Crc,\r
-        NULL\r
+        &DebugHeader->Crc\r
         );\r
       //\r
       // Send out the packet head.\r
@@ -1599,12 +1639,13 @@ ReadMemoryAndSendResponsePacket (
       //\r
       // Compress and send out the packet data.\r
       //\r
-      CompressDataThenSend (\r
+      CompressData (\r
+        Handle,\r
         (UINT8 *) (DebugHeader + 1),\r
         CurrentDataSize,\r
+        TRUE,\r
         NULL,\r
-        NULL,\r
-        Handle\r
+        NULL\r
         );\r
     } else {\r
 \r
@@ -1952,10 +1993,6 @@ CommandCommunication (
       if (Data8 == DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD || Data8 == DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD) {\r
         CpuContext->Dr0 = 0;\r
       }\r
-      //\r
-      // Clear Stepping Flag\r
-      //\r
-      SetDebugFlag (DEBUG_AGENT_FLAG_STEPPING, 0);\r
 \r
       if (!HaltDeferred) {\r
         //\r
@@ -2070,10 +2107,6 @@ CommandCommunication (
       }\r
 \r
       mDebugMpContext.BreakAtCpuIndex = (UINT32) (-1);\r
-      //\r
-      // Set Stepping Flag\r
-      //\r
-      SetDebugFlag (DEBUG_AGENT_FLAG_STEPPING, 1);\r
       ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);\r
       //\r
       // Executing stepping command directly without sending ACK packet,\r
@@ -2367,9 +2400,16 @@ InterruptProcess (
     // Check if this exception is issued by Debug Agent itself\r
     // If yes, fill the debug agent exception buffer and LongJump() back to\r
     // the saved CPU content in CommandCommunication()\r
+    // If exception is issued when executing Stepping, will be handled in\r
+    // exception handle procedure.\r
     //\r
     if (GetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS) == 1) {\r
-      DebugAgentMsgPrint (DEBUG_AGENT_ERROR, "Debug agent meet one Exception, ExceptionNum is %d, EIP = 0x%x.\n", Vector, (UINTN)CpuContext->Eip);\r
+      DebugAgentMsgPrint (\r
+        DEBUG_AGENT_ERROR,\r
+        "Debug agent meet one Exception, ExceptionNum is %d, EIP = 0x%x.\n",\r
+        Vector,\r
+        (UINTN)CpuContext->Eip\r
+        );\r
       ExceptionBuffer = (DEBUG_AGENT_EXCEPTION_BUFFER *) (UINTN) GetMailboxPointer()->ExceptionBufferPointer;\r
       ExceptionBuffer->ExceptionContent.ExceptionNum  = (UINT8) Vector;\r
       ExceptionBuffer->ExceptionContent.ExceptionData = (UINT32) CpuContext->ExceptionData;\r
@@ -2409,6 +2449,10 @@ InterruptProcess (
       if (MultiProcessorDebugSupport()) {\r
         mDebugMpContext.BreakAtCpuIndex = ProcessorIndex;\r
       }\r
+      //\r
+      // Clear Stepping Flag and restore EFLAGS.IF\r
+      //\r
+      CommandSteppingCleanup (CpuContext);\r
       SendAckPacket (DEBUG_COMMAND_OK);\r
       CommandCommunication (Vector, CpuContext, BreakReceived);\r
       break;\r
@@ -2575,13 +2619,24 @@ InterruptProcess (
 \r
   default:\r
     if (Vector <= DEBUG_EXCEPT_SIMD) {\r
+      DebugAgentMsgPrint (\r
+        DEBUG_AGENT_ERROR,\r
+        "Exception happened, ExceptionNum is %d, EIP = 0x%x.\n",\r
+        Vector,\r
+        (UINTN) CpuContext->Eip\r
+        );\r
       if (BreakCause == DEBUG_DATA_BREAK_CAUSE_STEPPING) {\r
         //\r
-        // Stepping is finished, send Ack package.\r
+        // If exception happened when executing Stepping, send Ack package.\r
+        // HOST consider Stepping command was finished.\r
         //\r
         if (MultiProcessorDebugSupport()) {\r
           mDebugMpContext.BreakAtCpuIndex = ProcessorIndex;\r
         }\r
+        //\r
+        // Clear Stepping flag and restore EFLAGS.IF\r
+        //\r
+        CommandSteppingCleanup (CpuContext);\r
         SendAckPacket (DEBUG_COMMAND_OK);\r
       } else {\r
         //\r