2 CPU exception handler library implementation for PEIM module.
4 Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "CpuExceptionCommon.h"
11 #include <Library/DebugLib.h>
12 #include <Library/HobLib.h>
13 #include <Library/MemoryAllocationLib.h>
14 #include <Library/PcdLib.h>
16 CONST UINTN mDoFarReturnFlag
= 0;
19 UINT8 ExceptionStubHeader
[HOOKAFTER_STUB_SIZE
];
20 EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
;
21 } EXCEPTION0_STUB_HEADER
;
24 Get exception handler data pointer from IDT[0].
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.
30 @return pointer to exception handler data.
32 EXCEPTION_HANDLER_DATA
*
33 GetExceptionHandlerData (
37 IA32_DESCRIPTOR IdtDescriptor
;
38 IA32_IDT_GATE_DESCRIPTOR
*IdtTable
;
39 EXCEPTION0_STUB_HEADER
*Exception0StubHeader
;
41 AsmReadIdtr (&IdtDescriptor
);
42 IdtTable
= (IA32_IDT_GATE_DESCRIPTOR
*)IdtDescriptor
.Base
;
44 Exception0StubHeader
= (EXCEPTION0_STUB_HEADER
*)ArchGetIdtHandler (&IdtTable
[0]);
45 return Exception0StubHeader
->ExceptionHandlerData
;
49 Set exception handler data pointer to IDT[0].
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.
55 @param ExceptionHandlerData pointer to exception handler data.
58 SetExceptionHandlerData (
59 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
62 EXCEPTION0_STUB_HEADER
*Exception0StubHeader
;
63 IA32_DESCRIPTOR IdtDescriptor
;
64 IA32_IDT_GATE_DESCRIPTOR
*IdtTable
;
67 // Duplicate the exception #0 stub header in pool and cache the ExceptionHandlerData just after the stub header.
68 // So AP can get the ExceptionHandlerData by reading the IDT[0].
70 AsmReadIdtr (&IdtDescriptor
);
71 IdtTable
= (IA32_IDT_GATE_DESCRIPTOR
*)IdtDescriptor
.Base
;
73 Exception0StubHeader
= AllocatePool (sizeof (*Exception0StubHeader
));
74 ASSERT (Exception0StubHeader
!= NULL
);
76 Exception0StubHeader
->ExceptionStubHeader
,
77 (VOID
*)ArchGetIdtHandler (&IdtTable
[0]),
78 sizeof (Exception0StubHeader
->ExceptionStubHeader
)
80 Exception0StubHeader
->ExceptionHandlerData
= ExceptionHandlerData
;
81 ArchUpdateIdtEntry (&IdtTable
[0], (UINTN
)Exception0StubHeader
->ExceptionStubHeader
);
85 Common exception handler.
87 @param ExceptionType Exception type.
88 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
92 CommonExceptionHandler (
93 IN EFI_EXCEPTION_TYPE ExceptionType
,
94 IN EFI_SYSTEM_CONTEXT SystemContext
97 EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
;
99 ExceptionHandlerData
= GetExceptionHandlerData ();
100 CommonExceptionHandlerWorker (ExceptionType
, SystemContext
, ExceptionHandlerData
);
104 Initializes all CPU exceptions entries and provides the default exception handlers.
106 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
107 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
108 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
109 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
110 Note: Before invoking this API, caller must allocate memory for IDT table and load
111 IDTR by AsmWriteIdtr().
113 @param[in] VectorInfo Pointer to reserved vector list.
115 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
116 with default exception handlers.
117 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
118 @retval EFI_UNSUPPORTED This function is not supported.
123 InitializeCpuExceptionHandlers (
124 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
128 EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
;
129 RESERVED_VECTORS_DATA
*ReservedVectors
;
131 ReservedVectors
= AllocatePool (sizeof (RESERVED_VECTORS_DATA
) * CPU_EXCEPTION_NUM
);
132 ASSERT (ReservedVectors
!= NULL
);
134 ExceptionHandlerData
= AllocatePool (sizeof (EXCEPTION_HANDLER_DATA
));
135 ASSERT (ExceptionHandlerData
!= NULL
);
136 ExceptionHandlerData
->ReservedVectors
= ReservedVectors
;
137 ExceptionHandlerData
->ExternalInterruptHandler
= NULL
;
138 InitializeSpinLock (&ExceptionHandlerData
->DisplayMessageSpinLock
);
140 Status
= InitializeCpuExceptionHandlersWorker (VectorInfo
, ExceptionHandlerData
);
141 if (EFI_ERROR (Status
)) {
142 FreePool (ReservedVectors
);
143 FreePool (ExceptionHandlerData
);
147 SetExceptionHandlerData (ExceptionHandlerData
);
152 Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.
154 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
155 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
156 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
157 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
159 @param[in] VectorInfo Pointer to reserved vector list.
161 @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized
162 with default interrupt/exception handlers.
163 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
164 @retval EFI_UNSUPPORTED This function is not supported.
169 InitializeCpuInterruptHandlers (
170 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
173 return EFI_UNSUPPORTED
;
177 Registers a function to be called from the processor interrupt handler.
179 This function registers and enables the handler specified by InterruptHandler for a processor
180 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
181 handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
182 The installed handler is called once for each processor interrupt or exception.
183 NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or
184 InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.
186 @param[in] InterruptType Defines which interrupt or exception to hook.
187 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
188 when a processor interrupt occurs. If this parameter is NULL, then the handler
191 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
192 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
193 previously installed.
194 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
195 previously installed.
196 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
197 or this function is not supported.
201 RegisterCpuInterruptHandler (
202 IN EFI_EXCEPTION_TYPE InterruptType
,
203 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
206 return EFI_UNSUPPORTED
;
210 Initializes all CPU exceptions entries with optional extra initializations.
212 By default, this method should include all functionalities implemented by
213 InitializeCpuExceptionHandlers(), plus extra initialization works, if any.
214 This could be done by calling InitializeCpuExceptionHandlers() directly
215 in this method besides the extra works.
217 InitData is optional and its use and content are processor arch dependent.
218 The typical usage of it is to convey resources which have to be reserved
219 elsewhere and are necessary for the extra initializations of exception.
221 @param[in] VectorInfo Pointer to reserved vector list.
222 @param[in] InitData Pointer to data optional for extra initializations
225 @retval EFI_SUCCESS The exceptions have been successfully
227 @retval EFI_INVALID_PARAMETER VectorInfo or InitData contains invalid
233 InitializeCpuExceptionHandlersEx (
234 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
,
235 IN CPU_EXCEPTION_INIT_DATA
*InitData OPTIONAL
241 // To avoid repeat initialization of default handlers, the caller should pass
242 // an extended init data with InitDefaultHandlers set to FALSE. There's no
243 // need to call this method to just initialize default handlers. Call non-ex
244 // version instead; or this method must be implemented as a simple wrapper of
245 // non-ex version of it, if this version has to be called.
247 if ((InitData
== NULL
) || InitData
->Ia32
.InitDefaultHandlers
) {
248 Status
= InitializeCpuExceptionHandlers (VectorInfo
);
250 Status
= EFI_SUCCESS
;
253 if (!EFI_ERROR (Status
)) {
255 // Initializing stack switch is only necessary for Stack Guard functionality.
257 if (PcdGetBool (PcdCpuStackGuard
) && (InitData
!= NULL
)) {
258 Status
= ArchSetupExceptionStack (InitData
);