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>
20 CONST UINTN mDoFarReturnFlag
= 0;
22 RESERVED_VECTORS_DATA mReservedVectorsData
[CPU_EXCEPTION_NUM
];
23 EFI_CPU_INTERRUPT_HANDLER mExternalInterruptHandlerTable
[CPU_EXCEPTION_NUM
];
24 UINTN mEnabledInterruptNum
= 0;
26 EXCEPTION_HANDLER_DATA mExceptionHandlerData
;
29 Common exception handler.
31 @param ExceptionType Exception type.
32 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
36 CommonExceptionHandler (
37 IN EFI_EXCEPTION_TYPE ExceptionType
,
38 IN EFI_SYSTEM_CONTEXT SystemContext
41 CommonExceptionHandlerWorker (ExceptionType
, SystemContext
, &mExceptionHandlerData
);
45 Initializes all CPU exceptions entries and provides the default exception handlers.
47 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
48 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
49 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
50 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
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
66 mExceptionHandlerData
.ReservedVectors
= mReservedVectorsData
;
67 mExceptionHandlerData
.ExternalInterruptHandler
= mExternalInterruptHandlerTable
;
68 InitializeSpinLock (&mExceptionHandlerData
.DisplayMessageSpinLock
);
69 return InitializeCpuExceptionHandlersWorker (VectorInfo
, &mExceptionHandlerData
);
73 Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.
75 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
76 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
77 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
78 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
80 @param[in] VectorInfo Pointer to reserved vector list.
82 @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized
83 with default interrupt/exception handlers.
84 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
85 @retval EFI_UNSUPPORTED This function is not supported.
90 InitializeCpuInterruptHandlers (
91 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
95 IA32_IDT_GATE_DESCRIPTOR
*IdtTable
;
96 IA32_DESCRIPTOR IdtDescriptor
;
98 EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap
;
100 UINTN InterruptEntry
;
101 UINT8
*InterruptEntryCode
;
102 RESERVED_VECTORS_DATA
*ReservedVectors
;
103 EFI_CPU_INTERRUPT_HANDLER
*ExternalInterruptHandler
;
105 ReservedVectors
= AllocatePool (sizeof (RESERVED_VECTORS_DATA
) * CPU_INTERRUPT_NUM
);
106 ASSERT (ReservedVectors
!= NULL
);
107 SetMem ((VOID
*) ReservedVectors
, sizeof (RESERVED_VECTORS_DATA
) * CPU_INTERRUPT_NUM
, 0xff);
108 if (VectorInfo
!= NULL
) {
109 Status
= ReadAndVerifyVectorInfo (VectorInfo
, ReservedVectors
, CPU_INTERRUPT_NUM
);
110 if (EFI_ERROR (Status
)) {
111 FreePool (ReservedVectors
);
112 return EFI_INVALID_PARAMETER
;
116 ExternalInterruptHandler
= AllocateZeroPool (sizeof (EFI_CPU_INTERRUPT_HANDLER
) * CPU_INTERRUPT_NUM
);
117 ASSERT (ExternalInterruptHandler
!= NULL
);
120 // Read IDT descriptor and calculate IDT size
122 AsmReadIdtr (&IdtDescriptor
);
123 IdtEntryCount
= (IdtDescriptor
.Limit
+ 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR
);
124 if (IdtEntryCount
> CPU_INTERRUPT_NUM
) {
125 IdtEntryCount
= CPU_INTERRUPT_NUM
;
128 // Create Interrupt Descriptor Table and Copy the old IDT table in
130 IdtTable
= AllocateZeroPool (sizeof (IA32_IDT_GATE_DESCRIPTOR
) * CPU_INTERRUPT_NUM
);
131 ASSERT (IdtTable
!= NULL
);
132 CopyMem (IdtTable
, (VOID
*)IdtDescriptor
.Base
, sizeof (IA32_IDT_GATE_DESCRIPTOR
) * IdtEntryCount
);
134 AsmGetTemplateAddressMap (&TemplateMap
);
135 ASSERT (TemplateMap
.ExceptionStubHeaderSize
<= HOOKAFTER_STUB_SIZE
);
136 InterruptEntryCode
= AllocatePool (TemplateMap
.ExceptionStubHeaderSize
* CPU_INTERRUPT_NUM
);
137 ASSERT (InterruptEntryCode
!= NULL
);
139 InterruptEntry
= (UINTN
) InterruptEntryCode
;
140 for (Index
= 0; Index
< CPU_INTERRUPT_NUM
; Index
++) {
142 (VOID
*) InterruptEntry
,
143 (VOID
*) TemplateMap
.ExceptionStart
,
144 TemplateMap
.ExceptionStubHeaderSize
146 AsmVectorNumFixup ((VOID
*) InterruptEntry
, (UINT8
) Index
, (VOID
*) TemplateMap
.ExceptionStart
);
147 InterruptEntry
+= TemplateMap
.ExceptionStubHeaderSize
;
150 TemplateMap
.ExceptionStart
= (UINTN
) InterruptEntryCode
;
151 mExceptionHandlerData
.IdtEntryCount
= CPU_INTERRUPT_NUM
;
152 mExceptionHandlerData
.ReservedVectors
= ReservedVectors
;
153 mExceptionHandlerData
.ExternalInterruptHandler
= ExternalInterruptHandler
;
154 InitializeSpinLock (&mExceptionHandlerData
.DisplayMessageSpinLock
);
156 UpdateIdtTable (IdtTable
, &TemplateMap
, &mExceptionHandlerData
);
159 // Load Interrupt Descriptor Table
161 IdtDescriptor
.Base
= (UINTN
) IdtTable
;
162 IdtDescriptor
.Limit
= (UINT16
) (sizeof (IA32_IDT_GATE_DESCRIPTOR
) * CPU_INTERRUPT_NUM
- 1);
163 AsmWriteIdtr ((IA32_DESCRIPTOR
*) &IdtDescriptor
);
169 Registers a function to be called from the processor interrupt handler.
171 This function registers and enables the handler specified by InterruptHandler for a processor
172 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
173 handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
174 The installed handler is called once for each processor interrupt or exception.
175 NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or
176 InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.
178 @param[in] InterruptType Defines which interrupt or exception to hook.
179 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
180 when a processor interrupt occurs. If this parameter is NULL, then the handler
183 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
184 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
185 previously installed.
186 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
187 previously installed.
188 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
189 or this function is not supported.
193 RegisterCpuInterruptHandler (
194 IN EFI_EXCEPTION_TYPE InterruptType
,
195 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
198 return RegisterCpuInterruptHandlerWorker (InterruptType
, InterruptHandler
, &mExceptionHandlerData
);