DxeException.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /** @file
  2. CPU exception handler library implemenation for DXE modules.
  3. Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <PiDxe.h>
  7. #include "CpuExceptionCommon.h"
  8. #include <Library/DebugLib.h>
  9. #include <Library/MemoryAllocationLib.h>
  10. #include <Library/UefiBootServicesTableLib.h>
  11. CONST UINTN mDoFarReturnFlag = 0;
  12. RESERVED_VECTORS_DATA mReservedVectorsData[CPU_EXCEPTION_NUM];
  13. EFI_CPU_INTERRUPT_HANDLER mExternalInterruptHandlerTable[CPU_EXCEPTION_NUM];
  14. UINTN mEnabledInterruptNum = 0;
  15. EXCEPTION_HANDLER_DATA mExceptionHandlerData;
  16. UINT8 mNewStack[CPU_STACK_SWITCH_EXCEPTION_NUMBER *
  17. CPU_KNOWN_GOOD_STACK_SIZE];
  18. UINT8 mNewGdt[CPU_TSS_GDT_SIZE];
  19. /**
  20. Common exception handler.
  21. @param ExceptionType Exception type.
  22. @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
  23. **/
  24. VOID
  25. EFIAPI
  26. CommonExceptionHandler (
  27. IN EFI_EXCEPTION_TYPE ExceptionType,
  28. IN EFI_SYSTEM_CONTEXT SystemContext
  29. )
  30. {
  31. CommonExceptionHandlerWorker (ExceptionType, SystemContext, &mExceptionHandlerData);
  32. }
  33. /**
  34. Initializes all CPU exceptions entries and provides the default exception handlers.
  35. Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
  36. persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
  37. If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
  38. If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
  39. @param[in] VectorInfo Pointer to reserved vector list.
  40. @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
  41. with default exception handlers.
  42. @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
  43. @retval EFI_UNSUPPORTED This function is not supported.
  44. **/
  45. EFI_STATUS
  46. EFIAPI
  47. InitializeCpuExceptionHandlers (
  48. IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
  49. )
  50. {
  51. mExceptionHandlerData.ReservedVectors = mReservedVectorsData;
  52. mExceptionHandlerData.ExternalInterruptHandler = mExternalInterruptHandlerTable;
  53. InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);
  54. return InitializeCpuExceptionHandlersWorker (VectorInfo, &mExceptionHandlerData);
  55. }
  56. /**
  57. Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.
  58. Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
  59. persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
  60. If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
  61. If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
  62. @param[in] VectorInfo Pointer to reserved vector list.
  63. @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized
  64. with default interrupt/exception handlers.
  65. @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
  66. @retval EFI_UNSUPPORTED This function is not supported.
  67. **/
  68. EFI_STATUS
  69. EFIAPI
  70. InitializeCpuInterruptHandlers (
  71. IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
  72. )
  73. {
  74. EFI_STATUS Status;
  75. IA32_IDT_GATE_DESCRIPTOR *IdtTable;
  76. IA32_DESCRIPTOR IdtDescriptor;
  77. UINTN IdtEntryCount;
  78. EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap;
  79. UINTN Index;
  80. UINTN InterruptEntry;
  81. UINT8 *InterruptEntryCode;
  82. RESERVED_VECTORS_DATA *ReservedVectors;
  83. EFI_CPU_INTERRUPT_HANDLER *ExternalInterruptHandler;
  84. Status = gBS->AllocatePool (
  85. EfiBootServicesCode,
  86. sizeof (RESERVED_VECTORS_DATA) * CPU_INTERRUPT_NUM,
  87. (VOID **)&ReservedVectors
  88. );
  89. ASSERT (!EFI_ERROR (Status) && ReservedVectors != NULL);
  90. SetMem ((VOID *)ReservedVectors, sizeof (RESERVED_VECTORS_DATA) * CPU_INTERRUPT_NUM, 0xff);
  91. if (VectorInfo != NULL) {
  92. Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectors, CPU_INTERRUPT_NUM);
  93. if (EFI_ERROR (Status)) {
  94. FreePool (ReservedVectors);
  95. return EFI_INVALID_PARAMETER;
  96. }
  97. }
  98. ExternalInterruptHandler = AllocateZeroPool (sizeof (EFI_CPU_INTERRUPT_HANDLER) * CPU_INTERRUPT_NUM);
  99. ASSERT (ExternalInterruptHandler != NULL);
  100. //
  101. // Read IDT descriptor and calculate IDT size
  102. //
  103. AsmReadIdtr (&IdtDescriptor);
  104. IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR);
  105. if (IdtEntryCount > CPU_INTERRUPT_NUM) {
  106. IdtEntryCount = CPU_INTERRUPT_NUM;
  107. }
  108. //
  109. // Create Interrupt Descriptor Table and Copy the old IDT table in
  110. //
  111. IdtTable = AllocateZeroPool (sizeof (IA32_IDT_GATE_DESCRIPTOR) * CPU_INTERRUPT_NUM);
  112. ASSERT (IdtTable != NULL);
  113. CopyMem (IdtTable, (VOID *)IdtDescriptor.Base, sizeof (IA32_IDT_GATE_DESCRIPTOR) * IdtEntryCount);
  114. AsmGetTemplateAddressMap (&TemplateMap);
  115. ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);
  116. Status = gBS->AllocatePool (
  117. EfiBootServicesCode,
  118. TemplateMap.ExceptionStubHeaderSize * CPU_INTERRUPT_NUM,
  119. (VOID **)&InterruptEntryCode
  120. );
  121. ASSERT (!EFI_ERROR (Status) && InterruptEntryCode != NULL);
  122. InterruptEntry = (UINTN)InterruptEntryCode;
  123. for (Index = 0; Index < CPU_INTERRUPT_NUM; Index++) {
  124. CopyMem (
  125. (VOID *)InterruptEntry,
  126. (VOID *)TemplateMap.ExceptionStart,
  127. TemplateMap.ExceptionStubHeaderSize
  128. );
  129. AsmVectorNumFixup ((VOID *)InterruptEntry, (UINT8)Index, (VOID *)TemplateMap.ExceptionStart);
  130. InterruptEntry += TemplateMap.ExceptionStubHeaderSize;
  131. }
  132. TemplateMap.ExceptionStart = (UINTN)InterruptEntryCode;
  133. mExceptionHandlerData.IdtEntryCount = CPU_INTERRUPT_NUM;
  134. mExceptionHandlerData.ReservedVectors = ReservedVectors;
  135. mExceptionHandlerData.ExternalInterruptHandler = ExternalInterruptHandler;
  136. InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);
  137. UpdateIdtTable (IdtTable, &TemplateMap, &mExceptionHandlerData);
  138. //
  139. // Load Interrupt Descriptor Table
  140. //
  141. IdtDescriptor.Base = (UINTN)IdtTable;
  142. IdtDescriptor.Limit = (UINT16)(sizeof (IA32_IDT_GATE_DESCRIPTOR) * CPU_INTERRUPT_NUM - 1);
  143. AsmWriteIdtr ((IA32_DESCRIPTOR *)&IdtDescriptor);
  144. return EFI_SUCCESS;
  145. }
  146. /**
  147. Registers a function to be called from the processor interrupt handler.
  148. This function registers and enables the handler specified by InterruptHandler for a processor
  149. interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
  150. handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
  151. The installed handler is called once for each processor interrupt or exception.
  152. NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or
  153. InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.
  154. @param[in] InterruptType Defines which interrupt or exception to hook.
  155. @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
  156. when a processor interrupt occurs. If this parameter is NULL, then the handler
  157. will be uninstalled.
  158. @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
  159. @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
  160. previously installed.
  161. @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
  162. previously installed.
  163. @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
  164. or this function is not supported.
  165. **/
  166. EFI_STATUS
  167. EFIAPI
  168. RegisterCpuInterruptHandler (
  169. IN EFI_EXCEPTION_TYPE InterruptType,
  170. IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
  171. )
  172. {
  173. return RegisterCpuInterruptHandlerWorker (InterruptType, InterruptHandler, &mExceptionHandlerData);
  174. }
  175. /**
  176. Initializes CPU exceptions entries and setup stack switch for given exceptions.
  177. This method will call InitializeCpuExceptionHandlers() to setup default
  178. exception handlers unless indicated not to do it explicitly.
  179. If InitData is passed with NULL, this method will use the resource reserved
  180. by global variables to initialize it; Otherwise it will use data in InitData
  181. to setup stack switch. This is for the different use cases in DxeCore and
  182. Cpu MP exception initialization.
  183. @param[in] VectorInfo Pointer to reserved vector list.
  184. @param[in] InitData Pointer to data required to setup stack switch for
  185. given exceptions.
  186. @retval EFI_SUCCESS The exceptions have been successfully
  187. initialized.
  188. @retval EFI_INVALID_PARAMETER VectorInfo or InitData contains invalid
  189. content.
  190. **/
  191. EFI_STATUS
  192. EFIAPI
  193. InitializeCpuExceptionHandlersEx (
  194. IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL,
  195. IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL
  196. )
  197. {
  198. EFI_STATUS Status;
  199. CPU_EXCEPTION_INIT_DATA EssData;
  200. IA32_DESCRIPTOR Idtr;
  201. IA32_DESCRIPTOR Gdtr;
  202. //
  203. // To avoid repeat initialization of default handlers, the caller should pass
  204. // an extended init data with InitDefaultHandlers set to FALSE. There's no
  205. // need to call this method to just initialize default handlers. Call non-ex
  206. // version instead; or this method must be implemented as a simple wrapper of
  207. // non-ex version of it, if this version has to be called.
  208. //
  209. if ((InitData == NULL) || InitData->X64.InitDefaultHandlers) {
  210. Status = InitializeCpuExceptionHandlers (VectorInfo);
  211. } else {
  212. Status = EFI_SUCCESS;
  213. }
  214. if (!EFI_ERROR (Status)) {
  215. //
  216. // Initializing stack switch is only necessary for Stack Guard functionality.
  217. //
  218. if (PcdGetBool (PcdCpuStackGuard)) {
  219. if (InitData == NULL) {
  220. SetMem (mNewGdt, sizeof (mNewGdt), 0);
  221. AsmReadIdtr (&Idtr);
  222. AsmReadGdtr (&Gdtr);
  223. EssData.X64.Revision = CPU_EXCEPTION_INIT_DATA_REV;
  224. EssData.X64.KnownGoodStackTop = (UINTN)mNewStack + sizeof (mNewStack);
  225. EssData.X64.KnownGoodStackSize = CPU_KNOWN_GOOD_STACK_SIZE;
  226. EssData.X64.StackSwitchExceptions = CPU_STACK_SWITCH_EXCEPTION_LIST;
  227. EssData.X64.StackSwitchExceptionNumber = CPU_STACK_SWITCH_EXCEPTION_NUMBER;
  228. EssData.X64.IdtTable = (VOID *)Idtr.Base;
  229. EssData.X64.IdtTableSize = Idtr.Limit + 1;
  230. EssData.X64.GdtTable = mNewGdt;
  231. EssData.X64.GdtTableSize = sizeof (mNewGdt);
  232. EssData.X64.ExceptionTssDesc = mNewGdt + Gdtr.Limit + 1;
  233. EssData.X64.ExceptionTssDescSize = CPU_TSS_DESC_SIZE;
  234. EssData.X64.ExceptionTss = mNewGdt + Gdtr.Limit + 1 + CPU_TSS_DESC_SIZE;
  235. EssData.X64.ExceptionTssSize = CPU_TSS_SIZE;
  236. InitData = &EssData;
  237. }
  238. Status = ArchSetupExceptionStack (InitData);
  239. }
  240. }
  241. return Status;
  242. }