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