CpuExceptionCommon.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /** @file
  2. CPU Exception Handler Library common functions.
  3. Copyright (c) 2012 - 2019, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "CpuExceptionCommon.h"
  7. //
  8. // Error code flag indicating whether or not an error code will be
  9. // pushed on the stack if an exception occurs.
  10. //
  11. // 1 means an error code will be pushed, otherwise 0
  12. //
  13. CONST UINT32 mErrorCodeFlag = 0x20227d00;
  14. //
  15. // Define the maximum message length
  16. //
  17. #define MAX_DEBUG_MESSAGE_LENGTH 0x100
  18. CONST CHAR8 mExceptionReservedStr[] = "Reserved";
  19. CONST CHAR8 *mExceptionNameStr[] = {
  20. "#DE - Divide Error",
  21. "#DB - Debug",
  22. "NMI Interrupt",
  23. "#BP - Breakpoint",
  24. "#OF - Overflow",
  25. "#BR - BOUND Range Exceeded",
  26. "#UD - Invalid Opcode",
  27. "#NM - Device Not Available",
  28. "#DF - Double Fault",
  29. "Coprocessor Segment Overrun",
  30. "#TS - Invalid TSS",
  31. "#NP - Segment Not Present",
  32. "#SS - Stack Fault Fault",
  33. "#GP - General Protection",
  34. "#PF - Page-Fault",
  35. "Reserved",
  36. "#MF - x87 FPU Floating-Point Error",
  37. "#AC - Alignment Check",
  38. "#MC - Machine-Check",
  39. "#XM - SIMD floating-point",
  40. "#VE - Virtualization",
  41. "#CP - Control Protection",
  42. "Reserved",
  43. "Reserved",
  44. "Reserved",
  45. "Reserved",
  46. "Reserved",
  47. "Reserved",
  48. "Reserved",
  49. "#VC - VMM Communication",
  50. };
  51. #define EXCEPTION_KNOWN_NAME_NUM (sizeof (mExceptionNameStr) / sizeof (CHAR8 *))
  52. /**
  53. Get ASCII format string exception name by exception type.
  54. @param ExceptionType Exception type.
  55. @return ASCII format string exception name.
  56. **/
  57. CONST CHAR8 *
  58. GetExceptionNameStr (
  59. IN EFI_EXCEPTION_TYPE ExceptionType
  60. )
  61. {
  62. if ((UINTN)ExceptionType < EXCEPTION_KNOWN_NAME_NUM) {
  63. return mExceptionNameStr[ExceptionType];
  64. } else {
  65. return mExceptionReservedStr;
  66. }
  67. }
  68. /**
  69. Prints a message to the serial port.
  70. @param Format Format string for the message to print.
  71. @param ... Variable argument list whose contents are accessed
  72. based on the format string specified by Format.
  73. **/
  74. VOID
  75. EFIAPI
  76. InternalPrintMessage (
  77. IN CONST CHAR8 *Format,
  78. ...
  79. )
  80. {
  81. CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];
  82. VA_LIST Marker;
  83. //
  84. // Convert the message to an ASCII String
  85. //
  86. VA_START (Marker, Format);
  87. AsciiVSPrint (Buffer, sizeof (Buffer), Format, Marker);
  88. VA_END (Marker);
  89. //
  90. // Send the print string to a Serial Port
  91. //
  92. SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));
  93. }
  94. /**
  95. Find and display image base address and return image base and its entry point.
  96. @param CurrentEip Current instruction pointer.
  97. **/
  98. VOID
  99. DumpModuleImageInfo (
  100. IN UINTN CurrentEip
  101. )
  102. {
  103. EFI_STATUS Status;
  104. UINTN Pe32Data;
  105. VOID *PdbPointer;
  106. VOID *EntryPoint;
  107. Pe32Data = PeCoffSearchImageBase (CurrentEip);
  108. if (Pe32Data == 0) {
  109. InternalPrintMessage ("!!!! Can't find image information. !!!!\n");
  110. } else {
  111. //
  112. // Find Image Base entry point
  113. //
  114. Status = PeCoffLoaderGetEntryPoint ((VOID *)Pe32Data, &EntryPoint);
  115. if (EFI_ERROR (Status)) {
  116. EntryPoint = NULL;
  117. }
  118. InternalPrintMessage ("!!!! Find image based on IP(0x%x) ", CurrentEip);
  119. PdbPointer = PeCoffLoaderGetPdbPointer ((VOID *)Pe32Data);
  120. if (PdbPointer != NULL) {
  121. InternalPrintMessage ("%a", PdbPointer);
  122. } else {
  123. InternalPrintMessage ("(No PDB) ");
  124. }
  125. InternalPrintMessage (
  126. " (ImageBase=%016lp, EntryPoint=%016p) !!!!\n",
  127. (VOID *)Pe32Data,
  128. EntryPoint
  129. );
  130. }
  131. }
  132. /**
  133. Read and save reserved vector information
  134. @param[in] VectorInfo Pointer to reserved vector list.
  135. @param[out] ReservedVector Pointer to reserved vector data buffer.
  136. @param[in] VectorCount Vector number to be updated.
  137. @return EFI_SUCCESS Read and save vector info successfully.
  138. @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
  139. **/
  140. EFI_STATUS
  141. ReadAndVerifyVectorInfo (
  142. IN EFI_VECTOR_HANDOFF_INFO *VectorInfo,
  143. OUT RESERVED_VECTORS_DATA *ReservedVector,
  144. IN UINTN VectorCount
  145. )
  146. {
  147. while (VectorInfo->Attribute != EFI_VECTOR_HANDOFF_LAST_ENTRY) {
  148. if (VectorInfo->Attribute > EFI_VECTOR_HANDOFF_HOOK_AFTER) {
  149. //
  150. // If vector attrubute is invalid
  151. //
  152. return EFI_INVALID_PARAMETER;
  153. }
  154. if (VectorInfo->VectorNumber < VectorCount) {
  155. ReservedVector[VectorInfo->VectorNumber].Attribute = VectorInfo->Attribute;
  156. }
  157. VectorInfo++;
  158. }
  159. return EFI_SUCCESS;
  160. }