]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
UefiCpuPkg: Apply uncrustify changes
[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 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
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 //
104 // Read IDT descriptor and calculate IDT size
105 //
106 AsmReadIdtr (&IdtDescriptor);
107 IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR);
108 if (IdtEntryCount > CPU_EXCEPTION_NUM) {
109 //
110 // CPU exception library only setup CPU_EXCEPTION_NUM exception handler at most
111 //
112 IdtEntryCount = CPU_EXCEPTION_NUM;
113 }
114
115 //
116 // Use current CS as the segment selector of interrupt gate in IDT
117 //
118 CodeSegment = AsmReadCs ();
119
120 AsmGetTemplateAddressMap (&TemplateMap);
121 IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtDescriptor.Base;
122 for (Index = 0; Index < IdtEntryCount; Index++) {
123 IdtTable[Index].Bits.Selector = CodeSegment;
124 //
125 // Check reserved vectors attributes if has, only EFI_VECTOR_HANDOFF_DO_NOT_HOOK
126 // supported in this instance
127 //
128 if (VectorInfo != NULL) {
129 if (ReservedVectorData[Index].Attribute == EFI_VECTOR_HANDOFF_DO_NOT_HOOK) {
130 continue;
131 }
132 }
133
134 //
135 // Update IDT entry
136 //
137 InterruptHandler = TemplateMap.ExceptionStart + Index * TemplateMap.ExceptionStubHeaderSize;
138 ArchUpdateIdtEntry (&IdtTable[Index], InterruptHandler);
139 }
140
141 return EFI_SUCCESS;
142 }
143
144 /**
145 Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.
146
147 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
148 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
149 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
150 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
151
152 @param[in] VectorInfo Pointer to reserved vector list.
153
154 @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized
155 with default interrupt/exception handlers.
156 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
157 @retval EFI_UNSUPPORTED This function is not supported.
158
159 **/
160 EFI_STATUS
161 EFIAPI
162 InitializeCpuInterruptHandlers (
163 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
164 )
165 {
166 return EFI_UNSUPPORTED;
167 }
168
169 /**
170 Registers a function to be called from the processor interrupt handler.
171
172 This function registers and enables the handler specified by InterruptHandler for a processor
173 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
174 handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
175 The installed handler is called once for each processor interrupt or exception.
176 NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or
177 InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.
178
179 @param[in] InterruptType Defines which interrupt or exception to hook.
180 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
181 when a processor interrupt occurs. If this parameter is NULL, then the handler
182 will be uninstalled.
183
184 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
185 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
186 previously installed.
187 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
188 previously installed.
189 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
190 or this function is not supported.
191 **/
192 EFI_STATUS
193 EFIAPI
194 RegisterCpuInterruptHandler (
195 IN EFI_EXCEPTION_TYPE InterruptType,
196 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
197 )
198 {
199 return EFI_UNSUPPORTED;
200 }
201
202 /**
203 Initializes all CPU exceptions entries with optional extra initializations.
204
205 By default, this method should include all functionalities implemented by
206 InitializeCpuExceptionHandlers(), plus extra initialization works, if any.
207 This could be done by calling InitializeCpuExceptionHandlers() directly
208 in this method besides the extra works.
209
210 InitData is optional and its use and content are processor arch dependent.
211 The typical usage of it is to convey resources which have to be reserved
212 elsewhere and are necessary for the extra initializations of exception.
213
214 @param[in] VectorInfo Pointer to reserved vector list.
215 @param[in] InitData Pointer to data optional for extra initializations
216 of exception.
217
218 @retval EFI_SUCCESS The exceptions have been successfully
219 initialized.
220 @retval EFI_INVALID_PARAMETER VectorInfo or InitData contains invalid
221 content.
222
223 **/
224 EFI_STATUS
225 EFIAPI
226 InitializeCpuExceptionHandlersEx (
227 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL,
228 IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL
229 )
230 {
231 return InitializeCpuExceptionHandlers (VectorInfo);
232 }