CpuExceptionHandlerLib.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/BaseLib.h>
  10. #include <Register/RiscV64/RiscVEncoding.h>
  11. #include "CpuExceptionHandlerLib.h"
  12. STATIC EFI_CPU_INTERRUPT_HANDLER mInterruptHandlers[2];
  13. /**
  14. Initializes all CPU exceptions entries and provides the default exception handlers.
  15. Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
  16. persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
  17. If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
  18. If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
  19. @param[in] VectorInfo Pointer to reserved vector list.
  20. @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
  21. with default exception handlers.
  22. @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
  23. @retval EFI_UNSUPPORTED This function is not supported.
  24. **/
  25. EFI_STATUS
  26. EFIAPI
  27. InitializeCpuExceptionHandlers (
  28. IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
  29. )
  30. {
  31. RiscVSetSupervisorStvec ((UINT64)SupervisorModeTrap);
  32. return EFI_SUCCESS;
  33. }
  34. /**
  35. Registers a function to be called from the processor interrupt handler.
  36. This function registers and enables the handler specified by InterruptHandler for a processor
  37. interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
  38. handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
  39. The installed handler is called once for each processor interrupt or exception.
  40. NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or
  41. InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.
  42. @param[in] InterruptType Defines which interrupt or exception to hook.
  43. @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
  44. when a processor interrupt occurs. If this parameter is NULL, then the handler
  45. will be uninstalled.
  46. @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
  47. @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
  48. previously installed.
  49. @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
  50. previously installed.
  51. @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
  52. or this function is not supported.
  53. **/
  54. EFI_STATUS
  55. EFIAPI
  56. RegisterCpuInterruptHandler (
  57. IN EFI_EXCEPTION_TYPE InterruptType,
  58. IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
  59. )
  60. {
  61. DEBUG ((DEBUG_INFO, "%a: Type:%x Handler: %x\n", __FUNCTION__, InterruptType, InterruptHandler));
  62. mInterruptHandlers[InterruptType] = InterruptHandler;
  63. return EFI_SUCCESS;
  64. }
  65. /**
  66. Setup separate stacks for certain exception handlers.
  67. If the input Buffer and BufferSize are both NULL, use global variable if possible.
  68. @param[in] Buffer Point to buffer used to separate exception stack.
  69. @param[in, out] BufferSize On input, it indicates the byte size of Buffer.
  70. If the size is not enough, the return status will
  71. be EFI_BUFFER_TOO_SMALL, and output BufferSize
  72. will be the size it needs.
  73. @retval EFI_SUCCESS The stacks are assigned successfully.
  74. @retval EFI_UNSUPPORTED This function is not supported.
  75. @retval EFI_BUFFER_TOO_SMALL This BufferSize is too small.
  76. **/
  77. EFI_STATUS
  78. EFIAPI
  79. InitializeSeparateExceptionStacks (
  80. IN VOID *Buffer,
  81. IN OUT UINTN *BufferSize
  82. )
  83. {
  84. return EFI_SUCCESS;
  85. }
  86. /**
  87. Supervisor 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)RiscVGetSupervisorTrapCause ();
  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 == IRQ_S_TIMER) && (mInterruptHandlers[EXCEPT_RISCV_TIMER_INT] != NULL)) {
  108. mInterruptHandlers[EXCEPT_RISCV_TIMER_INT](EXCEPT_RISCV_TIMER_INT, RiscVSystemContext);
  109. }
  110. }
  111. }