SecPeiCpuException.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /** @file
  2. CPU exception handler library implemenation for SEC/PEIM modules.
  3. Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <PiPei.h>
  7. #include <Library/VmgExitLib.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. if (ExceptionType == VC_EXCEPTION) {
  23. EFI_STATUS Status;
  24. //
  25. // #VC needs to be handled immediately upon enabling exception handling
  26. // and therefore can't use the RegisterCpuInterruptHandler() interface
  27. // (which isn't supported under Sec and Pei anyway).
  28. //
  29. // Handle the #VC:
  30. // On EFI_SUCCESS - Exception has been handled, return
  31. // On other - ExceptionType contains (possibly new) exception
  32. // value
  33. //
  34. Status = VmgExitHandleVc (&ExceptionType, SystemContext);
  35. if (!EFI_ERROR (Status)) {
  36. return;
  37. }
  38. }
  39. //
  40. // Initialize the serial port before dumping.
  41. //
  42. SerialPortInitialize ();
  43. //
  44. // Display ExceptionType, CPU information and Image information
  45. //
  46. DumpImageAndCpuContent (ExceptionType, SystemContext);
  47. //
  48. // Enter a dead loop.
  49. //
  50. CpuDeadLoop ();
  51. }
  52. /**
  53. Initializes all CPU exceptions entries and provides the default exception handlers.
  54. Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
  55. persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
  56. If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
  57. If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
  58. Note: Before invoking this API, caller must allocate memory for IDT table and load
  59. IDTR by AsmWriteIdtr().
  60. @param[in] VectorInfo Pointer to reserved vector list.
  61. @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
  62. with default exception handlers.
  63. @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
  64. @retval EFI_UNSUPPORTED This function is not supported.
  65. **/
  66. EFI_STATUS
  67. EFIAPI
  68. InitializeCpuExceptionHandlers (
  69. IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
  70. )
  71. {
  72. EFI_STATUS Status;
  73. RESERVED_VECTORS_DATA ReservedVectorData[CPU_EXCEPTION_NUM];
  74. IA32_DESCRIPTOR IdtDescriptor;
  75. UINTN IdtEntryCount;
  76. UINT16 CodeSegment;
  77. EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap;
  78. IA32_IDT_GATE_DESCRIPTOR *IdtTable;
  79. UINTN Index;
  80. UINTN InterruptHandler;
  81. if (VectorInfo != NULL) {
  82. SetMem ((VOID *)ReservedVectorData, sizeof (RESERVED_VECTORS_DATA) * CPU_EXCEPTION_NUM, 0xff);
  83. Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectorData, CPU_EXCEPTION_NUM);
  84. if (EFI_ERROR (Status)) {
  85. return EFI_INVALID_PARAMETER;
  86. }
  87. }
  88. //
  89. // Read IDT descriptor and calculate IDT size
  90. //
  91. AsmReadIdtr (&IdtDescriptor);
  92. IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR);
  93. if (IdtEntryCount > CPU_EXCEPTION_NUM) {
  94. //
  95. // CPU exception library only setup CPU_EXCEPTION_NUM exception handler at most
  96. //
  97. IdtEntryCount = CPU_EXCEPTION_NUM;
  98. }
  99. //
  100. // Use current CS as the segment selector of interrupt gate in IDT
  101. //
  102. CodeSegment = AsmReadCs ();
  103. AsmGetTemplateAddressMap (&TemplateMap);
  104. IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtDescriptor.Base;
  105. for (Index = 0; Index < IdtEntryCount; Index++) {
  106. IdtTable[Index].Bits.Selector = CodeSegment;
  107. //
  108. // Check reserved vectors attributes if has, only EFI_VECTOR_HANDOFF_DO_NOT_HOOK
  109. // supported in this instance
  110. //
  111. if (VectorInfo != NULL) {
  112. if (ReservedVectorData[Index].Attribute == EFI_VECTOR_HANDOFF_DO_NOT_HOOK) {
  113. continue;
  114. }
  115. }
  116. //
  117. // Update IDT entry
  118. //
  119. InterruptHandler = TemplateMap.ExceptionStart + Index * TemplateMap.ExceptionStubHeaderSize;
  120. ArchUpdateIdtEntry (&IdtTable[Index], InterruptHandler);
  121. }
  122. return EFI_SUCCESS;
  123. }
  124. /**
  125. Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.
  126. Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
  127. persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
  128. If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
  129. If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
  130. @param[in] VectorInfo Pointer to reserved vector list.
  131. @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized
  132. with default interrupt/exception handlers.
  133. @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
  134. @retval EFI_UNSUPPORTED This function is not supported.
  135. **/
  136. EFI_STATUS
  137. EFIAPI
  138. InitializeCpuInterruptHandlers (
  139. IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
  140. )
  141. {
  142. return EFI_UNSUPPORTED;
  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() or
  151. InitializeCpuInterruptHandlers() invoked, 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. Initializes all CPU exceptions entries with optional extra initializations.
  175. By default, this method should include all functionalities implemented by
  176. InitializeCpuExceptionHandlers(), plus extra initialization works, if any.
  177. This could be done by calling InitializeCpuExceptionHandlers() directly
  178. in this method besides the extra works.
  179. InitData is optional and its use and content are processor arch dependent.
  180. The typical usage of it is to convey resources which have to be reserved
  181. elsewhere and are necessary for the extra initializations of exception.
  182. @param[in] VectorInfo Pointer to reserved vector list.
  183. @param[in] InitData Pointer to data optional for extra initializations
  184. of exception.
  185. @retval EFI_SUCCESS The exceptions have been successfully
  186. initialized.
  187. @retval EFI_INVALID_PARAMETER VectorInfo or InitData contains invalid
  188. content.
  189. **/
  190. EFI_STATUS
  191. EFIAPI
  192. InitializeCpuExceptionHandlersEx (
  193. IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL,
  194. IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL
  195. )
  196. {
  197. return InitializeCpuExceptionHandlers (VectorInfo);
  198. }