2 CPU exception handler library implementation for PEIM module.
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.
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.
16 #include "CpuExceptionCommon.h"
17 #include <Library/DebugLib.h>
18 #include <Library/HobLib.h>
19 #include <Library/MemoryAllocationLib.h>
20 #include <Library/PcdLib.h>
22 CONST UINTN mDoFarReturnFlag
= 0;
25 UINT8 ExceptionStubHeader
[HOOKAFTER_STUB_SIZE
];
26 EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
;
27 } EXCEPTION0_STUB_HEADER
;
30 Get exception handler data pointer from IDT[0].
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.
36 @return pointer to exception handler data.
38 EXCEPTION_HANDLER_DATA
*
39 GetExceptionHandlerData (
43 IA32_DESCRIPTOR IdtDescriptor
;
44 IA32_IDT_GATE_DESCRIPTOR
*IdtTable
;
45 EXCEPTION0_STUB_HEADER
*Exception0StubHeader
;
47 AsmReadIdtr (&IdtDescriptor
);
48 IdtTable
= (IA32_IDT_GATE_DESCRIPTOR
*)IdtDescriptor
.Base
;
50 Exception0StubHeader
= (EXCEPTION0_STUB_HEADER
*)ArchGetIdtHandler (&IdtTable
[0]);
51 return Exception0StubHeader
->ExceptionHandlerData
;
55 Set exception handler data pointer to IDT[0].
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.
61 @param pointer to exception handler data.
64 SetExceptionHandlerData (
65 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
68 EXCEPTION0_STUB_HEADER
*Exception0StubHeader
;
69 IA32_DESCRIPTOR IdtDescriptor
;
70 IA32_IDT_GATE_DESCRIPTOR
*IdtTable
;
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].
75 AsmReadIdtr (&IdtDescriptor
);
76 IdtTable
= (IA32_IDT_GATE_DESCRIPTOR
*)IdtDescriptor
.Base
;
78 Exception0StubHeader
= AllocatePool (sizeof (*Exception0StubHeader
));
79 ASSERT (Exception0StubHeader
!= NULL
);
81 Exception0StubHeader
->ExceptionStubHeader
,
82 (VOID
*)ArchGetIdtHandler (&IdtTable
[0]),
83 sizeof (Exception0StubHeader
->ExceptionStubHeader
)
85 Exception0StubHeader
->ExceptionHandlerData
= ExceptionHandlerData
;
86 ArchUpdateIdtEntry (&IdtTable
[0], (UINTN
)Exception0StubHeader
->ExceptionStubHeader
);
90 Common exception handler.
92 @param ExceptionType Exception type.
93 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
97 CommonExceptionHandler (
98 IN EFI_EXCEPTION_TYPE ExceptionType
,
99 IN EFI_SYSTEM_CONTEXT SystemContext
102 EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
;
104 ExceptionHandlerData
= GetExceptionHandlerData ();
105 CommonExceptionHandlerWorker (ExceptionType
, SystemContext
, ExceptionHandlerData
);
109 Initializes all CPU exceptions entries and provides the default exception handlers.
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().
118 @param[in] VectorInfo Pointer to reserved vector list.
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.
128 InitializeCpuExceptionHandlers (
129 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
133 EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
;
134 RESERVED_VECTORS_DATA
*ReservedVectors
;
136 ReservedVectors
= AllocatePool (sizeof (RESERVED_VECTORS_DATA
) * CPU_EXCEPTION_NUM
);
137 ASSERT (ReservedVectors
!= NULL
);
139 ExceptionHandlerData
= AllocatePool (sizeof (EXCEPTION_HANDLER_DATA
));
140 ASSERT (ExceptionHandlerData
!= NULL
);
141 ExceptionHandlerData
->ReservedVectors
= ReservedVectors
;
142 ExceptionHandlerData
->ExternalInterruptHandler
= NULL
;
143 InitializeSpinLock (&ExceptionHandlerData
->DisplayMessageSpinLock
);
145 Status
= InitializeCpuExceptionHandlersWorker (VectorInfo
, ExceptionHandlerData
);
146 if (EFI_ERROR (Status
)) {
147 FreePool (ReservedVectors
);
148 FreePool (ExceptionHandlerData
);
152 SetExceptionHandlerData (ExceptionHandlerData
);
157 Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.
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.
164 @param[in] VectorInfo Pointer to reserved vector list.
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.
174 InitializeCpuInterruptHandlers (
175 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
178 return EFI_UNSUPPORTED
;
182 Registers a function to be called from the processor interrupt handler.
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.
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
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.
206 RegisterCpuInterruptHandler (
207 IN EFI_EXCEPTION_TYPE InterruptType
,
208 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
211 return EFI_UNSUPPORTED
;
215 Initializes all CPU exceptions entries with optional extra initializations.
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.
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.
226 @param[in] VectorInfo Pointer to reserved vector list.
227 @param[in] InitData Pointer to data optional for extra initializations
230 @retval EFI_SUCCESS The exceptions have been successfully
232 @retval EFI_INVALID_PARAMETER VectorInfo or InitData contains invalid
238 InitializeCpuExceptionHandlersEx (
239 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
,
240 IN CPU_EXCEPTION_INIT_DATA
*InitData OPTIONAL
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.
252 if (InitData
== NULL
|| InitData
->Ia32
.InitDefaultHandlers
) {
253 Status
= InitializeCpuExceptionHandlers (VectorInfo
);
255 Status
= EFI_SUCCESS
;
258 if (!EFI_ERROR (Status
)) {
260 // Initializing stack switch is only necessary for Stack Guard functionality.
262 if (PcdGetBool (PcdCpuStackGuard
) && InitData
!= NULL
) {
263 Status
= ArchSetupExcpetionStack (InitData
);