]>
Commit | Line | Data |
---|---|---|
a81abf16 JF |
1 | /** @file\r |
2 | CPU exception handler library implementation for PEIM module.\r | |
3 | \r | |
374168ae | 4 | Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>\r |
0acd8697 | 5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
a81abf16 JF |
6 | \r |
7 | **/\r | |
8 | \r | |
9 | #include <PiPei.h>\r | |
10 | #include "CpuExceptionCommon.h"\r | |
11 | #include <Library/DebugLib.h>\r | |
12 | #include <Library/HobLib.h>\r | |
13 | #include <Library/MemoryAllocationLib.h>\r | |
fc0e7fd5 | 14 | #include <Library/PcdLib.h>\r |
a81abf16 | 15 | \r |
a81abf16 JF |
16 | CONST UINTN mDoFarReturnFlag = 0;\r |
17 | \r | |
374168ae RN |
18 | typedef struct {\r |
19 | UINT8 ExceptionStubHeader[HOOKAFTER_STUB_SIZE];\r | |
20 | EXCEPTION_HANDLER_DATA *ExceptionHandlerData;\r | |
21 | } EXCEPTION0_STUB_HEADER;\r | |
a81abf16 JF |
22 | \r |
23 | /**\r | |
374168ae RN |
24 | Get exception handler data pointer from IDT[0].\r |
25 | \r | |
26 | The exception #0 stub header is duplicated in an allocated pool with extra 4-byte/8-byte to store the\r | |
27 | exception handler data. The new allocated memory layout follows structure EXCEPTION0_STUB_HEADER.\r | |
28 | The code assumes that all processors uses the same exception handler for #0 exception.\r | |
a81abf16 | 29 | \r |
dd563742 | 30 | @return pointer to exception handler data.\r |
a81abf16 JF |
31 | **/\r |
32 | EXCEPTION_HANDLER_DATA *\r | |
33 | GetExceptionHandlerData (\r | |
34 | VOID\r | |
35 | )\r | |
36 | {\r | |
374168ae RN |
37 | IA32_DESCRIPTOR IdtDescriptor;\r |
38 | IA32_IDT_GATE_DESCRIPTOR *IdtTable;\r | |
39 | EXCEPTION0_STUB_HEADER *Exception0StubHeader;\r | |
40 | \r | |
41 | AsmReadIdtr (&IdtDescriptor);\r | |
42 | IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtDescriptor.Base;\r | |
34c3405c | 43 | \r |
374168ae RN |
44 | Exception0StubHeader = (EXCEPTION0_STUB_HEADER *)ArchGetIdtHandler (&IdtTable[0]);\r |
45 | return Exception0StubHeader->ExceptionHandlerData;\r | |
46 | }\r | |
a81abf16 | 47 | \r |
374168ae RN |
48 | /**\r |
49 | Set exception handler data pointer to IDT[0].\r | |
50 | \r | |
51 | The exception #0 stub header is duplicated in an allocated pool with extra 4-byte/8-byte to store the\r | |
52 | exception handler data. The new allocated memory layout follows structure EXCEPTION0_STUB_HEADER.\r | |
53 | The code assumes that all processors uses the same exception handler for #0 exception.\r | |
54 | \r | |
34c3405c | 55 | @param ExceptionHandlerData pointer to exception handler data.\r |
374168ae RN |
56 | **/\r |
57 | VOID\r | |
58 | SetExceptionHandlerData (\r | |
59 | IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData\r | |
60 | )\r | |
61 | {\r | |
62 | EXCEPTION0_STUB_HEADER *Exception0StubHeader;\r | |
63 | IA32_DESCRIPTOR IdtDescriptor;\r | |
64 | IA32_IDT_GATE_DESCRIPTOR *IdtTable;\r | |
65 | //\r | |
66 | // Duplicate the exception #0 stub header in pool and cache the ExceptionHandlerData just after the stub header.\r | |
67 | // So AP can get the ExceptionHandlerData by reading the IDT[0].\r | |
68 | //\r | |
69 | AsmReadIdtr (&IdtDescriptor);\r | |
70 | IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtDescriptor.Base;\r | |
34c3405c | 71 | \r |
374168ae RN |
72 | Exception0StubHeader = AllocatePool (sizeof (*Exception0StubHeader));\r |
73 | ASSERT (Exception0StubHeader != NULL);\r | |
74 | CopyMem (\r | |
75 | Exception0StubHeader->ExceptionStubHeader,\r | |
76 | (VOID *)ArchGetIdtHandler (&IdtTable[0]),\r | |
77 | sizeof (Exception0StubHeader->ExceptionStubHeader)\r | |
78 | );\r | |
79 | Exception0StubHeader->ExceptionHandlerData = ExceptionHandlerData;\r | |
80 | ArchUpdateIdtEntry (&IdtTable[0], (UINTN)Exception0StubHeader->ExceptionStubHeader);\r | |
a81abf16 JF |
81 | }\r |
82 | \r | |
83 | /**\r | |
84 | Common exception handler.\r | |
85 | \r | |
86 | @param ExceptionType Exception type.\r | |
87 | @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.\r | |
88 | **/\r | |
89 | VOID\r | |
90 | EFIAPI\r | |
91 | CommonExceptionHandler (\r | |
dd563742 | 92 | IN EFI_EXCEPTION_TYPE ExceptionType,\r |
a81abf16 JF |
93 | IN EFI_SYSTEM_CONTEXT SystemContext\r |
94 | )\r | |
95 | {\r | |
96 | EXCEPTION_HANDLER_DATA *ExceptionHandlerData;\r | |
97 | \r | |
98 | ExceptionHandlerData = GetExceptionHandlerData ();\r | |
99 | CommonExceptionHandlerWorker (ExceptionType, SystemContext, ExceptionHandlerData);\r | |
100 | }\r | |
101 | \r | |
102 | /**\r | |
103 | Initializes all CPU exceptions entries and provides the default exception handlers.\r | |
dd563742 | 104 | \r |
a81abf16 JF |
105 | Caller should try to get an array of interrupt and/or exception vectors that are in use and need to\r |
106 | persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.\r | |
dd563742 | 107 | If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.\r |
a81abf16 | 108 | If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.\r |
dd563742 | 109 | Note: Before invoking this API, caller must allocate memory for IDT table and load\r |
a81abf16 JF |
110 | IDTR by AsmWriteIdtr().\r |
111 | \r | |
112 | @param[in] VectorInfo Pointer to reserved vector list.\r | |
dd563742 JF |
113 | \r |
114 | @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized\r | |
a81abf16 JF |
115 | with default exception handlers.\r |
116 | @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r | |
117 | @retval EFI_UNSUPPORTED This function is not supported.\r | |
118 | \r | |
119 | **/\r | |
120 | EFI_STATUS\r | |
121 | EFIAPI\r | |
122 | InitializeCpuExceptionHandlers (\r | |
123 | IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL\r | |
124 | )\r | |
125 | {\r | |
126 | EFI_STATUS Status;\r | |
127 | EXCEPTION_HANDLER_DATA *ExceptionHandlerData;\r | |
128 | RESERVED_VECTORS_DATA *ReservedVectors;\r | |
129 | \r | |
130 | ReservedVectors = AllocatePool (sizeof (RESERVED_VECTORS_DATA) * CPU_EXCEPTION_NUM);\r | |
131 | ASSERT (ReservedVectors != NULL);\r | |
132 | \r | |
133 | ExceptionHandlerData = AllocatePool (sizeof (EXCEPTION_HANDLER_DATA));\r | |
134 | ASSERT (ExceptionHandlerData != NULL);\r | |
135 | ExceptionHandlerData->ReservedVectors = ReservedVectors;\r | |
136 | ExceptionHandlerData->ExternalInterruptHandler = NULL;\r | |
137 | InitializeSpinLock (&ExceptionHandlerData->DisplayMessageSpinLock);\r | |
138 | \r | |
139 | Status = InitializeCpuExceptionHandlersWorker (VectorInfo, ExceptionHandlerData);\r | |
140 | if (EFI_ERROR (Status)) {\r | |
141 | FreePool (ReservedVectors);\r | |
142 | FreePool (ExceptionHandlerData);\r | |
143 | return Status;\r | |
144 | }\r | |
145 | \r | |
374168ae | 146 | SetExceptionHandlerData (ExceptionHandlerData);\r |
a81abf16 JF |
147 | return EFI_SUCCESS;\r |
148 | }\r | |
149 | \r | |
150 | /**\r | |
151 | Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.\r | |
dd563742 | 152 | \r |
a81abf16 JF |
153 | Caller should try to get an array of interrupt and/or exception vectors that are in use and need to\r |
154 | persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.\r | |
dd563742 | 155 | If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.\r |
a81abf16 JF |
156 | If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.\r |
157 | \r | |
158 | @param[in] VectorInfo Pointer to reserved vector list.\r | |
dd563742 JF |
159 | \r |
160 | @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized\r | |
a81abf16 JF |
161 | with default interrupt/exception handlers.\r |
162 | @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r | |
163 | @retval EFI_UNSUPPORTED This function is not supported.\r | |
164 | \r | |
165 | **/\r | |
166 | EFI_STATUS\r | |
167 | EFIAPI\r | |
168 | InitializeCpuInterruptHandlers (\r | |
169 | IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL\r | |
170 | )\r | |
171 | {\r | |
172 | return EFI_UNSUPPORTED;\r | |
173 | }\r | |
174 | \r | |
175 | /**\r | |
176 | Registers a function to be called from the processor interrupt handler.\r | |
177 | \r | |
dd563742 JF |
178 | This function registers and enables the handler specified by InterruptHandler for a processor\r |
179 | interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the\r | |
180 | handler for the processor interrupt or exception type specified by InterruptType is uninstalled.\r | |
a81abf16 JF |
181 | The installed handler is called once for each processor interrupt or exception.\r |
182 | NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or\r | |
183 | InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.\r | |
184 | \r | |
185 | @param[in] InterruptType Defines which interrupt or exception to hook.\r | |
186 | @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called\r | |
187 | when a processor interrupt occurs. If this parameter is NULL, then the handler\r | |
188 | will be uninstalled.\r | |
189 | \r | |
190 | @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r | |
191 | @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was\r | |
192 | previously installed.\r | |
193 | @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not\r | |
194 | previously installed.\r | |
195 | @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,\r | |
196 | or this function is not supported.\r | |
197 | **/\r | |
198 | EFI_STATUS\r | |
199 | EFIAPI\r | |
200 | RegisterCpuInterruptHandler (\r | |
201 | IN EFI_EXCEPTION_TYPE InterruptType,\r | |
202 | IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r | |
203 | )\r | |
204 | {\r | |
205 | return EFI_UNSUPPORTED;\r | |
0ff5aa9c JW |
206 | }\r |
207 | \r | |
208 | /**\r | |
209 | Initializes all CPU exceptions entries with optional extra initializations.\r | |
210 | \r | |
211 | By default, this method should include all functionalities implemented by\r | |
212 | InitializeCpuExceptionHandlers(), plus extra initialization works, if any.\r | |
213 | This could be done by calling InitializeCpuExceptionHandlers() directly\r | |
214 | in this method besides the extra works.\r | |
215 | \r | |
216 | InitData is optional and its use and content are processor arch dependent.\r | |
217 | The typical usage of it is to convey resources which have to be reserved\r | |
218 | elsewhere and are necessary for the extra initializations of exception.\r | |
219 | \r | |
220 | @param[in] VectorInfo Pointer to reserved vector list.\r | |
221 | @param[in] InitData Pointer to data optional for extra initializations\r | |
222 | of exception.\r | |
223 | \r | |
224 | @retval EFI_SUCCESS The exceptions have been successfully\r | |
225 | initialized.\r | |
226 | @retval EFI_INVALID_PARAMETER VectorInfo or InitData contains invalid\r | |
227 | content.\r | |
228 | \r | |
229 | **/\r | |
230 | EFI_STATUS\r | |
231 | EFIAPI\r | |
232 | InitializeCpuExceptionHandlersEx (\r | |
233 | IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL,\r | |
234 | IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL\r | |
235 | )\r | |
236 | {\r | |
fc0e7fd5 JW |
237 | EFI_STATUS Status;\r |
238 | \r | |
239 | //\r | |
240 | // To avoid repeat initialization of default handlers, the caller should pass\r | |
241 | // an extended init data with InitDefaultHandlers set to FALSE. There's no\r | |
242 | // need to call this method to just initialize default handlers. Call non-ex\r | |
243 | // version instead; or this method must be implemented as a simple wrapper of\r | |
244 | // non-ex version of it, if this version has to be called.\r | |
245 | //\r | |
246 | if (InitData == NULL || InitData->Ia32.InitDefaultHandlers) {\r | |
247 | Status = InitializeCpuExceptionHandlers (VectorInfo);\r | |
248 | } else {\r | |
249 | Status = EFI_SUCCESS;\r | |
250 | }\r | |
251 | \r | |
252 | if (!EFI_ERROR (Status)) {\r | |
253 | //\r | |
254 | // Initializing stack switch is only necessary for Stack Guard functionality.\r | |
255 | //\r | |
256 | if (PcdGetBool (PcdCpuStackGuard) && InitData != NULL) {\r | |
7c4207e9 | 257 | Status = ArchSetupExceptionStack (InitData);\r |
fc0e7fd5 JW |
258 | }\r |
259 | }\r | |
260 | \r | |
261 | return Status;\r | |
0ff5aa9c | 262 | }\r |