2 CPU exception handler library implemenation for SEC/PEIM modules.
4 Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Library/VmgExitLib.h>
11 #include "CpuExceptionCommon.h"
13 CONST UINTN mDoFarReturnFlag
= 0;
16 Common exception handler.
18 @param ExceptionType Exception type.
19 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
23 CommonExceptionHandler (
24 IN EFI_EXCEPTION_TYPE ExceptionType
,
25 IN EFI_SYSTEM_CONTEXT SystemContext
28 if (ExceptionType
== VC_EXCEPTION
) {
31 // #VC needs to be handled immediately upon enabling exception handling
32 // and therefore can't use the RegisterCpuInterruptHandler() interface
33 // (which isn't supported under Sec and Pei anyway).
36 // On EFI_SUCCESS - Exception has been handled, return
37 // On other - ExceptionType contains (possibly new) exception
40 Status
= VmgExitHandleVc (&ExceptionType
, SystemContext
);
41 if (!EFI_ERROR (Status
)) {
47 // Initialize the serial port before dumping.
49 SerialPortInitialize ();
51 // Display ExceptionType, CPU information and Image information
53 DumpImageAndCpuContent (ExceptionType
, SystemContext
);
62 Initializes all CPU exceptions entries and provides the default exception handlers.
64 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
65 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
66 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
67 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
68 Note: Before invoking this API, caller must allocate memory for IDT table and load
69 IDTR by AsmWriteIdtr().
71 @param[in] VectorInfo Pointer to reserved vector list.
73 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
74 with default exception handlers.
75 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
76 @retval EFI_UNSUPPORTED This function is not supported.
81 InitializeCpuExceptionHandlers (
82 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
86 RESERVED_VECTORS_DATA ReservedVectorData
[CPU_EXCEPTION_NUM
];
87 IA32_DESCRIPTOR IdtDescriptor
;
90 EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap
;
91 IA32_IDT_GATE_DESCRIPTOR
*IdtTable
;
93 UINTN InterruptHandler
;
95 if (VectorInfo
!= NULL
) {
96 SetMem ((VOID
*)ReservedVectorData
, sizeof (RESERVED_VECTORS_DATA
) * CPU_EXCEPTION_NUM
, 0xff);
97 Status
= ReadAndVerifyVectorInfo (VectorInfo
, ReservedVectorData
, CPU_EXCEPTION_NUM
);
98 if (EFI_ERROR (Status
)) {
99 return EFI_INVALID_PARAMETER
;
104 // Read IDT descriptor and calculate IDT size
106 AsmReadIdtr (&IdtDescriptor
);
107 IdtEntryCount
= (IdtDescriptor
.Limit
+ 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR
);
108 if (IdtEntryCount
> CPU_EXCEPTION_NUM
) {
110 // CPU exception library only setup CPU_EXCEPTION_NUM exception handler at most
112 IdtEntryCount
= CPU_EXCEPTION_NUM
;
116 // Use current CS as the segment selector of interrupt gate in IDT
118 CodeSegment
= AsmReadCs ();
120 AsmGetTemplateAddressMap (&TemplateMap
);
121 IdtTable
= (IA32_IDT_GATE_DESCRIPTOR
*)IdtDescriptor
.Base
;
122 for (Index
= 0; Index
< IdtEntryCount
; Index
++) {
123 IdtTable
[Index
].Bits
.Selector
= CodeSegment
;
125 // Check reserved vectors attributes if has, only EFI_VECTOR_HANDOFF_DO_NOT_HOOK
126 // supported in this instance
128 if (VectorInfo
!= NULL
) {
129 if (ReservedVectorData
[Index
].Attribute
== EFI_VECTOR_HANDOFF_DO_NOT_HOOK
) {
137 InterruptHandler
= TemplateMap
.ExceptionStart
+ Index
* TemplateMap
.ExceptionStubHeaderSize
;
138 ArchUpdateIdtEntry (&IdtTable
[Index
], InterruptHandler
);
145 Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.
147 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
148 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
149 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
150 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
152 @param[in] VectorInfo Pointer to reserved vector list.
154 @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized
155 with default interrupt/exception handlers.
156 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
157 @retval EFI_UNSUPPORTED This function is not supported.
162 InitializeCpuInterruptHandlers (
163 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
166 return EFI_UNSUPPORTED
;
170 Registers a function to be called from the processor interrupt handler.
172 This function registers and enables the handler specified by InterruptHandler for a processor
173 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
174 handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
175 The installed handler is called once for each processor interrupt or exception.
176 NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or
177 InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.
179 @param[in] InterruptType Defines which interrupt or exception to hook.
180 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
181 when a processor interrupt occurs. If this parameter is NULL, then the handler
184 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
185 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
186 previously installed.
187 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
188 previously installed.
189 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
190 or this function is not supported.
194 RegisterCpuInterruptHandler (
195 IN EFI_EXCEPTION_TYPE InterruptType
,
196 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
199 return EFI_UNSUPPORTED
;
203 Initializes all CPU exceptions entries with optional extra initializations.
205 By default, this method should include all functionalities implemented by
206 InitializeCpuExceptionHandlers(), plus extra initialization works, if any.
207 This could be done by calling InitializeCpuExceptionHandlers() directly
208 in this method besides the extra works.
210 InitData is optional and its use and content are processor arch dependent.
211 The typical usage of it is to convey resources which have to be reserved
212 elsewhere and are necessary for the extra initializations of exception.
214 @param[in] VectorInfo Pointer to reserved vector list.
215 @param[in] InitData Pointer to data optional for extra initializations
218 @retval EFI_SUCCESS The exceptions have been successfully
220 @retval EFI_INVALID_PARAMETER VectorInfo or InitData contains invalid
226 InitializeCpuExceptionHandlersEx (
227 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
,
228 IN CPU_EXCEPTION_INIT_DATA
*InitData OPTIONAL
231 return InitializeCpuExceptionHandlers (VectorInfo
);