]>
Commit | Line | Data |
---|---|---|
cbac2c74 S |
1 | /** @file\r |
2 | RISC-V Exception Handler library implementation.\r | |
3 | \r | |
4 | Copyright (c) 2016 - 2022, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>\r | |
5 | \r | |
6 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
7 | \r | |
8 | **/\r | |
9 | \r | |
10 | #include <PiPei.h>\r | |
11 | #include <Library/CpuExceptionHandlerLib.h>\r | |
12 | #include <Library/DebugLib.h>\r | |
13 | #include <Library/BaseLib.h>\r | |
14 | #include <Register/RiscV64/RiscVEncoding.h>\r | |
15 | \r | |
16 | #include "CpuExceptionHandlerLib.h"\r | |
17 | \r | |
18 | STATIC EFI_CPU_INTERRUPT_HANDLER mInterruptHandlers[2];\r | |
19 | \r | |
20 | /**\r | |
21 | Initializes all CPU exceptions entries and provides the default exception handlers.\r | |
22 | \r | |
23 | Caller should try to get an array of interrupt and/or exception vectors that are in use and need to\r | |
24 | persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.\r | |
25 | If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.\r | |
26 | If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.\r | |
27 | \r | |
28 | @param[in] VectorInfo Pointer to reserved vector list.\r | |
29 | \r | |
30 | @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized\r | |
31 | with default exception handlers.\r | |
32 | @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r | |
33 | @retval EFI_UNSUPPORTED This function is not supported.\r | |
34 | \r | |
35 | **/\r | |
36 | EFI_STATUS\r | |
37 | EFIAPI\r | |
38 | InitializeCpuExceptionHandlers (\r | |
39 | IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL\r | |
40 | )\r | |
41 | {\r | |
42 | RiscVSetSupervisorStvec ((UINT64)SupervisorModeTrap);\r | |
43 | return EFI_SUCCESS;\r | |
44 | }\r | |
45 | \r | |
46 | /**\r | |
47 | Registers a function to be called from the processor interrupt handler.\r | |
48 | \r | |
49 | This function registers and enables the handler specified by InterruptHandler for a processor\r | |
50 | interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the\r | |
51 | handler for the processor interrupt or exception type specified by InterruptType is uninstalled.\r | |
52 | The installed handler is called once for each processor interrupt or exception.\r | |
53 | NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or\r | |
54 | InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.\r | |
55 | \r | |
56 | @param[in] InterruptType Defines which interrupt or exception to hook.\r | |
57 | @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called\r | |
58 | when a processor interrupt occurs. If this parameter is NULL, then the handler\r | |
59 | will be uninstalled.\r | |
60 | \r | |
61 | @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r | |
62 | @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was\r | |
63 | previously installed.\r | |
64 | @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not\r | |
65 | previously installed.\r | |
66 | @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,\r | |
67 | or this function is not supported.\r | |
68 | **/\r | |
69 | EFI_STATUS\r | |
70 | EFIAPI\r | |
71 | RegisterCpuInterruptHandler (\r | |
72 | IN EFI_EXCEPTION_TYPE InterruptType,\r | |
73 | IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r | |
74 | )\r | |
75 | {\r | |
76 | DEBUG ((DEBUG_INFO, "%a: Type:%x Handler: %x\n", __FUNCTION__, InterruptType, InterruptHandler));\r | |
77 | mInterruptHandlers[InterruptType] = InterruptHandler;\r | |
78 | return EFI_SUCCESS;\r | |
79 | }\r | |
80 | \r | |
81 | /**\r | |
82 | Setup separate stacks for certain exception handlers.\r | |
83 | If the input Buffer and BufferSize are both NULL, use global variable if possible.\r | |
84 | \r | |
85 | @param[in] Buffer Point to buffer used to separate exception stack.\r | |
86 | @param[in, out] BufferSize On input, it indicates the byte size of Buffer.\r | |
87 | If the size is not enough, the return status will\r | |
88 | be EFI_BUFFER_TOO_SMALL, and output BufferSize\r | |
89 | will be the size it needs.\r | |
90 | \r | |
91 | @retval EFI_SUCCESS The stacks are assigned successfully.\r | |
92 | @retval EFI_UNSUPPORTED This function is not supported.\r | |
93 | @retval EFI_BUFFER_TOO_SMALL This BufferSize is too small.\r | |
94 | **/\r | |
95 | EFI_STATUS\r | |
96 | EFIAPI\r | |
97 | InitializeSeparateExceptionStacks (\r | |
98 | IN VOID *Buffer,\r | |
99 | IN OUT UINTN *BufferSize\r | |
100 | )\r | |
101 | {\r | |
102 | return EFI_SUCCESS;\r | |
103 | }\r | |
104 | \r | |
105 | /**\r | |
106 | Supervisor mode trap handler.\r | |
107 | \r | |
108 | @param[in] SmodeTrapReg Registers before trap occurred.\r | |
109 | \r | |
110 | **/\r | |
111 | VOID\r | |
112 | RiscVSupervisorModeTrapHandler (\r | |
113 | SMODE_TRAP_REGISTERS *SmodeTrapReg\r | |
114 | )\r | |
115 | {\r | |
116 | UINTN SCause;\r | |
117 | EFI_SYSTEM_CONTEXT RiscVSystemContext;\r | |
118 | \r | |
119 | RiscVSystemContext.SystemContextRiscV64 = (EFI_SYSTEM_CONTEXT_RISCV64 *)SmodeTrapReg;\r | |
120 | //\r | |
121 | // Check scasue register.\r | |
122 | //\r | |
123 | SCause = (UINTN)RiscVGetSupervisorTrapCause ();\r | |
124 | if ((SCause & (1UL << (sizeof (UINTN) * 8- 1))) != 0) {\r | |
125 | //\r | |
126 | // This is interrupt event.\r | |
127 | //\r | |
128 | SCause &= ~(1UL << (sizeof (UINTN) * 8- 1));\r | |
129 | if ((SCause == IRQ_S_TIMER) && (mInterruptHandlers[EXCEPT_RISCV_TIMER_INT] != NULL)) {\r | |
130 | mInterruptHandlers[EXCEPT_RISCV_TIMER_INT](EXCEPT_RISCV_TIMER_INT, RiscVSystemContext);\r | |
131 | }\r | |
132 | }\r | |
133 | }\r |