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 "CpuExceptionCommon.h"
12 CONST UINTN mDoFarReturnFlag
= 0;
15 Common exception handler.
17 @param ExceptionType Exception type.
18 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
22 CommonExceptionHandler (
23 IN EFI_EXCEPTION_TYPE ExceptionType
,
24 IN EFI_SYSTEM_CONTEXT SystemContext
28 // Initialize the serial port before dumping.
30 SerialPortInitialize ();
32 // Display ExceptionType, CPU information and Image information
34 DumpImageAndCpuContent (ExceptionType
, SystemContext
);
43 Initializes all CPU exceptions entries and provides the default exception handlers.
45 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
46 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
47 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
48 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
49 Note: Before invoking this API, caller must allocate memory for IDT table and load
50 IDTR by AsmWriteIdtr().
52 @param[in] VectorInfo Pointer to reserved vector list.
54 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
55 with default exception handlers.
56 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
57 @retval EFI_UNSUPPORTED This function is not supported.
62 InitializeCpuExceptionHandlers (
63 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
67 RESERVED_VECTORS_DATA ReservedVectorData
[CPU_EXCEPTION_NUM
];
68 IA32_DESCRIPTOR IdtDescriptor
;
71 EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap
;
72 IA32_IDT_GATE_DESCRIPTOR
*IdtTable
;
74 UINTN InterruptHandler
;
76 if (VectorInfo
!= NULL
) {
77 SetMem ((VOID
*) ReservedVectorData
, sizeof (RESERVED_VECTORS_DATA
) * CPU_EXCEPTION_NUM
, 0xff);
78 Status
= ReadAndVerifyVectorInfo (VectorInfo
, ReservedVectorData
, CPU_EXCEPTION_NUM
);
79 if (EFI_ERROR (Status
)) {
80 return EFI_INVALID_PARAMETER
;
84 // Read IDT descriptor and calculate IDT size
86 AsmReadIdtr (&IdtDescriptor
);
87 IdtEntryCount
= (IdtDescriptor
.Limit
+ 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR
);
88 if (IdtEntryCount
> CPU_EXCEPTION_NUM
) {
90 // CPU exeption library only setup CPU_EXCEPTION_NUM exception handler at most
92 IdtEntryCount
= CPU_EXCEPTION_NUM
;
95 // Use current CS as the segment selector of interrupt gate in IDT
97 CodeSegment
= AsmReadCs ();
99 AsmGetTemplateAddressMap (&TemplateMap
);
100 IdtTable
= (IA32_IDT_GATE_DESCRIPTOR
*)IdtDescriptor
.Base
;
101 for (Index
= 0; Index
< IdtEntryCount
; Index
++) {
102 IdtTable
[Index
].Bits
.Selector
= CodeSegment
;
104 // Check reserved vectors attributes if has, only EFI_VECTOR_HANDOFF_DO_NOT_HOOK
105 // supported in this instance
107 if (VectorInfo
!= NULL
) {
108 if (ReservedVectorData
[Index
].Attribute
== EFI_VECTOR_HANDOFF_DO_NOT_HOOK
) {
115 InterruptHandler
= TemplateMap
.ExceptionStart
+ Index
* TemplateMap
.ExceptionStubHeaderSize
;
116 ArchUpdateIdtEntry (&IdtTable
[Index
], InterruptHandler
);
122 Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.
124 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
125 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
126 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
127 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
129 @param[in] VectorInfo Pointer to reserved vector list.
131 @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized
132 with default interrupt/exception handlers.
133 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
134 @retval EFI_UNSUPPORTED This function is not supported.
139 InitializeCpuInterruptHandlers (
140 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
143 return EFI_UNSUPPORTED
;
147 Registers a function to be called from the processor interrupt handler.
149 This function registers and enables the handler specified by InterruptHandler for a processor
150 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
151 handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
152 The installed handler is called once for each processor interrupt or exception.
153 NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or
154 InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.
156 @param[in] InterruptType Defines which interrupt or exception to hook.
157 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
158 when a processor interrupt occurs. If this parameter is NULL, then the handler
161 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
162 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
163 previously installed.
164 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
165 previously installed.
166 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
167 or this function is not supported.
171 RegisterCpuInterruptHandler (
172 IN EFI_EXCEPTION_TYPE InterruptType
,
173 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
176 return EFI_UNSUPPORTED
;
180 Initializes all CPU exceptions entries with optional extra initializations.
182 By default, this method should include all functionalities implemented by
183 InitializeCpuExceptionHandlers(), plus extra initialization works, if any.
184 This could be done by calling InitializeCpuExceptionHandlers() directly
185 in this method besides the extra works.
187 InitData is optional and its use and content are processor arch dependent.
188 The typical usage of it is to convey resources which have to be reserved
189 elsewhere and are necessary for the extra initializations of exception.
191 @param[in] VectorInfo Pointer to reserved vector list.
192 @param[in] InitData Pointer to data optional for extra initializations
195 @retval EFI_SUCCESS The exceptions have been successfully
197 @retval EFI_INVALID_PARAMETER VectorInfo or InitData contains invalid
203 InitializeCpuExceptionHandlersEx (
204 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
,
205 IN CPU_EXCEPTION_INIT_DATA
*InitData OPTIONAL
208 return InitializeCpuExceptionHandlers (VectorInfo
);