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