2 CPU exception handler library implemenation for DXE modules.
4 Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. 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/MemoryAllocationLib.h>
19 #include <Library/UefiBootServicesTableLib.h>
21 CONST UINTN mDoFarReturnFlag
= 0;
23 RESERVED_VECTORS_DATA mReservedVectorsData
[CPU_EXCEPTION_NUM
];
24 EFI_CPU_INTERRUPT_HANDLER mExternalInterruptHandlerTable
[CPU_EXCEPTION_NUM
];
25 UINTN mEnabledInterruptNum
= 0;
27 EXCEPTION_HANDLER_DATA mExceptionHandlerData
;
29 UINT8 mNewStack
[CPU_STACK_SWITCH_EXCEPTION_NUMBER
*
30 CPU_KNOWN_GOOD_STACK_SIZE
];
31 UINT8 mNewGdt
[CPU_TSS_GDT_SIZE
];
34 Common exception handler.
36 @param ExceptionType Exception type.
37 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
41 CommonExceptionHandler (
42 IN EFI_EXCEPTION_TYPE ExceptionType
,
43 IN EFI_SYSTEM_CONTEXT SystemContext
46 CommonExceptionHandlerWorker (ExceptionType
, SystemContext
, &mExceptionHandlerData
);
50 Initializes all CPU exceptions entries and provides the default exception handlers.
52 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
53 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
54 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
55 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
57 @param[in] VectorInfo Pointer to reserved vector list.
59 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
60 with default exception handlers.
61 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
62 @retval EFI_UNSUPPORTED This function is not supported.
67 InitializeCpuExceptionHandlers (
68 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
71 mExceptionHandlerData
.ReservedVectors
= mReservedVectorsData
;
72 mExceptionHandlerData
.ExternalInterruptHandler
= mExternalInterruptHandlerTable
;
73 InitializeSpinLock (&mExceptionHandlerData
.DisplayMessageSpinLock
);
74 return InitializeCpuExceptionHandlersWorker (VectorInfo
, &mExceptionHandlerData
);
78 Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.
80 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
81 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
82 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
83 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
85 @param[in] VectorInfo Pointer to reserved vector list.
87 @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized
88 with default interrupt/exception handlers.
89 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
90 @retval EFI_UNSUPPORTED This function is not supported.
95 InitializeCpuInterruptHandlers (
96 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
100 IA32_IDT_GATE_DESCRIPTOR
*IdtTable
;
101 IA32_DESCRIPTOR IdtDescriptor
;
103 EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap
;
105 UINTN InterruptEntry
;
106 UINT8
*InterruptEntryCode
;
107 RESERVED_VECTORS_DATA
*ReservedVectors
;
108 EFI_CPU_INTERRUPT_HANDLER
*ExternalInterruptHandler
;
110 Status
= gBS
->AllocatePool (
112 sizeof (RESERVED_VECTORS_DATA
) * CPU_INTERRUPT_NUM
,
113 (VOID
**)&ReservedVectors
115 ASSERT (!EFI_ERROR (Status
) && ReservedVectors
!= NULL
);
116 SetMem ((VOID
*) ReservedVectors
, sizeof (RESERVED_VECTORS_DATA
) * CPU_INTERRUPT_NUM
, 0xff);
117 if (VectorInfo
!= NULL
) {
118 Status
= ReadAndVerifyVectorInfo (VectorInfo
, ReservedVectors
, CPU_INTERRUPT_NUM
);
119 if (EFI_ERROR (Status
)) {
120 FreePool (ReservedVectors
);
121 return EFI_INVALID_PARAMETER
;
125 ExternalInterruptHandler
= AllocateZeroPool (sizeof (EFI_CPU_INTERRUPT_HANDLER
) * CPU_INTERRUPT_NUM
);
126 ASSERT (ExternalInterruptHandler
!= NULL
);
129 // Read IDT descriptor and calculate IDT size
131 AsmReadIdtr (&IdtDescriptor
);
132 IdtEntryCount
= (IdtDescriptor
.Limit
+ 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR
);
133 if (IdtEntryCount
> CPU_INTERRUPT_NUM
) {
134 IdtEntryCount
= CPU_INTERRUPT_NUM
;
137 // Create Interrupt Descriptor Table and Copy the old IDT table in
139 IdtTable
= AllocateZeroPool (sizeof (IA32_IDT_GATE_DESCRIPTOR
) * CPU_INTERRUPT_NUM
);
140 ASSERT (IdtTable
!= NULL
);
141 CopyMem (IdtTable
, (VOID
*)IdtDescriptor
.Base
, sizeof (IA32_IDT_GATE_DESCRIPTOR
) * IdtEntryCount
);
143 AsmGetTemplateAddressMap (&TemplateMap
);
144 ASSERT (TemplateMap
.ExceptionStubHeaderSize
<= HOOKAFTER_STUB_SIZE
);
146 Status
= gBS
->AllocatePool (
148 TemplateMap
.ExceptionStubHeaderSize
* CPU_INTERRUPT_NUM
,
149 (VOID
**)&InterruptEntryCode
151 ASSERT (!EFI_ERROR (Status
) && InterruptEntryCode
!= NULL
);
153 InterruptEntry
= (UINTN
) InterruptEntryCode
;
154 for (Index
= 0; Index
< CPU_INTERRUPT_NUM
; Index
++) {
156 (VOID
*) InterruptEntry
,
157 (VOID
*) TemplateMap
.ExceptionStart
,
158 TemplateMap
.ExceptionStubHeaderSize
160 AsmVectorNumFixup ((VOID
*) InterruptEntry
, (UINT8
) Index
, (VOID
*) TemplateMap
.ExceptionStart
);
161 InterruptEntry
+= TemplateMap
.ExceptionStubHeaderSize
;
164 TemplateMap
.ExceptionStart
= (UINTN
) InterruptEntryCode
;
165 mExceptionHandlerData
.IdtEntryCount
= CPU_INTERRUPT_NUM
;
166 mExceptionHandlerData
.ReservedVectors
= ReservedVectors
;
167 mExceptionHandlerData
.ExternalInterruptHandler
= ExternalInterruptHandler
;
168 InitializeSpinLock (&mExceptionHandlerData
.DisplayMessageSpinLock
);
170 UpdateIdtTable (IdtTable
, &TemplateMap
, &mExceptionHandlerData
);
173 // Load Interrupt Descriptor Table
175 IdtDescriptor
.Base
= (UINTN
) IdtTable
;
176 IdtDescriptor
.Limit
= (UINT16
) (sizeof (IA32_IDT_GATE_DESCRIPTOR
) * CPU_INTERRUPT_NUM
- 1);
177 AsmWriteIdtr ((IA32_DESCRIPTOR
*) &IdtDescriptor
);
183 Registers a function to be called from the processor interrupt handler.
185 This function registers and enables the handler specified by InterruptHandler for a processor
186 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
187 handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
188 The installed handler is called once for each processor interrupt or exception.
189 NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or
190 InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.
192 @param[in] InterruptType Defines which interrupt or exception to hook.
193 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
194 when a processor interrupt occurs. If this parameter is NULL, then the handler
197 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
198 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
199 previously installed.
200 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
201 previously installed.
202 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
203 or this function is not supported.
207 RegisterCpuInterruptHandler (
208 IN EFI_EXCEPTION_TYPE InterruptType
,
209 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
212 return RegisterCpuInterruptHandlerWorker (InterruptType
, InterruptHandler
, &mExceptionHandlerData
);
216 Initializes CPU exceptions entries and setup stack switch for given exceptions.
218 This method will call InitializeCpuExceptionHandlers() to setup default
219 exception handlers unless indicated not to do it explicitly.
221 If InitData is passed with NULL, this method will use the resource reserved
222 by global variables to initialize it; Otherwise it will use data in InitData
223 to setup stack switch. This is for the different use cases in DxeCore and
224 Cpu MP exception initialization.
226 @param[in] VectorInfo Pointer to reserved vector list.
227 @param[in] InitData Pointer to data required to setup stack switch for
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
244 CPU_EXCEPTION_INIT_DATA EssData
;
245 IA32_DESCRIPTOR Idtr
;
246 IA32_DESCRIPTOR Gdtr
;
249 // To avoid repeat initialization of default handlers, the caller should pass
250 // an extended init data with InitDefaultHandlers set to FALSE. There's no
251 // need to call this method to just initialize default handlers. Call non-ex
252 // version instead; or this method must be implemented as a simple wrapper of
253 // non-ex version of it, if this version has to be called.
255 if (InitData
== NULL
|| InitData
->X64
.InitDefaultHandlers
) {
256 Status
= InitializeCpuExceptionHandlers (VectorInfo
);
258 Status
= EFI_SUCCESS
;
261 if (!EFI_ERROR (Status
)) {
263 // Initializing stack switch is only necessary for Stack Guard functionality.
265 if (PcdGetBool (PcdCpuStackGuard
)) {
266 if (InitData
== NULL
) {
267 SetMem (mNewGdt
, sizeof (mNewGdt
), 0);
272 EssData
.X64
.Revision
= CPU_EXCEPTION_INIT_DATA_REV
;
273 EssData
.X64
.KnownGoodStackTop
= (UINTN
)mNewStack
+ sizeof (mNewStack
);
274 EssData
.X64
.KnownGoodStackSize
= CPU_KNOWN_GOOD_STACK_SIZE
;
275 EssData
.X64
.StackSwitchExceptions
= CPU_STACK_SWITCH_EXCEPTION_LIST
;
276 EssData
.X64
.StackSwitchExceptionNumber
= CPU_STACK_SWITCH_EXCEPTION_NUMBER
;
277 EssData
.X64
.IdtTable
= (VOID
*)Idtr
.Base
;
278 EssData
.X64
.IdtTableSize
= Idtr
.Limit
+ 1;
279 EssData
.X64
.GdtTable
= mNewGdt
;
280 EssData
.X64
.GdtTableSize
= sizeof (mNewGdt
);
281 EssData
.X64
.ExceptionTssDesc
= mNewGdt
+ Gdtr
.Limit
+ 1;
282 EssData
.X64
.ExceptionTssDescSize
= CPU_TSS_DESC_SIZE
;
283 EssData
.X64
.ExceptionTss
= mNewGdt
+ Gdtr
.Limit
+ 1 + CPU_TSS_DESC_SIZE
;
284 EssData
.X64
.ExceptionTssSize
= CPU_TSS_SIZE
;
288 Status
= ArchSetupExceptionStack (InitData
);