SecPeiCpuException.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /** @file
  2. CPU exception handler library implemenation for SEC/PEIM modules.
  3. Copyright (c) 2012 - 2022, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <PiPei.h>
  7. #include <Library/CcExitLib.h>
  8. #include "CpuExceptionCommon.h"
  9. CONST UINTN mDoFarReturnFlag = 0;
  10. /**
  11. Common exception handler.
  12. @param ExceptionType Exception type.
  13. @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
  14. **/
  15. VOID
  16. EFIAPI
  17. CommonExceptionHandler (
  18. IN EFI_EXCEPTION_TYPE ExceptionType,
  19. IN EFI_SYSTEM_CONTEXT SystemContext
  20. )
  21. {
  22. EFI_STATUS Status;
  23. switch (ExceptionType) {
  24. case VC_EXCEPTION:
  25. //
  26. // #VC needs to be handled immediately upon enabling exception handling
  27. // and therefore can't use the RegisterCpuInterruptHandler() interface
  28. // (which isn't supported under Sec and Pei anyway).
  29. //
  30. // Handle the #VC:
  31. // On EFI_SUCCESS - Exception has been handled, return
  32. // On other - ExceptionType contains (possibly new) exception
  33. // value
  34. //
  35. Status = CcExitHandleVc (&ExceptionType, SystemContext);
  36. if (!EFI_ERROR (Status)) {
  37. return;
  38. }
  39. break;
  40. case VE_EXCEPTION:
  41. //
  42. // #VE needs to be handled immediately upon enabling exception handling
  43. // and therefore can't use the RegisterCpuInterruptHandler() interface
  44. // (which isn't supported under Sec and Pei anyway).
  45. //
  46. // Handle the #VE:
  47. // On EFI_SUCCESS - Exception has been handled, return
  48. // On other - ExceptionType contains (possibly new) exception
  49. // value
  50. //
  51. Status = CcExitHandleVe (&ExceptionType, SystemContext);
  52. if (!EFI_ERROR (Status)) {
  53. return;
  54. }
  55. break;
  56. default:
  57. break;
  58. }
  59. //
  60. // Initialize the serial port before dumping.
  61. //
  62. SerialPortInitialize ();
  63. //
  64. // Display ExceptionType, CPU information and Image information
  65. //
  66. DumpImageAndCpuContent (ExceptionType, SystemContext);
  67. //
  68. // Enter a dead loop.
  69. //
  70. CpuDeadLoop ();
  71. }
  72. /**
  73. Initializes all CPU exceptions entries and provides the default exception handlers.
  74. Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
  75. persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
  76. If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
  77. If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
  78. Note: Before invoking this API, caller must allocate memory for IDT table and load
  79. IDTR by AsmWriteIdtr().
  80. @param[in] VectorInfo Pointer to reserved vector list.
  81. @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
  82. with default exception handlers.
  83. @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
  84. @retval EFI_UNSUPPORTED This function is not supported.
  85. **/
  86. EFI_STATUS
  87. EFIAPI
  88. InitializeCpuExceptionHandlers (
  89. IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
  90. )
  91. {
  92. EFI_STATUS Status;
  93. RESERVED_VECTORS_DATA ReservedVectorData[CPU_EXCEPTION_NUM];
  94. IA32_DESCRIPTOR IdtDescriptor;
  95. UINTN IdtEntryCount;
  96. UINT16 CodeSegment;
  97. EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap;
  98. IA32_IDT_GATE_DESCRIPTOR *IdtTable;
  99. UINTN Index;
  100. UINTN InterruptHandler;
  101. if (VectorInfo != NULL) {
  102. SetMem ((VOID *)ReservedVectorData, sizeof (RESERVED_VECTORS_DATA) * CPU_EXCEPTION_NUM, 0xff);
  103. Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectorData, CPU_EXCEPTION_NUM);
  104. if (EFI_ERROR (Status)) {
  105. return EFI_INVALID_PARAMETER;
  106. }
  107. }
  108. //
  109. // Read IDT descriptor and calculate IDT size
  110. //
  111. AsmReadIdtr (&IdtDescriptor);
  112. IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR);
  113. if (IdtEntryCount > CPU_EXCEPTION_NUM) {
  114. //
  115. // CPU exception library only setup CPU_EXCEPTION_NUM exception handler at most
  116. //
  117. IdtEntryCount = CPU_EXCEPTION_NUM;
  118. }
  119. //
  120. // Use current CS as the segment selector of interrupt gate in IDT
  121. //
  122. CodeSegment = AsmReadCs ();
  123. AsmGetTemplateAddressMap (&TemplateMap);
  124. IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtDescriptor.Base;
  125. for (Index = 0; Index < IdtEntryCount; Index++) {
  126. IdtTable[Index].Bits.Selector = CodeSegment;
  127. //
  128. // Check reserved vectors attributes if has, only EFI_VECTOR_HANDOFF_DO_NOT_HOOK
  129. // supported in this instance
  130. //
  131. if (VectorInfo != NULL) {
  132. if (ReservedVectorData[Index].Attribute == EFI_VECTOR_HANDOFF_DO_NOT_HOOK) {
  133. continue;
  134. }
  135. }
  136. //
  137. // Update IDT entry
  138. //
  139. InterruptHandler = TemplateMap.ExceptionStart + Index * TemplateMap.ExceptionStubHeaderSize;
  140. ArchUpdateIdtEntry (&IdtTable[Index], InterruptHandler);
  141. }
  142. return EFI_SUCCESS;
  143. }
  144. /**
  145. Registers a function to be called from the processor interrupt handler.
  146. This function registers and enables the handler specified by InterruptHandler for a processor
  147. interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
  148. handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
  149. The installed handler is called once for each processor interrupt or exception.
  150. NOTE: This function should be invoked after InitializeCpuExceptionHandlers() is invoked,
  151. otherwise EFI_UNSUPPORTED returned.
  152. @param[in] InterruptType Defines which interrupt or exception to hook.
  153. @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
  154. when a processor interrupt occurs. If this parameter is NULL, then the handler
  155. will be uninstalled.
  156. @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
  157. @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
  158. previously installed.
  159. @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
  160. previously installed.
  161. @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
  162. or this function is not supported.
  163. **/
  164. EFI_STATUS
  165. EFIAPI
  166. RegisterCpuInterruptHandler (
  167. IN EFI_EXCEPTION_TYPE InterruptType,
  168. IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
  169. )
  170. {
  171. return EFI_UNSUPPORTED;
  172. }
  173. /**
  174. Setup separate stacks for certain exception handlers.
  175. If the input Buffer and BufferSize are both NULL, use global variable if possible.
  176. @param[in] Buffer Point to buffer used to separate exception stack.
  177. @param[in, out] BufferSize On input, it indicates the byte size of Buffer.
  178. If the size is not enough, the return status will
  179. be EFI_BUFFER_TOO_SMALL, and output BufferSize
  180. will be the size it needs.
  181. @retval EFI_SUCCESS The stacks are assigned successfully.
  182. @retval EFI_UNSUPPORTED This function is not supported.
  183. @retval EFI_BUFFER_TOO_SMALL This BufferSize is too small.
  184. **/
  185. EFI_STATUS
  186. EFIAPI
  187. InitializeSeparateExceptionStacks (
  188. IN VOID *Buffer,
  189. IN OUT UINTN *BufferSize
  190. )
  191. {
  192. return EFI_UNSUPPORTED;
  193. }