CpuExceptionHandlerLib.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /** @file
  2. RISC-V Exception Handler library implementation.
  3. Copyright (c) 2016 - 2022, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <PiPei.h>
  7. #include <Library/CpuExceptionHandlerLib.h>
  8. #include <Library/DebugLib.h>
  9. #include <Library/RiscVCpuLib.h>
  10. #include <sbi/riscv_asm.h>
  11. #include <sbi/riscv_encoding.h>
  12. #include <sbi/sbi_types.h>
  13. #include "CpuExceptionHandlerLib.h"
  14. STATIC EFI_CPU_INTERRUPT_HANDLER mInterruptHandlers[2];
  15. /**
  16. Initializes all CPU exceptions entries and provides the default exception handlers.
  17. Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
  18. persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
  19. If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
  20. If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
  21. @param[in] VectorInfo Pointer to reserved vector list.
  22. @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
  23. with default exception handlers.
  24. @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
  25. @retval EFI_UNSUPPORTED This function is not supported.
  26. **/
  27. EFI_STATUS
  28. EFIAPI
  29. InitializeCpuExceptionHandlers (
  30. IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
  31. )
  32. {
  33. return EFI_SUCCESS;
  34. }
  35. /**
  36. Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.
  37. Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
  38. persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
  39. If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
  40. If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
  41. @param[in] VectorInfo Pointer to reserved vector list.
  42. @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized
  43. with default interrupt/exception handlers.
  44. @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
  45. @retval EFI_UNSUPPORTED This function is not supported.
  46. **/
  47. EFI_STATUS
  48. EFIAPI
  49. InitializeCpuInterruptHandlers (
  50. IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
  51. )
  52. {
  53. return EFI_SUCCESS;
  54. }
  55. /**
  56. Registers a function to be called from the processor interrupt handler.
  57. This function registers and enables the handler specified by InterruptHandler for a processor
  58. interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
  59. handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
  60. The installed handler is called once for each processor interrupt or exception.
  61. NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or
  62. InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.
  63. @param[in] InterruptType Defines which interrupt or exception to hook.
  64. @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
  65. when a processor interrupt occurs. If this parameter is NULL, then the handler
  66. will be uninstalled.
  67. @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
  68. @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
  69. previously installed.
  70. @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
  71. previously installed.
  72. @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
  73. or this function is not supported.
  74. **/
  75. EFI_STATUS
  76. EFIAPI
  77. RegisterCpuInterruptHandler (
  78. IN EFI_EXCEPTION_TYPE InterruptType,
  79. IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
  80. )
  81. {
  82. DEBUG ((DEBUG_INFO, "%a: Type:%x Handler: %x\n", __FUNCTION__, InterruptType, InterruptHandler));
  83. mInterruptHandlers[InterruptType] = InterruptHandler;
  84. return EFI_SUCCESS;
  85. }
  86. /**
  87. Machine mode trap handler.
  88. @param[in] SmodeTrapReg Registers before trap occurred.
  89. **/
  90. VOID
  91. RiscVSupervisorModeTrapHandler (
  92. SMODE_TRAP_REGISTERS *SmodeTrapReg
  93. )
  94. {
  95. UINTN SCause;
  96. EFI_SYSTEM_CONTEXT RiscVSystemContext;
  97. RiscVSystemContext.SystemContextRiscV64 = (EFI_SYSTEM_CONTEXT_RISCV64 *)SmodeTrapReg;
  98. //
  99. // Check scasue register.
  100. //
  101. SCause = (UINTN)csr_read (RISCV_CSR_SUPERVISOR_SCAUSE);
  102. if ((SCause & (1UL << (sizeof (UINTN) * 8- 1))) != 0) {
  103. //
  104. // This is interrupt event.
  105. //
  106. SCause &= ~(1UL << (sizeof (UINTN) * 8- 1));
  107. if ((SCause == SCAUSE_SUPERVISOR_TIMER_INT) && (mInterruptHandlers[EXCEPT_RISCV_TIMER_INT] != NULL)) {
  108. mInterruptHandlers[EXCEPT_RISCV_TIMER_INT](EXCEPT_RISCV_TIMER_INT, RiscVSystemContext);
  109. }
  110. }
  111. }
  112. /**
  113. Initializes all CPU exceptions entries with optional extra initializations.
  114. By default, this method should include all functionalities implemented by
  115. InitializeCpuExceptionHandlers(), plus extra initialization works, if any.
  116. This could be done by calling InitializeCpuExceptionHandlers() directly
  117. in this method besides the extra works.
  118. InitData is optional and its use and content are processor arch dependent.
  119. The typical usage of it is to convey resources which have to be reserved
  120. elsewhere and are necessary for the extra initializations of exception.
  121. @param[in] VectorInfo Pointer to reserved vector list.
  122. @param[in] InitData Pointer to data optional for extra initializations
  123. of exception.
  124. @retval EFI_SUCCESS The exceptions have been successfully
  125. initialized.
  126. @retval EFI_INVALID_PARAMETER VectorInfo or InitData contains invalid
  127. content.
  128. @retval EFI_UNSUPPORTED This function is not supported.
  129. **/
  130. EFI_STATUS
  131. EFIAPI
  132. InitializeCpuExceptionHandlersEx (
  133. IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL,
  134. IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL
  135. )
  136. {
  137. return InitializeCpuExceptionHandlers (VectorInfo);
  138. }
  139. /**
  140. The constructor function to initial interrupt handlers in
  141. RISCV_MACHINE_MODE_CONTEXT.
  142. @param ImageHandle The firmware allocated handle for the EFI image.
  143. @param SystemTable A pointer to the EFI System Table.
  144. @retval EFI_SUCCESS The destructor completed successfully.
  145. @retval Other value The destructor did not complete successfully.
  146. **/
  147. EFI_STATUS
  148. EFIAPI
  149. CpuExceptionHandlerLibConstructor (
  150. IN EFI_HANDLE ImageHandle,
  151. IN EFI_SYSTEM_TABLE *SystemTable
  152. )
  153. {
  154. //
  155. // Set Supervisor mode trap handler.
  156. //
  157. csr_write (CSR_STVEC, SupervisorModeTrap);
  158. return EFI_SUCCESS;
  159. }