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