PeiCpuException.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /** @file
  2. CPU exception handler library implementation for PEIM module.
  3. Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <PiPei.h>
  7. #include "CpuExceptionCommon.h"
  8. #include <Library/DebugLib.h>
  9. #include <Library/HobLib.h>
  10. #include <Library/MemoryAllocationLib.h>
  11. #include <Library/PcdLib.h>
  12. CONST UINTN mDoFarReturnFlag = 0;
  13. typedef struct {
  14. UINT8 ExceptionStubHeader[HOOKAFTER_STUB_SIZE];
  15. EXCEPTION_HANDLER_DATA *ExceptionHandlerData;
  16. } EXCEPTION0_STUB_HEADER;
  17. /**
  18. Get exception handler data pointer from IDT[0].
  19. The exception #0 stub header is duplicated in an allocated pool with extra 4-byte/8-byte to store the
  20. exception handler data. The new allocated memory layout follows structure EXCEPTION0_STUB_HEADER.
  21. The code assumes that all processors uses the same exception handler for #0 exception.
  22. @return pointer to exception handler data.
  23. **/
  24. EXCEPTION_HANDLER_DATA *
  25. GetExceptionHandlerData (
  26. VOID
  27. )
  28. {
  29. IA32_DESCRIPTOR IdtDescriptor;
  30. IA32_IDT_GATE_DESCRIPTOR *IdtTable;
  31. EXCEPTION0_STUB_HEADER *Exception0StubHeader;
  32. AsmReadIdtr (&IdtDescriptor);
  33. IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtDescriptor.Base;
  34. Exception0StubHeader = (EXCEPTION0_STUB_HEADER *)ArchGetIdtHandler (&IdtTable[0]);
  35. return Exception0StubHeader->ExceptionHandlerData;
  36. }
  37. /**
  38. Set exception handler data pointer to IDT[0].
  39. The exception #0 stub header is duplicated in an allocated pool with extra 4-byte/8-byte to store the
  40. exception handler data. The new allocated memory layout follows structure EXCEPTION0_STUB_HEADER.
  41. The code assumes that all processors uses the same exception handler for #0 exception.
  42. @param ExceptionHandlerData pointer to exception handler data.
  43. **/
  44. VOID
  45. SetExceptionHandlerData (
  46. IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData
  47. )
  48. {
  49. EXCEPTION0_STUB_HEADER *Exception0StubHeader;
  50. IA32_DESCRIPTOR IdtDescriptor;
  51. IA32_IDT_GATE_DESCRIPTOR *IdtTable;
  52. //
  53. // Duplicate the exception #0 stub header in pool and cache the ExceptionHandlerData just after the stub header.
  54. // So AP can get the ExceptionHandlerData by reading the IDT[0].
  55. //
  56. AsmReadIdtr (&IdtDescriptor);
  57. IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtDescriptor.Base;
  58. Exception0StubHeader = AllocatePool (sizeof (*Exception0StubHeader));
  59. ASSERT (Exception0StubHeader != NULL);
  60. CopyMem (
  61. Exception0StubHeader->ExceptionStubHeader,
  62. (VOID *)ArchGetIdtHandler (&IdtTable[0]),
  63. sizeof (Exception0StubHeader->ExceptionStubHeader)
  64. );
  65. Exception0StubHeader->ExceptionHandlerData = ExceptionHandlerData;
  66. ArchUpdateIdtEntry (&IdtTable[0], (UINTN)Exception0StubHeader->ExceptionStubHeader);
  67. }
  68. /**
  69. Common exception handler.
  70. @param ExceptionType Exception type.
  71. @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
  72. **/
  73. VOID
  74. EFIAPI
  75. CommonExceptionHandler (
  76. IN EFI_EXCEPTION_TYPE ExceptionType,
  77. IN EFI_SYSTEM_CONTEXT SystemContext
  78. )
  79. {
  80. EXCEPTION_HANDLER_DATA *ExceptionHandlerData;
  81. ExceptionHandlerData = GetExceptionHandlerData ();
  82. CommonExceptionHandlerWorker (ExceptionType, SystemContext, ExceptionHandlerData);
  83. }
  84. /**
  85. Initializes all CPU exceptions entries and provides the default exception handlers.
  86. Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
  87. persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
  88. If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
  89. If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
  90. Note: Before invoking this API, caller must allocate memory for IDT table and load
  91. IDTR by AsmWriteIdtr().
  92. @param[in] VectorInfo Pointer to reserved vector list.
  93. @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
  94. with default exception handlers.
  95. @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
  96. @retval EFI_UNSUPPORTED This function is not supported.
  97. **/
  98. EFI_STATUS
  99. EFIAPI
  100. InitializeCpuExceptionHandlers (
  101. IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
  102. )
  103. {
  104. EFI_STATUS Status;
  105. EXCEPTION_HANDLER_DATA *ExceptionHandlerData;
  106. RESERVED_VECTORS_DATA *ReservedVectors;
  107. ReservedVectors = AllocatePool (sizeof (RESERVED_VECTORS_DATA) * CPU_EXCEPTION_NUM);
  108. ASSERT (ReservedVectors != NULL);
  109. ExceptionHandlerData = AllocatePool (sizeof (EXCEPTION_HANDLER_DATA));
  110. ASSERT (ExceptionHandlerData != NULL);
  111. ExceptionHandlerData->ReservedVectors = ReservedVectors;
  112. ExceptionHandlerData->ExternalInterruptHandler = NULL;
  113. InitializeSpinLock (&ExceptionHandlerData->DisplayMessageSpinLock);
  114. Status = InitializeCpuExceptionHandlersWorker (VectorInfo, ExceptionHandlerData);
  115. if (EFI_ERROR (Status)) {
  116. FreePool (ReservedVectors);
  117. FreePool (ExceptionHandlerData);
  118. return Status;
  119. }
  120. SetExceptionHandlerData (ExceptionHandlerData);
  121. return EFI_SUCCESS;
  122. }
  123. /**
  124. Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.
  125. Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
  126. persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
  127. If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
  128. If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
  129. @param[in] VectorInfo Pointer to reserved vector list.
  130. @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized
  131. with default interrupt/exception handlers.
  132. @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
  133. @retval EFI_UNSUPPORTED This function is not supported.
  134. **/
  135. EFI_STATUS
  136. EFIAPI
  137. InitializeCpuInterruptHandlers (
  138. IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
  139. )
  140. {
  141. return EFI_UNSUPPORTED;
  142. }
  143. /**
  144. Registers a function to be called from the processor interrupt handler.
  145. This function registers and enables the handler specified by InterruptHandler for a processor
  146. interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
  147. handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
  148. The installed handler is called once for each processor interrupt or exception.
  149. NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or
  150. InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.
  151. @param[in] InterruptType Defines which interrupt or exception to hook.
  152. @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
  153. when a processor interrupt occurs. If this parameter is NULL, then the handler
  154. will be uninstalled.
  155. @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
  156. @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
  157. previously installed.
  158. @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
  159. previously installed.
  160. @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
  161. or this function is not supported.
  162. **/
  163. EFI_STATUS
  164. EFIAPI
  165. RegisterCpuInterruptHandler (
  166. IN EFI_EXCEPTION_TYPE InterruptType,
  167. IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
  168. )
  169. {
  170. return EFI_UNSUPPORTED;
  171. }
  172. /**
  173. Initializes all CPU exceptions entries with optional extra initializations.
  174. By default, this method should include all functionalities implemented by
  175. InitializeCpuExceptionHandlers(), plus extra initialization works, if any.
  176. This could be done by calling InitializeCpuExceptionHandlers() directly
  177. in this method besides the extra works.
  178. InitData is optional and its use and content are processor arch dependent.
  179. The typical usage of it is to convey resources which have to be reserved
  180. elsewhere and are necessary for the extra initializations of exception.
  181. @param[in] VectorInfo Pointer to reserved vector list.
  182. @param[in] InitData Pointer to data optional for extra initializations
  183. of exception.
  184. @retval EFI_SUCCESS The exceptions have been successfully
  185. initialized.
  186. @retval EFI_INVALID_PARAMETER VectorInfo or InitData contains invalid
  187. content.
  188. **/
  189. EFI_STATUS
  190. EFIAPI
  191. InitializeCpuExceptionHandlersEx (
  192. IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL,
  193. IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL
  194. )
  195. {
  196. EFI_STATUS Status;
  197. //
  198. // To avoid repeat initialization of default handlers, the caller should pass
  199. // an extended init data with InitDefaultHandlers set to FALSE. There's no
  200. // need to call this method to just initialize default handlers. Call non-ex
  201. // version instead; or this method must be implemented as a simple wrapper of
  202. // non-ex version of it, if this version has to be called.
  203. //
  204. if ((InitData == NULL) || InitData->Ia32.InitDefaultHandlers) {
  205. Status = InitializeCpuExceptionHandlers (VectorInfo);
  206. } else {
  207. Status = EFI_SUCCESS;
  208. }
  209. if (!EFI_ERROR (Status)) {
  210. //
  211. // Initializing stack switch is only necessary for Stack Guard functionality.
  212. //
  213. if (PcdGetBool (PcdCpuStackGuard) && (InitData != NULL)) {
  214. Status = ArchSetupExceptionStack (InitData);
  215. }
  216. }
  217. return Status;
  218. }