CpuExceptionCommon.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /** @file
  2. Common header file for CPU Exception Handler Library.
  3. Copyright (c) 2012 - 2019, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #ifndef _CPU_EXCEPTION_COMMON_H_
  7. #define _CPU_EXCEPTION_COMMON_H_
  8. #include <Ppi/VectorHandoffInfo.h>
  9. #include <Protocol/Cpu.h>
  10. #include <Library/BaseLib.h>
  11. #include <Library/SerialPortLib.h>
  12. #include <Library/PrintLib.h>
  13. #include <Library/LocalApicLib.h>
  14. #include <Library/PeCoffGetEntryPointLib.h>
  15. #include <Library/BaseMemoryLib.h>
  16. #include <Library/SynchronizationLib.h>
  17. #include <Library/CpuExceptionHandlerLib.h>
  18. #define CPU_EXCEPTION_NUM 32
  19. #define CPU_INTERRUPT_NUM 256
  20. #define HOOKAFTER_STUB_SIZE 16
  21. //
  22. // Exception Error Code of Page-Fault Exception
  23. //
  24. #define IA32_PF_EC_P BIT0
  25. #define IA32_PF_EC_WR BIT1
  26. #define IA32_PF_EC_US BIT2
  27. #define IA32_PF_EC_RSVD BIT3
  28. #define IA32_PF_EC_ID BIT4
  29. #define IA32_PF_EC_PK BIT5
  30. #define IA32_PF_EC_SS BIT6
  31. #define IA32_PF_EC_SGX BIT15
  32. #include "ArchInterruptDefs.h"
  33. #define CPU_STACK_SWITCH_EXCEPTION_NUMBER \
  34. FixedPcdGetSize (PcdCpuStackSwitchExceptionList)
  35. #define CPU_STACK_SWITCH_EXCEPTION_LIST \
  36. FixedPcdGetPtr (PcdCpuStackSwitchExceptionList)
  37. #define CPU_KNOWN_GOOD_STACK_SIZE \
  38. FixedPcdGet32 (PcdCpuKnownGoodStackSize)
  39. #define CPU_TSS_GDT_SIZE (SIZE_2KB + CPU_TSS_DESC_SIZE + CPU_TSS_SIZE)
  40. //
  41. // Record exception handler information
  42. //
  43. typedef struct {
  44. UINTN ExceptionStart;
  45. UINTN ExceptionStubHeaderSize;
  46. UINTN HookAfterStubHeaderStart;
  47. } EXCEPTION_HANDLER_TEMPLATE_MAP;
  48. typedef struct {
  49. UINTN IdtEntryCount;
  50. SPIN_LOCK DisplayMessageSpinLock;
  51. RESERVED_VECTORS_DATA *ReservedVectors;
  52. EFI_CPU_INTERRUPT_HANDLER *ExternalInterruptHandler;
  53. } EXCEPTION_HANDLER_DATA;
  54. extern CONST UINT32 mErrorCodeFlag;
  55. extern CONST UINTN mDoFarReturnFlag;
  56. /**
  57. Return address map of exception handler template so that C code can generate
  58. exception tables.
  59. @param AddressMap Pointer to a buffer where the address map is returned.
  60. **/
  61. VOID
  62. EFIAPI
  63. AsmGetTemplateAddressMap (
  64. OUT EXCEPTION_HANDLER_TEMPLATE_MAP *AddressMap
  65. );
  66. /**
  67. Return address map of exception handler template so that C code can generate
  68. exception tables.
  69. @param IdtEntry Pointer to IDT entry to be updated.
  70. @param InterruptHandler IDT handler value.
  71. **/
  72. VOID
  73. ArchUpdateIdtEntry (
  74. OUT IA32_IDT_GATE_DESCRIPTOR *IdtEntry,
  75. IN UINTN InterruptHandler
  76. );
  77. /**
  78. Read IDT handler value from IDT entry.
  79. @param IdtEntry Pointer to IDT entry to be read.
  80. **/
  81. UINTN
  82. ArchGetIdtHandler (
  83. IN IA32_IDT_GATE_DESCRIPTOR *IdtEntry
  84. );
  85. /**
  86. Prints a message to the serial port.
  87. @param Format Format string for the message to print.
  88. @param ... Variable argument list whose contents are accessed
  89. based on the format string specified by Format.
  90. **/
  91. VOID
  92. EFIAPI
  93. InternalPrintMessage (
  94. IN CONST CHAR8 *Format,
  95. ...
  96. );
  97. /**
  98. Find and display image base address and return image base and its entry point.
  99. @param CurrentEip Current instruction pointer.
  100. **/
  101. VOID
  102. DumpModuleImageInfo (
  103. IN UINTN CurrentEip
  104. );
  105. /**
  106. Display CPU information.
  107. @param ExceptionType Exception type.
  108. @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
  109. **/
  110. VOID
  111. DumpImageAndCpuContent (
  112. IN EFI_EXCEPTION_TYPE ExceptionType,
  113. IN EFI_SYSTEM_CONTEXT SystemContext
  114. );
  115. /**
  116. Internal worker function to initialize exception handler.
  117. @param[in] VectorInfo Pointer to reserved vector list.
  118. @param[in, out] ExceptionHandlerData Pointer to exception handler data.
  119. @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
  120. with default exception handlers.
  121. @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
  122. @retval EFI_UNSUPPORTED This function is not supported.
  123. **/
  124. EFI_STATUS
  125. InitializeCpuExceptionHandlersWorker (
  126. IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL,
  127. IN OUT EXCEPTION_HANDLER_DATA *ExceptionHandlerData
  128. );
  129. /**
  130. Registers a function to be called from the processor interrupt handler.
  131. @param[in] InterruptType Defines which interrupt or exception to hook.
  132. @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
  133. when a processor interrupt occurs. If this parameter is NULL, then the handler
  134. will be uninstalled
  135. @param[in] ExceptionHandlerData Pointer to exception handler data.
  136. @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
  137. @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
  138. previously installed.
  139. @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
  140. previously installed.
  141. @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
  142. or this function is not supported.
  143. **/
  144. EFI_STATUS
  145. RegisterCpuInterruptHandlerWorker (
  146. IN EFI_EXCEPTION_TYPE InterruptType,
  147. IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler,
  148. IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData
  149. );
  150. /**
  151. Internal worker function to update IDT entries accordling to vector attributes.
  152. @param[in] IdtTable Pointer to IDT table.
  153. @param[in] TemplateMap Pointer to a buffer where the address map is
  154. returned.
  155. @param[in] ExceptionHandlerData Pointer to exception handler data.
  156. **/
  157. VOID
  158. UpdateIdtTable (
  159. IN IA32_IDT_GATE_DESCRIPTOR *IdtTable,
  160. IN EXCEPTION_HANDLER_TEMPLATE_MAP *TemplateMap,
  161. IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData
  162. );
  163. /**
  164. Save CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.
  165. @param[in] ExceptionType Exception type.
  166. @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT.
  167. @param[in] ExceptionHandlerData Pointer to exception handler data.
  168. **/
  169. VOID
  170. ArchSaveExceptionContext (
  171. IN UINTN ExceptionType,
  172. IN EFI_SYSTEM_CONTEXT SystemContext,
  173. IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData
  174. );
  175. /**
  176. Restore CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.
  177. @param[in] ExceptionType Exception type.
  178. @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT.
  179. @param[in] ExceptionHandlerData Pointer to exception handler data.
  180. **/
  181. VOID
  182. ArchRestoreExceptionContext (
  183. IN UINTN ExceptionType,
  184. IN EFI_SYSTEM_CONTEXT SystemContext,
  185. IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData
  186. );
  187. /**
  188. Fix up the vector number and function address in the vector code.
  189. @param[in] NewVectorAddr New vector handler address.
  190. @param[in] VectorNum Index of vector.
  191. @param[in] OldVectorAddr Old vector handler address.
  192. **/
  193. VOID
  194. EFIAPI
  195. AsmVectorNumFixup (
  196. IN VOID *NewVectorAddr,
  197. IN UINT8 VectorNum,
  198. IN VOID *OldVectorAddr
  199. );
  200. /**
  201. Read and save reserved vector information
  202. @param[in] VectorInfo Pointer to reserved vector list.
  203. @param[out] ReservedVector Pointer to reserved vector data buffer.
  204. @param[in] VectorCount Vector number to be updated.
  205. @return EFI_SUCCESS Read and save vector info successfully.
  206. @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
  207. **/
  208. EFI_STATUS
  209. ReadAndVerifyVectorInfo (
  210. IN EFI_VECTOR_HANDOFF_INFO *VectorInfo,
  211. OUT RESERVED_VECTORS_DATA *ReservedVector,
  212. IN UINTN VectorCount
  213. );
  214. /**
  215. Get ASCII format string exception name by exception type.
  216. @param ExceptionType Exception type.
  217. @return ASCII format string exception name.
  218. **/
  219. CONST CHAR8 *
  220. GetExceptionNameStr (
  221. IN EFI_EXCEPTION_TYPE ExceptionType
  222. );
  223. /**
  224. Internal worker function for common exception handler.
  225. @param ExceptionType Exception type.
  226. @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
  227. @param ExceptionHandlerData Pointer to exception handler data.
  228. **/
  229. VOID
  230. CommonExceptionHandlerWorker (
  231. IN EFI_EXCEPTION_TYPE ExceptionType,
  232. IN EFI_SYSTEM_CONTEXT SystemContext,
  233. IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData
  234. );
  235. /**
  236. Setup separate stack for specific exceptions.
  237. @param[in] StackSwitchData Pointer to data required for setuping up
  238. stack switch.
  239. @retval EFI_SUCCESS The exceptions have been successfully
  240. initialized with new stack.
  241. @retval EFI_INVALID_PARAMETER StackSwitchData contains invalid content.
  242. **/
  243. EFI_STATUS
  244. ArchSetupExceptionStack (
  245. IN CPU_EXCEPTION_INIT_DATA *StackSwitchData
  246. );
  247. /**
  248. Return address map of exception handler template so that C code can generate
  249. exception tables. The template is only for exceptions using task gate instead
  250. of interrupt gate.
  251. @param AddressMap Pointer to a buffer where the address map is returned.
  252. **/
  253. VOID
  254. EFIAPI
  255. AsmGetTssTemplateMap (
  256. OUT EXCEPTION_HANDLER_TEMPLATE_MAP *AddressMap
  257. );
  258. #endif