]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EmbeddedPkg/Library/GdbDebugAgent/Arm/Processor.c
EmbeddedPkg: Fix typos in comments
[mirror_edk2.git] / EmbeddedPkg / Library / GdbDebugAgent / Arm / Processor.c
old mode 100755 (executable)
new mode 100644 (file)
index e09e18d..8226c80
@@ -1,9 +1,9 @@
 /** @file\r
   Processor specific parts of the GDB stub\r
 \r
-  Copyright (c) 2008-2010, Apple Inc. All rights reserved.\r
-  \r
-  All rights reserved. This program and the accompanying materials\r
+  Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>\r
+\r
+  This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
   http://opensource.org/licenses/bsd-license.php\r
@@ -15,7 +15,6 @@
 \r
 \r
 #include <GdbDebugAgent.h>\r
-#include <Library/CacheMaintenanceLib.h>\r
 #include <Library/PrintLib.h>\r
 #include <Library/ArmLib.h>\r
 \r
@@ -49,8 +48,8 @@ AsmCommonExceptionEntry (
 //\r
 EFI_EXCEPTION_TYPE_ENTRY gExceptionType[] = {\r
   { EXCEPT_ARM_SOFTWARE_INTERRUPT,    GDB_SIGTRAP },\r
-  { EXCEPT_ARM_UNDEFINED_INSTRUCTION, GDB_SIGTRAP },  \r
-  { EXCEPT_ARM_PREFETCH_ABORT,        GDB_SIGTRAP }, \r
+  { EXCEPT_ARM_UNDEFINED_INSTRUCTION, GDB_SIGTRAP },\r
+  { EXCEPT_ARM_PREFETCH_ABORT,        GDB_SIGTRAP },\r
   { EXCEPT_ARM_DATA_ABORT,            GDB_SIGTRAP },    // GDB_SIGEMT\r
   { EXCEPT_ARM_RESERVED,              GDB_SIGTRAP },    // GDB_SIGILL\r
   { EXCEPT_ARM_FIQ,                   GDB_SIGINT }      // Used for ctrl-c\r
@@ -114,8 +113,8 @@ UINTN gRegisterOffsets[] = {
 \r
 /**\r
  Return the number of entries in the gExceptionType[]\r
\r
- @retval  UINTN, the number of entries in the gExceptionType[] array.    \r
+\r
+ @retval  UINTN, the number of entries in the gExceptionType[] array.\r
  **/\r
 UINTN\r
 MaxEfiException (\r
@@ -129,10 +128,10 @@ MaxEfiException (
 \r
 \r
 /**\r
- Check to see if the ISA is supported. \r
+ Check to see if the ISA is supported.\r
  ISA = Instruction Set Architecture\r
 \r
- @retval TRUE if Isa is supported    \r
+ @retval TRUE if Isa is supported\r
 \r
 **/\r
 BOOLEAN\r
@@ -151,14 +150,14 @@ CheckIsa (
 /**\r
  This takes in the register number and the System Context, and returns a pointer to the RegNumber-th register in gdb ordering\r
  It is, by default, set to find the register pointer of the ARM member\r
- @param   SystemContext     Register content at time of the exception \r
+ @param   SystemContext     Register content at time of the exception\r
  @param   RegNumber       The register to which we want to find a pointer\r
  @retval  the pointer to the RegNumber-th pointer\r
- **/ \r
+ **/\r
 UINTN *\r
 FindPointerToRegister(\r
   IN  EFI_SYSTEM_CONTEXT      SystemContext,\r
-  IN  UINTN           RegNumber  \r
+  IN  UINTN           RegNumber\r
   )\r
 {\r
   UINT8 *TempPtr;\r
@@ -184,7 +183,7 @@ BasicReadRegister (
 {\r
   UINTN RegSize;\r
   CHAR8 Char;\r
-  \r
+\r
   if (gRegisterOffsets[RegNumber] > 0xF00) {\r
     AsciiSPrint(OutBufPtr, 9, "00000000");\r
     OutBufPtr += 8;\r
@@ -198,21 +197,21 @@ BasicReadRegister (
       Char = Char - 'A' + 'a';\r
     }\r
     *OutBufPtr++ = Char;\r
-    \r
+\r
     Char = mHexToStr[(UINT8)((*FindPointerToRegister(SystemContext, RegNumber) >> RegSize) & 0xf)];\r
     if ((Char >= 'A') && (Char <= 'F')) {\r
       Char = Char - 'A' + 'a';\r
     }\r
     *OutBufPtr++ = Char;\r
-    \r
+\r
     RegSize = RegSize + 8;\r
   }\r
   return OutBufPtr;\r
 }\r
 \r
 \r
-/** ‘p n’ \r
- Reads the n-th register's value into an output buffer and sends it as a packet \r
+/**\r
+ Reads the n-th register's value into an output buffer and sends it as a packet\r
  @param   SystemContext   Register content at time of the exception\r
  @param   InBuffer      Pointer to the input buffer received from gdb server\r
  **/\r
@@ -225,29 +224,29 @@ ReadNthRegister (
   UINTN RegNumber;\r
   CHAR8 OutBuffer[9]; // 1 reg=8 hex chars, and the end '\0' (escape seq)\r
   CHAR8 *OutBufPtr;   // pointer to the output buffer\r
-  \r
+\r
   RegNumber = AsciiStrHexToUintn (&InBuffer[1]);\r
-  \r
+\r
   if (RegNumber >= (sizeof (gRegisterOffsets)/sizeof (UINTN))) {\r
-    SendError (GDB_EINVALIDREGNUM); \r
+    SendError (GDB_EINVALIDREGNUM);\r
     return;\r
   }\r
-  \r
+\r
   OutBufPtr = OutBuffer;\r
   OutBufPtr = BasicReadRegister (SystemContext, RegNumber, OutBufPtr);\r
-  \r
+\r
   *OutBufPtr = '\0';  // the end of the buffer\r
   SendPacket(OutBuffer);\r
 }\r
 \r
 \r
-/** ‘g’ \r
- Reads the general registers into an output buffer  and sends it as a packet \r
+/**\r
+ Reads the general registers into an output buffer  and sends it as a packet\r
  @param   SystemContext     Register content at time of the exception\r
  **/\r
 VOID\r
 EFIAPI\r
-ReadGeneralRegisters (  \r
+ReadGeneralRegisters (\r
   IN  EFI_SYSTEM_CONTEXT      SystemContext\r
   )\r
 {\r
@@ -255,13 +254,13 @@ ReadGeneralRegisters (
   // a UINT32 takes 8 ascii characters\r
   CHAR8   OutBuffer[(sizeof (gRegisterOffsets) * 2) + 1];\r
   CHAR8   *OutBufPtr;\r
-  \r
+\r
   // It is not safe to allocate pool here....\r
   OutBufPtr = OutBuffer;\r
   for (Index = 0; Index < (sizeof (gRegisterOffsets)/sizeof (UINTN)); Index++) {\r
     OutBufPtr = BasicReadRegister (SystemContext, Index, OutBufPtr);\r
   }\r
-  \r
+\r
   *OutBufPtr = '\0';\r
   SendPacket(OutBuffer);\r
 }\r
@@ -284,7 +283,7 @@ BasicWriteRegister (
   UINTN RegSize;\r
   UINTN TempValue; // the value transferred from a hex char\r
   UINT32 NewValue; // the new value of the RegNumber-th Register\r
-  \r
+\r
   if (gRegisterOffsets[RegNumber] > 0xF00) {\r
     return InBufPtr + 8;\r
   }\r
@@ -293,21 +292,21 @@ BasicWriteRegister (
   RegSize = 0;\r
   while (RegSize < 32) {\r
     TempValue = HexCharToInt(*InBufPtr++);\r
-    \r
+\r
     if ((INTN)TempValue < 0) {\r
-      SendError (GDB_EBADMEMDATA); \r
+      SendError (GDB_EBADMEMDATA);\r
       return NULL;\r
     }\r
-    \r
+\r
     NewValue += (TempValue << (RegSize+4));\r
     TempValue = HexCharToInt(*InBufPtr++);\r
-    \r
+\r
     if ((INTN)TempValue < 0) {\r
-      SendError (GDB_EBADMEMDATA); \r
+      SendError (GDB_EBADMEMDATA);\r
       return NULL;\r
     }\r
-    \r
-    NewValue += (TempValue << RegSize); \r
+\r
+    NewValue += (TempValue << RegSize);\r
     RegSize = RegSize + 8;\r
   }\r
   *(FindPointerToRegister(SystemContext, RegNumber)) = NewValue;\r
@@ -330,19 +329,19 @@ WriteNthRegister (
   CHAR8 RegNumBuffer[MAX_REG_NUM_BUF_SIZE];  // put the 'n..' part of the message into this array\r
   CHAR8 *RegNumBufPtr;\r
   CHAR8 *InBufPtr; // pointer to the input buffer\r
-  \r
+\r
   // find the register number to write\r
   InBufPtr = &InBuffer[1];\r
   RegNumBufPtr = RegNumBuffer;\r
   while (*InBufPtr != '=') {\r
     *RegNumBufPtr++ = *InBufPtr++;\r
-  } \r
+  }\r
   *RegNumBufPtr = '\0';\r
-  RegNumber = AsciiStrHexToUintn (RegNumBuffer); \r
-  \r
+  RegNumber = AsciiStrHexToUintn (RegNumBuffer);\r
+\r
   // check if this is a valid Register Number\r
   if (RegNumber >= (sizeof (gRegisterOffsets)/sizeof (UINTN))) {\r
-    SendError (GDB_EINVALIDREGNUM); \r
+    SendError (GDB_EINVALIDREGNUM);\r
     return;\r
   }\r
   InBufPtr++;  // skips the '=' character\r
@@ -370,32 +369,32 @@ WriteGeneralRegisters (
   UINTN  RegisterCount = (sizeof (gRegisterOffsets)/sizeof (UINTN));\r
 \r
   MinLength = (RegisterCount * 8) + 1;  // 'G' plus the registers in ASCII format\r
-  \r
+\r
   if (AsciiStrLen(InBuffer) < MinLength) {\r
-    //Bad message. Message is not the right length \r
-    SendError (GDB_EBADBUFSIZE); \r
+    //Bad message. Message is not the right length\r
+    SendError (GDB_EBADBUFSIZE);\r
     return;\r
   }\r
-  \r
+\r
   InBufPtr = &InBuffer[1];\r
-  \r
+\r
   // Read the new values for the registers from the input buffer to an array, NewValueArray.\r
   // The values in the array are in the gdb ordering\r
   for(i = 0; i < RegisterCount; i++) {\r
     InBufPtr = BasicWriteRegister (SystemContext, i, InBufPtr);\r
   }\r
-  \r
+\r
   SendSuccess ();\r
 }\r
 \r
 \r
 \r
 \r
-/** ‘c [addr ]’ \r
- Continue. addr is Address to resume. If addr is omitted, resume at current \r
+/**\r
+ Continue. addr is Address to resume. If addr is omitted, resume at current\r
  Address.\r
\r
- @param   SystemContext     Register content at time of the exception  \r
+\r
+ @param   SystemContext     Register content at time of the exception\r
  **/\r
 VOID\r
 EFIAPI\r
@@ -406,15 +405,15 @@ ContinueAtAddress (
 {\r
   if (PacketData[1] != '\0') {\r
     SystemContext.SystemContextArm->PC = AsciiStrHexToUintn(&PacketData[1]);\r
-  } \r
+  }\r
 }\r
 \r
 \r
 /** ‘s [addr ]’\r
- Single step. addr is the Address at which to resume. If addr is omitted, resume \r
+ Single step. addr is the Address at which to resume. If addr is omitted, resume\r
  at same Address.\r
\r
- @param   SystemContext     Register content at time of the exception  \r
+\r
+ @param   SystemContext     Register content at time of the exception\r
  **/\r
 VOID\r
 EFIAPI\r
@@ -448,44 +447,72 @@ RemoveBreakPoint (
 }\r
 \r
 \r
-/**
- Send the T signal with the given exception type (in gdb order) and possibly 
- with n:r pairs related to the watchpoints
- @param  SystemContext        Register content at time of the exception
- @param  GdbExceptionType     GDB exception type
- **/
-VOID
-ProcessorSendTSignal (
-  IN  EFI_SYSTEM_CONTEXT  SystemContext,
-  IN  UINT8               GdbExceptionType,
-  IN  OUT CHAR8           *TSignalPtr,
-  IN  UINTN               SizeOfBuffer
-  )
-{
-  *TSignalPtr = '\0';
-}
+/**\r
+ Send the T signal with the given exception type (in gdb order) and possibly\r
+ with n:r pairs related to the watchpoints\r
+\r
+ @param  SystemContext        Register content at time of the exception\r
+ @param  GdbExceptionType     GDB exception type\r
+ **/\r
+VOID\r
+ProcessorSendTSignal (\r
+  IN  EFI_SYSTEM_CONTEXT  SystemContext,\r
+  IN  UINT8               GdbExceptionType,\r
+  IN  OUT CHAR8           *TSignalPtr,\r
+  IN  UINTN               SizeOfBuffer\r
+  )\r
+{\r
+  *TSignalPtr = '\0';\r
+}\r
+\r
+/**\r
+ FIQ state is only changed by FIQ exception. We don't want to take FIQ\r
+ ticks in the GDB stub. The stub disables FIQ on entry, but this is the\r
+ third instruction that executes in the execption handler. Thus we have\r
+ a crack we need to test for.\r
+\r
+ @param PC     PC of execption\r
+\r
+ @return  TRUE  We are in the GDB stub exception preamble\r
+ @return  FALSE We are not in GDB stub code\r
+ **/\r
+BOOLEAN\r
+InFiqCrack (\r
+  IN UINT32 PC\r
+  )\r
+{\r
+  UINT64 VectorBase = PcdGet64 (PcdCpuVectorBaseAddress);\r
+  UINT32 Length     = (UINTN)ExceptionHandlersEnd - (UINTN)ExceptionHandlersStart;\r
+\r
+  if ((PC >= VectorBase) && (PC <= (VectorBase + Length))) {\r
+    return TRUE;\r
+  }\r
+\r
+  return FALSE;\r
+}\r
+\r
 \r
 /**\r
  Check to see if this exception is related to ctrl-c handling.\r
 \r
- In this scheme we dedicate FIQ to the ctrl-c handler so it is \r
- independent of the rest of the system. \r
\r
- SaveAndSetDebugTimerInterrupt () can be used to \r
+ In this scheme we dedicate FIQ to the ctrl-c handler so it is\r
+ independent of the rest of the system.\r
+\r
+ SaveAndSetDebugTimerInterrupt () can be used to\r
 \r
  @param ExceptionType     Exception that is being processed\r
- @param SystemContext     Register content at time of the exception  \r
+ @param SystemContext     Register content at time of the exception\r
 \r
  @return  TRUE  This was a ctrl-c check that did not find a ctrl-c\r
  @return  FALSE This was not a ctrl-c check or some one hit ctrl-c\r
  **/\r
 BOOLEAN\r
-ProcessorControlC ( \r
-  IN  EFI_EXCEPTION_TYPE        ExceptionType, \r
-  IN OUT EFI_SYSTEM_CONTEXT     SystemContext \r
+ProcessorControlC (\r
+  IN  EFI_EXCEPTION_TYPE        ExceptionType,\r
+  IN OUT EFI_SYSTEM_CONTEXT     SystemContext\r
   )\r
 {\r
+  CHAR8     Char;\r
   BOOLEAN   Return = TRUE;\r
 \r
   if (ExceptionType != EXCEPT_ARM_FIQ) {\r
@@ -493,23 +520,30 @@ ProcessorControlC (
     return FALSE;\r
   }\r
 \r
-  while (TRUE) {
-    if (!GdbIsCharAvailable ()) {
-      //
-      // No characters are pending so exit the loop
-      //
-      Return = TRUE;
-      break;
-    }
-    
-    if (GdbGetChar () == 0x03) {
-      //
-      // We have a ctrl-c so exit and process exception for ctrl-c
-      //
-      Return = FALSE;
-      break;
-    }
-  }
+  if (InFiqCrack (SystemContext.SystemContextArm->PC)) {\r
+    // We are in our own interrupt preable, so skip this tick.\r
+    // We never want to let gdb see the debug stub running if we can help it\r
+    return FALSE;\r
+  }\r
+\r
+  while (TRUE) {\r
+    if (!GdbIsCharAvailable ()) {\r
+      //\r
+      // No characters are pending so exit the loop\r
+      //\r
+      Return = TRUE;\r
+      break;\r
+    }\r
+\r
+    Char = GdbGetChar ();\r
+    if (Char == 0x03) {\r
+      //\r
+      // We have a ctrl-c so exit and process exception for ctrl-c\r
+      //\r
+      Return = FALSE;\r
+      break;\r
+    }\r
+  }\r
 \r
   DebugAgentTimerEndOfInterrupt ();\r
 \r
@@ -527,7 +561,8 @@ ProcessorControlC (
 \r
   @param[in] EnableStatus    Enable/Disable.\r
 \r
-  @return FALSE always.\r
+  @retval TRUE  Debug timer interrupt were enabled on entry to this call.\r
+  @retval FALSE Debug timer interrupt were disabled on entry to this call.\r
 \r
 **/\r
 BOOLEAN\r
@@ -541,7 +576,7 @@ SaveAndSetDebugTimerInterrupt (
   FiqEnabled = ArmGetFiqState ();\r
 \r
   if (EnableStatus) {\r
-    DebugAgentTimerSetPeriod (100);\r
+    DebugAgentTimerSetPeriod (PcdGet32 (PcdGdbTimerPeriodMilliseconds));\r
     ArmEnableFiq ();\r
   } else {\r
     DebugAgentTimerSetPeriod (0);\r
@@ -551,6 +586,8 @@ SaveAndSetDebugTimerInterrupt (
   return FiqEnabled;\r
 }\r
 \r
+\r
+\r
 VOID\r
 GdbFPutString (\r
   IN CHAR8  *String\r
@@ -559,37 +596,44 @@ GdbFPutString (
 /**\r
   Initialize debug agent.\r
 \r
-  This function is used to set up debug enviroment. It may enable interrupts.\r
+  This function is used to set up debug environment to support source level debugging.\r
+  If certain Debug Agent Library instance has to save some private data in the stack,\r
+  this function must work on the mode that doesn't return to the caller, then\r
+  the caller needs to wrap up all rest of logic after InitializeDebugAgent() into one\r
+  function and pass it into InitializeDebugAgent(). InitializeDebugAgent() is\r
+  responsible to invoke the passing-in function at the end of InitializeDebugAgent().\r
+\r
+  If the parameter Function is not NULL, Debug Agent Library instance will invoke it by\r
+  passing in the Context to be its parameter.\r
+\r
+  If Function() is NULL, Debug Agent Library instance will return after setup debug\r
+  environment.\r
 \r
-  @param[in] InitFlag   Init flag is used to decide initialize process.\r
-  @param[in] Context    Context needed according to InitFlag, it was optional.\r
+  @param[in] InitFlag     Init flag is used to decide the initialize process.\r
+  @param[in] Context      Context needed according to InitFlag; it was optional.\r
+  @param[in] Function     Continue function called by debug agent library; it was\r
+                          optional.\r
 \r
 **/\r
 VOID\r
 EFIAPI\r
 InitializeDebugAgent (\r
   IN UINT32                InitFlag,\r
-  IN VOID                  *Context  OPTIONAL\r
+  IN VOID                  *Context, OPTIONAL\r
+  IN DEBUG_AGENT_CONTINUE  Function  OPTIONAL\r
   )\r
-{  
+{\r
   UINTN                Offset;\r
   UINTN                Length;\r
   BOOLEAN              IrqEnabled;\r
-  BOOLEAN              FiqEnabled;\r
-  UINT32               *VectorBase;\r
+  UINT64               *VectorBase;\r
+\r
 \r
-    
   //\r
   // Disable interrupts\r
   //\r
   IrqEnabled = ArmGetInterruptState ();\r
   ArmDisableInterrupts ();\r
-\r
-  //\r
-  // EFI does not use the FIQ, but a debugger might so we must disable \r
-  // as we take over the exception vectors. \r
-  //\r
-  FiqEnabled = ArmGetFiqState ();\r
   ArmDisableFiq ();\r
 \r
   //\r
@@ -600,10 +644,10 @@ InitializeDebugAgent (
   //\r
   // Reserve space for the exception handlers\r
   //\r
-  VectorBase = (UINT32 *)(UINTN)PcdGet32 (PcdCpuVectorBaseAddress);\r
+  VectorBase = (UINT64 *)(UINTN)PcdGet64 (PcdCpuVectorBaseAddress);\r
 \r
 \r
-  // Copy our assembly code into the page that contains the exception vectors. \r
+  // Copy our assembly code into the page that contains the exception vectors.\r
   CopyMem ((VOID *)VectorBase, (VOID *)ExceptionHandlersStart, Length);\r
 \r
   //\r
@@ -613,18 +657,19 @@ InitializeDebugAgent (
   *(UINTN *) (((UINT8 *)VectorBase) + Offset) = (UINTN)AsmCommonExceptionEntry;\r
 \r
   // Flush Caches since we updated executable stuff\r
-  InvalidateInstructionCacheRange ((VOID *)PcdGet32(PcdCpuVectorBaseAddress), Length);\r
+  InvalidateInstructionCacheRange ((VOID *)PcdGet64(PcdCpuVectorBaseAddress), Length);\r
 \r
+  // setup a timer so gdb can break in via ctrl-c\r
   DebugAgentTimerIntialize ();\r
 \r
-  if (FiqEnabled) {\r
-    ArmEnableFiq ();\r
-  }\r
-\r
   if (IrqEnabled) {\r
     ArmEnableInterrupts ();\r
   }\r
 \r
+  if (Function != NULL) {\r
+    Function (Context);\r
+  }\r
+\r
   return;\r
 }\r
 \r