]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
UefiCpuPkg/CpuExceptionHandler: Add base support for the #VE exception
[mirror_edk2.git] / UefiCpuPkg / Library / CpuExceptionHandlerLib / SecPeiCpuException.c
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
23 CommonExceptionHandler (
24 IN EFI_EXCEPTION_TYPE ExceptionType,
25 IN EFI_SYSTEM_CONTEXT SystemContext
26 )
27 {
28 EFI_STATUS Status;
29
30 switch (ExceptionType) {
31 case VC_EXCEPTION:
32 //
33 // #VC needs to be handled immediately upon enabling exception handling
34 // and therefore can't use the RegisterCpuInterruptHandler() interface
35 // (which isn't supported under Sec and Pei anyway).
36 //
37 // Handle the #VC:
38 // On EFI_SUCCESS - Exception has been handled, return
39 // On other - ExceptionType contains (possibly new) exception
40 // value
41 //
42 Status = VmgExitHandleVc (&ExceptionType, SystemContext);
43 if (!EFI_ERROR (Status)) {
44 return;
45 }
46
47 break;
48
49 case VE_EXCEPTION:
50 //
51 // #VE needs to be handled immediately upon enabling exception handling
52 // and therefore can't use the RegisterCpuInterruptHandler() interface
53 // (which isn't supported under Sec and Pei anyway).
54 //
55 // Handle the #VE:
56 // On EFI_SUCCESS - Exception has been handled, return
57 // On other - ExceptionType contains (possibly new) exception
58 // value
59 //
60 Status = VmTdExitHandleVe (&ExceptionType, SystemContext);
61 if (!EFI_ERROR (Status)) {
62 return;
63 }
64
65 break;
66
67 default:
68 break;
69 }
70
71 //
72 // Initialize the serial port before dumping.
73 //
74 SerialPortInitialize ();
75 //
76 // Display ExceptionType, CPU information and Image information
77 //
78 DumpImageAndCpuContent (ExceptionType, SystemContext);
79
80 //
81 // Enter a dead loop.
82 //
83 CpuDeadLoop ();
84 }
85
86 /**
87 Initializes all CPU exceptions entries and provides the default exception handlers.
88
89 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
90 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
91 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
92 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
93 Note: Before invoking this API, caller must allocate memory for IDT table and load
94 IDTR by AsmWriteIdtr().
95
96 @param[in] VectorInfo Pointer to reserved vector list.
97
98 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
99 with default exception handlers.
100 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
101 @retval EFI_UNSUPPORTED This function is not supported.
102
103 **/
104 EFI_STATUS
105 EFIAPI
106 InitializeCpuExceptionHandlers (
107 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
108 )
109 {
110 EFI_STATUS Status;
111 RESERVED_VECTORS_DATA ReservedVectorData[CPU_EXCEPTION_NUM];
112 IA32_DESCRIPTOR IdtDescriptor;
113 UINTN IdtEntryCount;
114 UINT16 CodeSegment;
115 EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap;
116 IA32_IDT_GATE_DESCRIPTOR *IdtTable;
117 UINTN Index;
118 UINTN InterruptHandler;
119
120 if (VectorInfo != NULL) {
121 SetMem ((VOID *)ReservedVectorData, sizeof (RESERVED_VECTORS_DATA) * CPU_EXCEPTION_NUM, 0xff);
122 Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectorData, CPU_EXCEPTION_NUM);
123 if (EFI_ERROR (Status)) {
124 return EFI_INVALID_PARAMETER;
125 }
126 }
127
128 //
129 // Read IDT descriptor and calculate IDT size
130 //
131 AsmReadIdtr (&IdtDescriptor);
132 IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR);
133 if (IdtEntryCount > CPU_EXCEPTION_NUM) {
134 //
135 // CPU exception library only setup CPU_EXCEPTION_NUM exception handler at most
136 //
137 IdtEntryCount = CPU_EXCEPTION_NUM;
138 }
139
140 //
141 // Use current CS as the segment selector of interrupt gate in IDT
142 //
143 CodeSegment = AsmReadCs ();
144
145 AsmGetTemplateAddressMap (&TemplateMap);
146 IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtDescriptor.Base;
147 for (Index = 0; Index < IdtEntryCount; Index++) {
148 IdtTable[Index].Bits.Selector = CodeSegment;
149 //
150 // Check reserved vectors attributes if has, only EFI_VECTOR_HANDOFF_DO_NOT_HOOK
151 // supported in this instance
152 //
153 if (VectorInfo != NULL) {
154 if (ReservedVectorData[Index].Attribute == EFI_VECTOR_HANDOFF_DO_NOT_HOOK) {
155 continue;
156 }
157 }
158
159 //
160 // Update IDT entry
161 //
162 InterruptHandler = TemplateMap.ExceptionStart + Index * TemplateMap.ExceptionStubHeaderSize;
163 ArchUpdateIdtEntry (&IdtTable[Index], InterruptHandler);
164 }
165
166 return EFI_SUCCESS;
167 }
168
169 /**
170 Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.
171
172 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
173 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
174 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
175 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
176
177 @param[in] VectorInfo Pointer to reserved vector list.
178
179 @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized
180 with default interrupt/exception handlers.
181 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
182 @retval EFI_UNSUPPORTED This function is not supported.
183
184 **/
185 EFI_STATUS
186 EFIAPI
187 InitializeCpuInterruptHandlers (
188 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
189 )
190 {
191 return EFI_UNSUPPORTED;
192 }
193
194 /**
195 Registers a function to be called from the processor interrupt handler.
196
197 This function registers and enables the handler specified by InterruptHandler for a processor
198 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
199 handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
200 The installed handler is called once for each processor interrupt or exception.
201 NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or
202 InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.
203
204 @param[in] InterruptType Defines which interrupt or exception to hook.
205 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
206 when a processor interrupt occurs. If this parameter is NULL, then the handler
207 will be uninstalled.
208
209 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
210 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
211 previously installed.
212 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
213 previously installed.
214 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
215 or this function is not supported.
216 **/
217 EFI_STATUS
218 EFIAPI
219 RegisterCpuInterruptHandler (
220 IN EFI_EXCEPTION_TYPE InterruptType,
221 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
222 )
223 {
224 return EFI_UNSUPPORTED;
225 }
226
227 /**
228 Initializes all CPU exceptions entries with optional extra initializations.
229
230 By default, this method should include all functionalities implemented by
231 InitializeCpuExceptionHandlers(), plus extra initialization works, if any.
232 This could be done by calling InitializeCpuExceptionHandlers() directly
233 in this method besides the extra works.
234
235 InitData is optional and its use and content are processor arch dependent.
236 The typical usage of it is to convey resources which have to be reserved
237 elsewhere and are necessary for the extra initializations of exception.
238
239 @param[in] VectorInfo Pointer to reserved vector list.
240 @param[in] InitData Pointer to data optional for extra initializations
241 of exception.
242
243 @retval EFI_SUCCESS The exceptions have been successfully
244 initialized.
245 @retval EFI_INVALID_PARAMETER VectorInfo or InitData contains invalid
246 content.
247
248 **/
249 EFI_STATUS
250 EFIAPI
251 InitializeCpuExceptionHandlersEx (
252 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL,
253 IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL
254 )
255 {
256 return InitializeCpuExceptionHandlers (VectorInfo);
257 }