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