1 /** @file
2 CPU exception handler library implemenation for SEC/PEIM modules.
3
4 Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <PiPei.h>
10 #include <Library/VmgExitLib.h>
11 #include "CpuExceptionCommon.h"
12
13 CONST UINTN mDoFarReturnFlag = 0;
14
15 /**
16 Common exception handler.
17
18 @param ExceptionType Exception type.
19 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
20 **/
21 VOID
22 EFIAPI
CommonExceptionHandler(IN EFI_EXCEPTION_TYPE ExceptionType,IN EFI_SYSTEM_CONTEXT SystemContext)23 CommonExceptionHandler (
24 IN EFI_EXCEPTION_TYPE ExceptionType,
25 IN EFI_SYSTEM_CONTEXT SystemContext
26 )
27 {
28 if (ExceptionType == VC_EXCEPTION) {
29 EFI_STATUS Status;
30 //
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).
34 //
35 // Handle the #VC:
36 // On EFI_SUCCESS - Exception has been handled, return
37 // On other - ExceptionType contains (possibly new) exception
38 // value
39 //
40 Status = VmgExitHandleVc (&ExceptionType, SystemContext);
41 if (!EFI_ERROR (Status)) {
42 return;
43 }
44 }
45
46 //
47 // Initialize the serial port before dumping.
48 //
49 SerialPortInitialize ();
50 //
51 // Display ExceptionType, CPU information and Image information
52 //
53 DumpImageAndCpuContent (ExceptionType, SystemContext);
54
55 //
56 // Enter a dead loop.
57 //
58 CpuDeadLoop ();
59 }
60
61 /**
62 Initializes all CPU exceptions entries and provides the default exception handlers.
63
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().
70
71 @param[in] VectorInfo Pointer to reserved vector list.
72
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.
77
78 **/
79 EFI_STATUS
80 EFIAPI
InitializeCpuExceptionHandlers(IN EFI_VECTOR_HANDOFF_INFO * VectorInfo OPTIONAL)81 InitializeCpuExceptionHandlers (
82 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
83 )
84 {
85 EFI_STATUS Status;
86 RESERVED_VECTORS_DATA ReservedVectorData[CPU_EXCEPTION_NUM];
87 IA32_DESCRIPTOR IdtDescriptor;
88 UINTN IdtEntryCount;
89 UINT16 CodeSegment;
90 EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap;
91 IA32_IDT_GATE_DESCRIPTOR *IdtTable;
92 UINTN Index;
93 UINTN InterruptHandler;
94
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;
100 }
101 }
102 //
103 // Read IDT descriptor and calculate IDT size
104 //
105 AsmReadIdtr (&IdtDescriptor);
106 IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR);
107 if (IdtEntryCount > CPU_EXCEPTION_NUM) {
108 //
109 // CPU exception library only setup CPU_EXCEPTION_NUM exception handler at most
110 //
111 IdtEntryCount = CPU_EXCEPTION_NUM;
112 }
113 //
114 // Use current CS as the segment selector of interrupt gate in IDT
115 //
116 CodeSegment = AsmReadCs ();
117
118 AsmGetTemplateAddressMap (&TemplateMap);
119 IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtDescriptor.Base;
120 for (Index = 0; Index < IdtEntryCount; Index ++) {
121 IdtTable[Index].Bits.Selector = CodeSegment;
122 //
123 // Check reserved vectors attributes if has, only EFI_VECTOR_HANDOFF_DO_NOT_HOOK
124 // supported in this instance
125 //
126 if (VectorInfo != NULL) {
127 if (ReservedVectorData[Index].Attribute == EFI_VECTOR_HANDOFF_DO_NOT_HOOK) {
128 continue;
129 }
130 }
131 //
132 // Update IDT entry
133 //
134 InterruptHandler = TemplateMap.ExceptionStart + Index * TemplateMap.ExceptionStubHeaderSize;
135 ArchUpdateIdtEntry (&IdtTable[Index], InterruptHandler);
136 }
137 return EFI_SUCCESS;
138 }
139
140 /**
141 Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.
142
143 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
144 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
145 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
146 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
147
148 @param[in] VectorInfo Pointer to reserved vector list.
149
150 @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized
151 with default interrupt/exception handlers.
152 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
153 @retval EFI_UNSUPPORTED This function is not supported.
154
155 **/
156 EFI_STATUS
157 EFIAPI
InitializeCpuInterruptHandlers(IN EFI_VECTOR_HANDOFF_INFO * VectorInfo OPTIONAL)158 InitializeCpuInterruptHandlers (
159 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
160 )
161 {
162 return EFI_UNSUPPORTED;
163 }
164
165 /**
166 Registers a function to be called from the processor interrupt handler.
167
168 This function registers and enables the handler specified by InterruptHandler for a processor
169 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
170 handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
171 The installed handler is called once for each processor interrupt or exception.
172 NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or
173 InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.
174
175 @param[in] InterruptType Defines which interrupt or exception to hook.
176 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
177 when a processor interrupt occurs. If this parameter is NULL, then the handler
178 will be uninstalled.
179
180 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
181 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
182 previously installed.
183 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
184 previously installed.
185 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
186 or this function is not supported.
187 **/
188 EFI_STATUS
189 EFIAPI
RegisterCpuInterruptHandler(IN EFI_EXCEPTION_TYPE InterruptType,IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler)190 RegisterCpuInterruptHandler (
191 IN EFI_EXCEPTION_TYPE InterruptType,
192 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
193 )
194 {
195 return EFI_UNSUPPORTED;
196 }
197
198 /**
199 Initializes all CPU exceptions entries with optional extra initializations.
200
201 By default, this method should include all functionalities implemented by
202 InitializeCpuExceptionHandlers(), plus extra initialization works, if any.
203 This could be done by calling InitializeCpuExceptionHandlers() directly
204 in this method besides the extra works.
205
206 InitData is optional and its use and content are processor arch dependent.
207 The typical usage of it is to convey resources which have to be reserved
208 elsewhere and are necessary for the extra initializations of exception.
209
210 @param[in] VectorInfo Pointer to reserved vector list.
211 @param[in] InitData Pointer to data optional for extra initializations
212 of exception.
213
214 @retval EFI_SUCCESS The exceptions have been successfully
215 initialized.
216 @retval EFI_INVALID_PARAMETER VectorInfo or InitData contains invalid
217 content.
218
219 **/
220 EFI_STATUS
221 EFIAPI
InitializeCpuExceptionHandlersEx(IN EFI_VECTOR_HANDOFF_INFO * VectorInfo OPTIONAL,IN CPU_EXCEPTION_INIT_DATA * InitData OPTIONAL)222 InitializeCpuExceptionHandlersEx (
223 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL,
224 IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL
225 )
226 {
227 return InitializeCpuExceptionHandlers (VectorInfo);
228 }
229